2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.repo
;
18 import javax
.jcr
.Binary
;
19 import javax
.jcr
.Node
;
20 import javax
.jcr
.Property
;
21 import javax
.jcr
.RepositoryException
;
22 import javax
.jcr
.Session
;
24 import org
.apache
.commons
.io
.FilenameUtils
;
25 import org
.argeo
.jcr
.JcrUtils
;
26 import org
.argeo
.slc
.NameVersion
;
27 import org
.argeo
.slc
.SlcException
;
28 import org
.argeo
.slc
.aether
.AetherUtils
;
29 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
30 import org
.sonatype
.aether
.artifact
.Artifact
;
31 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
34 * Creates pde sources from a source {@link Artifact} with name
37 public class PdeSourcesIndexer
implements NodeIndexer
{
38 // private Log log = LogFactory.getLog(PdeSourcesIndexer.class);
40 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
42 private ArtifactIndexer artifactIndexer
;
43 private JarFileIndexer jarFileIndexer
;
45 public PdeSourcesIndexer(ArtifactIndexer artifactIndexer
,
46 JarFileIndexer jarFileIndexer
) {
47 this.artifactIndexer
= artifactIndexer
;
48 this.jarFileIndexer
= jarFileIndexer
;
51 public Boolean
support(String path
) {
52 // TODO implement clean management of same name siblings
53 String name
= FilenameUtils
.getBaseName(path
);
54 // int lastInd = name.lastIndexOf("[");
56 // name = name.substring(0, lastInd);
57 return name
.endsWith("-sources")
58 && FilenameUtils
.getExtension(path
).equals("jar");
61 public void index(Node sourcesNode
) {
63 packageSourcesAsPdeSource(sourcesNode
);
64 } catch (Exception e
) {
65 throw new SlcException("Cannot generate pde sources for node "
70 protected void packageSourcesAsPdeSource(Node sourcesNode
) {
71 Binary origBinary
= null;
72 Binary osgiBinary
= null;
74 Session session
= sourcesNode
.getSession();
75 Artifact sourcesArtifact
= AetherUtils
.convertPathToArtifact(
76 sourcesNode
.getPath(), null);
78 // read name version from manifest
79 Artifact osgiArtifact
= new DefaultArtifact(
80 sourcesArtifact
.getGroupId(),
81 sourcesArtifact
.getArtifactId(),
82 sourcesArtifact
.getExtension(),
83 sourcesArtifact
.getVersion());
84 String osgiPath
= MavenConventionsUtils
.artifactPath(
85 artifactBasePath
, osgiArtifact
);
86 osgiBinary
= session
.getNode(osgiPath
).getNode(Node
.JCR_CONTENT
)
87 .getProperty(Property
.JCR_DATA
).getBinary();
89 NameVersion nameVersion
= RepoUtils
.readNameVersion(osgiBinary
92 // create PDe sources artifact
93 Artifact pdeSourceArtifact
= new DefaultArtifact(
94 sourcesArtifact
.getGroupId(),
95 sourcesArtifact
.getArtifactId() + ".source",
96 sourcesArtifact
.getExtension(),
97 sourcesArtifact
.getVersion());
98 String targetSourceParentPath
= MavenConventionsUtils
99 .artifactParentPath(artifactBasePath
, pdeSourceArtifact
);
100 String targetSourceFileName
= MavenConventionsUtils
101 .artifactFileName(pdeSourceArtifact
);
102 String targetSourceJarPath
= targetSourceParentPath
+ '/'
103 + targetSourceFileName
;
105 Node targetSourceParentNode
= JcrUtils
.mkfolders(session
,
106 targetSourceParentPath
);
107 origBinary
= sourcesNode
.getNode(Node
.JCR_CONTENT
)
108 .getProperty(Property
.JCR_DATA
).getBinary();
109 byte[] targetJarBytes
= RepoUtils
.packageAsPdeSource(
110 origBinary
.getStream(), nameVersion
);
111 JcrUtils
.copyBytesAsFile(targetSourceParentNode
,
112 targetSourceFileName
, targetJarBytes
);
115 Node targetSourceJarNode
= session
.getNode(targetSourceJarPath
);
116 artifactIndexer
.index(targetSourceJarNode
);
117 jarFileIndexer
.index(targetSourceJarNode
);
118 } catch (RepositoryException e
) {
119 throw new SlcException("Cannot add PDE sources for " + sourcesNode
,
122 JcrUtils
.closeQuietly(origBinary
);
123 JcrUtils
.closeQuietly(osgiBinary
);