View Javadoc
1   package org.argeo.slc.repo.osgi;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.InputStream;
5   
6   import javax.jcr.Node;
7   import javax.jcr.Property;
8   import javax.jcr.Session;
9   
10  import org.apache.commons.io.IOUtils;
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  import org.argeo.jcr.JcrUtils;
14  import org.argeo.slc.DefaultNameVersion;
15  import org.argeo.slc.SlcException;
16  import org.argeo.slc.repo.OsgiFactory;
17  import org.argeo.slc.repo.RepoUtils;
18  import org.eclipse.aether.artifact.Artifact;
19  import org.eclipse.aether.artifact.DefaultArtifact;
20  
21  /**
22   * BND wrapper based on a Maven artifact available from one of the configured
23   * repositories.
24   */
25  public class MavenWrapper extends BndWrapper implements Runnable {
26  	private final static Log log = LogFactory.getLog(MavenWrapper.class);
27  
28  	private String sourceCoords;
29  
30  	private OsgiFactory osgiFactory;
31  
32  	private Boolean doNotModifySources = false;
33  
34  	public MavenWrapper() {
35  		setFactory(this);
36  	}
37  
38  	@Override
39  	public String getVersion() {
40  		String version = super.getVersion();
41  		if (version != null)
42  			return version;
43  		return new DefaultArtifact(sourceCoords).getVersion();
44  	}
45  
46  	public void run() {
47  		Session distSession = null;
48  		Session javaSession = null;
49  		InputStream in = null;
50  		ByteArrayOutputStream out = null;
51  		try {
52  			distSession = osgiFactory.openDistSession();
53  			javaSession = osgiFactory.openJavaSession();
54  			Node origArtifact;
55  			try {
56  				origArtifact = osgiFactory.getMaven(distSession, sourceCoords);
57  			} catch (Exception e1) {
58  				origArtifact = osgiFactory.getMaven(distSession, sourceCoords
59  						+ ":" + getVersion());
60  			}
61  
62  			in = origArtifact.getNode(Node.JCR_CONTENT)
63  					.getProperty(Property.JCR_DATA).getBinary().getStream();
64  			out = new ByteArrayOutputStream();
65  			wrapJar(in, out);
66  			Node newJarNode = RepoUtils
67  					.copyBytesAsArtifact(javaSession.getRootNode(),
68  							getArtifact(), out.toByteArray());
69  			osgiFactory.indexNode(newJarNode);
70  			newJarNode.getSession().save();
71  
72  			if (log.isDebugEnabled())
73  				log.debug("Wrapped Maven " + sourceCoords + " to "
74  						+ newJarNode.getPath());
75  
76  			// sources
77  			Artifact sourcesArtifact = new SubArtifact(new DefaultArtifact(
78  					sourceCoords), "sources", null);
79  			Node sourcesArtifactNode;
80  			try {
81  
82  				sourcesArtifactNode = osgiFactory.getMaven(distSession,
83  						sourcesArtifact.toString());
84  			} catch (SlcException e) {
85  				// no sources available
86  				return;
87  			}
88  
89  			IOUtils.closeQuietly(in);
90  			in = sourcesArtifactNode.getNode(Node.JCR_CONTENT)
91  					.getProperty(Property.JCR_DATA).getBinary().getStream();
92  			byte[] pdeSource;
93  			if (doNotModifySources)
94  				pdeSource = IOUtils.toByteArray(in);
95  			else
96  				pdeSource = RepoUtils.packageAsPdeSource(in,
97  						new DefaultNameVersion(getName(), getVersion()));
98  			Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(javaSession
99  					.getRootNode(), new DefaultArtifact(getCategory(),
100 					getName() + ".source", "jar", getVersion()), pdeSource);
101 			osgiFactory.indexNode(pdeSourceNode);
102 			pdeSourceNode.getSession().save();
103 
104 			if (log.isDebugEnabled())
105 				log.debug("Wrapped Maven " + sourcesArtifact
106 						+ " to PDE sources " + pdeSourceNode.getPath());
107 		} catch (Exception e) {
108 			throw new SlcException("Cannot wrap Maven " + sourceCoords, e);
109 		} finally {
110 			JcrUtils.logoutQuietly(distSession);
111 			JcrUtils.logoutQuietly(javaSession);
112 			IOUtils.closeQuietly(in);
113 			IOUtils.closeQuietly(out);
114 		}
115 	}
116 
117 	public void setSourceCoords(String sourceCoords) {
118 		this.sourceCoords = sourceCoords;
119 	}
120 
121 	public void setOsgiFactory(OsgiFactory osgiFactory) {
122 		this.osgiFactory = osgiFactory;
123 	}
124 
125 	public void setDoNotModifySources(Boolean doNotModifySources) {
126 		this.doNotModifySources = doNotModifySources;
127 	}
128 
129 }