Mercurial > hg > thermostat
changeset 2484:c6d0e7c16513
Windows - add OS.IS_UNIX to thermostat-shared-config
Reviewed-by: sgehwolf
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-October/021330.html
line wrap: on
line diff
--- a/agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/CommandChannelDelegate.java Thu Oct 20 11:08:56 2016 -0400 +++ b/agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/CommandChannelDelegate.java Thu Oct 20 11:32:47 2016 -0400 @@ -51,6 +51,7 @@ import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; + import com.redhat.thermostat.agent.command.ConfigurationServer; import com.redhat.thermostat.agent.command.ReceiverRegistry; import com.redhat.thermostat.agent.command.RequestReceiver; @@ -63,6 +64,7 @@ import com.redhat.thermostat.common.command.Response.ResponseType; import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.shared.config.SSLConfiguration; +import com.redhat.thermostat.shared.config.OS; import com.redhat.thermostat.storage.core.AuthToken; import com.redhat.thermostat.storage.core.SecureStorage; import com.redhat.thermostat.storage.core.Storage; @@ -72,7 +74,6 @@ private static final Logger logger = LoggingUtils.getLogger(CommandChannelDelegate.class); private static final String CMD_NAME = "thermostat-command-channel"; private static final String IPC_SERVER_NAME = "command-channel"; - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); // States for 'state' field private static final int STATE_NOT_STARTED = 0; @@ -207,7 +208,7 @@ } private void startServer(String hostname, int port) throws IOException { - String[] processArgs = IS_UNIX + String[] processArgs = OS.IS_UNIX ? new String[]{ binPath.getAbsolutePath() + File.separator + CMD_NAME, hostname, String.valueOf(port), ipcConfig.getAbsolutePath() } : new String[] { "cmd", "/c", binPath.getAbsolutePath() + File.separator + CMD_NAME + ".cmd", hostname,
--- a/agent/ipc/server/src/main/java/com/redhat/thermostat/agent/ipc/server/internal/IPCConfigurationWriter.java Thu Oct 20 11:08:56 2016 -0400 +++ b/agent/ipc/server/src/main/java/com/redhat/thermostat/agent/ipc/server/internal/IPCConfigurationWriter.java Thu Oct 20 11:32:47 2016 -0400 @@ -43,6 +43,7 @@ import java.util.Properties; import com.redhat.thermostat.agent.ipc.common.internal.IPCType; +import com.redhat.thermostat.shared.config.OS; class IPCConfigurationWriter { @@ -57,8 +58,6 @@ private static int TEST_SOCKET_LOW = 51200; private static int TEST_SOCKET_HIGH = 55000; - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - private static final String COMMENTS = "Configuration for Inter-process Communication (IPC) used in the Thermostat agent.\n" + "The agent is configured to use Unix sockets for IPC by default on Linux,\n" @@ -94,7 +93,7 @@ Properties props = helper.createProperties(); - props.setProperty(PROP_IPC_TYPE, IS_UNIX ? IPCType.UNIX_SOCKET.getConfigValue() : IPCType.TCP_SOCKET.getConfigValue()); + props.setProperty(PROP_IPC_TYPE, OS.IS_UNIX ? IPCType.UNIX_SOCKET.getConfigValue() : IPCType.TCP_SOCKET.getConfigValue()); // unix socket will work without configuration (creates sockets in tmp directory // but tcpsocket always needs ports (in the future, should support service discovery)
--- a/agent/ipc/server/src/test/java/com/redhat/thermostat/agent/ipc/server/internal/IPCConfigurationWriterTest.java Thu Oct 20 11:08:56 2016 -0400 +++ b/agent/ipc/server/src/test/java/com/redhat/thermostat/agent/ipc/server/internal/IPCConfigurationWriterTest.java Thu Oct 20 11:32:47 2016 -0400 @@ -52,6 +52,7 @@ import java.io.OutputStream; import java.util.Properties; +import com.redhat.thermostat.shared.config.OS; import org.junit.Before; import org.junit.Test; @@ -60,8 +61,6 @@ public class IPCConfigurationWriterTest { - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - private IPCConfigurationWriter writer; private Properties props; private FileOutputStream fos; @@ -84,7 +83,7 @@ public void testWrite() throws Exception { writer.write(); - final IPCType expectedType = IS_UNIX ? IPCType.UNIX_SOCKET : IPCType.TCP_SOCKET; + final IPCType expectedType = OS.IS_UNIX ? IPCType.UNIX_SOCKET : IPCType.TCP_SOCKET; verify(props).setProperty(IPCConfigurationWriter.PROP_IPC_TYPE, expectedType.getConfigValue()); verify(props).store(eq(fos), anyString()); verify(fos).close();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config/src/main/java/com/redhat/thermostat/shared/config/OS.java Thu Oct 20 11:32:47 2016 -0400 @@ -0,0 +1,74 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.shared.config; + +/** + * Utility class for OS-specific behaviour + */ +public class OS { + + /** + * The current OS is Linux, BSD, Darwin, AIX, Solaris (etc.) + */ + public static final boolean IS_UNIX; + + /** + * The current OS is Windows of any flavour (implies IS_UNIX is false) + */ + + public static final boolean IS_WINDOWS; + + /** + * The current OS is Linux (implies IS_UNIX is also true) + */ + public static final boolean IS_LINUX; + + /** + * The current OS is MacOS (implies IS_UNIX is also true) + */ + public static final boolean IS_MACOS; + + static { + final String osname = System.getProperty("os.name").toLowerCase(); + + IS_UNIX = !osname.contains("win"); + IS_WINDOWS = !IS_UNIX; + + IS_MACOS = osname.contains("os x") || osname.contains("mac") || osname.contains("darwin"); + IS_LINUX = osname.contains("linux"); + } +}
--- a/keyring/src/main/java/com/redhat/thermostat/utils/keyring/internal/Activator.java Thu Oct 20 11:08:56 2016 -0400 +++ b/keyring/src/main/java/com/redhat/thermostat/utils/keyring/internal/Activator.java Thu Oct 20 11:32:47 2016 -0400 @@ -37,6 +37,7 @@ package com.redhat.thermostat.utils.keyring.internal; import com.redhat.thermostat.shared.config.CommonPaths; +import com.redhat.thermostat.shared.config.OS; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -47,8 +48,6 @@ public class Activator implements BundleActivator { - private static final boolean IS_UNIX = !System.getProperty("os.name").toLowerCase().contains("win"); - private ServiceRegistration reg; private static class DummyKeyringImpl implements Keyring { @@ -80,20 +79,20 @@ try { theKeyring = new KeyringImpl(); } catch (UnsatisfiedLinkError e) { - if (IS_UNIX) { + if (OS.IS_UNIX) { theKeyring = new DummyKeyringImpl(); } else { - ServiceTracker<CommonPaths,CommonPaths> pathTracker = new ServiceTracker(context,CommonPaths.class.getName(), null) { + ServiceTracker<CommonPaths,CommonPaths> pathTracker = new ServiceTracker<CommonPaths,CommonPaths>(context,CommonPaths.class.getName(), null) { @Override - public Object addingService(ServiceReference reference) { - CommonPaths paths = (CommonPaths) super.addingService(reference); + public CommonPaths addingService(ServiceReference<CommonPaths> reference) { + CommonPaths paths = super.addingService(reference); final Keyring theKeyring = new InsecureFileBasedKeyringImpl(paths); reg = context.registerService(Keyring.class.getName(), theKeyring, null); return paths; } @Override - public void removedService(ServiceReference reference, Object service) { + public void removedService(ServiceReference<CommonPaths> reference, CommonPaths service) { if (reg != null) { reg.unregister(); reg = null; @@ -107,10 +106,9 @@ if (theKeyring != null) context.registerService(Keyring.class.getName(), theKeyring, null); } - + @Override public void stop(BundleContext context) throws Exception { // Nothing to do } } -
--- a/process-handler/src/main/java/com/redhat/thermostat/service/internal/ProcessHandlerImpl.java Thu Oct 20 11:08:56 2016 -0400 +++ b/process-handler/src/main/java/com/redhat/thermostat/service/internal/ProcessHandlerImpl.java Thu Oct 20 11:32:47 2016 -0400 @@ -40,12 +40,11 @@ import com.redhat.thermostat.service.internal.windows.WindowsProcessUtilities; import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.service.process.UNIXSignal; +import com.redhat.thermostat.shared.config.OS; public class ProcessHandlerImpl implements ProcessHandler { - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - - private static final ProcessUtilitiesBase proxy = IS_UNIX ? new UnixProcessUtilities() : new WindowsProcessUtilities(); + private static final ProcessUtilitiesBase proxy = OS.IS_UNIX ? new UnixProcessUtilities() : new WindowsProcessUtilities(); public static ProcessHandler getInstance() { return proxy;
--- a/process-handler/src/test/java/com/redhat/thermostat/service/internal/ProcessHendlerImplTest.java Thu Oct 20 11:08:56 2016 -0400 +++ b/process-handler/src/test/java/com/redhat/thermostat/service/internal/ProcessHendlerImplTest.java Thu Oct 20 11:32:47 2016 -0400 @@ -40,21 +40,20 @@ import com.redhat.thermostat.service.internal.windows.WindowsProcessUtilities; import com.redhat.thermostat.service.process.ProcessHandler; +import com.redhat.thermostat.shared.config.OS; import org.junit.Test; import static org.junit.Assert.assertNotNull; public class ProcessHendlerImplTest { - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - @Test public void testCorrectImplementation() { final ProcessHandler hnd = ProcessHandlerImpl.getInstance(); assertNotNull(hnd); - if (IS_UNIX) { + if (OS.IS_UNIX) { assert(hnd instanceof UnixProcessUtilities); } else { assert(hnd instanceof WindowsProcessUtilities);
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/CredentialsFileCreator.java Thu Oct 20 11:08:56 2016 -0400 +++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/CredentialsFileCreator.java Thu Oct 20 11:32:47 2016 -0400 @@ -36,6 +36,8 @@ package com.redhat.thermostat.setup.command.internal.model; +import com.redhat.thermostat.shared.config.OS; + import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -46,8 +48,6 @@ class CredentialsFileCreator { - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - private static final Set<PosixFilePermission> CREDS_FILE_PERMISSIONS = EnumSet.of( PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE @@ -56,7 +56,7 @@ void create(File file) throws IOException { if (!file.exists()) { // create file and set file permissions to 600 - if ( IS_UNIX ) + if ( OS.IS_UNIX ) Files.createFile(file.toPath(), PosixFilePermissions.asFileAttribute(CREDS_FILE_PERMISSIONS)); else { // on windows, credentials may be globally visible.
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetup.java Thu Oct 20 11:08:56 2016 -0400 +++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetup.java Thu Oct 20 11:32:47 2016 -0400 @@ -66,13 +66,13 @@ import com.redhat.thermostat.launcher.Launcher; import com.redhat.thermostat.service.process.ProcessHandler; import com.redhat.thermostat.shared.config.CommonPaths; +import com.redhat.thermostat.shared.config.OS; class MongodbUserSetup implements UserSetup { static final String[] STORAGE_START_ARGS = {"storage", "--start", "--permitLocalhostException"}; static final String[] STORAGE_STOP_ARGS = {"storage", "--stop"}; - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); private static final String WEB_AUTH_FILE = "web.auth"; private static final String MONGO_PROCESS = "mongod"; private static final Logger logger = LoggingUtils.getLogger(MongodbUserSetup.class); @@ -250,7 +250,7 @@ } String[] getMongoRunnerCmdline( final String arg ) { - return IS_UNIX ? new String[] { "mongo", arg} : new String[] { "cmd", "/C", "mongo.exe", arg }; + return OS.IS_UNIX ? new String[] { "mongo", arg } : new String[] { "cmd", "/C", "mongo.exe", arg }; } //package-private for testing
--- a/storage/cli/src/main/java/com/redhat/thermostat/storage/cli/internal/MongoOSUtilFactory.java Thu Oct 20 11:08:56 2016 -0400 +++ b/storage/cli/src/main/java/com/redhat/thermostat/storage/cli/internal/MongoOSUtilFactory.java Thu Oct 20 11:32:47 2016 -0400 @@ -36,18 +36,18 @@ package com.redhat.thermostat.storage.cli.internal; +import com.redhat.thermostat.shared.config.OS; + /** * factory class to create OS-specific implementations of MongoOSUtil */ class MongoOSUtilFactory { - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - private static final MongoOSUtilFactory theInstance = new MongoOSUtilFactory(); static MongoOSUtilFactory instance() { return theInstance; } MongoOSUtilInterface createMongoOSUtil() { - return IS_UNIX ? new MongoUnixUtil() : new MongoWindowsUtil(); + return OS.IS_UNIX ? new MongoUnixUtil() : new MongoWindowsUtil(); } }
--- a/storage/cli/src/test/java/com/redhat/thermostat/storage/cli/internal/MongoOSUtilFactoryTest.java Thu Oct 20 11:08:56 2016 -0400 +++ b/storage/cli/src/test/java/com/redhat/thermostat/storage/cli/internal/MongoOSUtilFactoryTest.java Thu Oct 20 11:32:47 2016 -0400 @@ -36,7 +36,7 @@ package com.redhat.thermostat.storage.cli.internal; -import org.junit.Assert; +import com.redhat.thermostat.shared.config.OS; import org.junit.Test; import static org.junit.Assert.assertNotNull; @@ -44,8 +44,6 @@ public class MongoOSUtilFactoryTest { - private static final boolean IS_UNIX = !System.getProperty("os.name").contains("Windows"); - @Test public void instanceIsntNullTest() { assertNotNull(MongoOSUtilFactory.instance()); @@ -58,7 +56,7 @@ MongoOSUtilInterface thing = MongoOSUtilFactory.instance().createMongoOSUtil(); assertNotNull(thing); - if (IS_UNIX) + if (OS.IS_UNIX) assertTrue("createMongoOSUtil() must return an instance of MongoUnixUtil on unix", thing instanceof MongoUnixUtil); else assertTrue("createMongoOSUtil() must return an instance of MongoWindowsUtil on Windows", thing instanceof MongoWindowsUtil);