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