]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/ImportMavenDependencies.java
Working indexation (artifact, jar, osgi) in repo
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / maven / ImportMavenDependencies.java
index a6eb8df50d2eb192140fa77a32cd2f12c5d09205..962e429ea0dafc5d0a382ada39a880d666461810 100644 (file)
@@ -20,7 +20,6 @@ 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.RepositoryException;
 import javax.jcr.Session;
@@ -35,8 +34,10 @@ 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.argeo.slc.jcr.SlcNames;
+import org.argeo.slc.jcr.SlcTypes;
+import org.argeo.slc.repo.ArtifactIndexer;
+import org.argeo.slc.repo.RepoConstants;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Version;
 import org.sonatype.aether.artifact.Artifact;
@@ -55,10 +56,12 @@ public class ImportMavenDependencies implements Runnable {
        private Set<String> excludedArtifacts = new HashSet<String>();
 
        private Session jcrSession;
-       private String artifactBasePath = "/slc/repo/artifacts";
+       private String artifactBasePath = RepoConstants.ARTIFACTS_BASE_PATH;
        private String distributionsBasePath = "/slc/repo/distributions";
        private String distributionName;
 
+       private ArtifactIndexer artifactIndexer = new ArtifactIndexer();
+
        public void run() {
                log.debug(jcrSession.getUserID());
                Set<Artifact> artifacts = resolveDistribution();
@@ -143,7 +146,7 @@ public class ImportMavenDependencies implements Runnable {
                                        Node parentNode;
                                        if (!jcrSession.itemExists(parentPath)) {
                                                parentNode = JcrUtils.mkdirs(jcrSession, parentPath,
-                                                               NodeType.NT_FOLDER, false);
+                                                               NodeType.NT_FOLDER);
                                        } else {
                                                parentNode = jcrSession.getNode(parentPath);
                                        }
@@ -154,8 +157,10 @@ public class ImportMavenDependencies implements Runnable {
                                        } else {
                                                fileNode = parentNode.getNode(file.getName());
                                        }
-                                       processArtifact(fileNode, artifact);
-                                       if (fileNode.isNodeType(RepoTypes.SLC_JAR_FILE)) {
+
+                                       if (artifactIndexer.support(fileNode.getPath()))
+                                               artifactIndexer.index(fileNode);
+                                       if (fileNode.isNodeType(SlcTypes.SLC_JAR_FILE)) {
                                                processOsgiBundle(fileNode);
                                        }
                                        jcrSession.save();
@@ -163,7 +168,7 @@ public class ImportMavenDependencies implements Runnable {
                                        if (!jcrSession
                                                        .itemExists(bundleDistributionPath(fileNode))
                                                        && fileNode
-                                                                       .isNodeType(RepoTypes.SLC_BUNDLE_ARTIFACT))
+                                                                       .isNodeType(SlcTypes.SLC_BUNDLE_ARTIFACT))
                                                jcrSession.getWorkspace().clone(
                                                                jcrSession.getWorkspace().getName(),
                                                                fileNode.getPath(),
@@ -197,10 +202,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 +213,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
@@ -252,7 +238,7 @@ public class ImportMavenDependencies implements Runnable {
                Binary manifestBinary = null;
                InputStream manifestIn = null;
                try {
-                       manifestBinary = fileNode.getProperty(RepoNames.SLC_MANIFEST)
+                       manifestBinary = fileNode.getProperty(SlcNames.SLC_MANIFEST)
                                        .getBinary();
                        manifestIn = manifestBinary.getStream();
                        Manifest manifest = new Manifest(manifestIn);
@@ -267,13 +253,13 @@ public class ImportMavenDependencies implements Runnable {
                                return;// not an osgi bundle
                        }
 
-                       fileNode.addMixin(RepoTypes.SLC_BUNDLE_ARTIFACT);
+                       fileNode.addMixin(SlcTypes.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);
+                       fileNode.setProperty(SlcNames.SLC_SYMBOLIC_NAME, symbolicName);
 
                        // direct mapping
                        addAttr(Constants.BUNDLE_SYMBOLICNAME, fileNode, attrs);
@@ -295,7 +281,7 @@ public class ImportMavenDependencies implements Runnable {
                        if (attrs.containsKey(new Name(
                                        Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT)))
                                fileNode.setProperty(
-                                               RepoNames.SLC_
+                                               SlcNames.SLC_
                                                                + Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT,
                                                attrs.getValue(
                                                                Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT)
@@ -303,57 +289,58 @@ public class ImportMavenDependencies implements Runnable {
 
                        // bundle classpath
                        if (attrs.containsKey(new Name(Constants.BUNDLE_CLASSPATH)))
-                               fileNode.setProperty(RepoNames.SLC_
-                                               + Constants.BUNDLE_CLASSPATH,
-                                               attrs.getValue(Constants.BUNDLE_CLASSPATH).split(","));
+                               fileNode.setProperty(
+                                               SlcNames.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,
+                       fileNode.setProperty(SlcNames.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);
+                       cleanSubNodes(fileNode, SlcNames.SLC_ + Constants.BUNDLE_VERSION);
+                       Node bundleVersionNode = fileNode.addNode(SlcNames.SLC_
+                                       + Constants.BUNDLE_VERSION, SlcTypes.SLC_OSGI_VERSION);
                        mapOsgiVersion(version, bundleVersionNode);
 
                        // fragment
-                       cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.FRAGMENT_HOST);
+                       cleanSubNodes(fileNode, SlcNames.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]);
+                               Node node = fileNode.addNode(SlcNames.SLC_
+                                               + Constants.FRAGMENT_HOST, SlcTypes.SLC_FRAGMENT_HOST);
+                               node.setProperty(SlcNames.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,
+                                               node.setProperty(SlcNames.SLC_BUNDLE_VERSION,
                                                                attributeValue(tokens[i]));
                                        }
                                }
                        }
 
                        // imported packages
-                       cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.IMPORT_PACKAGE);
+                       cleanSubNodes(fileNode, SlcNames.SLC_ + Constants.IMPORT_PACKAGE);
                        if (attrs.containsKey(new Name(Constants.IMPORT_PACKAGE))) {
                                String importPackages = attrs
                                                .getValue(Constants.IMPORT_PACKAGE);
                                List<String> packages = parsePackages(importPackages);
                                for (String pkg : packages) {
                                        String[] tokens = pkg.split(";");
-                                       Node node = fileNode.addNode(RepoNames.SLC_
+                                       Node node = fileNode.addNode(SlcNames.SLC_
                                                        + Constants.IMPORT_PACKAGE,
-                                                       RepoTypes.SLC_IMPORTED_PACKAGE);
-                                       node.setProperty(RepoNames.SLC_NAME, tokens[0]);
+                                                       SlcTypes.SLC_IMPORTED_PACKAGE);
+                                       node.setProperty(SlcNames.SLC_NAME, tokens[0]);
                                        for (int i = 1; i < tokens.length; i++) {
                                                if (tokens[i].startsWith(Constants.VERSION_ATTRIBUTE)) {
-                                                       node.setProperty(RepoNames.SLC_VERSION,
+                                                       node.setProperty(SlcNames.SLC_VERSION,
                                                                        attributeValue(tokens[i]));
                                                } else if (tokens[i]
                                                                .startsWith(Constants.RESOLUTION_DIRECTIVE)) {
                                                        node.setProperty(
-                                                                       RepoNames.SLC_OPTIONAL,
+                                                                       SlcNames.SLC_OPTIONAL,
                                                                        directiveValue(tokens[i]).equals(
                                                                                        Constants.RESOLUTION_OPTIONAL));
                                                }
@@ -362,7 +349,7 @@ public class ImportMavenDependencies implements Runnable {
                        }
 
                        // dynamic import package
-                       cleanSubNodes(fileNode, RepoNames.SLC_
+                       cleanSubNodes(fileNode, SlcNames.SLC_
                                        + Constants.DYNAMICIMPORT_PACKAGE);
                        if (attrs.containsKey(new Name(Constants.DYNAMICIMPORT_PACKAGE))) {
                                String importPackages = attrs
@@ -370,13 +357,13 @@ public class ImportMavenDependencies implements Runnable {
                                List<String> packages = parsePackages(importPackages);
                                for (String pkg : packages) {
                                        String[] tokens = pkg.split(";");
-                                       Node node = fileNode.addNode(RepoNames.SLC_
+                                       Node node = fileNode.addNode(SlcNames.SLC_
                                                        + Constants.DYNAMICIMPORT_PACKAGE,
-                                                       RepoTypes.SLC_DYNAMIC_IMPORTED_PACKAGE);
-                                       node.setProperty(RepoNames.SLC_NAME, tokens[0]);
+                                                       SlcTypes.SLC_DYNAMIC_IMPORTED_PACKAGE);
+                                       node.setProperty(SlcNames.SLC_NAME, tokens[0]);
                                        for (int i = 1; i < tokens.length; i++) {
                                                if (tokens[i].startsWith(Constants.VERSION_ATTRIBUTE)) {
-                                                       node.setProperty(RepoNames.SLC_VERSION,
+                                                       node.setProperty(SlcNames.SLC_VERSION,
                                                                        attributeValue(tokens[i]));
                                                }
                                        }
@@ -384,26 +371,26 @@ public class ImportMavenDependencies implements Runnable {
                        }
 
                        // exported packages
-                       cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.EXPORT_PACKAGE);
+                       cleanSubNodes(fileNode, SlcNames.SLC_ + Constants.EXPORT_PACKAGE);
                        if (attrs.containsKey(new Name(Constants.EXPORT_PACKAGE))) {
                                String exportPackages = attrs
                                                .getValue(Constants.EXPORT_PACKAGE);
                                List<String> packages = parsePackages(exportPackages);
                                for (String pkg : packages) {
                                        String[] tokens = pkg.split(";");
-                                       Node node = fileNode.addNode(RepoNames.SLC_
+                                       Node node = fileNode.addNode(SlcNames.SLC_
                                                        + Constants.EXPORT_PACKAGE,
-                                                       RepoTypes.SLC_EXPORTED_PACKAGE);
-                                       node.setProperty(RepoNames.SLC_NAME, tokens[0]);
+                                                       SlcTypes.SLC_EXPORTED_PACKAGE);
+                                       node.setProperty(SlcNames.SLC_NAME, tokens[0]);
                                        // TODO: are these cleans really necessary?
-                                       cleanSubNodes(node, RepoNames.SLC_USES);
-                                       cleanSubNodes(node, RepoNames.SLC_VERSION);
+                                       cleanSubNodes(node, SlcNames.SLC_USES);
+                                       cleanSubNodes(node, SlcNames.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);
+                                                                       SlcNames.SLC_VERSION,
+                                                                       SlcTypes.SLC_OSGI_VERSION);
                                                        mapOsgiVersion(new Version(versionStr), versionNode);
                                                } else if (tokens[i]
                                                                .startsWith(Constants.USES_DIRECTIVE)) {
@@ -412,10 +399,9 @@ public class ImportMavenDependencies implements Runnable {
                                                        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,
+                                                               Node usesNode = node.addNode(SlcNames.SLC_USES,
+                                                                               SlcTypes.SLC_JAVA_PACKAGE);
+                                                               usesNode.setProperty(SlcNames.SLC_NAME,
                                                                                usedPackage);
                                                        }
                                                }
@@ -424,25 +410,25 @@ public class ImportMavenDependencies implements Runnable {
                        }
 
                        // required bundle
-                       cleanSubNodes(fileNode, RepoNames.SLC_ + Constants.REQUIRE_BUNDLE);
+                       cleanSubNodes(fileNode, SlcNames.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_
+                                       Node node = fileNode.addNode(SlcNames.SLC_
                                                        + Constants.REQUIRE_BUNDLE,
-                                                       RepoTypes.SLC_REQUIRED_BUNDLE);
-                                       node.setProperty(RepoNames.SLC_SYMBOLIC_NAME, tokens[0]);
+                                                       SlcTypes.SLC_REQUIRED_BUNDLE);
+                                       node.setProperty(SlcNames.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,
+                                                       node.setProperty(SlcNames.SLC_BUNDLE_VERSION,
                                                                        attributeValue(tokens[i]));
                                                } else if (tokens[i]
                                                                .startsWith(Constants.RESOLUTION_DIRECTIVE)) {
                                                        node.setProperty(
-                                                                       RepoNames.SLC_OPTIONAL,
+                                                                       SlcNames.SLC_OPTIONAL,
                                                                        directiveValue(tokens[i]).equals(
                                                                                        Constants.RESOLUTION_OPTIONAL));
                                                }
@@ -477,6 +463,7 @@ public class ImportMavenDependencies implements Runnable {
                        }
                }
                res.add(curr.toString());
+               log.debug(res);
                return res;
        }
 
@@ -489,7 +476,7 @@ public class ImportMavenDependencies implements Runnable {
                        throws RepositoryException {
                if (attrs.containsKey(key)) {
                        String value = attrs.getValue(key);
-                       node.setProperty(RepoNames.SLC_ + key, value);
+                       node.setProperty(SlcNames.SLC_ + key, value);
                }
        }
 
@@ -504,12 +491,12 @@ public class ImportMavenDependencies implements Runnable {
 
        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());
+               versionNode.setProperty(SlcNames.SLC_AS_STRING, version.toString());
+               versionNode.setProperty(SlcNames.SLC_MAJOR, version.getMajor());
+               versionNode.setProperty(SlcNames.SLC_MINOR, version.getMinor());
+               versionNode.setProperty(SlcNames.SLC_MICRO, version.getMicro());
                if (!version.getQualifier().equals(""))
-                       versionNode.setProperty(RepoNames.SLC_QUALIFIER,
+                       versionNode.setProperty(SlcNames.SLC_QUALIFIER,
                                        version.getQualifier());
        }
 
@@ -557,8 +544,8 @@ public class ImportMavenDependencies implements Runnable {
                                        bi = new ByteArrayInputStream(bo.toByteArray());
                                        manifestBinary = jcrSession.getValueFactory().createBinary(
                                                        bi);
-                                       fileNode.addMixin(RepoTypes.SLC_JAR_FILE);
-                                       fileNode.setProperty(RepoNames.SLC_MANIFEST, manifestBinary);
+                                       fileNode.addMixin(SlcTypes.SLC_JAR_FILE);
+                                       fileNode.setProperty(SlcNames.SLC_MANIFEST, manifestBinary);
                                        Attributes attrs = manifest.getMainAttributes();
                                        addAttr(Attributes.Name.MANIFEST_VERSION, fileNode, attrs);
                                        addAttr(Attributes.Name.SIGNATURE_VERSION, fileNode, attrs);