1 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
4 import java
.io
.FileWriter
;
5 import java
.io
.InputStream
;
6 import java
.io
.OutputStream
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.NodeIterator
;
12 import javax
.jcr
.Repository
;
13 import javax
.jcr
.Session
;
15 import org
.apache
.commons
.io
.FileUtils
;
16 import org
.apache
.commons
.io
.IOUtils
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
20 import org
.argeo
.jcr
.JcrUtils
;
21 import org
.argeo
.slc
.SlcException
;
22 import org
.argeo
.slc
.SlcNames
;
23 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
24 import org
.argeo
.slc
.core
.execution
.tasks
.JvmProcess
;
25 import org
.argeo
.slc
.repo
.RepoUtils
;
26 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
27 import org
.eclipse
.aether
.artifact
.Artifact
;
28 import org
.eclipse
.core
.commands
.AbstractHandler
;
29 import org
.eclipse
.core
.commands
.ExecutionEvent
;
30 import org
.eclipse
.core
.commands
.ExecutionException
;
31 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
32 import org
.eclipse
.core
.runtime
.IStatus
;
33 import org
.eclipse
.core
.runtime
.Status
;
34 import org
.eclipse
.core
.runtime
.jobs
.Job
;
35 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
37 /** <b>UNDER DEVELOPMENT</b>. Download and prepare an OSGi runtime */
38 public class RunInOsgi
extends AbstractHandler
implements SlcNames
{
39 private final static Log log
= LogFactory
.getLog(RunInOsgi
.class);
41 public final static String ID
= DistPlugin
.PLUGIN_ID
+ ".runInOsgi";
42 public final static String DEFAULT_LABEL
= "Run in OSGi";
43 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
44 .getImageDescriptor("icons/runInOsgi.gif");
46 public final static String PARAM_WORKSPACE_NAME
= "workspaceName";
47 public final static String PARAM_MODULE_PATH
= "modulePath";
49 /* DEPENDENCY INJECTION */
50 private Repository repository
;
52 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
54 String workspace
= event
.getParameter(PARAM_WORKSPACE_NAME
);
55 String modulePath
= event
.getParameter(PARAM_MODULE_PATH
);
56 String port
= System
.getProperty("argeo.server.port.http");
57 // String localMavenBase = System.getProperty("user.home")
58 // + "/.m2/repository";
60 InputStream jarStream
= null;
61 OutputStream out
= null;
63 Session session
= null;
65 // Bundle distPluginBundle = DistPlugin.getDefault().getBundle();
66 // File baseDir = distPluginBundle.getBundleContext().getDataFile(
68 File baseDir
= new File(System
.getProperty("java.io.tmpdir")
69 + "/runInOSGi-" + System
.getProperty("user.name"));
71 FileUtils
.deleteDirectory(baseDir
);
72 File libDir
= new File(baseDir
, "lib");
74 File confDir
= new File(baseDir
, "configuration");
76 File dataDir
= new File(baseDir
, "data");
79 session
= repository
.login(workspace
);
81 // NodeIterator bundles = listBundleArtifacts(session);
82 // if (log.isDebugEnabled())
83 // log.debug("## Copying to " + libDir);
85 // File equinoxJar = null;
86 // List<File> files = new ArrayList<File>();
87 // bundles: while (bundles.hasNext()) {
88 // Node bundleNode = bundles.nextNode();
89 // String symbolicName = JcrUtils.get(bundleNode,
90 // SLC_SYMBOLIC_NAME);
93 // if (symbolicName.endsWith(".source"))
96 // if (symbolicName.startsWith("org.eclipse")
97 // && !symbolicName.equals("org.eclipse.osgi"))
99 // if (symbolicName.equals("org.polymap.openlayers.rap.widget"))
102 // File targetFile = new File(libDir, bundleNode.getName());
103 // out = new FileOutputStream(targetFile);
104 // jarStream = bundleNode.getNode(Node.JCR_CONTENT)
105 // .getProperty(Property.JCR_DATA).getBinary().getStream();
106 // IOUtils.copy(jarStream, out);
107 // if (symbolicName.equals("org.eclipse.osgi"))
108 // equinoxJar = targetFile;
110 // files.add(targetFile);
111 // if (log.isDebugEnabled())
112 // log.debug("Copied " + targetFile.getName());
114 // IOUtils.closeQuietly(out);
115 // IOUtils.closeQuietly(jarStream);
118 // StringBuffer osgiBundles = new StringBuffer("osgi.bundles=");
119 // for (int i = 0; i < files.size(); i++) {
121 // osgiBundles.append(',');
122 // osgiBundles.append(files.get(i).getName());
125 String equinoxJar
= null;
127 Node distModule
= session
.getNode(modulePath
);
128 NodeIterator coordinates
= distModule
.getNode(SLC_MODULES
)
130 StringBuilder conf
= new StringBuilder(1024 * 1024);
131 conf
.append("osgi.clean=true\n");
132 conf
.append("osgi.console=7777\n");
133 // conf.append("osgi.console.enable.builtin=true\n");
135 conf
.append("osgi.bundles=");
136 coords
: while (coordinates
.hasNext()) {
137 Node coord
= coordinates
.nextNode();
139 // coord.getProperty(SLC_CATEGORY).getString();
140 String name
= coord
.getProperty(SLC_NAME
).getString();
141 String version
= coord
.getProperty(SLC_VERSION
).getString();
142 Artifact artifact
= RepoUtils
.asArtifact(coord
);
143 String path
= MavenConventionsUtils
.artifactPath("", artifact
);
144 String url
= "http://localhost:" + port
+ "/data/public/java/"
146 if (log
.isDebugEnabled())
148 File f
= new File(libDir
, name
+ "-" + version
+ ".jar");
149 FileUtils
.copyURLToFile(new URL(url
), f
);
150 if (name
.equals("org.eclipse.osgi")) {
151 // File f = new File(localMavenBase + path);
153 // FileUtils.copyURLToFile(new URL(url), f);
154 equinoxJar
= f
.getCanonicalPath();
157 conf
.append(f
.getName());
158 if (coordinates
.hasNext())
159 conf
.append(",\\\n");
162 File confIni
= new File(confDir
, "config.ini");
163 writer
= new FileWriter(confIni
);
164 writer
.write(conf
.toString());
165 IOUtils
.closeQuietly(writer
);
167 // Map<String, String> configuration = new HashMap<String,
169 // configuration.put("osgi.configuration.area",
170 // confDir.getCanonicalPath());
171 // configuration.put("osgi.instance.area",
172 // dataDir.getCanonicalPath());
174 // configuration.put("osgi.clean", "true");
176 JvmProcess osgiRuntime
= new JvmProcess();
177 osgiRuntime
.setExecDir(baseDir
.getCanonicalPath());
178 if (equinoxJar
== null)
179 throw new SlcException("Cannot find OSGi runtime.");
180 osgiRuntime
.setMainJar(equinoxJar
);
181 osgiRuntime
.arg("-configuration", confDir
.getCanonicalPath()).arg(
182 "-data", dataDir
.getCanonicalPath());
183 // .arg("-console", "7777").arg("-clean");
184 osgiRuntime
.setLogCommand(true);
185 osgiRuntime
.afterPropertiesSet();
186 Job job
= new RunInOsgiJob(osgiRuntime
);
188 // osgiRuntime.run();
190 // Map<String, String> configuration = new HashMap<String,
192 // configuration.put("osgi.configuration.area",
193 // confDir.getCanonicalPath());
194 // configuration.put("osgi.instance.area",
195 // dataDir.getCanonicalPath());
197 // configuration.put("osgi.clean", "true");
198 // ServiceLoader<FrameworkFactory> ff = ServiceLoader
199 // .load(FrameworkFactory.class);
200 // FrameworkFactory frameworkFactory = ff.iterator().next();
201 // Framework framework =
202 // frameworkFactory.newFramework(configuration);
203 // framework.start();
204 // BundleContext testBundleContext = framework.getBundleContext();
206 // for (int i = 0; i < files.size(); i++) {
207 // testBundleContext.installBundle("file://"
208 // + files.get(i).getCanonicalPath());
211 // Bundle[] testBundles = testBundleContext.getBundles();
212 // for (Bundle bundle : testBundles) {
213 // if (log.isDebugEnabled())
214 // log.debug(bundle.getSymbolicName() + " "
215 // + bundle.getVersion());
218 } catch (Exception e
) {
219 ErrorFeedback
.show("Cannot run in OSGi", e
);
221 IOUtils
.closeQuietly(jarStream
);
222 IOUtils
.closeQuietly(out
);
223 IOUtils
.closeQuietly(writer
);
224 JcrUtils
.logoutQuietly(session
);
230 // private NodeIterator listBundleArtifacts(Session session)
231 // throws RepositoryException {
233 // QueryManager queryManager = session.getWorkspace().getQueryManager();
234 // QueryObjectModelFactory factory = queryManager.getQOMFactory();
236 // final String bundleArtifactsSelector = "bundleArtifacts";
237 // Selector source = factory.selector(SlcTypes.SLC_BUNDLE_ARTIFACT,
238 // bundleArtifactsSelector);
240 // Ordering order = factory.ascending(factory.propertyValue(
241 // bundleArtifactsSelector, SlcNames.SLC_SYMBOLIC_NAME));
242 // Ordering[] orderings = { order };
244 // QueryObjectModel query = factory.createQuery(source, null, orderings,
247 // QueryResult result = query.execute();
248 // return result.getNodes();
251 private class RunInOsgiJob
extends Job
{
252 final JvmProcess osgiRuntime
;
254 public RunInOsgiJob(JvmProcess osgiRuntime
) {
256 this.osgiRuntime
= osgiRuntime
;
260 protected IStatus
run(IProgressMonitor monitor
) {
261 osgiRuntime
.setSynchronous(false);
263 while (!monitor
.isCanceled()) {
266 } catch (InterruptedException e
) {
270 if (monitor
.isCanceled()) {
272 return Status
.CANCEL_STATUS
;
274 if (!osgiRuntime
.isRunning())
277 return Status
.OK_STATUS
;
282 public void setRepository(Repository repository
) {
283 this.repository
= repository
;