changeset 934:4290490a2e05

Add a way to access "libs" dir in Configuration Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-January/005335.html
author Omair Majid <omajid@redhat.com>
date Fri, 25 Jan 2013 16:33:19 -0500
parents 102f62b0715a
children 186115da601f
files common/core/src/main/java/com/redhat/thermostat/common/config/Configuration.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoImpl.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoSourceImpl.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommandInfoImplTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommandInfoSourceTest.java
diffstat 7 files changed, 40 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/config/Configuration.java	Fri Jan 25 17:43:23 2013 +0100
+++ b/common/core/src/main/java/com/redhat/thermostat/common/config/Configuration.java	Fri Jan 25 16:33:19 2013 -0500
@@ -73,7 +73,11 @@
         File file = new File(loc);
         return file;
     }
-    
+
+    public String getLibRoot() {
+        return home + File.separator + "libs";
+    }
+
     public File getStorageBaseDirectory() throws InvalidConfigurationException {
         String loc = getThermostatHome() + File.separatorChar + "storage";
         File file = new File(loc);
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java	Fri Jan 25 17:43:23 2013 +0100
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java	Fri Jan 25 16:33:19 2013 -0500
@@ -36,6 +36,8 @@
 
 package com.redhat.thermostat.launcher.internal;
 
+import java.io.File;
+
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
@@ -70,10 +72,14 @@
         public Object addingService(ServiceReference reference) {
             // keyring is now ready
             Keyring keyring = (Keyring)context.getService(reference);
-            // Register Launcher service since FrameworkProvider is waiting for it blockingly.
-            CommandInfoSourceImpl commands = new CommandInfoSourceImpl(bundleService.getConfiguration().getThermostatHome());
+            Configuration config = bundleService.getConfiguration();
+            String commandsDir = config.getThermostatHome() + File.separator + "etc" +
+                    File.separator + "commands";
+            CommandInfoSourceImpl commands =
+                    new CommandInfoSourceImpl(commandsDir, config.getLibRoot());
             cmdInfoReg = context.registerService(CommandInfoSource.class, commands, null);
             bundleService.setCommandInfoSource(commands);
+            // Register Launcher service since FrameworkProvider is waiting for it blockingly.
             LauncherImpl launcher = new LauncherImpl(context,
                     new CommandContextFactory(context), bundleService);
             launcherReg = context.registerService(Launcher.class.getName(), launcher, null);
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoImpl.java	Fri Jan 25 17:43:23 2013 +0100
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoImpl.java	Fri Jan 25 16:33:19 2013 -0500
@@ -70,13 +70,13 @@
     private Options options;
     private List<String> dependencies;
 
-    CommandInfoImpl(String name, Properties properties, String thermostatHome) {
+    CommandInfoImpl(String name, Properties properties, String libRoot) {
         options = new Options();
         this.name = name;
         for (Entry<Object,Object> entry: properties.entrySet()) {
             String key = (String) entry.getKey();
             if (key.equals(PROPERTY_BUNDLES)) {
-                learnDependencies((String) entry.getValue(), thermostatHome);
+                learnDependencies((String) entry.getValue(), libRoot);
             } else if (key.equals(PROPERTY_DESC)) {
                 description = properties.getProperty(key);
             } else if (key.equals(PROPERTY_USAGE)) {
@@ -87,8 +87,7 @@
         }
     }
 
-    private void learnDependencies(String bundlesValue, String thermostatHome) {
-        String libRoot = thermostatHome + File.separator + "libs";
+    private void learnDependencies(String bundlesValue, String libRoot) {
         List<String> resourceNames = Arrays.asList(bundlesValue.split(","));
         dependencies = new ArrayList<>(resourceNames.size());
         for (String value : resourceNames) {
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoSourceImpl.java	Fri Jan 25 17:43:23 2013 +0100
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommandInfoSourceImpl.java	Fri Jan 25 16:33:19 2013 -0500
@@ -56,9 +56,9 @@
     private static final Logger logger = LoggingUtils.getLogger(CommandInfoSourceImpl.class);
     private Map<String, CommandInfo> commands;
 
-    CommandInfoSourceImpl(String thermostatHome) {
+    CommandInfoSourceImpl(String commandsDir, String libRoot) {
         commands = new HashMap<>();
-        final File dir = new File(thermostatHome + File.separator + "etc", "commands");
+        final File dir = new File(commandsDir);
         if (dir.isDirectory()) {
             FilenameFilter filter = new FilenameFilter() {
 
@@ -82,7 +82,7 @@
                     logger.warning("Issue loading properties file: " + file.getPath());
                 }
                 String commandName = deduceCommandName(file.getName());
-                commands.put(commandName, new CommandInfoImpl(commandName, commandProps, thermostatHome));
+                commands.put(commandName, new CommandInfoImpl(commandName, commandProps, libRoot));
             }
         } else {
             logger.warning("Command configuration directory not found or not a directory: " + dir.getPath());
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java	Fri Jan 25 17:43:23 2013 +0100
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java	Fri Jan 25 16:33:19 2013 -0500
@@ -127,8 +127,8 @@
         CommandInfoSourceImpl commands = mock(CommandInfoSourceImpl.class);
         when(commands.getCommandInfos()).thenReturn(new ArrayList<CommandInfo>());
         whenNew(CommandInfoSourceImpl.class).
-                withParameterTypes(String.class).
-                withArguments(isA(String.class)).thenReturn(commands);
+                withParameterTypes(String.class, String.class).
+                withArguments(isA(String.class), isA(String.class)).thenReturn(commands);
 
         tracker = mock(MultipleServiceTracker.class);
         whenNew(MultipleServiceTracker.class).
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommandInfoImplTest.java	Fri Jan 25 17:43:23 2013 +0100
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommandInfoImplTest.java	Fri Jan 25 16:33:19 2013 -0500
@@ -60,6 +60,7 @@
 public class CommandInfoImplTest {
 
     private Path tempThermostatHome, someJarName1, someJarName2, missingJarName;
+    private File tempLibs;
 
     @Before
     public void setUp() throws IOException {
@@ -67,7 +68,7 @@
         tempThermostatHome.toFile().deleteOnExit();
         System.setProperty("THERMOSTAT_HOME", tempThermostatHome.toString());
 
-        File tempLibs = new File(tempThermostatHome.toFile(), "libs");
+        tempLibs = new File(tempThermostatHome.toFile(), "libs");
         tempLibs.mkdirs();
         tempLibs.deleteOnExit();
 
@@ -104,7 +105,7 @@
         Properties props = new Properties();
         props.setProperty("bundles", someJarName1.getFileName().toString());
         String name = "name";
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         List<String> resources = info.getDependencyResourceNames();
         assertEquals(1, resources.size());
@@ -116,7 +117,7 @@
         Properties props = new Properties();
         props.setProperty("bundles", someJarName1.getFileName() + "," + someJarName2.getFileName());
         String name = "name";
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         List<String> resources = info.getDependencyResourceNames();
         assertEquals(2, resources.size());
@@ -129,7 +130,7 @@
         Properties props = new Properties();
         props.setProperty("bundles", missingJarName.getFileName().toString());
         String name = "name";
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         List<String> resources = info.getDependencyResourceNames();
         assertEquals(0, resources.size());
@@ -142,7 +143,7 @@
         String name = "name";
         String desc = "desc";
         props.put("description", desc);
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         String commandDesc = info.getDescription();
         assertEquals(desc, commandDesc);
@@ -154,7 +155,7 @@
         String name = "name";
         String usage = "some sort of usage message";
         props.put("usage", usage);
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         String commandUsage = info.getUsage();
         assertEquals(usage, commandUsage);
@@ -175,7 +176,7 @@
         props.put("bar.hasarg", "FALSE");
         props.put("bar.required", "this will evaluate as false");
         props.put("bar.description", "the bar option");
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         Options options = info.getOptions();
         Option foo = options.getOption("foo");
@@ -199,7 +200,7 @@
         Properties props = new Properties();
         String name = "name";
         props.put("options", "AUTO_DB_OPTIONS");
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         Options options = info.getOptions();
         assertTrue(options.hasOption(CommonOptions.DB_URL_ARG));
@@ -220,7 +221,7 @@
         props.put("options", "AUTO_DB_OPTIONS, dbUrl");
         props.put("dbUrl.long", "ignored");
         props.put("dbUrl.required", "true");
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         Options options = info.getOptions();
         assertTrue(options.hasOption(CommonOptions.DB_URL_ARG));
@@ -234,7 +235,7 @@
         Properties props = new Properties();
         String name = "name";
         props.put("options", "AUTO_LOG_OPTION");
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         Options options = info.getOptions();
         assertTrue(options.hasOption(CommonOptions.LOG_LEVEL_ARG));
@@ -248,7 +249,7 @@
         props.put("options", "foo|bar");
         props.put("foo.short", "f");
         props.put("bar.short", "b");
-        CommandInfoImpl info = new CommandInfoImpl(name, props, tempThermostatHome.toString());
+        CommandInfoImpl info = new CommandInfoImpl(name, props, tempLibs.toString());
 
         Options options = info.getOptions();
         Option foo = options.getOption("f");
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommandInfoSourceTest.java	Fri Jan 25 17:43:23 2013 +0100
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommandInfoSourceTest.java	Fri Jan 25 16:33:19 2013 -0500
@@ -57,6 +57,7 @@
 
     private Path tempThermostatHome;
 
+    private File tempLibs;
     private File tempEtc;
     private File tempCommands;
     private File tempPropsFile;
@@ -68,6 +69,8 @@
         tempThermostatHome.toFile().deleteOnExit();
         System.setProperty("THERMOSTAT_HOME", tempThermostatHome.toString());
         
+        tempLibs = new File(tempThermostatHome.toFile(), "libs");
+
         tempEtc = new File(tempThermostatHome.toFile(), "etc");
         tempEtc.mkdirs();
         tempEtc.deleteOnExit();
@@ -93,7 +96,8 @@
 
     @Test
     public void testGetCommandInfo() {
-        CommandInfoSourceImpl bundles = new CommandInfoSourceImpl(tempThermostatHome.toString());
+        CommandInfoSourceImpl bundles =
+                new CommandInfoSourceImpl(tempCommands.toString(), tempLibs.toString());
         CommandInfo info = bundles.getCommandInfo("foo");
         assertNotNull(info);
         assertEquals("foo", info.getName());
@@ -101,7 +105,8 @@
 
     @Test
     public void testGetCommandInfos() {
-        CommandInfoSourceImpl bundles = new CommandInfoSourceImpl(tempThermostatHome.toString());
+        CommandInfoSourceImpl bundles =
+                new CommandInfoSourceImpl(tempCommands.toString(), tempLibs.toString());
         Collection<CommandInfo> infos = bundles.getCommandInfos();
         assertNotNull(infos);
         assertEquals(1, infos.size());