]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/detached/launcher/Main.java
Introduce log4j conf for SLC Detached (not working yet)
[gpl/argeo-slc.git] / org.argeo.slc.detached.launcher / src / main / java / org / argeo / slc / detached / launcher / Main.java
index 5eff46c2606a65f2fb44e339245e91c54323cc36..ba181b5885f5bf8f9d4ab18b0b1413a944f4cae1 100644 (file)
@@ -1,6 +1,7 @@
 package org.argeo.slc.detached.launcher;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Method;
@@ -10,6 +11,7 @@ import java.util.List;
 import java.util.Properties;
 import java.util.Vector;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.felix.framework.Felix;
@@ -20,12 +22,21 @@ public class Main {
        private final static Log log = LogFactory.getLog(Main.class);
 
        public static void main(String[] args) {
+               log.info("Argeo SLC Detached launcher starting...");
                try {
                        // Load properties
-                       Properties config = prepareConfig();
+                       String propertyPath = "slc-detached.properties";
+                       Properties config = prepareConfig(propertyPath);
+
+                       // Create cache dir
+                       if (!config.containsKey(BundleCache.CACHE_PROFILE_DIR_PROP)) {
+                               final File cachedir = createTemporaryCacheDir();
+                               config.put(BundleCache.CACHE_PROFILE_DIR_PROP, cachedir
+                                               .getAbsolutePath());
+                       }
 
-                       // Start UI (in main class loader)
-                       startUi(config);
+                       // Start app (in main class loader)
+                       startApp(config);
                        // Thread.sleep(10000);
 
                        // Start OSGi system
@@ -40,35 +51,49 @@ public class Main {
                }
        }
 
-       protected static Properties prepareConfig() throws Exception {
-               final File cachedir = createTemporaryCacheDir();
+       protected static Properties prepareConfig(String propertyFilePath)
+                       throws Exception {
+               // Format slc.home
+               String slcHome = System.getProperty("slc.home");
+               if (slcHome != null) {
+                       slcHome = new File(slcHome).getCanonicalPath();
+                       System.setProperty("slc.home", slcHome);
+               }
 
                // Load config
                Properties config = new Properties();
                InputStream in = null;
-               ;
+
                try {
                        in = Main.class
                                        .getResourceAsStream("/org/argeo/slc/detached/launcher/felix.properties");
                        config.load(in);
                } finally {
-                       if (in != null)
-                               in.close();
+                       IOUtils.closeQuietly(in);
                }
 
+               try {
+                       File file = new File(propertyFilePath);
+                       if (file.exists()) {
+                               in = new FileInputStream(propertyFilePath);
+                               config.load(in);
+                       }
+               } finally {
+                       IOUtils.closeQuietly(in);
+               }
+
+               // System properties have priority.
+               config.putAll(System.getProperties());
+
                // Perform variable substitution for system properties.
                for (Enumeration e = config.propertyNames(); e.hasMoreElements();) {
                        String name = (String) e.nextElement();
                        config.setProperty(name, org.apache.felix.main.Main.substVars(
                                        config.getProperty(name), name, null, config));
+                       if (log.isTraceEnabled())
+                               log.trace(name + "=" + config.getProperty(name));
                }
 
-               config.put(BundleCache.CACHE_PROFILE_DIR_PROP, cachedir
-                               .getAbsolutePath());
-
-               // System properties have priority.
-               config.putAll(System.getProperties());
-
                return config;
        }
 
@@ -100,16 +125,25 @@ public class Main {
                return felix;
        }
 
-       public static void startUi(Properties config) throws Exception {
-               String className = config.getProperty("argeo.scl.autoui.uiclass");
+       public static void startApp(Properties config) throws Exception {
+               String className = config.getProperty("slc.detached.appclass");
                String[] uiArgs = readArgumentsFromLine(config.getProperty(
-                               "argeo.slc.autoui.uiargs", ""));
+                               "slc.detached.appargs", ""));
+
+               if (className == null)
+                       throw new Exception(
+                                       "A main class has to be defined with the system property slc.detached.appclass");
 
                // Launch main method using reflection
                Class clss = Class.forName(className);
                Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
                Object[] mainArgs = { uiArgs };
                Method mainMethod = clss.getMethod("main", mainArgsClasses);
+               String[] passedArgs = (String[])mainArgs[0];
+               System.out.println("PASSED ARGS:");
+               for(int i=0;i<passedArgs.length;i++){
+                       System.out.println(passedArgs[i]);
+               }
                mainMethod.invoke(null, mainArgs);
        }