]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/JarFileIndexer.java
Introduce OSGi Factory
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / JarFileIndexer.java
index 6ab3c529dd84a4b237a3556bb16c6c2bc2de5e6c..d3abf9bad0c52434c9753e8a2fbc00c0914c1bc3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2012 Mathieu Baudier
+ * Copyright (C) 2007-2012 Argeo GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.argeo.slc.repo;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.jar.Attributes;
 import java.util.jar.Attributes.Name;
@@ -47,7 +48,7 @@ import org.osgi.framework.Version;
  * Indexes jar file, currently supports standard J2SE and OSGi metadata (both
  * from MANIFEST)
  */
-public class JarFileIndexer implements NodeIndexer {
+public class JarFileIndexer implements NodeIndexer, SlcNames {
        private final static Log log = LogFactory.getLog(JarFileIndexer.class);
 
        public Boolean support(String path) {
@@ -61,6 +62,9 @@ public class JarFileIndexer implements NodeIndexer {
                ByteArrayInputStream bi = null;
                Binary manifestBinary = null;
                try {
+                       if(!support(fileNode.getPath()))
+                               return;
+                       
                        if (!fileNode.isNodeType(NodeType.NT_FILE))
                                return;
 
@@ -78,13 +82,27 @@ public class JarFileIndexer implements NodeIndexer {
                                log.error(fileNode + " has no MANIFEST");
                                return;
                        }
+
                        bo = new ByteArrayOutputStream();
                        manifest.write(bo);
-                       bi = new ByteArrayInputStream(bo.toByteArray());
+                       byte[] newManifest = bo.toByteArray();
+                       if (fileNode.hasProperty(SLC_MANIFEST)) {
+                               byte[] storedManifest = JcrUtils.getBinaryAsBytes(fileNode
+                                               .getProperty(SLC_MANIFEST));
+                               if (Arrays.equals(newManifest, storedManifest)) {
+                                       if (log.isTraceEnabled())
+                                               log.trace("Manifest not changed, doing nothing "
+                                                               + fileNode);
+                                       return;
+                               }
+                       }
+
+                       bi = new ByteArrayInputStream(newManifest);
                        manifestBinary = jcrSession.getValueFactory().createBinary(bi);
 
                        // standard jar file
                        fileNode.addMixin(SlcTypes.SLC_JAR_FILE);
+
                        fileNode.setProperty(SlcNames.SLC_MANIFEST, manifestBinary);
                        Attributes attrs = manifest.getMainAttributes();
                        if (log.isTraceEnabled())
@@ -116,6 +134,7 @@ public class JarFileIndexer implements NodeIndexer {
                                if (log.isTraceEnabled())
                                        log.trace("Indexed JAR file " + fileNode);
                        }
+
                } catch (Exception e) {
                        throw new SlcException("Cannot index jar " + fileNode, e);
                } finally {
@@ -195,7 +214,7 @@ public class JarFileIndexer implements NodeIndexer {
                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);
+                       List<String> packages = parseCommaSeparated(importPackages);
                        for (String pkg : packages) {
                                String[] tokens = pkg.split(";");
                                Node node = fileNode.addNode(SlcNames.SLC_
@@ -222,7 +241,7 @@ public class JarFileIndexer implements NodeIndexer {
                if (attrs.containsKey(new Name(Constants.DYNAMICIMPORT_PACKAGE))) {
                        String importPackages = attrs
                                        .getValue(Constants.DYNAMICIMPORT_PACKAGE);
-                       List<String> packages = parsePackages(importPackages);
+                       List<String> packages = parseCommaSeparated(importPackages);
                        for (String pkg : packages) {
                                String[] tokens = pkg.split(";");
                                Node node = fileNode.addNode(SlcNames.SLC_
@@ -242,7 +261,7 @@ public class JarFileIndexer implements NodeIndexer {
                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);
+                       List<String> packages = parseCommaSeparated(exportPackages);
                        for (String pkg : packages) {
                                String[] tokens = pkg.split(";");
                                Node node = fileNode.addNode(SlcNames.SLC_
@@ -278,7 +297,7 @@ public class JarFileIndexer implements NodeIndexer {
                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(",");
+                       List<String> bundles = parseCommaSeparated(requireBundle);
                        for (String bundle : bundles) {
                                String[] tokens = bundle.split(";");
                                Node node = fileNode.addNode(SlcNames.SLC_
@@ -346,15 +365,17 @@ public class JarFileIndexer implements NodeIndexer {
        }
 
        /** Parse package list with nested directive with ',' */
-       private List<String> parsePackages(String str) {
+       private List<String> parseCommaSeparated(String str) {
                List<String> res = new ArrayList<String>();
                StringBuffer curr = new StringBuffer("");
                boolean in = false;
                for (char c : str.toCharArray()) {
                        if (c == ',') {
-                               if (!in) {
+                               if (!in) {// new package
                                        res.add(curr.toString());
                                        curr = new StringBuffer("");
+                               } else {// a ',' within " "
+                                       curr.append(c);
                                }
                        } else if (c == '\"') {
                                in = !in;