2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.repo
.osgi
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
23 import java
.util
.TreeSet
;
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
;
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
;
48 * Make sure that all JCR metadata and Maven metadata are consistent for this
49 * group of OSGi bundles.
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";
56 private final static Log log
= LogFactory
.getLog(NormalizeGroup
.class);
58 private Repository repository
;
59 private String workspace
;
60 private String groupId
;
61 private String artifactBasePath
= "/";
62 private String version
= "1.3.0";
64 private ArtifactIndexer artifactIndexer
= new ArtifactIndexer();
65 private JarFileIndexer jarFileIndexer
= new JarFileIndexer();
67 private List
<String
> systemPackages
= OsgiProfile
.PROFILE_JAVA_SE_1_6
71 private Map
<String
, String
> packagesToSymbolicNames
= new HashMap
<String
, String
>();
72 private Map
<String
, Node
> symbolicNamesToNodes
= new HashMap
<String
, Node
>();
74 private Set
<Artifact
> binaries
= new TreeSet
<Artifact
>(
75 new ArtifactIdComparator());
76 private Set
<Artifact
> sources
= new TreeSet
<Artifact
>(
77 new ArtifactIdComparator());
80 Session session
= null;
82 session
= repository
.login(workspace
);
84 Node groupNode
= session
.getNode(MavenConventionsUtils
.groupPath(
85 artifactBasePath
, groupId
));
86 // TODO factorize with a traverser pattern?
87 for (NodeIterator artifactBases
= groupNode
.getNodes(); artifactBases
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();
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 "
111 // NodeIterator bundlesIt = listBundleArtifacts(session);
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());
121 int bundleCount
= symbolicNamesToNodes
.size();
122 if (log
.isDebugEnabled())
123 log
.debug("Indexed " + bundleCount
+ " bundles");
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());
136 Set
<Artifact
> indexes
= new TreeSet
<Artifact
>(
137 new ArtifactIdComparator());
138 Artifact indexArtifact
= writeIndex(session
, BINARIES_ARTIFACT_ID
,
140 indexes
.add(indexArtifact
);
141 indexArtifact
= writeIndex(session
, SOURCES_ARTIFACT_ID
, sources
);
142 indexes
.add(indexArtifact
);
144 writeIndex(session
, SDK_ARTIFACT_ID
, indexes
);
145 } catch (Exception e
) {
146 throw new SlcException("Cannot normalize group " + groupId
+ " in "
149 JcrUtils
.logoutQuietly(session
);
153 private Artifact
writeIndex(Session session
, String artifactId
,
154 Set
<Artifact
> artifacts
) throws RepositoryException
{
155 Artifact artifact
= new DefaultArtifact(groupId
, artifactId
, "pom",
157 String pom
= MavenConventionsUtils
.artifactsAsDependencyPom(artifact
,
159 Node node
= RepoUtils
.copyBytesAsArtifact(
160 session
.getNode(artifactBasePath
), artifact
, pom
.getBytes());
161 artifactIndexer
.index(node
);
166 protected void preProcessBundleArtifact(Node bundleNode
)
167 throws RepositoryException
{
168 artifactIndexer
.index(bundleNode
);
169 jarFileIndexer
.index(bundleNode
);
171 String symbolicName
= JcrUtils
.get(bundleNode
, SLC_SYMBOLIC_NAME
);
173 if (symbolicName
.endsWith(".source")) {
174 // TODO make a shared node with classifier 'sources'
175 sources
.add(RepoUtils
.asArtifact(bundleNode
));
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
);
187 symbolicNamesToNodes
.put(symbolicName
, bundleNode
);
188 binaries
.add(RepoUtils
.asArtifact(bundleNode
));
191 protected void processBundleArtifact(Node bundleNode
)
192 throws RepositoryException
{
193 Node artifactFolder
= bundleNode
.getParent();
194 String baseName
= FilenameUtils
.getBaseName(bundleNode
.getName());
197 String pom
= generatePomForBundle(bundleNode
);
198 String pomName
= baseName
+ ".pom";
199 Node pomNode
= JcrUtils
.copyBytesAsFile(artifactFolder
, pomName
,
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",
211 private String
generatePomForBundle(Node n
) throws RepositoryException
{
212 String ownSymbolicName
= JcrUtils
.get(n
, SLC_SYMBOLIC_NAME
);
214 StringBuffer p
= new StringBuffer();
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>");
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
))
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>")
237 .get(n
, SLC_
+ Constants
.BUNDLE_DESCRIPTION
))
238 .append("</description>\n");
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
251 if (JcrUtils
.check(importPackage
, SLC_OPTIONAL
))
252 optionalSymbolicNames
.add(dependencySymbolicName
);
254 dependenciesSymbolicNames
.add(dependencySymbolicName
);
256 if (!JcrUtils
.check(importPackage
, SLC_OPTIONAL
)
257 && !systemPackages
.contains(importPackage
))
258 log
.warn("No bundle found for pkg " + pkg
);
262 if (n
.hasNode(SLC_
+ Constants
.FRAGMENT_HOST
)) {
263 String fragmentHost
= JcrUtils
.get(
264 n
.getNode(SLC_
+ Constants
.FRAGMENT_HOST
),
266 dependenciesSymbolicNames
.add(fragmentHost
);
269 // TODO require bundles
271 List
<Node
> dependencyNodes
= new ArrayList
<Node
>();
272 for (String depSymbName
: dependenciesSymbolicNames
) {
273 if (depSymbName
.equals(ownSymbolicName
))
274 continue;// skip self
276 if (symbolicNamesToNodes
.containsKey(depSymbName
))
277 dependencyNodes
.add(symbolicNamesToNodes
.get(depSymbName
));
279 log
.warn("Could not find node for " + depSymbName
);
281 List
<Node
> optionalDependencyNodes
= new ArrayList
<Node
>();
282 for (String depSymbName
: optionalSymbolicNames
) {
283 if (symbolicNamesToNodes
.containsKey(depSymbName
))
284 optionalDependencyNodes
.add(symbolicNamesToNodes
287 log
.warn("Could not find node for " + depSymbName
);
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");
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");
315 p
.append("</dependencies>\n");
317 // Dependency management
318 p
.append("<dependencyManagement>\n");
319 p
.append("<dependencies>\n");
321 p
.append("</dependencies>\n");
322 p
.append("</dependencyManagement>\n");
324 p
.append("</project>\n");
328 public void setRepository(Repository repository
) {
329 this.repository
= repository
;
332 public void setWorkspace(String workspace
) {
333 this.workspace
= workspace
;
336 public void setGroupId(String groupId
) {
337 this.groupId
= groupId
;