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
.RepositoryException
;
9 import javax
.jcr
.Session
;
11 import org
.apache
.commons
.io
.IOUtils
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.jcr
.JcrUtils
;
15 import org
.argeo
.slc
.DefaultNameVersion
;
16 import org
.argeo
.slc
.SlcException
;
17 import org
.argeo
.slc
.repo
.OsgiFactory
;
18 import org
.argeo
.slc
.repo
.RepoUtils
;
19 import org
.eclipse
.aether
.artifact
.Artifact
;
20 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
21 import org
.eclipse
.aether
.util
.artifact
.SubArtifact
;
24 * BND wrapper based on a Maven artifact available from one of the configured
27 public class MavenWrapper
extends BndWrapper
implements Runnable
{
28 private final static Log log
= LogFactory
.getLog(MavenWrapper
.class);
30 private String sourceCoords
;
32 private OsgiFactory osgiFactory
;
34 private Boolean doNotModifySources
= false;
36 public MavenWrapper() {
41 public String
getVersion() {
42 String version
= super.getVersion();
45 return new DefaultArtifact(sourceCoords
).getVersion();
49 Session distSession
= null;
50 Session javaSession
= null;
51 InputStream in
= null;
52 ByteArrayOutputStream out
= null;
54 distSession
= osgiFactory
.openDistSession();
55 javaSession
= osgiFactory
.openJavaSession();
58 origArtifact
= osgiFactory
.getMaven(distSession
, sourceCoords
);
59 } catch (Exception e1
) {
60 origArtifact
= osgiFactory
.getMaven(distSession
, sourceCoords
61 + ":" + getVersion());
64 in
= origArtifact
.getNode(Node
.JCR_CONTENT
)
65 .getProperty(Property
.JCR_DATA
).getBinary().getStream();
66 out
= new ByteArrayOutputStream();
68 Node newJarNode
= RepoUtils
69 .copyBytesAsArtifact(javaSession
.getRootNode(),
70 getArtifact(), out
.toByteArray());
71 osgiFactory
.indexNode(newJarNode
);
72 newJarNode
.getSession().save();
74 if (log
.isDebugEnabled())
75 log
.debug("Wrapped Maven " + sourceCoords
+ " to "
76 + newJarNode
.getPath());
79 Artifact sourcesArtifact
= new SubArtifact(new DefaultArtifact(
80 sourceCoords
), "sources", null);
81 Node sourcesArtifactNode
;
84 sourcesArtifactNode
= osgiFactory
.getMaven(distSession
,
85 sourcesArtifact
.toString());
86 } catch (SlcException e
) {
87 // no sources available
91 IOUtils
.closeQuietly(in
);
92 in
= sourcesArtifactNode
.getNode(Node
.JCR_CONTENT
)
93 .getProperty(Property
.JCR_DATA
).getBinary().getStream();
95 if (doNotModifySources
)
96 pdeSource
= IOUtils
.toByteArray(in
);
98 pdeSource
= RepoUtils
.packageAsPdeSource(in
,
99 new DefaultNameVersion(getName(), getVersion()));
100 Node pdeSourceNode
= RepoUtils
.copyBytesAsArtifact(javaSession
101 .getRootNode(), new DefaultArtifact(getCategory(),
102 getName() + ".source", "jar", getVersion()), pdeSource
);
103 osgiFactory
.indexNode(pdeSourceNode
);
104 pdeSourceNode
.getSession().save();
106 if (log
.isDebugEnabled())
107 log
.debug("Wrapped Maven " + sourcesArtifact
108 + " to PDE sources " + pdeSourceNode
.getPath());
109 } catch (Exception e
) {
110 throw new SlcException("Cannot wrap Maven " + sourceCoords
, e
);
112 JcrUtils
.logoutQuietly(distSession
);
113 JcrUtils
.logoutQuietly(javaSession
);
114 IOUtils
.closeQuietly(in
);
115 IOUtils
.closeQuietly(out
);
119 public void setSourceCoords(String sourceCoords
) {
120 this.sourceCoords
= sourceCoords
;
123 public void setOsgiFactory(OsgiFactory osgiFactory
) {
124 this.osgiFactory
= osgiFactory
;
127 public void setDoNotModifySources(Boolean doNotModifySources
) {
128 this.doNotModifySources
= doNotModifySources
;