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