View Javadoc
1   package org.argeo.slc.repo.osgi;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.InputStream;
6   import java.util.List;
7   import java.util.zip.ZipOutputStream;
8   
9   import javax.jcr.Node;
10  import javax.jcr.Property;
11  import javax.jcr.Session;
12  
13  import org.apache.commons.io.IOUtils;
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.argeo.jcr.JcrUtils;
17  import org.argeo.slc.DefaultNameVersion;
18  import org.argeo.slc.SlcException;
19  import org.argeo.slc.repo.OsgiFactory;
20  import org.argeo.slc.repo.RepoUtils;
21  import org.eclipse.aether.artifact.Artifact;
22  import org.eclipse.aether.artifact.DefaultArtifact;
23  
24  import aQute.bnd.osgi.Jar;
25  
26  public class UriWrapper extends BndWrapper implements Runnable {
27  	private final static Log log = LogFactory.getLog(UriWrapper.class);
28  
29  	private String uri;
30  	private String baseUri;
31  	private String versionSeparator = "-";
32  	private String extension = "jar";
33  
34  	private OsgiFactory osgiFactory;
35  
36  	private SourcesProvider sourcesProvider;
37  
38  	public UriWrapper() {
39  		setFactory(this);
40  	}
41  
42  	public void run() {
43  		Session distSession = null;
44  		Session javaSession = null;
45  		InputStream in = null;
46  		ByteArrayOutputStream out = null;
47  		Jar jar = null;
48  		try {
49  			distSession = osgiFactory.openDistSession();
50  			javaSession = osgiFactory.openJavaSession();
51  			if (uri == null) {
52  				uri = baseUri + '/' + getName() + versionSeparator
53  						+ getVersion() + "." + extension;
54  			}
55  			Node sourceArtifact = osgiFactory.getDist(distSession, uri);
56  
57  			// TODO factorize with Maven
58  			in = sourceArtifact.getNode(Node.JCR_CONTENT)
59  					.getProperty(Property.JCR_DATA).getBinary().getStream();
60  			out = new ByteArrayOutputStream();
61  			wrapJar(in, out);
62  			Node newJarNode = RepoUtils
63  					.copyBytesAsArtifact(javaSession.getRootNode(),
64  							getArtifact(), out.toByteArray());
65  			osgiFactory.indexNode(newJarNode);
66  			newJarNode.getSession().save();
67  			if (log.isDebugEnabled())
68  				log.debug("Wrapped " + uri + " to " + newJarNode.getPath());
69  
70  			// sources
71  			if (sourcesProvider != null) {
72  				IOUtils.closeQuietly(in);
73  				in = new ByteArrayInputStream(out.toByteArray());
74  				jar = new Jar(null, in);
75  				List<String> packages = jar.getPackages();
76  
77  				IOUtils.closeQuietly(out);
78  				out = new ByteArrayOutputStream();
79  				sourcesProvider
80  						.writeSources(packages, new ZipOutputStream(out));
81  
82  				IOUtils.closeQuietly(in);
83  				in = new ByteArrayInputStream(out.toByteArray());
84  				byte[] sourcesJar = RepoUtils.packageAsPdeSource(in,
85  						new DefaultNameVersion(this));
86  				Artifact sourcesArtifact = new DefaultArtifact(getArtifact()
87  						.getGroupId(), getArtifact().getArtifactId()
88  						+ ".source", "jar", getArtifact().getVersion());
89  				Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(
90  						javaSession.getRootNode(), sourcesArtifact, sourcesJar);
91  				sourcesJarNode.getSession().save();
92  
93  				if (log.isDebugEnabled())
94  					log.debug("Added sources " + sourcesArtifact
95  							+ " for bundle " + getArtifact());
96  			}
97  		} catch (Exception e) {
98  			throw new SlcException("Cannot wrap URI " + uri, e);
99  		} finally {
100 			IOUtils.closeQuietly(in);
101 			IOUtils.closeQuietly(out);
102 			JcrUtils.logoutQuietly(distSession);
103 			JcrUtils.logoutQuietly(javaSession);
104 			if (jar != null)
105 				jar.close();
106 		}
107 	}
108 
109 	public void setUri(String sourceCoords) {
110 		this.uri = sourceCoords;
111 	}
112 
113 	public void setOsgiFactory(OsgiFactory osgiFactory) {
114 		this.osgiFactory = osgiFactory;
115 	}
116 
117 	public void setBaseUri(String baseUri) {
118 		this.baseUri = baseUri;
119 	}
120 
121 	public void setVersionSeparator(String versionSeparator) {
122 		this.versionSeparator = versionSeparator;
123 	}
124 
125 	public void setSourcesProvider(SourcesProvider sourcesProvider) {
126 		this.sourcesProvider = sourcesProvider;
127 	}
128 }