]> 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
e85f06eb73b4d81b90b5bf87bff9bdd298571550
[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.osgi.framework.Bundle;
57
58 /** <b>UNDER DEVELOPMENT</b>. Download and prepare an OSGi runtime */
59 public class RunInOsgi extends AbstractHandler implements SlcNames {
60 private final static Log log = LogFactory.getLog(RunInOsgi.class);
61
62 private Repository repository;
63 private String workspace;
64
65 public Object execute(ExecutionEvent event) throws ExecutionException {
66
67 InputStream jarStream = null;
68 OutputStream out = null;
69 Writer writer = null;
70 Session session = null;
71 try {
72 // Target directory
73 Bundle distPluginBundle = DistPlugin.getDefault().getBundle();
74 File baseDirectory = distPluginBundle.getBundleContext()
75 .getDataFile("runInOSGi");
76 if (baseDirectory.exists())
77 FileUtils.deleteDirectory(baseDirectory);
78 File targetDirectory = new File(baseDirectory, "lib");
79 targetDirectory.mkdirs();
80 File confDir = new File(baseDirectory, "configuration");
81 confDir.mkdirs();
82 File dataDir = new File(baseDirectory, "data");
83 dataDir.mkdirs();
84
85 session = repository.login(workspace);
86 NodeIterator bundles = listBundleArtifacts(session);
87
88 if (log.isDebugEnabled())
89 log.debug("## Copying to " + targetDirectory);
90
91 File equinoxJar = null;
92 List<File> files = new ArrayList<File>();
93 bundles: while (bundles.hasNext()) {
94 Node bundleNode = bundles.nextNode();
95 String symbolicName = JcrUtils.get(bundleNode,
96 SLC_SYMBOLIC_NAME);
97
98 // skip sources
99 if (symbolicName.endsWith(".source"))
100 continue bundles;
101 // skip eclipse
102 if (symbolicName.startsWith("org.eclipse")
103 && !symbolicName.equals("org.eclipse.osgi"))
104 continue bundles;
105 if (symbolicName.equals("org.polymap.openlayers.rap.widget"))
106 continue bundles;
107
108 File targetFile = new File(targetDirectory,
109 bundleNode.getName());
110 out = new FileOutputStream(targetFile);
111 jarStream = bundleNode.getNode(Node.JCR_CONTENT)
112 .getProperty(Property.JCR_DATA).getBinary().getStream();
113 IOUtils.copy(jarStream, out);
114 if (symbolicName.equals("org.eclipse.osgi"))
115 equinoxJar = targetFile;
116 else
117 files.add(targetFile);
118 if (log.isDebugEnabled())
119 log.debug("Copied " + targetFile.getName());
120
121 IOUtils.closeQuietly(out);
122 IOUtils.closeQuietly(jarStream);
123 }
124
125 StringBuffer osgiBundles = new StringBuffer("osgi.bundles=");
126 for (int i = 0; i < files.size(); i++) {
127 if (i != 0)
128 osgiBundles.append(',');
129 osgiBundles.append(files.get(i).getName());
130 }
131
132 File confIni = new File(confDir, "config.ini");
133 writer = new FileWriter(confIni);
134 writer.write(osgiBundles.toString());
135 IOUtils.closeQuietly(writer);
136
137 Map<String, String> configuration = new HashMap<String, String>();
138 configuration.put("osgi.configuration.area",
139 confDir.getCanonicalPath());
140 configuration.put("osgi.instance.area", dataDir.getCanonicalPath());
141 // Do clean
142 configuration.put("osgi.clean", "true");
143
144 JvmProcess osgiRuntime = new JvmProcess();
145 osgiRuntime.setExecDir(baseDirectory.getCanonicalPath());
146 if (equinoxJar == null)
147 throw new SlcException("Cannot find OSGi runtime.");
148 osgiRuntime.setMainJar(equinoxJar.getCanonicalPath());
149 osgiRuntime.arg("-configuration", confDir.getCanonicalPath())
150 .arg("-data", dataDir.getCanonicalPath())
151 .arg("-console", "7777").arg("-clean");
152 osgiRuntime.setLogCommand(true);
153 osgiRuntime.afterPropertiesSet();
154 osgiRuntime.run();
155
156 // Map<String, String> configuration = new HashMap<String,
157 // String>();
158 // configuration.put("osgi.configuration.area",
159 // confDir.getCanonicalPath());
160 // configuration.put("osgi.instance.area",
161 // dataDir.getCanonicalPath());
162 // // Do clean
163 // configuration.put("osgi.clean", "true");
164 // ServiceLoader<FrameworkFactory> ff = ServiceLoader
165 // .load(FrameworkFactory.class);
166 // FrameworkFactory frameworkFactory = ff.iterator().next();
167 // Framework framework =
168 // frameworkFactory.newFramework(configuration);
169 // framework.start();
170 // BundleContext testBundleContext = framework.getBundleContext();
171
172 // for (int i = 0; i < files.size(); i++) {
173 // testBundleContext.installBundle("file://"
174 // + files.get(i).getCanonicalPath());
175 // }
176 //
177 // Bundle[] testBundles = testBundleContext.getBundles();
178 // for (Bundle bundle : testBundles) {
179 // if (log.isDebugEnabled())
180 // log.debug(bundle.getSymbolicName() + " "
181 // + bundle.getVersion());
182 // }
183
184 } catch (Exception e) {
185 ErrorFeedback.show("Cannot run in OSGi", e);
186 } finally {
187 IOUtils.closeQuietly(jarStream);
188 IOUtils.closeQuietly(out);
189 IOUtils.closeQuietly(writer);
190 JcrUtils.logoutQuietly(session);
191 }
192
193 return null;
194 }
195
196 static NodeIterator listBundleArtifacts(Session session)
197 throws RepositoryException {
198 QueryManager queryManager = session.getWorkspace().getQueryManager();
199 QueryObjectModelFactory factory = queryManager.getQOMFactory();
200
201 final String bundleArtifactsSelector = "bundleArtifacts";
202 Selector source = factory.selector(SlcTypes.SLC_BUNDLE_ARTIFACT,
203 bundleArtifactsSelector);
204
205 Ordering order = factory.ascending(factory.propertyValue(
206 bundleArtifactsSelector, SlcNames.SLC_SYMBOLIC_NAME));
207 Ordering[] orderings = { order };
208
209 QueryObjectModel query = factory.createQuery(source, null, orderings,
210 null);
211
212 QueryResult result = query.execute();
213 return result.getNodes();
214 }
215
216 public void setRepository(Repository repository) {
217 this.repository = repository;
218 }
219
220 public void setWorkspace(String workspace) {
221 this.workspace = workspace;
222 }
223
224 }