]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ProcessDistribution.java
Move SLC Support
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / argeo / slc / repo / osgi / ProcessDistribution.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.util.Iterator;
4
5 import javax.jcr.RepositoryException;
6 import javax.jcr.Session;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.jcr.JcrUtils;
11 import org.argeo.slc.CategorizedNameVersion;
12 import org.argeo.slc.DefaultNameVersion;
13 import org.argeo.slc.NameVersion;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.repo.ArgeoOsgiDistribution;
16 import org.argeo.slc.repo.ModularDistributionFactory;
17 import org.argeo.slc.repo.OsgiFactory;
18 import org.argeo.slc.repo.maven.MavenConventionsUtils;
19 import org.sonatype.aether.artifact.Artifact;
20 import org.sonatype.aether.util.artifact.DefaultArtifact;
21
22 /** Executes the processes required so that all managed bundles are available. */
23 public class ProcessDistribution implements Runnable {
24 private final static Log log = LogFactory.getLog(ProcessDistribution.class);
25
26 private ArgeoOsgiDistribution osgiDistribution;
27 private OsgiFactory osgiFactory;
28
29 public void run() {
30 Session javaSession = null;
31 try {
32 javaSession = osgiFactory.openJavaSession();
33 for (Iterator<? extends NameVersion> it = osgiDistribution
34 .nameVersions(); it.hasNext();)
35 processNameVersion(javaSession, it.next());
36
37 // Check sources
38 for (Iterator<? extends NameVersion> it = osgiDistribution
39 .nameVersions(); it.hasNext();) {
40 CategorizedNameVersion nv = (CategorizedNameVersion) it.next();
41 Artifact artifact = new DefaultArtifact(nv.getCategory(),
42 nv.getName() + ".source", "jar", nv.getVersion());
43 String path = MavenConventionsUtils.artifactPath("/", artifact);
44 if (!javaSession.itemExists(path))
45 log.warn("No source available for " + nv);
46 }
47
48 // explicitly create the corresponding modular distribution as we
49 // have here all necessary info.
50 ModularDistributionFactory mdf = new ModularDistributionFactory(
51 osgiFactory, osgiDistribution);
52 mdf.run();
53
54 } catch (RepositoryException e) {
55 throw new SlcException("Cannot process distribution "
56 + osgiDistribution, e);
57 } finally {
58 JcrUtils.logoutQuietly(javaSession);
59 }
60 }
61
62 protected void processNameVersion(Session javaSession,
63 NameVersion nameVersion) throws RepositoryException {
64 if (log.isTraceEnabled())
65 log.trace("Check " + nameVersion + "...");
66 if (!(nameVersion instanceof CategorizedNameVersion))
67 throw new SlcException("Unsupported type " + nameVersion.getClass());
68 CategorizedNameVersion nv = (CategorizedNameVersion) nameVersion;
69 Artifact artifact = new DefaultArtifact(nv.getCategory(), nv.getName(),
70 "jar", nv.getVersion());
71 String path = MavenConventionsUtils.artifactPath("/", artifact);
72 if (!javaSession.itemExists(path)) {
73 if (nv instanceof BndWrapper) {
74 if (log.isDebugEnabled())
75 log.debug("Run factory for : " + nv + "...");
76 ((BndWrapper) nv).getFactory().run();
77 } else if (nv instanceof Runnable) {
78 ((Runnable) nv).run();
79 } else {
80 log.warn("Skip unsupported : " + nv);
81 }
82 } else {
83 if (log.isTraceEnabled())
84 log.trace("Already available : " + nv);
85 }
86 }
87
88 /* DEPENDENCY INJECTION */
89 public void setOsgiDistribution(ArgeoOsgiDistribution osgiDistribution) {
90 this.osgiDistribution = osgiDistribution;
91 }
92
93 public void setOsgiFactory(OsgiFactory osgiFactory) {
94 this.osgiFactory = osgiFactory;
95 }
96 }