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
.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 import org
.eclipse
.aether
.util
.artifact
.SubArtifact
;
23 * BND wrapper based on a Maven artifact available from one of the configured
26 public class MavenWrapper
extends BndWrapper
implements Runnable
{
27 private final static Log log
= LogFactory
.getLog(MavenWrapper
.class);
29 private String sourceCoords
;
31 private OsgiFactory osgiFactory
;
33 private Boolean doNotModifySources
= false;
35 public MavenWrapper() {
40 public String
getVersion() {
41 String version
= super.getVersion();
44 return new DefaultArtifact(sourceCoords
).getVersion();
48 Session distSession
= null;
49 Session javaSession
= null;
50 InputStream in
= null;
51 ByteArrayOutputStream out
= null;
53 distSession
= osgiFactory
.openDistSession();
54 javaSession
= osgiFactory
.openJavaSession();
57 origArtifact
= osgiFactory
.getMaven(distSession
, sourceCoords
);
58 } catch (Exception e1
) {
59 origArtifact
= osgiFactory
.getMaven(distSession
, sourceCoords
60 + ":" + getVersion());
63 in
= origArtifact
.getNode(Node
.JCR_CONTENT
)
64 .getProperty(Property
.JCR_DATA
).getBinary().getStream();
65 out
= new ByteArrayOutputStream();
67 Node newJarNode
= RepoUtils
68 .copyBytesAsArtifact(javaSession
.getRootNode(),
69 getArtifact(), out
.toByteArray());
70 osgiFactory
.indexNode(newJarNode
);
71 newJarNode
.getSession().save();
73 if (log
.isDebugEnabled())
74 log
.debug("Wrapped Maven " + sourceCoords
+ " to "
75 + newJarNode
.getPath());
78 Artifact sourcesArtifact
= new SubArtifact(new DefaultArtifact(
79 sourceCoords
), "sources", null);
80 Node sourcesArtifactNode
;
83 sourcesArtifactNode
= osgiFactory
.getMaven(distSession
,
84 sourcesArtifact
.toString());
85 } catch (SlcException e
) {
86 // no sources available
90 IOUtils
.closeQuietly(in
);
91 in
= sourcesArtifactNode
.getNode(Node
.JCR_CONTENT
)
92 .getProperty(Property
.JCR_DATA
).getBinary().getStream();
94 if (doNotModifySources
)
95 pdeSource
= IOUtils
.toByteArray(in
);
97 pdeSource
= RepoUtils
.packageAsPdeSource(in
,
98 new DefaultNameVersion(getName(), getVersion()));
99 Node pdeSourceNode
= RepoUtils
.copyBytesAsArtifact(javaSession
100 .getRootNode(), new DefaultArtifact(getCategory(),
101 getName() + ".source", "jar", getVersion()), pdeSource
);
102 osgiFactory
.indexNode(pdeSourceNode
);
103 pdeSourceNode
.getSession().save();
105 if (log
.isDebugEnabled())
106 log
.debug("Wrapped Maven " + sourcesArtifact
107 + " to PDE sources " + pdeSourceNode
.getPath());
108 } catch (Exception e
) {
109 throw new SlcException("Cannot wrap Maven " + sourceCoords
, e
);
111 JcrUtils
.logoutQuietly(distSession
);
112 JcrUtils
.logoutQuietly(javaSession
);
113 IOUtils
.closeQuietly(in
);
114 IOUtils
.closeQuietly(out
);
118 public void setSourceCoords(String sourceCoords
) {
119 this.sourceCoords
= sourceCoords
;
122 public void setOsgiFactory(OsgiFactory osgiFactory
) {
123 this.osgiFactory
= osgiFactory
;
126 public void setDoNotModifySources(Boolean doNotModifySources
) {
127 this.doNotModifySources
= doNotModifySources
;