]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/NormalizeGroup.java
Introduce NormalizeGroup
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / NormalizeGroup.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.repo.osgi;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.TreeSet;
24
25 import javax.jcr.Node;
26 import javax.jcr.NodeIterator;
27 import javax.jcr.Repository;
28 import javax.jcr.RepositoryException;
29 import javax.jcr.Session;
30
31 import org.apache.commons.io.FilenameUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.argeo.jcr.JcrUtils;
35 import org.argeo.slc.SlcException;
36 import org.argeo.slc.aether.ArtifactIdComparator;
37 import org.argeo.slc.jcr.SlcNames;
38 import org.argeo.slc.jcr.SlcTypes;
39 import org.argeo.slc.repo.ArtifactIndexer;
40 import org.argeo.slc.repo.JarFileIndexer;
41 import org.argeo.slc.repo.RepoUtils;
42 import org.argeo.slc.repo.maven.MavenConventionsUtils;
43 import org.osgi.framework.Constants;
44 import org.sonatype.aether.artifact.Artifact;
45 import org.sonatype.aether.util.artifact.DefaultArtifact;
46
47 /**
48 * Make sure that all JCR metadata and Maven metadata are consistent for this
49 * group of OSGi bundles.
50 */
51 public class NormalizeGroup implements Runnable, SlcNames {
52 public final static String BINARIES_ARTIFACT_ID = "binaries";
53 public final static String SOURCES_ARTIFACT_ID = "sources";
54 public final static String SDK_ARTIFACT_ID = "sdk";
55
56 private final static Log log = LogFactory.getLog(NormalizeGroup.class);
57
58 private Repository repository;
59 private String workspace;
60 private String groupId;
61 private String artifactBasePath = "/";
62 private String version = "1.3.0";
63
64 private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
65 private JarFileIndexer jarFileIndexer = new JarFileIndexer();
66
67 private List<String> systemPackages = OsgiProfile.PROFILE_JAVA_SE_1_6
68 .getSystemPackages();
69
70 // indexes
71 private Map<String, String> packagesToSymbolicNames = new HashMap<String, String>();
72 private Map<String, Node> symbolicNamesToNodes = new HashMap<String, Node>();
73
74 private Set<Artifact> binaries = new TreeSet<Artifact>(
75 new ArtifactIdComparator());
76 private Set<Artifact> sources = new TreeSet<Artifact>(
77 new ArtifactIdComparator());
78
79 public void run() {
80 Session session = null;
81 try {
82 session = repository.login(workspace);
83
84 Node groupNode = session.getNode(MavenConventionsUtils.groupPath(
85 artifactBasePath, groupId));
86 // TODO factorize with a traverser pattern?
87 for (NodeIterator artifactBases = groupNode.getNodes(); artifactBases
88 .hasNext();) {
89 Node artifactBase = artifactBases.nextNode();
90 if (artifactBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
91 for (NodeIterator artifactVersions = artifactBase
92 .getNodes(); artifactVersions.hasNext();) {
93 Node artifactVersion = artifactVersions.nextNode();
94 if (artifactVersion
95 .isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE))
96 for (NodeIterator files = artifactVersion
97 .getNodes(); files.hasNext();) {
98 Node file = files.nextNode();
99 if (file.isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT)) {
100 preProcessBundleArtifact(file);
101 file.getSession().save();
102 if (log.isDebugEnabled())
103 log.debug("Pre-processed "
104 + file.getName());
105 }
106
107 }
108 }
109 }
110 }
111 // NodeIterator bundlesIt = listBundleArtifacts(session);
112 //
113 // while (bundlesIt.hasNext()) {
114 // Node bundleNode = bundlesIt.nextNode();
115 // preProcessBundleArtifact(bundleNode);
116 // bundleNode.getSession().save();
117 // if (log.isDebugEnabled())
118 // log.debug("Pre-processed " + bundleNode.getName());
119 // }
120
121 int bundleCount = symbolicNamesToNodes.size();
122 if (log.isDebugEnabled())
123 log.debug("Indexed " + bundleCount + " bundles");
124
125 int count = 1;
126 for (Node bundleNode : symbolicNamesToNodes.values()) {
127 processBundleArtifact(bundleNode);
128 bundleNode.getSession().save();
129 if (log.isDebugEnabled())
130 log.debug(count + "/" + bundleCount + " Processed "
131 + bundleNode.getName());
132 count++;
133 }
134
135 // indexes
136 Set<Artifact> indexes = new TreeSet<Artifact>(
137 new ArtifactIdComparator());
138 Artifact indexArtifact = writeIndex(session, BINARIES_ARTIFACT_ID,
139 binaries);
140 indexes.add(indexArtifact);
141 indexArtifact = writeIndex(session, SOURCES_ARTIFACT_ID, sources);
142 indexes.add(indexArtifact);
143 // sdk
144 writeIndex(session, SDK_ARTIFACT_ID, indexes);
145 } catch (Exception e) {
146 throw new SlcException("Cannot normalize group " + groupId + " in "
147 + workspace, e);
148 } finally {
149 JcrUtils.logoutQuietly(session);
150 }
151 }
152
153 private Artifact writeIndex(Session session, String artifactId,
154 Set<Artifact> artifacts) throws RepositoryException {
155 Artifact artifact = new DefaultArtifact(groupId, artifactId, "pom",
156 version);
157 String pom = MavenConventionsUtils.artifactsAsDependencyPom(artifact,
158 artifacts);
159 Node node = RepoUtils.copyBytesAsArtifact(
160 session.getNode(artifactBasePath), artifact, pom.getBytes());
161 artifactIndexer.index(node);
162 session.save();
163 return artifact;
164 }
165
166 protected void preProcessBundleArtifact(Node bundleNode)
167 throws RepositoryException {
168 artifactIndexer.index(bundleNode);
169 jarFileIndexer.index(bundleNode);
170
171 String symbolicName = JcrUtils.get(bundleNode, SLC_SYMBOLIC_NAME);
172
173 if (symbolicName.endsWith(".source")) {
174 // TODO make a shared node with classifier 'sources'
175 sources.add(RepoUtils.asArtifact(bundleNode));
176 return;
177 }
178
179 NodeIterator exportPackages = bundleNode.getNodes(SLC_
180 + Constants.EXPORT_PACKAGE);
181 while (exportPackages.hasNext()) {
182 Node exportPackage = exportPackages.nextNode();
183 String pkg = JcrUtils.get(exportPackage, SLC_NAME);
184 packagesToSymbolicNames.put(pkg, symbolicName);
185 }
186
187 symbolicNamesToNodes.put(symbolicName, bundleNode);
188 binaries.add(RepoUtils.asArtifact(bundleNode));
189 }
190
191 protected void processBundleArtifact(Node bundleNode)
192 throws RepositoryException {
193 Node artifactFolder = bundleNode.getParent();
194 String baseName = FilenameUtils.getBaseName(bundleNode.getName());
195
196 // pom
197 String pom = generatePomForBundle(bundleNode);
198 String pomName = baseName + ".pom";
199 Node pomNode = JcrUtils.copyBytesAsFile(artifactFolder, pomName,
200 pom.getBytes());
201
202 // checksum
203 String bundleSha = JcrUtils.checksumFile(bundleNode, "SHA-1");
204 JcrUtils.copyBytesAsFile(artifactFolder,
205 bundleNode.getName() + ".sha1", bundleSha.getBytes());
206 String pomSha = JcrUtils.checksumFile(bundleNode, "SHA-1");
207 JcrUtils.copyBytesAsFile(artifactFolder, pomNode.getName() + ".sha1",
208 pomSha.getBytes());
209 }
210
211 private String generatePomForBundle(Node n) throws RepositoryException {
212 String ownSymbolicName = JcrUtils.get(n, SLC_SYMBOLIC_NAME);
213
214 StringBuffer p = new StringBuffer();
215
216 // XML header
217 p.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
218 p.append("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n");
219 p.append("<modelVersion>4.0.0</modelVersion>");
220
221 // Artifact
222 // p.append("<parent><groupId>org.argeo</groupId><artifactId>parent</artifactId><version>1.2.0</version></parent>\n");
223 p.append("<groupId>").append(JcrUtils.get(n, SLC_GROUP_ID))
224 .append("</groupId>\n");
225 p.append("<artifactId>").append(JcrUtils.get(n, SLC_ARTIFACT_ID))
226 .append("</artifactId>\n");
227 p.append("<version>").append(JcrUtils.get(n, SLC_ARTIFACT_VERSION))
228 .append("</version>\n");
229 p.append("<packaging>pom</packaging>\n");
230 if (n.hasProperty(SLC_ + Constants.BUNDLE_NAME))
231 p.append("<name>")
232 .append(JcrUtils.get(n, SLC_ + Constants.BUNDLE_NAME))
233 .append("</name>\n");
234 if (n.hasProperty(SLC_ + Constants.BUNDLE_DESCRIPTION))
235 p.append("<description>")
236 .append(JcrUtils
237 .get(n, SLC_ + Constants.BUNDLE_DESCRIPTION))
238 .append("</description>\n");
239
240 // Dependencies
241 Set<String> dependenciesSymbolicNames = new TreeSet<String>();
242 Set<String> optionalSymbolicNames = new TreeSet<String>();
243 NodeIterator importPackages = n.getNodes(SLC_
244 + Constants.IMPORT_PACKAGE);
245 while (importPackages.hasNext()) {
246 Node importPackage = importPackages.nextNode();
247 String pkg = JcrUtils.get(importPackage, SLC_NAME);
248 if (packagesToSymbolicNames.containsKey(pkg)) {
249 String dependencySymbolicName = packagesToSymbolicNames
250 .get(pkg);
251 if (JcrUtils.check(importPackage, SLC_OPTIONAL))
252 optionalSymbolicNames.add(dependencySymbolicName);
253 else
254 dependenciesSymbolicNames.add(dependencySymbolicName);
255 } else {
256 if (!JcrUtils.check(importPackage, SLC_OPTIONAL)
257 && !systemPackages.contains(importPackage))
258 log.warn("No bundle found for pkg " + pkg);
259 }
260 }
261
262 if (n.hasNode(SLC_ + Constants.FRAGMENT_HOST)) {
263 String fragmentHost = JcrUtils.get(
264 n.getNode(SLC_ + Constants.FRAGMENT_HOST),
265 SLC_SYMBOLIC_NAME);
266 dependenciesSymbolicNames.add(fragmentHost);
267 }
268
269 // TODO require bundles
270
271 List<Node> dependencyNodes = new ArrayList<Node>();
272 for (String depSymbName : dependenciesSymbolicNames) {
273 if (depSymbName.equals(ownSymbolicName))
274 continue;// skip self
275
276 if (symbolicNamesToNodes.containsKey(depSymbName))
277 dependencyNodes.add(symbolicNamesToNodes.get(depSymbName));
278 else
279 log.warn("Could not find node for " + depSymbName);
280 }
281 List<Node> optionalDependencyNodes = new ArrayList<Node>();
282 for (String depSymbName : optionalSymbolicNames) {
283 if (symbolicNamesToNodes.containsKey(depSymbName))
284 optionalDependencyNodes.add(symbolicNamesToNodes
285 .get(depSymbName));
286 else
287 log.warn("Could not find node for " + depSymbName);
288 }
289
290 p.append("<dependencies>\n");
291 for (Node dependencyNode : dependencyNodes) {
292 p.append("<dependency>\n");
293 p.append("\t<groupId>")
294 .append(JcrUtils.get(dependencyNode, SLC_GROUP_ID))
295 .append("</groupId>\n");
296 p.append("\t<artifactId>")
297 .append(JcrUtils.get(dependencyNode, SLC_ARTIFACT_ID))
298 .append("</artifactId>\n");
299 p.append("</dependency>\n");
300 }
301
302 if (optionalDependencyNodes.size() > 0)
303 p.append("<!-- OPTIONAL -->\n");
304 for (Node dependencyNode : optionalDependencyNodes) {
305 p.append("<dependency>\n");
306 p.append("\t<groupId>")
307 .append(JcrUtils.get(dependencyNode, SLC_GROUP_ID))
308 .append("</groupId>\n");
309 p.append("\t<artifactId>")
310 .append(JcrUtils.get(dependencyNode, SLC_ARTIFACT_ID))
311 .append("</artifactId>\n");
312 p.append("\t<optional>true</optional>\n");
313 p.append("</dependency>\n");
314 }
315 p.append("</dependencies>\n");
316
317 // Dependency management
318 p.append("<dependencyManagement>\n");
319 p.append("<dependencies>\n");
320 // TODO import SDK
321 p.append("</dependencies>\n");
322 p.append("</dependencyManagement>\n");
323
324 p.append("</project>\n");
325 return p.toString();
326 }
327
328 public void setRepository(Repository repository) {
329 this.repository = repository;
330 }
331
332 public void setWorkspace(String workspace) {
333 this.workspace = workspace;
334 }
335
336 public void setGroupId(String groupId) {
337 this.groupId = groupId;
338 }
339
340 }