]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/RunInOsgi.java
Fix file download for rap. Add various links in the bundle detail editor.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / RunInOsgi.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.dist.commands;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.FileWriter;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.Writer;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.jcr.Node;
30 import javax.jcr.NodeIterator;
31 import javax.jcr.Property;
32 import javax.jcr.Repository;
33 import javax.jcr.RepositoryException;
34 import javax.jcr.Session;
35 import javax.jcr.query.QueryManager;
36 import javax.jcr.query.QueryResult;
37 import javax.jcr.query.qom.Ordering;
38 import javax.jcr.query.qom.QueryObjectModel;
39 import javax.jcr.query.qom.QueryObjectModelFactory;
40 import javax.jcr.query.qom.Selector;
41
42 import org.apache.commons.io.FileUtils;
43 import org.apache.commons.io.IOUtils;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.argeo.eclipse.ui.ErrorFeedback;
47 import org.argeo.jcr.JcrUtils;
48 import org.argeo.slc.SlcException;
49 import org.argeo.slc.client.ui.dist.DistPlugin;
50 import org.argeo.slc.core.execution.tasks.JvmProcess;
51 import org.argeo.slc.jcr.SlcNames;
52 import org.argeo.slc.jcr.SlcTypes;
53 import org.eclipse.core.commands.AbstractHandler;
54 import org.eclipse.core.commands.ExecutionEvent;
55 import org.eclipse.core.commands.ExecutionException;
56 import org.eclipse.jface.resource.ImageDescriptor;
57 import org.osgi.framework.Bundle;
58
59 /** <b>UNDER DEVELOPMENT</b>. Download and prepare an OSGi runtime */
60 public class RunInOsgi extends AbstractHandler implements SlcNames {
61 private final static Log log = LogFactory.getLog(RunInOsgi.class);
62
63 public final static String ID = DistPlugin.ID + ".runInOsgi";
64 public final static String DEFAULT_LABEL = "Run in OSGi";
65 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
66 .getImageDescriptor("icons/runInOsgi.gif");
67
68 public final static String PARAM_WORKSPACE_NAME = "workspaceName";
69
70 /* DEPENDENCY INJECTION */
71 private Repository repository;
72
73 public Object execute(ExecutionEvent event) throws ExecutionException {
74
75 String workspace = event.getParameter(PARAM_WORKSPACE_NAME);
76
77 InputStream jarStream = null;
78 OutputStream out = null;
79 Writer writer = null;
80 Session session = null;
81 try {
82 // Target directory
83 Bundle distPluginBundle = DistPlugin.getDefault().getBundle();
84 File baseDirectory = distPluginBundle.getBundleContext()
85 .getDataFile("runInOSGi");
86 if (baseDirectory.exists())
87 FileUtils.deleteDirectory(baseDirectory);
88 File targetDirectory = new File(baseDirectory, "lib");
89 targetDirectory.mkdirs();
90 File confDir = new File(baseDirectory, "configuration");
91 confDir.mkdirs();
92 File dataDir = new File(baseDirectory, "data");
93 dataDir.mkdirs();
94
95 session = repository.login(workspace);
96 NodeIterator bundles = listBundleArtifacts(session);
97
98 if (log.isDebugEnabled())
99 log.debug("## Copying to " + targetDirectory);
100
101 File equinoxJar = null;
102 List<File> files = new ArrayList<File>();
103 bundles: while (bundles.hasNext()) {
104 Node bundleNode = bundles.nextNode();
105 String symbolicName = JcrUtils.get(bundleNode,
106 SLC_SYMBOLIC_NAME);
107
108 // skip sources
109 if (symbolicName.endsWith(".source"))
110 continue bundles;
111 // skip eclipse
112 if (symbolicName.startsWith("org.eclipse")
113 && !symbolicName.equals("org.eclipse.osgi"))
114 continue bundles;
115 if (symbolicName.equals("org.polymap.openlayers.rap.widget"))
116 continue bundles;
117
118 File targetFile = new File(targetDirectory,
119 bundleNode.getName());
120 out = new FileOutputStream(targetFile);
121 jarStream = bundleNode.getNode(Node.JCR_CONTENT)
122 .getProperty(Property.JCR_DATA).getBinary().getStream();
123 IOUtils.copy(jarStream, out);
124 if (symbolicName.equals("org.eclipse.osgi"))
125 equinoxJar = targetFile;
126 else
127 files.add(targetFile);
128 if (log.isDebugEnabled())
129 log.debug("Copied " + targetFile.getName());
130
131 IOUtils.closeQuietly(out);
132 IOUtils.closeQuietly(jarStream);
133 }
134
135 StringBuffer osgiBundles = new StringBuffer("osgi.bundles=");
136 for (int i = 0; i < files.size(); i++) {
137 if (i != 0)
138 osgiBundles.append(',');
139 osgiBundles.append(files.get(i).getName());
140 }
141
142 File confIni = new File(confDir, "config.ini");
143 writer = new FileWriter(confIni);
144 writer.write(osgiBundles.toString());
145 IOUtils.closeQuietly(writer);
146
147 Map<String, String> configuration = new HashMap<String, String>();
148 configuration.put("osgi.configuration.area",
149 confDir.getCanonicalPath());
150 configuration.put("osgi.instance.area", dataDir.getCanonicalPath());
151 // Do clean
152 configuration.put("osgi.clean", "true");
153
154 JvmProcess osgiRuntime = new JvmProcess();
155 osgiRuntime.setExecDir(baseDirectory.getCanonicalPath());
156 if (equinoxJar == null)
157 throw new SlcException("Cannot find OSGi runtime.");
158 osgiRuntime.setMainJar(equinoxJar.getCanonicalPath());
159 osgiRuntime.arg("-configuration", confDir.getCanonicalPath())
160 .arg("-data", dataDir.getCanonicalPath())
161 .arg("-console", "7777").arg("-clean");
162 osgiRuntime.setLogCommand(true);
163 osgiRuntime.afterPropertiesSet();
164 osgiRuntime.run();
165
166 // Map<String, String> configuration = new HashMap<String,
167 // String>();
168 // configuration.put("osgi.configuration.area",
169 // confDir.getCanonicalPath());
170 // configuration.put("osgi.instance.area",
171 // dataDir.getCanonicalPath());
172 // // Do clean
173 // configuration.put("osgi.clean", "true");
174 // ServiceLoader<FrameworkFactory> ff = ServiceLoader
175 // .load(FrameworkFactory.class);
176 // FrameworkFactory frameworkFactory = ff.iterator().next();
177 // Framework framework =
178 // frameworkFactory.newFramework(configuration);
179 // framework.start();
180 // BundleContext testBundleContext = framework.getBundleContext();
181
182 // for (int i = 0; i < files.size(); i++) {
183 // testBundleContext.installBundle("file://"
184 // + files.get(i).getCanonicalPath());
185 // }
186 //
187 // Bundle[] testBundles = testBundleContext.getBundles();
188 // for (Bundle bundle : testBundles) {
189 // if (log.isDebugEnabled())
190 // log.debug(bundle.getSymbolicName() + " "
191 // + bundle.getVersion());
192 // }
193
194 } catch (Exception e) {
195 ErrorFeedback.show("Cannot run in OSGi", e);
196 } finally {
197 IOUtils.closeQuietly(jarStream);
198 IOUtils.closeQuietly(out);
199 IOUtils.closeQuietly(writer);
200 JcrUtils.logoutQuietly(session);
201 }
202
203 return null;
204 }
205
206 static NodeIterator listBundleArtifacts(Session session)
207 throws RepositoryException {
208 QueryManager queryManager = session.getWorkspace().getQueryManager();
209 QueryObjectModelFactory factory = queryManager.getQOMFactory();
210
211 final String bundleArtifactsSelector = "bundleArtifacts";
212 Selector source = factory.selector(SlcTypes.SLC_BUNDLE_ARTIFACT,
213 bundleArtifactsSelector);
214
215 Ordering order = factory.ascending(factory.propertyValue(
216 bundleArtifactsSelector, SlcNames.SLC_SYMBOLIC_NAME));
217 Ordering[] orderings = { order };
218
219 QueryObjectModel query = factory.createQuery(source, null, orderings,
220 null);
221
222 QueryResult result = query.execute();
223 return result.getNodes();
224 }
225
226 public void setRepository(Repository repository) {
227 this.repository = repository;
228 }
229 }