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
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.argeo
.jcr
.JcrUtils
;
28 import org
.argeo
.slc
.NameVersion
;
29 import org
.argeo
.slc
.SlcException
;
30 import org
.argeo
.slc
.aether
.AetherUtils
;
31 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
32 import org
.sonatype
.aether
.artifact
.Artifact
;
33 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
36 * Creates pde sources from a source {@link Artifact} with name
39 public class PdeSourcesIndexer
implements NodeIndexer
{
40 private Log log
= LogFactory
.getLog(PdeSourcesIndexer
.class);
42 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
44 // private ArtifactIndexer artifactIndexer;
45 // private JarFileIndexer jarFileIndexer;
47 // public PdeSourcesIndexer(){
48 // // ArtifactIndexer artifactIndexer,
49 // // JarFileIndexer jarFileIndexer) {
50 // // this.artifactIndexer = artifactIndexer;
51 // // this.jarFileIndexer = jarFileIndexer;
54 public Boolean
support(String path
) {
55 // TODO implement clean management of same name siblings
56 String name
= FilenameUtils
.getBaseName(path
);
57 // int lastInd = name.lastIndexOf("[");
59 // name = name.substring(0, lastInd);
60 return name
.endsWith("-sources")
61 && FilenameUtils
.getExtension(path
).equals("jar");
64 public void index(Node sourcesNode
) {
66 if (!support(sourcesNode
.getPath()))
69 packageSourcesAsPdeSource(sourcesNode
);
70 } catch (Exception e
) {
71 throw new SlcException("Cannot generate pde sources for node "
76 protected void packageSourcesAsPdeSource(Node sourcesNode
) {
77 Binary origBinary
= null;
78 Binary osgiBinary
= null;
80 Session session
= sourcesNode
.getSession();
81 Artifact sourcesArtifact
= AetherUtils
.convertPathToArtifact(
82 sourcesNode
.getPath(), null);
84 // read name version from manifest
85 Artifact osgiArtifact
= new DefaultArtifact(
86 sourcesArtifact
.getGroupId(),
87 sourcesArtifact
.getArtifactId(),
88 sourcesArtifact
.getExtension(),
89 sourcesArtifact
.getVersion());
90 String osgiPath
= MavenConventionsUtils
.artifactPath(
91 artifactBasePath
, osgiArtifact
);
92 osgiBinary
= session
.getNode(osgiPath
).getNode(Node
.JCR_CONTENT
)
93 .getProperty(Property
.JCR_DATA
).getBinary();
95 NameVersion nameVersion
= RepoUtils
.readNameVersion(osgiBinary
98 // create PDe sources artifact
99 Artifact pdeSourceArtifact
= new DefaultArtifact(
100 sourcesArtifact
.getGroupId(),
101 sourcesArtifact
.getArtifactId() + ".source",
102 sourcesArtifact
.getExtension(),
103 sourcesArtifact
.getVersion());
104 String targetSourceParentPath
= MavenConventionsUtils
105 .artifactParentPath(artifactBasePath
, pdeSourceArtifact
);
106 String targetSourceFileName
= MavenConventionsUtils
107 .artifactFileName(pdeSourceArtifact
);
108 // String targetSourceJarPath = targetSourceParentPath + '/'
109 // + targetSourceFileName;
111 Node targetSourceParentNode
= JcrUtils
.mkfolders(session
,
112 targetSourceParentPath
);
113 origBinary
= sourcesNode
.getNode(Node
.JCR_CONTENT
)
114 .getProperty(Property
.JCR_DATA
).getBinary();
115 byte[] targetJarBytes
= RepoUtils
.packageAsPdeSource(
116 origBinary
.getStream(), nameVersion
);
117 JcrUtils
.copyBytesAsFile(targetSourceParentNode
,
118 targetSourceFileName
, targetJarBytes
);
121 // Automagically done via the various listeners or manually
123 // Node targetSourceJarNode = session.getNode(targetSourceJarPath);
124 // artifactIndexer.index(targetSourceJarNode);
125 // jarFileIndexer.index(targetSourceJarNode);
126 if (log
.isTraceEnabled())
127 log
.trace("Created pde source artifact " + pdeSourceArtifact
128 + " from " + sourcesNode
);
130 } catch (RepositoryException e
) {
131 throw new SlcException("Cannot add PDE sources for " + sourcesNode
,
134 JcrUtils
.closeQuietly(origBinary
);
135 JcrUtils
.closeQuietly(osgiBinary
);