2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.dist
.commands
;
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
;
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
;
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
;
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);
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");
68 public final static String PARAM_WORKSPACE_NAME
= "workspaceName";
70 /* DEPENDENCY INJECTION */
71 private Repository repository
;
73 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
75 String workspace
= event
.getParameter(PARAM_WORKSPACE_NAME
);
77 InputStream jarStream
= null;
78 OutputStream out
= null;
80 Session session
= null;
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");
92 File dataDir
= new File(baseDirectory
, "data");
95 session
= repository
.login(workspace
);
96 NodeIterator bundles
= listBundleArtifacts(session
);
98 if (log
.isDebugEnabled())
99 log
.debug("## Copying to " + targetDirectory
);
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
,
109 if (symbolicName
.endsWith(".source"))
112 if (symbolicName
.startsWith("org.eclipse")
113 && !symbolicName
.equals("org.eclipse.osgi"))
115 if (symbolicName
.equals("org.polymap.openlayers.rap.widget"))
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
;
127 files
.add(targetFile
);
128 if (log
.isDebugEnabled())
129 log
.debug("Copied " + targetFile
.getName());
131 IOUtils
.closeQuietly(out
);
132 IOUtils
.closeQuietly(jarStream
);
135 StringBuffer osgiBundles
= new StringBuffer("osgi.bundles=");
136 for (int i
= 0; i
< files
.size(); i
++) {
138 osgiBundles
.append(',');
139 osgiBundles
.append(files
.get(i
).getName());
142 File confIni
= new File(confDir
, "config.ini");
143 writer
= new FileWriter(confIni
);
144 writer
.write(osgiBundles
.toString());
145 IOUtils
.closeQuietly(writer
);
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());
152 configuration
.put("osgi.clean", "true");
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();
166 // Map<String, String> configuration = new HashMap<String,
168 // configuration.put("osgi.configuration.area",
169 // confDir.getCanonicalPath());
170 // configuration.put("osgi.instance.area",
171 // dataDir.getCanonicalPath());
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();
182 // for (int i = 0; i < files.size(); i++) {
183 // testBundleContext.installBundle("file://"
184 // + files.get(i).getCanonicalPath());
187 // Bundle[] testBundles = testBundleContext.getBundles();
188 // for (Bundle bundle : testBundles) {
189 // if (log.isDebugEnabled())
190 // log.debug(bundle.getSymbolicName() + " "
191 // + bundle.getVersion());
194 } catch (Exception e
) {
195 ErrorFeedback
.show("Cannot run in OSGi", e
);
197 IOUtils
.closeQuietly(jarStream
);
198 IOUtils
.closeQuietly(out
);
199 IOUtils
.closeQuietly(writer
);
200 JcrUtils
.logoutQuietly(session
);
206 static NodeIterator
listBundleArtifacts(Session session
)
207 throws RepositoryException
{
208 QueryManager queryManager
= session
.getWorkspace().getQueryManager();
209 QueryObjectModelFactory factory
= queryManager
.getQOMFactory();
211 final String bundleArtifactsSelector
= "bundleArtifacts";
212 Selector source
= factory
.selector(SlcTypes
.SLC_BUNDLE_ARTIFACT
,
213 bundleArtifactsSelector
);
215 Ordering order
= factory
.ascending(factory
.propertyValue(
216 bundleArtifactsSelector
, SlcNames
.SLC_SYMBOLIC_NAME
));
217 Ordering
[] orderings
= { order
};
219 QueryObjectModel query
= factory
.createQuery(source
, null, orderings
,
222 QueryResult result
= query
.execute();
223 return result
.getNodes();
226 public void setRepository(Repository repository
) {
227 this.repository
= repository
;