]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/commands/RunInOsgi.java
Clarify overall project structure.
[gpl/argeo-slc.git] / legacy / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / commands / RunInOsgi.java
1 package org.argeo.slc.client.ui.dist.commands;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.io.Writer;
8 import java.net.URL;
9
10 import javax.jcr.Node;
11 import javax.jcr.NodeIterator;
12 import javax.jcr.Repository;
13 import javax.jcr.Session;
14
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;
36
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);
40
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");
45
46 public final static String PARAM_WORKSPACE_NAME = "workspaceName";
47 public final static String PARAM_MODULE_PATH = "modulePath";
48
49 /* DEPENDENCY INJECTION */
50 private Repository repository;
51
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53
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";
59
60 InputStream jarStream = null;
61 OutputStream out = null;
62 Writer writer = null;
63 Session session = null;
64 try {
65 // Bundle distPluginBundle = DistPlugin.getDefault().getBundle();
66 // File baseDir = distPluginBundle.getBundleContext().getDataFile(
67 // "runInOSGi");
68 File baseDir = new File(System.getProperty("java.io.tmpdir")
69 + "/runInOSGi-" + System.getProperty("user.name"));
70 if (baseDir.exists())
71 FileUtils.deleteDirectory(baseDir);
72 File libDir = new File(baseDir, "lib");
73 libDir.mkdirs();
74 File confDir = new File(baseDir, "configuration");
75 confDir.mkdirs();
76 File dataDir = new File(baseDir, "data");
77 dataDir.mkdirs();
78
79 session = repository.login(workspace);
80
81 // NodeIterator bundles = listBundleArtifacts(session);
82 // if (log.isDebugEnabled())
83 // log.debug("## Copying to " + libDir);
84 //
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);
91 //
92 // // skip sources
93 // if (symbolicName.endsWith(".source"))
94 // continue bundles;
95 // // skip eclipse
96 // if (symbolicName.startsWith("org.eclipse")
97 // && !symbolicName.equals("org.eclipse.osgi"))
98 // continue bundles;
99 // if (symbolicName.equals("org.polymap.openlayers.rap.widget"))
100 // continue bundles;
101 //
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;
109 // else
110 // files.add(targetFile);
111 // if (log.isDebugEnabled())
112 // log.debug("Copied " + targetFile.getName());
113 //
114 // IOUtils.closeQuietly(out);
115 // IOUtils.closeQuietly(jarStream);
116 // }
117 //
118 // StringBuffer osgiBundles = new StringBuffer("osgi.bundles=");
119 // for (int i = 0; i < files.size(); i++) {
120 // if (i != 0)
121 // osgiBundles.append(',');
122 // osgiBundles.append(files.get(i).getName());
123 // }
124
125 String equinoxJar = null;
126
127 Node distModule = session.getNode(modulePath);
128 NodeIterator coordinates = distModule.getNode(SLC_MODULES)
129 .getNodes();
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");
134
135 conf.append("osgi.bundles=");
136 coords: while (coordinates.hasNext()) {
137 Node coord = coordinates.nextNode();
138 // String category =
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/"
145 + workspace + path;
146 if (log.isDebugEnabled())
147 log.debug(url);
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);
152 // if (!f.exists())
153 // FileUtils.copyURLToFile(new URL(url), f);
154 equinoxJar = f.getCanonicalPath();
155 continue coords;
156 }
157 conf.append(f.getName());
158 if (coordinates.hasNext())
159 conf.append(",\\\n");
160 }
161
162 File confIni = new File(confDir, "config.ini");
163 writer = new FileWriter(confIni);
164 writer.write(conf.toString());
165 IOUtils.closeQuietly(writer);
166
167 // Map<String, String> configuration = new HashMap<String,
168 // String>();
169 // configuration.put("osgi.configuration.area",
170 // confDir.getCanonicalPath());
171 // configuration.put("osgi.instance.area",
172 // dataDir.getCanonicalPath());
173 // // Do clean
174 // configuration.put("osgi.clean", "true");
175
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);
187 job.schedule();
188 // osgiRuntime.run();
189
190 // Map<String, String> configuration = new HashMap<String,
191 // String>();
192 // configuration.put("osgi.configuration.area",
193 // confDir.getCanonicalPath());
194 // configuration.put("osgi.instance.area",
195 // dataDir.getCanonicalPath());
196 // // Do clean
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();
205
206 // for (int i = 0; i < files.size(); i++) {
207 // testBundleContext.installBundle("file://"
208 // + files.get(i).getCanonicalPath());
209 // }
210 //
211 // Bundle[] testBundles = testBundleContext.getBundles();
212 // for (Bundle bundle : testBundles) {
213 // if (log.isDebugEnabled())
214 // log.debug(bundle.getSymbolicName() + " "
215 // + bundle.getVersion());
216 // }
217
218 } catch (Exception e) {
219 ErrorFeedback.show("Cannot run in OSGi", e);
220 } finally {
221 IOUtils.closeQuietly(jarStream);
222 IOUtils.closeQuietly(out);
223 IOUtils.closeQuietly(writer);
224 JcrUtils.logoutQuietly(session);
225 }
226
227 return null;
228 }
229
230 // private NodeIterator listBundleArtifacts(Session session)
231 // throws RepositoryException {
232
233 // QueryManager queryManager = session.getWorkspace().getQueryManager();
234 // QueryObjectModelFactory factory = queryManager.getQOMFactory();
235 //
236 // final String bundleArtifactsSelector = "bundleArtifacts";
237 // Selector source = factory.selector(SlcTypes.SLC_BUNDLE_ARTIFACT,
238 // bundleArtifactsSelector);
239 //
240 // Ordering order = factory.ascending(factory.propertyValue(
241 // bundleArtifactsSelector, SlcNames.SLC_SYMBOLIC_NAME));
242 // Ordering[] orderings = { order };
243 //
244 // QueryObjectModel query = factory.createQuery(source, null, orderings,
245 // null);
246 //
247 // QueryResult result = query.execute();
248 // return result.getNodes();
249 // }
250
251 private class RunInOsgiJob extends Job {
252 final JvmProcess osgiRuntime;
253
254 public RunInOsgiJob(JvmProcess osgiRuntime) {
255 super("OSGi Test");
256 this.osgiRuntime = osgiRuntime;
257 }
258
259 @Override
260 protected IStatus run(IProgressMonitor monitor) {
261 osgiRuntime.setSynchronous(false);
262 osgiRuntime.run();
263 while (!monitor.isCanceled()) {
264 try {
265 Thread.sleep(500);
266 } catch (InterruptedException e) {
267 // silent
268 }
269
270 if (monitor.isCanceled()) {
271 osgiRuntime.kill();
272 return Status.CANCEL_STATUS;
273 }
274 if (!osgiRuntime.isRunning())
275 break;
276 }
277 return Status.OK_STATUS;
278 }
279
280 }
281
282 public void setRepository(Repository repository) {
283 this.repository = repository;
284 }
285 }