View Javadoc
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.eclipse.aether.artifact.Artifact;
19  import org.eclipse.aether.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  			// Check sources
37  			for (Iterator<? extends NameVersion> it = osgiDistribution
38  					.nameVersions(); it.hasNext();) {
39  				CategorizedNameVersion nv = (CategorizedNameVersion) it.next();
40  				Artifact artifact = new DefaultArtifact(nv.getCategory(),
41  						nv.getName() + ".source", "jar", nv.getVersion());
42  				String path = MavenConventionsUtils.artifactPath("/", artifact);
43  				if (!javaSession.itemExists(path))
44  					log.warn("No source available for " + nv);
45  			}
46  
47  			// explicitly create the corresponding modular distribution as we
48  			// have here all necessary info.
49  			ModularDistributionFactory mdf = new ModularDistributionFactory(
50  					osgiFactory, osgiDistribution);
51  			mdf.run();
52  
53  		} catch (RepositoryException e) {
54  			throw new SlcException("Cannot process distribution "
55  					+ osgiDistribution, e);
56  		} finally {
57  			JcrUtils.logoutQuietly(javaSession);
58  		}
59  	}
60  
61  	protected void processNameVersion(Session javaSession,
62  			NameVersion nameVersion) throws RepositoryException {
63  		if (log.isTraceEnabled())
64  			log.trace("Check " + nameVersion + "...");
65  		if (!(nameVersion instanceof CategorizedNameVersion))
66  			throw new SlcException("Unsupported type " + nameVersion.getClass());
67  		CategorizedNameVersion nv = (CategorizedNameVersion) nameVersion;
68  		Artifact artifact = new DefaultArtifact(nv.getCategory(), nv.getName(),
69  				"jar", nv.getVersion());
70  		String path = MavenConventionsUtils.artifactPath("/", artifact);
71  		if (!javaSession.itemExists(path)) {
72  			if (nv instanceof BndWrapper) {
73  				if (log.isDebugEnabled())
74  					log.debug("Run factory for   : " + nv + "...");
75  				((BndWrapper) nv).getFactory().run();
76  			} else if (nv instanceof Runnable) {
77  				((Runnable) nv).run();
78  			} else {
79  				log.warn("Skip unsupported   : " + nv);
80  			}
81  		} else {
82  			if (log.isTraceEnabled())
83  				log.trace("Already available : " + nv);
84  		}
85  	}
86  
87  	/* DEPENDENCY INJECTION */
88  	public void setOsgiDistribution(ArgeoOsgiDistribution osgiDistribution) {
89  		this.osgiDistribution = osgiDistribution;
90  	}
91  
92  	public void setOsgiFactory(OsgiFactory osgiFactory) {
93  		this.osgiFactory = osgiFactory;
94  	}
95  }