]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ProcessDistribution.java
Make modular distribution more robust
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / 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.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
33 for (Iterator<? extends NameVersion> it = osgiDistribution
34 .nameVersions(); it.hasNext();)
35 processNameVersion(javaSession, it.next());
36
37 ModularDistributionFactory mdf = new ModularDistributionFactory(
38 javaSession, osgiDistribution);
39 mdf.run();
40
41 // TODO why is the created distribution not automatically indexed?
42 // osgiFactory.indexNode(node);
43 // javaSession.save();
44
45 // Node artifact = createDistributionArtifact(javaSession,
46 // osgiDistribution);
47
48 } catch (RepositoryException e) {
49 throw new SlcException("Cannot process distribution "
50 + osgiDistribution, e);
51 } finally {
52 JcrUtils.logoutQuietly(javaSession);
53 }
54 }
55
56 protected void processNameVersion(Session javaSession,
57 NameVersion nameVersion) throws RepositoryException {
58 if (log.isTraceEnabled())
59 log.trace("Check " + nameVersion + "...");
60 if (!(nameVersion instanceof CategorizedNameVersion))
61 throw new SlcException("Unsupported type " + nameVersion.getClass());
62 CategorizedNameVersion nv = (CategorizedNameVersion) nameVersion;
63 Artifact artifact = new DefaultArtifact(nv.getCategory(), nv.getName(),
64 "jar", nv.getVersion());
65 String path = MavenConventionsUtils.artifactPath("/", artifact);
66 if (!javaSession.itemExists(path)) {
67 if (nv instanceof BndWrapper) {
68 if (log.isDebugEnabled())
69 log.debug("Run factory for : " + nv + "...");
70 ((BndWrapper) nv).getFactory().run();
71 } else if (nv instanceof Runnable) {
72 ((Runnable) nv).run();
73 } else {
74 log.warn("Skip unsupported : " + nv);
75 }
76 } else {
77 if (log.isTraceEnabled())
78 log.trace("Already available : " + nv);
79 }
80 }
81
82 /* DEPENDENCY INJECTION */
83 public void setOsgiDistribution(ArgeoOsgiDistribution osgiDistribution) {
84 this.osgiDistribution = osgiDistribution;
85 }
86
87 public void setOsgiFactory(OsgiFactory osgiFactory) {
88 this.osgiFactory = osgiFactory;
89 }
90 }