]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/PdeSourcesIndexer.java
Fix unclosed jar
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / PdeSourcesIndexer.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.repo;
17
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;
23
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;
34
35 /**
36 * Creates pde sources from a source {@link Artifact} with name
37 * "...-sources.jar"
38 */
39 public class PdeSourcesIndexer implements NodeIndexer {
40 private Log log = LogFactory.getLog(PdeSourcesIndexer.class);
41
42 private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
43
44 // private ArtifactIndexer artifactIndexer;
45 // private JarFileIndexer jarFileIndexer;
46
47 // public PdeSourcesIndexer(){
48 // // ArtifactIndexer artifactIndexer,
49 // // JarFileIndexer jarFileIndexer) {
50 // // this.artifactIndexer = artifactIndexer;
51 // // this.jarFileIndexer = jarFileIndexer;
52 // }
53
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("[");
58 // if (lastInd != -1)
59 // name = name.substring(0, lastInd);
60 return name.endsWith("-sources")
61 && FilenameUtils.getExtension(path).equals("jar");
62 }
63
64 public void index(Node sourcesNode) {
65 try {
66 if (!support(sourcesNode.getPath()))
67 return;
68
69 packageSourcesAsPdeSource(sourcesNode);
70 } catch (Exception e) {
71 throw new SlcException("Cannot generate pde sources for node "
72 + sourcesNode, e);
73 }
74 }
75
76 protected void packageSourcesAsPdeSource(Node sourcesNode) {
77 Binary origBinary = null;
78 Binary osgiBinary = null;
79 try {
80 Session session = sourcesNode.getSession();
81 Artifact sourcesArtifact = AetherUtils.convertPathToArtifact(
82 sourcesNode.getPath(), null);
83
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();
94
95 NameVersion nameVersion = RepoUtils.readNameVersion(osgiBinary
96 .getStream());
97
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;
110
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);
119
120 // reindex
121 // Automagically done via the various listeners or manually
122 // triggered.
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);
129
130 } catch (RepositoryException e) {
131 throw new SlcException("Cannot add PDE sources for " + sourcesNode,
132 e);
133 } finally {
134 JcrUtils.closeQuietly(origBinary);
135 JcrUtils.closeQuietly(osgiBinary);
136 }
137 }
138 }