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
.osgi
.framework
.Bundle
;
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);
62 private Repository repository
;
63 private String workspace
;
65 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
67 InputStream jarStream
= null;
68 OutputStream out
= null;
70 Session session
= null;
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");
82 File dataDir
= new File(baseDirectory
, "data");
85 session
= repository
.login(workspace
);
86 NodeIterator bundles
= listBundleArtifacts(session
);
88 if (log
.isDebugEnabled())
89 log
.debug("## Copying to " + targetDirectory
);
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
,
99 if (symbolicName
.endsWith(".source"))
102 if (symbolicName
.startsWith("org.eclipse")
103 && !symbolicName
.equals("org.eclipse.osgi"))
105 if (symbolicName
.equals("org.polymap.openlayers.rap.widget"))
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
;
117 files
.add(targetFile
);
118 if (log
.isDebugEnabled())
119 log
.debug("Copied " + targetFile
.getName());
121 IOUtils
.closeQuietly(out
);
122 IOUtils
.closeQuietly(jarStream
);
125 StringBuffer osgiBundles
= new StringBuffer("osgi.bundles=");
126 for (int i
= 0; i
< files
.size(); i
++) {
128 osgiBundles
.append(',');
129 osgiBundles
.append(files
.get(i
).getName());
132 File confIni
= new File(confDir
, "config.ini");
133 writer
= new FileWriter(confIni
);
134 writer
.write(osgiBundles
.toString());
135 IOUtils
.closeQuietly(writer
);
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());
142 configuration
.put("osgi.clean", "true");
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();
156 // Map<String, String> configuration = new HashMap<String,
158 // configuration.put("osgi.configuration.area",
159 // confDir.getCanonicalPath());
160 // configuration.put("osgi.instance.area",
161 // dataDir.getCanonicalPath());
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();
172 // for (int i = 0; i < files.size(); i++) {
173 // testBundleContext.installBundle("file://"
174 // + files.get(i).getCanonicalPath());
177 // Bundle[] testBundles = testBundleContext.getBundles();
178 // for (Bundle bundle : testBundles) {
179 // if (log.isDebugEnabled())
180 // log.debug(bundle.getSymbolicName() + " "
181 // + bundle.getVersion());
184 } catch (Exception e
) {
185 ErrorFeedback
.show("Cannot run in OSGi", e
);
187 IOUtils
.closeQuietly(jarStream
);
188 IOUtils
.closeQuietly(out
);
189 IOUtils
.closeQuietly(writer
);
190 JcrUtils
.logoutQuietly(session
);
196 static NodeIterator
listBundleArtifacts(Session session
)
197 throws RepositoryException
{
198 QueryManager queryManager
= session
.getWorkspace().getQueryManager();
199 QueryObjectModelFactory factory
= queryManager
.getQOMFactory();
201 final String bundleArtifactsSelector
= "bundleArtifacts";
202 Selector source
= factory
.selector(SlcTypes
.SLC_BUNDLE_ARTIFACT
,
203 bundleArtifactsSelector
);
205 Ordering order
= factory
.ascending(factory
.propertyValue(
206 bundleArtifactsSelector
, SlcNames
.SLC_SYMBOLIC_NAME
));
207 Ordering
[] orderings
= { order
};
209 QueryObjectModel query
= factory
.createQuery(source
, null, orderings
,
212 QueryResult result
= query
.execute();
213 return result
.getNodes();
216 public void setRepository(Repository repository
) {
217 this.repository
= repository
;
220 public void setWorkspace(String workspace
) {
221 this.workspace
= workspace
;