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