1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.ByteArrayOutputStream
;
4 import java
.io
.InputStream
;
7 import javax
.jcr
.Property
;
8 import javax
.jcr
.Session
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.argeo
.api
.cms
.CmsLog
;
12 import org
.argeo
.jcr
.JcrUtils
;
13 import org
.argeo
.slc
.DefaultNameVersion
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.repo
.OsgiFactory
;
16 import org
.argeo
.slc
.repo
.RepoUtils
;
17 import org
.eclipse
.aether
.artifact
.Artifact
;
18 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
21 * BND wrapper based on a Maven artifact available from one of the configured
24 public class MavenWrapper
extends BndWrapper
implements Runnable
{
25 private final static CmsLog log
= CmsLog
.getLog(MavenWrapper
.class);
27 private String sourceCoords
;
29 private OsgiFactory osgiFactory
;
31 private Boolean doNotModifySources
= false;
33 public MavenWrapper() {
38 public String
getVersion() {
39 String version
= super.getVersion();
42 return new DefaultArtifact(sourceCoords
).getVersion();
46 Session distSession
= null;
47 Session javaSession
= null;
48 InputStream in
= null;
49 ByteArrayOutputStream out
= null;
51 distSession
= osgiFactory
.openDistSession();
52 javaSession
= osgiFactory
.openJavaSession();
55 origArtifact
= osgiFactory
.getMaven(distSession
, sourceCoords
);
56 } catch (Exception e1
) {
57 origArtifact
= osgiFactory
.getMaven(distSession
, sourceCoords
+ ":" + getVersion());
60 in
= origArtifact
.getNode(Node
.JCR_CONTENT
).getProperty(Property
.JCR_DATA
).getBinary().getStream();
61 out
= new ByteArrayOutputStream();
63 Node newJarNode
= RepoUtils
.copyBytesAsArtifact(javaSession
.getRootNode(), getArtifact(),
65 osgiFactory
.indexNode(newJarNode
);
66 newJarNode
.getSession().save();
68 if (log
.isDebugEnabled())
69 log
.debug("Wrapped Maven " + sourceCoords
+ " to " + newJarNode
.getPath());
72 Artifact sourcesArtifact
= new SubArtifact(new DefaultArtifact(sourceCoords
), "sources", null);
73 Node sourcesArtifactNode
;
76 sourcesArtifactNode
= osgiFactory
.getMaven(distSession
, sourcesArtifact
.toString());
77 } catch (SlcException e
) {
78 // no sources available
82 IOUtils
.closeQuietly(in
);
83 in
= sourcesArtifactNode
.getNode(Node
.JCR_CONTENT
).getProperty(Property
.JCR_DATA
).getBinary().getStream();
85 if (doNotModifySources
)
86 pdeSource
= IOUtils
.toByteArray(in
);
88 pdeSource
= RepoUtils
.packageAsPdeSource(in
, new DefaultNameVersion(getName(), getVersion()));
89 Node pdeSourceNode
= RepoUtils
.copyBytesAsArtifact(javaSession
.getRootNode(),
90 new DefaultArtifact(getCategory(), getName() + ".source", "jar", getVersion()), pdeSource
);
91 osgiFactory
.indexNode(pdeSourceNode
);
92 pdeSourceNode
.getSession().save();
94 if (log
.isDebugEnabled())
95 log
.debug("Wrapped Maven " + sourcesArtifact
+ " to PDE sources " + pdeSourceNode
.getPath());
96 } catch (Exception e
) {
97 throw new SlcException("Cannot wrap Maven " + sourceCoords
, e
);
99 JcrUtils
.logoutQuietly(distSession
);
100 JcrUtils
.logoutQuietly(javaSession
);
101 IOUtils
.closeQuietly(in
);
102 IOUtils
.closeQuietly(out
);
106 public void setSourceCoords(String sourceCoords
) {
107 this.sourceCoords
= sourceCoords
;
110 public String
getSourceCoords() {
114 public void setOsgiFactory(OsgiFactory osgiFactory
) {
115 this.osgiFactory
= osgiFactory
;
118 public void setDoNotModifySources(Boolean doNotModifySources
) {
119 this.doNotModifySources
= doNotModifySources
;