]> 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 grouping issue for workspaces that have the same prefix.
[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.List;
26
27 import javax.jcr.Node;
28 import javax.jcr.NodeIterator;
29 import javax.jcr.Property;
30 import javax.jcr.Repository;
31 import javax.jcr.RepositoryException;
32 import javax.jcr.Session;
33 import javax.jcr.query.QueryManager;
34 import javax.jcr.query.QueryResult;
35 import javax.jcr.query.qom.Ordering;
36 import javax.jcr.query.qom.QueryObjectModel;
37 import javax.jcr.query.qom.QueryObjectModelFactory;
38 import javax.jcr.query.qom.Selector;
39
40 import org.apache.commons.io.FileUtils;
41 import org.apache.commons.io.IOUtils;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.argeo.jcr.JcrUtils;
45 import org.argeo.slc.jcr.SlcNames;
46 import org.argeo.slc.jcr.SlcTypes;
47 import org.eclipse.core.commands.AbstractHandler;
48 import org.eclipse.core.commands.ExecutionEvent;
49 import org.eclipse.core.commands.ExecutionException;
50
51 /** <b>UNDER DEVELOPMENT</b>. Download and prepare an OSGi runtime*/
52 public class RunInOsgi extends AbstractHandler implements SlcNames {
53 private final static Log log = LogFactory.getLog(RunInOsgi.class);
54
55 private Repository repository;
56 private String workspace;
57
58 public Object execute(ExecutionEvent event) throws ExecutionException {
59 File targetDirectory = new File(
60 "/home/mbaudier/dev/work/120517-ArgeoTP/" + workspace);
61
62 InputStream jarStream = null;
63 OutputStream out = null;
64 Writer writer = null;
65 Session session = null;
66 try {
67 FileUtils.deleteDirectory(targetDirectory);
68 targetDirectory.mkdirs();
69
70 session = repository.login(workspace);
71 NodeIterator bundles = listBundleArtifacts(session);
72
73 List<File> files = new ArrayList<File>();
74 bundles: while (bundles.hasNext()) {
75 Node bundleNode = bundles.nextNode();
76 String symbolicName = JcrUtils.get(bundleNode,
77 SLC_SYMBOLIC_NAME);
78
79 // skip sources
80 if (symbolicName.endsWith(".source"))
81 continue bundles;
82 // skip eclipse
83 if (symbolicName.startsWith("org.eclipse")
84 && !symbolicName.equals("org.eclipse.osgi"))
85 continue bundles;
86 if (symbolicName.equals("org.polymap.openlayers.rap.widget"))
87 continue bundles;
88
89 File targetFile = new File(targetDirectory,
90 bundleNode.getName());
91 out = new FileOutputStream(targetFile);
92 jarStream = bundleNode.getNode(Node.JCR_CONTENT)
93 .getProperty(Property.JCR_DATA).getBinary().getStream();
94 IOUtils.copy(jarStream, out);
95 if (!symbolicName.equals("org.eclipse.osgi"))
96 files.add(targetFile);
97 if (log.isDebugEnabled())
98 log.debug("Copied " + targetFile.getName());
99
100 IOUtils.closeQuietly(out);
101 IOUtils.closeQuietly(jarStream);
102 }
103
104 StringBuffer osgiBundles = new StringBuffer("osgi.bundles=");
105 for (int i = 0; i < files.size(); i++) {
106 if (i != 0)
107 osgiBundles.append(',');
108 osgiBundles.append(files.get(i).getName());
109 }
110
111 File confDir = new File(targetDirectory, "configuration");
112 confDir.mkdirs();
113 File confIni = new File(confDir, "config.ini");
114 writer = new FileWriter(confIni);
115 writer.write(osgiBundles.toString());
116 IOUtils.closeQuietly(writer);
117 } catch (Exception e) {
118 // TODO Auto-generated catch block
119 e.printStackTrace();
120 } finally {
121 IOUtils.closeQuietly(jarStream);
122 IOUtils.closeQuietly(out);
123 IOUtils.closeQuietly(writer);
124 JcrUtils.logoutQuietly(session);
125 }
126
127 return null;
128 }
129
130 static NodeIterator listBundleArtifacts(Session session)
131 throws RepositoryException {
132 QueryManager queryManager = session.getWorkspace().getQueryManager();
133 QueryObjectModelFactory factory = queryManager.getQOMFactory();
134
135 final String bundleArtifactsSelector = "bundleArtifacts";
136 Selector source = factory.selector(SlcTypes.SLC_BUNDLE_ARTIFACT,
137 bundleArtifactsSelector);
138
139 Ordering order = factory.ascending(factory.propertyValue(
140 bundleArtifactsSelector, SlcNames.SLC_SYMBOLIC_NAME));
141 Ordering[] orderings = { order };
142
143 QueryObjectModel query = factory.createQuery(source, null, orderings,
144 null);
145
146 QueryResult result = query.execute();
147 return result.getNodes();
148 }
149
150 public void setRepository(Repository repository) {
151 this.repository = repository;
152 }
153
154 public void setWorkspace(String workspace) {
155 this.workspace = workspace;
156 }
157
158 }