Improve RunInOSGi command
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 5 Mar 2014 13:38:34 +0000 (13:38 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 5 Mar 2014 13:38:34 +0000 (13:38 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@6871 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

plugins/org.argeo.slc.client.ui.dist/META-INF/spring/commands.xml
plugins/org.argeo.slc.client.ui.dist/plugin.xml
plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/RunInOsgi.java
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java

index 858d11fbb92262639bcfc595d1606e15d6d782ae..dba791e79cba6ed42f3736e13d3cbafb6eafc5ab 100644 (file)
        <bean id="runInOSGi" class="org.argeo.slc.client.ui.dist.commands.RunInOsgi"
                scope="prototype">
                <property name="repository" ref="javaRepository" />
-               <property name="workspace" value="org.argeo.tp-1.3.1" />
+               <property name="workspace" value="org.argeo.tp-1.4.x" />
        </bean>
 
        <bean id="refreshDistributionsView"
index 7600f25a4f4fae9dca3ea68ad1b2f13122a45c57..f905e139b29d5c93049bfe2566cea6575f018593 100644 (file)
@@ -5,7 +5,7 @@
        <extension
                point="org.eclipse.ui.perspectives">
                <perspective
-            name="SLC Repo"
+            name="SLC Factory"
             class="org.argeo.slc.client.ui.dist.DistributionPerspective"
             id="org.argeo.slc.client.ui.dist.distributionPerspective"
             icon="icons/distribution_perspective.gif">
index bdca7c057815218892c1b76fece4d2c598425b33..e85f06eb73b4d81b90b5bf87bff9bdd298571550 100644 (file)
@@ -22,7 +22,9 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Writer;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
@@ -41,14 +43,19 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.eclipse.ui.ErrorFeedback;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.client.ui.dist.DistPlugin;
+import org.argeo.slc.core.execution.tasks.JvmProcess;
 import org.argeo.slc.jcr.SlcNames;
 import org.argeo.slc.jcr.SlcTypes;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
+import org.osgi.framework.Bundle;
 
-/** <b>UNDER DEVELOPMENT</b>. Download and prepare an OSGi runtime*/
+/** <b>UNDER DEVELOPMENT</b>. Download and prepare an OSGi runtime */
 public class RunInOsgi extends AbstractHandler implements SlcNames {
        private final static Log log = LogFactory.getLog(RunInOsgi.class);
 
@@ -56,20 +63,32 @@ public class RunInOsgi extends AbstractHandler implements SlcNames {
        private String workspace;
 
        public Object execute(ExecutionEvent event) throws ExecutionException {
-               File targetDirectory = new File(
-                               "/home/mbaudier/dev/work/120517-ArgeoTP/" + workspace);
 
                InputStream jarStream = null;
                OutputStream out = null;
                Writer writer = null;
                Session session = null;
                try {
-                       FileUtils.deleteDirectory(targetDirectory);
+                       // Target directory
+                       Bundle distPluginBundle = DistPlugin.getDefault().getBundle();
+                       File baseDirectory = distPluginBundle.getBundleContext()
+                                       .getDataFile("runInOSGi");
+                       if (baseDirectory.exists())
+                               FileUtils.deleteDirectory(baseDirectory);
+                       File targetDirectory = new File(baseDirectory, "lib");
                        targetDirectory.mkdirs();
+                       File confDir = new File(baseDirectory, "configuration");
+                       confDir.mkdirs();
+                       File dataDir = new File(baseDirectory, "data");
+                       dataDir.mkdirs();
 
                        session = repository.login(workspace);
                        NodeIterator bundles = listBundleArtifacts(session);
 
+                       if (log.isDebugEnabled())
+                               log.debug("## Copying to " + targetDirectory);
+
+                       File equinoxJar = null;
                        List<File> files = new ArrayList<File>();
                        bundles: while (bundles.hasNext()) {
                                Node bundleNode = bundles.nextNode();
@@ -92,7 +111,9 @@ public class RunInOsgi extends AbstractHandler implements SlcNames {
                                jarStream = bundleNode.getNode(Node.JCR_CONTENT)
                                                .getProperty(Property.JCR_DATA).getBinary().getStream();
                                IOUtils.copy(jarStream, out);
-                               if (!symbolicName.equals("org.eclipse.osgi"))
+                               if (symbolicName.equals("org.eclipse.osgi"))
+                                       equinoxJar = targetFile;
+                               else
                                        files.add(targetFile);
                                if (log.isDebugEnabled())
                                        log.debug("Copied " + targetFile.getName());
@@ -108,15 +129,60 @@ public class RunInOsgi extends AbstractHandler implements SlcNames {
                                osgiBundles.append(files.get(i).getName());
                        }
 
-                       File confDir = new File(targetDirectory, "configuration");
-                       confDir.mkdirs();
                        File confIni = new File(confDir, "config.ini");
                        writer = new FileWriter(confIni);
                        writer.write(osgiBundles.toString());
                        IOUtils.closeQuietly(writer);
+
+                       Map<String, String> configuration = new HashMap<String, String>();
+                       configuration.put("osgi.configuration.area",
+                                       confDir.getCanonicalPath());
+                       configuration.put("osgi.instance.area", dataDir.getCanonicalPath());
+                       // Do clean
+                       configuration.put("osgi.clean", "true");
+
+                       JvmProcess osgiRuntime = new JvmProcess();
+                       osgiRuntime.setExecDir(baseDirectory.getCanonicalPath());
+                       if (equinoxJar == null)
+                               throw new SlcException("Cannot find OSGi runtime.");
+                       osgiRuntime.setMainJar(equinoxJar.getCanonicalPath());
+                       osgiRuntime.arg("-configuration", confDir.getCanonicalPath())
+                                       .arg("-data", dataDir.getCanonicalPath())
+                                       .arg("-console", "7777").arg("-clean");
+                       osgiRuntime.setLogCommand(true);
+                       osgiRuntime.afterPropertiesSet();
+                       osgiRuntime.run();
+
+                       // Map<String, String> configuration = new HashMap<String,
+                       // String>();
+                       // configuration.put("osgi.configuration.area",
+                       // confDir.getCanonicalPath());
+                       // configuration.put("osgi.instance.area",
+                       // dataDir.getCanonicalPath());
+                       // // Do clean
+                       // configuration.put("osgi.clean", "true");
+                       // ServiceLoader<FrameworkFactory> ff = ServiceLoader
+                       // .load(FrameworkFactory.class);
+                       // FrameworkFactory frameworkFactory = ff.iterator().next();
+                       // Framework framework =
+                       // frameworkFactory.newFramework(configuration);
+                       // framework.start();
+                       // BundleContext testBundleContext = framework.getBundleContext();
+
+                       // for (int i = 0; i < files.size(); i++) {
+                       // testBundleContext.installBundle("file://"
+                       // + files.get(i).getCanonicalPath());
+                       // }
+                       //
+                       // Bundle[] testBundles = testBundleContext.getBundles();
+                       // for (Bundle bundle : testBundles) {
+                       // if (log.isDebugEnabled())
+                       // log.debug(bundle.getSymbolicName() + " "
+                       // + bundle.getVersion());
+                       // }
+
                } catch (Exception e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
+                       ErrorFeedback.show("Cannot run in OSGi", e);
                } finally {
                        IOUtils.closeQuietly(jarStream);
                        IOUtils.closeQuietly(out);
index 037b7ff4f67bc7a343ae6bd54d0e4ed260aa7583..01f11012fba228d8b49200020e5fea9c8a632db4 100644 (file)
@@ -29,12 +29,14 @@ import org.argeo.slc.SlcException;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.core.io.Resource;
 
+/** A Java Virtual Machine process. */
 public class JvmProcess extends SystemCall implements InitializingBean {
        private Properties systemProperties = new Properties();
        private List<Resource> classpath = new ArrayList<Resource>();
        private List<Resource> pBootClasspath = new ArrayList<Resource>();
        private Resource jvm = null;
        private String mainClass;
+       private String mainJar;
        private List<String> jvmArgs = new ArrayList<String>();
        private List<String> args = new ArrayList<String>();
 
@@ -67,14 +69,16 @@ public class JvmProcess extends SystemCall implements InitializingBean {
                        command.add(jvmArg);
                }
 
-               command.add("-cp");
-               StringBuffer buf = new StringBuffer("");
-               for (Resource res : classpath) {
-                       if (buf.length() != 0)
-                               buf.append(File.pathSeparatorChar);
-                       buf.append(asFile(res));
+               if (classpath.size() > 0) {
+                       command.add("-cp");
+                       StringBuffer buf = new StringBuffer("");
+                       for (Resource res : classpath) {
+                               if (buf.length() != 0)
+                                       buf.append(File.pathSeparatorChar);
+                               buf.append(asFile(res));
+                       }
+                       command.add(buf.toString());
                }
-               command.add(buf.toString());
 
                if (systemPropertiesFileProperty == null) {
                        // pass system properties as argument
@@ -112,7 +116,14 @@ public class JvmProcess extends SystemCall implements InitializingBean {
                }
 
                // Program
-               command.add(mainClass);
+               if (mainClass != null) {
+                       command.add(mainClass);
+               } else if (mainJar != null) {
+                       command.add("-jar");
+                       command.add(mainJar);
+               } else {
+                       throw new SlcException("No main class or jar defined");
+               }
 
                for (String arg : args) {
                        command.add(arg);
@@ -147,6 +158,21 @@ public class JvmProcess extends SystemCall implements InitializingBean {
                return tempFile;
        }
 
+       /** Append the argument (for chaining) */
+       @Override
+       public SystemCall arg(String arg) {
+               args.add(arg);
+               return this;
+       }
+
+       /** Append the argument (for chaining) */
+       @Override
+       public SystemCall arg(String arg, String value) {
+               args.add(arg);
+               args.add(value);
+               return this;
+       }
+
        public Properties getSystemProperties() {
                return systemProperties;
        }
@@ -187,6 +213,14 @@ public class JvmProcess extends SystemCall implements InitializingBean {
                this.mainClass = mainClass;
        }
 
+       public String getMainJar() {
+               return mainJar;
+       }
+
+       public void setMainJar(String mainJar) {
+               this.mainJar = mainJar;
+       }
+
        public List<String> getJvmArgs() {
                return jvmArgs;
        }