View Javadoc
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.repo.maven.AetherUtils;
31  import org.argeo.slc.repo.maven.MavenConventionsUtils;
32  import org.eclipse.aether.artifact.Artifact;
33  import org.eclipse.aether.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") && FilenameUtils.getExtension(path).equals("jar");
61  	}
62  
63  	public void index(Node sourcesNode) {
64  		try {
65  			if (!support(sourcesNode.getPath()))
66  				return;
67  
68  			packageSourcesAsPdeSource(sourcesNode);
69  		} catch (Exception e) {
70  			throw new SlcException("Cannot generate pde sources for node " + sourcesNode, e);
71  		}
72  	}
73  
74  	protected void packageSourcesAsPdeSource(Node sourcesNode) {
75  		Binary origBinary = null;
76  		Binary osgiBinary = null;
77  		try {
78  			Session session = sourcesNode.getSession();
79  			Artifact sourcesArtifact = AetherUtils.convertPathToArtifact(sourcesNode.getPath(), null);
80  
81  			// read name version from manifest
82  			Artifact osgiArtifact = new DefaultArtifact(sourcesArtifact.getGroupId(), sourcesArtifact.getArtifactId(),
83  					sourcesArtifact.getExtension(), sourcesArtifact.getVersion());
84  			String osgiPath = MavenConventionsUtils.artifactPath(artifactBasePath, osgiArtifact);
85  			osgiBinary = session.getNode(osgiPath).getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
86  
87  			NameVersion nameVersion = RepoUtils.readNameVersion(osgiBinary.getStream());
88  			if (nameVersion == null) {
89  				log.warn("Cannot package PDE sources for " + osgiPath + " as it is probably not an OSGi bundle");
90  				return;
91  			}
92  
93  			// create PDe sources artifact
94  			Artifact pdeSourceArtifact = new DefaultArtifact(sourcesArtifact.getGroupId(),
95  					sourcesArtifact.getArtifactId() + ".source", sourcesArtifact.getExtension(),
96  					sourcesArtifact.getVersion());
97  			String targetSourceParentPath = MavenConventionsUtils.artifactParentPath(artifactBasePath,
98  					pdeSourceArtifact);
99  			String targetSourceFileName = MavenConventionsUtils.artifactFileName(pdeSourceArtifact);
100 			// String targetSourceJarPath = targetSourceParentPath + '/'
101 			// + targetSourceFileName;
102 
103 			Node targetSourceParentNode = JcrUtils.mkfolders(session, targetSourceParentPath);
104 			origBinary = sourcesNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
105 			byte[] targetJarBytes = RepoUtils.packageAsPdeSource(origBinary.getStream(), nameVersion);
106 			JcrUtils.copyBytesAsFile(targetSourceParentNode, targetSourceFileName, targetJarBytes);
107 
108 			// reindex
109 			// Automagically done via the various listeners or manually
110 			// triggered.
111 			// Node targetSourceJarNode = session.getNode(targetSourceJarPath);
112 			// artifactIndexer.index(targetSourceJarNode);
113 			// jarFileIndexer.index(targetSourceJarNode);
114 			if (log.isTraceEnabled())
115 				log.trace("Created pde source artifact " + pdeSourceArtifact + " from " + sourcesNode);
116 
117 		} catch (RepositoryException e) {
118 			throw new SlcException("Cannot add PDE sources for " + sourcesNode, e);
119 		} finally {
120 			JcrUtils.closeQuietly(origBinary);
121 			JcrUtils.closeQuietly(osgiBinary);
122 		}
123 	}
124 }