X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.repo%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Frepo%2Fmaven%2FImportMavenDependencies.java;h=c6ae576d59f3452d74acca15d6924cec0788172e;hb=5c3aaff50cc324fbfce95b6d961d844e4e6315ff;hp=a6eb8df50d2eb192140fa77a32cd2f12c5d09205;hpb=5f38bb8fc715b7fdf124c096a190f15eff42910e;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java index a6eb8df50..c6ae576d5 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java @@ -1,44 +1,50 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.slc.repo.maven; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.InputStream; -import java.util.ArrayList; import java.util.Comparator; import java.util.HashSet; -import java.util.List; import java.util.Properties; import java.util.Set; import java.util.TreeSet; -import java.util.jar.Attributes; -import java.util.jar.Attributes.Name; -import java.util.jar.JarInputStream; -import java.util.jar.Manifest; import javax.jcr.Binary; import javax.jcr.Node; import javax.jcr.NodeIterator; -import javax.jcr.PathNotFoundException; import javax.jcr.Property; +import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.nodetype.NodeType; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import org.apache.commons.io.FilenameUtils; -import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.jcr.JcrUtils; import org.argeo.slc.SlcException; import org.argeo.slc.aether.AetherTemplate; -import org.argeo.slc.repo.RepoNames; -import org.argeo.slc.repo.RepoTypes; -import org.osgi.framework.Constants; -import org.osgi.framework.Version; +import org.argeo.slc.jcr.SlcNames; +import org.argeo.slc.jcr.SlcTypes; +import org.argeo.slc.repo.ArtifactIndexer; +import org.argeo.slc.repo.JarFileIndexer; +import org.argeo.slc.repo.RepoConstants; import org.sonatype.aether.artifact.Artifact; import org.sonatype.aether.graph.DependencyNode; import org.sonatype.aether.util.artifact.DefaultArtifact; @@ -54,15 +60,38 @@ public class ImportMavenDependencies implements Runnable { private String rootCoordinates; private Set excludedArtifacts = new HashSet(); - private Session jcrSession; - private String artifactBasePath = "/slc/repo/artifacts"; - private String distributionsBasePath = "/slc/repo/distributions"; + private Repository repository; + private String workspace; + + private String artifactBasePath = RepoConstants.ARTIFACTS_BASE_PATH; + private String distributionsBasePath = RepoConstants.DISTRIBUTIONS_BASE_PATH; private String distributionName; + private ArtifactIndexer artifactIndexer = new ArtifactIndexer(); + private JarFileIndexer jarFileIndexer = new JarFileIndexer(); + public void run() { - log.debug(jcrSession.getUserID()); + // resolve Set artifacts = resolveDistribution(); - syncDistribution(artifacts); + + // sync + Session session = null; + try { + session = JcrUtils.loginOrCreateWorkspace(repository, workspace); + // clear + NodeIterator nit = session.getNode(artifactBasePath).getNodes(); + while (nit.hasNext()) { + Node node = nit.nextNode(); + if (node.isNodeType(NodeType.NT_FOLDER)) + node.remove(); + } + session.save(); + syncDistribution(session, artifacts); + } catch (Exception e) { + throw new SlcException("Cannot import distribution", e); + } finally { + JcrUtils.logoutQuietly(session); + } } public Set resolveDistribution() { @@ -117,7 +146,7 @@ public class ImportMavenDependencies implements Runnable { } } - protected void syncDistribution(Set artifacts) { + protected void syncDistribution(Session jcrSession, Set artifacts) { Long begin = System.currentTimeMillis(); try { JcrUtils.mkdirs(jcrSession, artifactBasePath); @@ -126,7 +155,7 @@ public class ImportMavenDependencies implements Runnable { artifacts: for (Artifact artifact : artifacts) { File file = artifact.getFile(); if (file == null) { - log.warn("File not found for " + artifact); + // log.warn("File not found for " + artifact); file = artifactToFile(artifact); @@ -138,12 +167,13 @@ public class ImportMavenDependencies implements Runnable { } try { - String parentPath = artifactBasePath + '/' + String parentPath = artifactBasePath + + (artifactBasePath.endsWith("/") ? "" : "/") + artifactParentPath(artifact); Node parentNode; if (!jcrSession.itemExists(parentPath)) { parentNode = JcrUtils.mkdirs(jcrSession, parentPath, - NodeType.NT_FOLDER, false); + NodeType.NT_FOLDER); } else { parentNode = jcrSession.getNode(parentPath); } @@ -154,23 +184,24 @@ public class ImportMavenDependencies implements Runnable { } else { fileNode = parentNode.getNode(file.getName()); } - processArtifact(fileNode, artifact); - if (fileNode.isNodeType(RepoTypes.SLC_JAR_FILE)) { - processOsgiBundle(fileNode); - } + + if (artifactIndexer.support(fileNode.getPath())) + artifactIndexer.index(fileNode); + if (jarFileIndexer.support(fileNode.getPath())) + jarFileIndexer.index(fileNode); jcrSession.save(); - if (!jcrSession - .itemExists(bundleDistributionPath(fileNode)) - && fileNode - .isNodeType(RepoTypes.SLC_BUNDLE_ARTIFACT)) - jcrSession.getWorkspace().clone( - jcrSession.getWorkspace().getName(), - fileNode.getPath(), - bundleDistributionPath(fileNode), false); - - if (log.isDebugEnabled()) - log.debug("Synchronized " + fileNode); + if (fileNode.hasProperty(SlcNames.SLC_SYMBOLIC_NAME)) { + String distPath = bundleDistributionPath(fileNode); + if (!jcrSession.itemExists(distPath) + && fileNode + .isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT)) + jcrSession.getWorkspace().clone( + jcrSession.getWorkspace().getName(), + fileNode.getPath(), distPath, false); + if (log.isDebugEnabled()) + log.debug("Synchronized " + fileNode); + } } catch (Exception e) { log.error("Could not synchronize " + artifact, e); jcrSession.refresh(false); @@ -197,10 +228,10 @@ public class ImportMavenDependencies implements Runnable { + '/' + distributionName + '/' - + fileNode.getProperty(RepoNames.SLC_SYMBOLIC_NAME) + + fileNode.getProperty(SlcNames.SLC_SYMBOLIC_NAME) .getString() + '_' - + fileNode.getProperty(RepoNames.SLC_BUNDLE_VERSION) + + fileNode.getProperty(SlcNames.SLC_BUNDLE_VERSION) .getString(); } catch (RepositoryException e) { throw new SlcException("Cannot create distribution path for " @@ -208,25 +239,6 @@ public class ImportMavenDependencies implements Runnable { } } - protected void processArtifact(Node fileNode, Artifact artifact) { - try { - fileNode.addMixin(RepoTypes.SLC_ARTIFACT); - fileNode.setProperty(RepoNames.SLC_ARTIFACT_ID, - artifact.getArtifactId()); - fileNode.setProperty(RepoNames.SLC_GROUP_ID, artifact.getGroupId()); - fileNode.setProperty(RepoNames.SLC_ARTIFACT_VERSION, - artifact.getVersion()); - fileNode.setProperty(RepoNames.SLC_ARTIFACT_EXTENSION, - artifact.getExtension()); - fileNode.setProperty(RepoNames.SLC_ARTIFACT_CLASSIFIER, - artifact.getClassifier()); - } catch (RepositoryException e) { - throw new SlcException("Cannot process artifact " + artifact - + " on node " + fileNode, e); - } - - } - protected File artifactToFile(Artifact artifact) { return new File(System.getProperty("user.home") + File.separator @@ -248,344 +260,16 @@ public class ImportMavenDependencies implements Runnable { + artifact.getExtension()); } - protected void processOsgiBundle(Node fileNode) { - Binary manifestBinary = null; - InputStream manifestIn = null; - try { - manifestBinary = fileNode.getProperty(RepoNames.SLC_MANIFEST) - .getBinary(); - manifestIn = manifestBinary.getStream(); - Manifest manifest = new Manifest(manifestIn); - Attributes attrs = manifest.getMainAttributes(); - - if (log.isTraceEnabled()) - for (Object key : attrs.keySet()) - log.trace(key + ": " + attrs.getValue(key.toString())); - - if (!attrs.containsKey(new Name(Constants.BUNDLE_SYMBOLICNAME))) { - log.warn(fileNode + " is not an OSGi bundle"); - return;// not an osgi bundle - } - - fileNode.addMixin(RepoTypes.SLC_BUNDLE_ARTIFACT); - - // symbolic name - String symbolicName = attrs.getValue(Constants.BUNDLE_SYMBOLICNAME); - // make sure there is no directive - symbolicName = symbolicName.split(";")[0]; - fileNode.setProperty(RepoNames.SLC_SYMBOLIC_NAME, symbolicName); - - // direct mapping - addAttr(Constants.BUNDLE_SYMBOLICNAME, fileNode, attrs); - addAttr(Constants.BUNDLE_NAME, fileNode, attrs); - addAttr(Constants.BUNDLE_DESCRIPTION, fileNode, attrs); - addAttr(Constants.BUNDLE_MANIFESTVERSION, fileNode, attrs); - addAttr(Constants.BUNDLE_CATEGORY, fileNode, attrs); - addAttr(Constants.BUNDLE_ACTIVATIONPOLICY, fileNode, attrs); - addAttr(Constants.BUNDLE_COPYRIGHT, fileNode, attrs); - addAttr(Constants.BUNDLE_VENDOR, fileNode, attrs); - addAttr("Bundle-License", fileNode, attrs); - addAttr(Constants.BUNDLE_DOCURL, fileNode, attrs); - addAttr(Constants.BUNDLE_CONTACTADDRESS, fileNode, attrs); - addAttr(Constants.BUNDLE_ACTIVATOR, fileNode, attrs); - addAttr(Constants.BUNDLE_UPDATELOCATION, fileNode, attrs); - addAttr(Constants.BUNDLE_LOCALIZATION, fileNode, attrs); - - // required execution environment - if (attrs.containsKey(new Name( - Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT))) - fileNode.setProperty( - RepoNames.SLC_ - + Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, - attrs.getValue( - Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT) - .split(",")); - - // bundle classpath - if (attrs.containsKey(new Name(Constants.BUNDLE_CLASSPATH))) - fileNode.setProperty(RepoNames.SLC_ - + Constants.BUNDLE_CLASSPATH, - attrs.getValue(Constants.BUNDLE_CLASSPATH).split(",")); - - // version - Version version = new Version( - attrs.getValue(Constants.BUNDLE_VERSION)); - fileNode.setProperty(RepoNames.SLC_BUNDLE_VERSION, - version.toString()); - cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.BUNDLE_VERSION); - Node bundleVersionNode = fileNode.addNode(RepoNames.SLC_ - + Constants.BUNDLE_VERSION, RepoTypes.SLC_OSGI_VERSION); - mapOsgiVersion(version, bundleVersionNode); - - // fragment - cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.FRAGMENT_HOST); - if (attrs.containsKey(new Name(Constants.FRAGMENT_HOST))) { - String fragmentHost = attrs.getValue(Constants.FRAGMENT_HOST); - String[] tokens = fragmentHost.split(";"); - Node node = fileNode.addNode(RepoNames.SLC_ - + Constants.FRAGMENT_HOST, RepoTypes.SLC_FRAGMENT_HOST); - node.setProperty(RepoNames.SLC_SYMBOLIC_NAME, tokens[0]); - for (int i = 1; i < tokens.length; i++) { - if (tokens[i] - .startsWith(Constants.BUNDLE_VERSION_ATTRIBUTE)) { - node.setProperty(RepoNames.SLC_BUNDLE_VERSION, - attributeValue(tokens[i])); - } - } - } - - // imported packages - cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.IMPORT_PACKAGE); - if (attrs.containsKey(new Name(Constants.IMPORT_PACKAGE))) { - String importPackages = attrs - .getValue(Constants.IMPORT_PACKAGE); - List packages = parsePackages(importPackages); - for (String pkg : packages) { - String[] tokens = pkg.split(";"); - Node node = fileNode.addNode(RepoNames.SLC_ - + Constants.IMPORT_PACKAGE, - RepoTypes.SLC_IMPORTED_PACKAGE); - node.setProperty(RepoNames.SLC_NAME, tokens[0]); - for (int i = 1; i < tokens.length; i++) { - if (tokens[i].startsWith(Constants.VERSION_ATTRIBUTE)) { - node.setProperty(RepoNames.SLC_VERSION, - attributeValue(tokens[i])); - } else if (tokens[i] - .startsWith(Constants.RESOLUTION_DIRECTIVE)) { - node.setProperty( - RepoNames.SLC_OPTIONAL, - directiveValue(tokens[i]).equals( - Constants.RESOLUTION_OPTIONAL)); - } - } - } - } - - // dynamic import package - cleanSubNodes(fileNode, RepoNames.SLC_ - + Constants.DYNAMICIMPORT_PACKAGE); - if (attrs.containsKey(new Name(Constants.DYNAMICIMPORT_PACKAGE))) { - String importPackages = attrs - .getValue(Constants.DYNAMICIMPORT_PACKAGE); - List packages = parsePackages(importPackages); - for (String pkg : packages) { - String[] tokens = pkg.split(";"); - Node node = fileNode.addNode(RepoNames.SLC_ - + Constants.DYNAMICIMPORT_PACKAGE, - RepoTypes.SLC_DYNAMIC_IMPORTED_PACKAGE); - node.setProperty(RepoNames.SLC_NAME, tokens[0]); - for (int i = 1; i < tokens.length; i++) { - if (tokens[i].startsWith(Constants.VERSION_ATTRIBUTE)) { - node.setProperty(RepoNames.SLC_VERSION, - attributeValue(tokens[i])); - } - } - } - } - - // exported packages - cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.EXPORT_PACKAGE); - if (attrs.containsKey(new Name(Constants.EXPORT_PACKAGE))) { - String exportPackages = attrs - .getValue(Constants.EXPORT_PACKAGE); - List packages = parsePackages(exportPackages); - for (String pkg : packages) { - String[] tokens = pkg.split(";"); - Node node = fileNode.addNode(RepoNames.SLC_ - + Constants.EXPORT_PACKAGE, - RepoTypes.SLC_EXPORTED_PACKAGE); - node.setProperty(RepoNames.SLC_NAME, tokens[0]); - // TODO: are these cleans really necessary? - cleanSubNodes(node, RepoNames.SLC_USES); - cleanSubNodes(node, RepoNames.SLC_VERSION); - for (int i = 1; i < tokens.length; i++) { - if (tokens[i].startsWith(Constants.VERSION_ATTRIBUTE)) { - String versionStr = attributeValue(tokens[i]); - Node versionNode = node.addNode( - RepoNames.SLC_VERSION, - RepoTypes.SLC_OSGI_VERSION); - mapOsgiVersion(new Version(versionStr), versionNode); - } else if (tokens[i] - .startsWith(Constants.USES_DIRECTIVE)) { - String usedPackages = directiveValue(tokens[i]); - // log.debug("uses='" + usedPackages + "'"); - for (String usedPackage : usedPackages.split(",")) { - // log.debug("usedPackage='" + usedPackage + - // "'"); - Node usesNode = node.addNode( - RepoNames.SLC_USES, - RepoTypes.SLC_JAVA_PACKAGE); - usesNode.setProperty(RepoNames.SLC_NAME, - usedPackage); - } - } - } - } - } - - // required bundle - cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.REQUIRE_BUNDLE); - if (attrs.containsKey(new Name(Constants.REQUIRE_BUNDLE))) { - String requireBundle = attrs.getValue(Constants.REQUIRE_BUNDLE); - String[] bundles = requireBundle.split(","); - for (String bundle : bundles) { - String[] tokens = bundle.split(";"); - Node node = fileNode.addNode(RepoNames.SLC_ - + Constants.REQUIRE_BUNDLE, - RepoTypes.SLC_REQUIRED_BUNDLE); - node.setProperty(RepoNames.SLC_SYMBOLIC_NAME, tokens[0]); - for (int i = 1; i < tokens.length; i++) { - if (tokens[i] - .startsWith(Constants.BUNDLE_VERSION_ATTRIBUTE)) { - node.setProperty(RepoNames.SLC_BUNDLE_VERSION, - attributeValue(tokens[i])); - } else if (tokens[i] - .startsWith(Constants.RESOLUTION_DIRECTIVE)) { - node.setProperty( - RepoNames.SLC_OPTIONAL, - directiveValue(tokens[i]).equals( - Constants.RESOLUTION_OPTIONAL)); - } - } - } - } - } catch (Exception e) { - throw new SlcException("Cannot process OSGi bundle " + fileNode, e); - } finally { - if (manifestBinary != null) - manifestBinary.dispose(); - IOUtils.closeQuietly(manifestIn); - } - } - - /** Parse package list with nested directive with ',' */ - private List parsePackages(String str) { - List res = new ArrayList(); - StringBuffer curr = new StringBuffer(""); - boolean in = false; - for (char c : str.toCharArray()) { - if (c == ',') { - if (!in) { - res.add(curr.toString()); - curr = new StringBuffer(""); - } - } else if (c == '\"') { - in = !in; - curr.append(c); - } else { - curr.append(c); - } - } - res.add(curr.toString()); - return res; - } - - private void addAttr(String key, Node node, Attributes attrs) - throws RepositoryException { - addAttr(new Name(key), node, attrs); - } - - private void addAttr(Name key, Node node, Attributes attrs) - throws RepositoryException { - if (attrs.containsKey(key)) { - String value = attrs.getValue(key); - node.setProperty(RepoNames.SLC_ + key, value); - } - } - - private void cleanSubNodes(Node node, String name) - throws RepositoryException { - if (node.hasNode(name)) { - NodeIterator nit = node.getNodes(name); - while (nit.hasNext()) - nit.nextNode().remove(); - } - } - - protected void mapOsgiVersion(Version version, Node versionNode) - throws RepositoryException { - versionNode.setProperty(RepoNames.SLC_AS_STRING, version.toString()); - versionNode.setProperty(RepoNames.SLC_MAJOR, version.getMajor()); - versionNode.setProperty(RepoNames.SLC_MINOR, version.getMinor()); - versionNode.setProperty(RepoNames.SLC_MICRO, version.getMicro()); - if (!version.getQualifier().equals("")) - versionNode.setProperty(RepoNames.SLC_QUALIFIER, - version.getQualifier()); - } - - private String attributeValue(String str) { - return extractValue(str, "="); - } - - private String directiveValue(String str) { - return extractValue(str, ":="); - } - - private String extractValue(String str, String eq) { - String[] tokens = str.split(eq); - // String key = tokens[0]; - String value = tokens[1].trim(); - // TODO: optimize? - if (value.startsWith("\"")) - value = value.substring(1); - if (value.endsWith("\"")) - value = value.substring(0, value.length() - 1); - return value; - } - - protected Node createFileNode(Node parentNode, File file) { + private Node createFileNode(Node parentNode, File file) { Binary binary = null; try { Node fileNode = parentNode .addNode(file.getName(), NodeType.NT_FILE); Node contentNode = fileNode.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE); - binary = jcrSession.getValueFactory().createBinary( - new FileInputStream(file)); + binary = contentNode.getSession().getValueFactory() + .createBinary(new FileInputStream(file)); contentNode.setProperty(Property.JCR_DATA, binary); - // jar file - if (FilenameUtils.isExtension(file.getName(), "jar")) { - JarInputStream jarIn = null; - ByteArrayOutputStream bo = null; - ByteArrayInputStream bi = null; - Binary manifestBinary = null; - try { - jarIn = new JarInputStream(binary.getStream()); - Manifest manifest = jarIn.getManifest(); - bo = new ByteArrayOutputStream(); - manifest.write(bo); - bi = new ByteArrayInputStream(bo.toByteArray()); - manifestBinary = jcrSession.getValueFactory().createBinary( - bi); - fileNode.addMixin(RepoTypes.SLC_JAR_FILE); - fileNode.setProperty(RepoNames.SLC_MANIFEST, manifestBinary); - Attributes attrs = manifest.getMainAttributes(); - addAttr(Attributes.Name.MANIFEST_VERSION, fileNode, attrs); - addAttr(Attributes.Name.SIGNATURE_VERSION, fileNode, attrs); - addAttr(Attributes.Name.CLASS_PATH, fileNode, attrs); - addAttr(Attributes.Name.MAIN_CLASS, fileNode, attrs); - addAttr(Attributes.Name.EXTENSION_NAME, fileNode, attrs); - addAttr(Attributes.Name.IMPLEMENTATION_VERSION, fileNode, - attrs); - addAttr(Attributes.Name.IMPLEMENTATION_VENDOR, fileNode, - attrs); - addAttr(Attributes.Name.IMPLEMENTATION_VENDOR_ID, fileNode, - attrs); - addAttr(Attributes.Name.SPECIFICATION_TITLE, fileNode, - attrs); - addAttr(Attributes.Name.SPECIFICATION_VERSION, fileNode, - attrs); - addAttr(Attributes.Name.SPECIFICATION_VENDOR, fileNode, - attrs); - addAttr(Attributes.Name.SEALED, fileNode, attrs); - } finally { - if (manifestBinary != null) - manifestBinary.dispose(); - IOUtils.closeQuietly(bi); - IOUtils.closeQuietly(bo); - IOUtils.closeQuietly(jarIn); - } - } return fileNode; } catch (Exception e) { throw new SlcException("Cannot create file node based on " + file @@ -695,12 +379,16 @@ public class ImportMavenDependencies implements Runnable { this.rootCoordinates = rootCoordinates; } - public void setJcrSession(Session jcrSession) { - this.jcrSession = jcrSession; - } - public void setDistributionName(String distributionName) { this.distributionName = distributionName; } + public void setRepository(Repository repository) { + this.repository = repository; + } + + public void setWorkspace(String workspace) { + this.workspace = workspace; + } + }