sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r670670 - in /incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal: ContentCreator.java ContentLoader.java Loader.java PathEntry.java ZipReader.java
Date Mon, 23 Jun 2008 16:42:47 GMT
Author: cziegeler
Date: Mon Jun 23 09:42:47 2008
New Revision: 670670

URL: http://svn.apache.org/viewvc?rev=670670&view=rev
Log:
SLING-548: Refactor code and provide a mechanism to disable import providers.

Modified:
    incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
    incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
    incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java
    incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/PathEntry.java
    incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ZipReader.java

Modified: incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java?rev=670670&r1=670669&r2=670670&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
(original)
+++ incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
Mon Jun 23 09:42:47 2008
@@ -25,6 +25,8 @@
 /**
  * The <code>ContentCreator</code>
  * is used by the {@link ContentReader} to create the actual content.
+ *
+ * @since 2.0.4
  */
 interface ContentCreator {
 

Modified: incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java?rev=670670&r1=670669&r2=670670&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
(original)
+++ incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
Mon Jun 23 09:42:47 2008
@@ -23,6 +23,7 @@
 import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Stack;
@@ -38,6 +39,7 @@
 
 /**
  * The <code>ContentLoader</code> creates the nodes and properties.
+ * @since 2.0.4
  */
 public class ContentLoader implements ContentCreator {
 
@@ -62,21 +64,39 @@
 
     private final ContentLoaderService jcrContentHelper;
 
+    private Map<String, ImportProvider> importProviders;
+
     public ContentLoader(ContentLoaderService jcrContentHelper) {
         this.jcrContentHelper = jcrContentHelper;
     }
 
     /**
      * Initialize this component.
-     * If the defaultRootName is null, we are in ROOT_NODE import mode.
      * @param pathEntry
-     * @param parentNode
-     * @param defaultRootName
      */
     public void init(final PathEntry pathEntry,
-                     final Node parentNode,
-                     final String defaultRootName) {
+                     final Map<String, ImportProvider> defaultImportProviders) {
+
         this.configuration = pathEntry;
+        // create list of allowed import providers
+        this.importProviders = new HashMap<String, ImportProvider>();
+        final Iterator<Map.Entry<String, ImportProvider>> entryIter = defaultImportProviders.entrySet().iterator();
+        while ( entryIter.hasNext() ) {
+            final Map.Entry<String, ImportProvider> current = entryIter.next();
+            if (!configuration.isIgnoredImportProvider(current.getKey()) ) {
+                importProviders.put(current.getKey(), current.getValue());
+            }
+        }
+    }
+
+    /**
+     *
+     * If the defaultRootName is null, we are in ROOT_NODE import mode.
+     * @param parentNode
+     * @param defaultRootName
+     */
+    public void prepareParsing(final Node parentNode,
+                               final String defaultRootName) {
         this.parentNodeStack.clear();
         this.parentNodeStack.push(parentNode);
         this.defaultRootName = defaultRootName;
@@ -105,6 +125,33 @@
         return this.rootNode;
     }
 
+    public Map<String, ImportProvider> getImportProviders() {
+        return this.importProviders;
+    }
+
+    public ImportProvider getImportProvider(String name) {
+        ImportProvider provider = null;
+        final Iterator<String> ipIter = importProviders.keySet().iterator();
+        while (provider == null && ipIter.hasNext()) {
+            final String ext = ipIter.next();
+            if (name.endsWith(ext)) {
+                provider = importProviders.get(ext);
+            }
+        }
+        return provider;
+    }
+
+    public String getImportProviderExtension(String name) {
+        String providerExt = null;
+        final Iterator<String> ipIter = importProviders.keySet().iterator();
+        while (providerExt == null && ipIter.hasNext()) {
+            final String ext = ipIter.next();
+            if (name.endsWith(ext)) {
+                providerExt = ext;
+            }
+        }
+        return providerExt;
+    }
 
     /**
      * @see org.apache.sling.jcr.contentloader.internal.ContentCreator#createNode(java.lang.String,
java.lang.String, java.lang.String[])

Modified: incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java?rev=670670&r1=670669&r2=670670&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java
(original)
+++ incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java
Mon Jun 23 09:42:47 2008
@@ -57,6 +57,10 @@
 
     public static final String EXT_JSON = ".json";
 
+    public static final String EXT_JAR = ".jar";
+
+    public static final String EXT_ZIP = ".zip";
+
     public static final String ROOT_DESCRIPTOR = "/ROOT";
 
     /** default log */
@@ -64,7 +68,8 @@
 
     private ContentLoaderService jcrContentHelper;
 
-    private Map<String, ImportProvider> importProviders;
+    /** All available import providers. */
+    private Map<String, ImportProvider> defaultImportProviders;
 
     private final ContentLoader contentCreator;
 
@@ -76,12 +81,12 @@
         this.contentCreator = new ContentLoader(jcrContentHelper);
         this.delayedBundles = new LinkedList<Bundle>();
 
-        importProviders = new LinkedHashMap<String, ImportProvider>();
-        importProviders.put(EXT_JCR_XML, null);
-        importProviders.put(EXT_JSON, JsonReader.PROVIDER);
-        importProviders.put(EXT_XML, XmlReader.PROVIDER);
-        importProviders.put(".jar", ZipReader.JAR_PROVIDER);
-        importProviders.put(".zip", ZipReader.ZIP_PROVIDER);
+        defaultImportProviders = new LinkedHashMap<String, ImportProvider>();
+        defaultImportProviders.put(EXT_JCR_XML, null);
+        defaultImportProviders.put(EXT_JSON, JsonReader.PROVIDER);
+        defaultImportProviders.put(EXT_XML, XmlReader.PROVIDER);
+        defaultImportProviders.put(EXT_JAR, ZipReader.JAR_PROVIDER);
+        defaultImportProviders.put(EXT_ZIP, ZipReader.ZIP_PROVIDER);
     }
 
     public void dispose() {
@@ -90,7 +95,7 @@
             delayedBundles = null;
         }
         jcrContentHelper = null;
-        importProviders.clear();
+        defaultImportProviders = null;
     }
 
     /**
@@ -290,11 +295,12 @@
             log.info("install: No initial content entries at {}", path);
             return;
         }
+        //  init content creator
+        this.contentCreator.init(configuration, this.defaultImportProviders);
 
-        Map<URL, Node> processedEntries = new HashMap<URL, Node>();
-
+        final Map<URL, Node> processedEntries = new HashMap<URL, Node>();
         // potential root node import/extension
-        URL rootNodeDescriptor = importRootNode(parent.getSession(), bundle, path, configuration);
+        URL rootNodeDescriptor = importRootNode(parent.getSession(), bundle, path);
         if (rootNodeDescriptor != null) {
             processedEntries.put(rootNodeDescriptor,
                 parent.getSession().getRootNode());
@@ -310,7 +316,7 @@
                 String name = getName(base);
 
                 URL nodeDescriptor = null;
-                for (String ext : importProviders.keySet()) {
+                for (String ext : this.contentCreator.getImportProviders().keySet()) {
                     nodeDescriptor = bundle.getEntry(base + ext);
                     if (nodeDescriptor != null) {
                         break;
@@ -325,7 +331,7 @@
                     node = processedEntries.get(nodeDescriptor);
                     if (node == null) {
                         node = createNode(parent, name, nodeDescriptor,
-                            configuration);
+                                          configuration);
                         processedEntries.put(nodeDescriptor, node);
                     }
                 } else {
@@ -347,14 +353,8 @@
                 }
 
                 // install if it is a descriptor
-                boolean foundProvider = false;
-                final Iterator<String> ipIter = importProviders.keySet().iterator();
-                while (!foundProvider && ipIter.hasNext()) {
-                    final String ext = ipIter.next();
-                    if (entry.endsWith(ext)) {
-                        foundProvider = true;
-                    }
-                }
+                boolean foundProvider = this.contentCreator.getImportProvider(entry) != null;
+
                 if (foundProvider) {
                     Node node = null;
                     if ((node = createNode(parent, getName(entry), file, configuration))
!= null) {
@@ -398,20 +398,18 @@
             }
 
             // get the node reader for this resource
-            ContentReader nodeReader = null;
-            for (Map.Entry<String, ImportProvider> e : importProviders.entrySet())
{
-                if (resourcePath.endsWith(e.getKey())) {
-                    nodeReader = e.getValue().getReader();
-                    break;
-                }
+            final ImportProvider ip = this.contentCreator.getImportProvider(resourcePath);
+            if ( ip == null ) {
+                return null;
             }
+            final ContentReader nodeReader = ip.getReader();
 
             // cannot find out the type
             if (nodeReader == null) {
                 return null;
             }
 
-            this.contentCreator.init(configuration, parent, toPlainName(name));
+            this.contentCreator.prepareParsing(parent, toPlainName(name));
             ins = resourceUrl.openStream();
             nodeReader.parse(ins, this.contentCreator);
 
@@ -487,7 +485,8 @@
             path = srcPath.substring(0, pos + 1) + name;
         }
 
-        this.contentCreator.init(configuration, parent, name);
+        this.contentCreator.init(configuration, defaultImportProviders);
+        this.contentCreator.prepareParsing(parent, name);
         final URLConnection conn = source.openConnection();
         final long lastModified = conn.getLastModified();
         final String type = conn.getContentType();
@@ -575,7 +574,7 @@
                 if (entry.isUninstall()) {
                     Node targetNode = getTargetNode(session, entry.getTarget());
                     if (targetNode != null)
-                        uninstallFromPath(bundle, entry.getPath(), targetNode);
+                        uninstallFromPath(bundle, entry.getPath(), entry, targetNode);
                 } else {
                     log.debug(
                         "Ignoring to uninstall content at {}, uninstall directive is not
set.",
@@ -611,15 +610,19 @@
      * @param parent The parent node.
      * @throws RepositoryException
      */
-    private void uninstallFromPath(final Bundle bundle, final String path,
-            final Node parent) throws RepositoryException {
+    private void uninstallFromPath(final Bundle bundle,
+                                   final String path,
+                                   final PathEntry configuration,
+                                   final Node parent) throws RepositoryException {
         @SuppressWarnings("unchecked")
         Enumeration<String> entries = bundle.getEntryPaths(path);
         if (entries == null) {
             return;
         }
 
-        Set<URL> ignoreEntry = new HashSet<URL>();
+        this.contentCreator.init(configuration, defaultImportProviders);
+
+        final Set<URL> ignoreEntry = new HashSet<URL>();
 
         // potential root node import/extension
         Descriptor rootNodeDescriptor = getRootNodeDescriptor(bundle, path);
@@ -636,7 +639,7 @@
                 String name = getName(base);
 
                 URL nodeDescriptor = null;
-                for (String ext : importProviders.keySet()) {
+                for (String ext : this.contentCreator.getImportProviders().keySet()) {
                     nodeDescriptor = bundle.getEntry(base + ext);
                     if (nodeDescriptor != null) {
                         break;
@@ -657,7 +660,7 @@
 
                 if (node != null) {
                     // walk down the line
-                    uninstallFromPath(bundle, entry, node);
+                    uninstallFromPath(bundle, entry, configuration, node);
                 }
 
                 if (delete) {
@@ -674,14 +677,7 @@
                 }
 
                 // uninstall if it is a descriptor
-                boolean foundProvider = false;
-                final Iterator<String> ipIter = importProviders.keySet().iterator();
-                while (!foundProvider && ipIter.hasNext()) {
-                    final String ext = ipIter.next();
-                    if (entry.endsWith(ext)) {
-                        foundProvider = true;
-                    }
-                }
+                boolean foundProvider = this.contentCreator.getImportProvider(entry) != null;
                 if (foundProvider) {
                     deleteNode(parent, toPlainName(getName(entry)));
                     ignoreEntry.add(file);
@@ -710,13 +706,15 @@
      * @throws IOException If an IO error occurrs reading the XML file.
      */
     private Node importSystemView(Node parent, String name, URL nodeXML)
-            throws IOException {
+    throws IOException {
 
         InputStream ins = null;
         try {
 
             // check whether we have the content already, nothing to do then
-            name = toPlainName(name);
+            if ( name.endsWith(EXT_JCR_XML) ) {
+                name = name.substring(0, name.length() - EXT_JCR_XML.length());
+            }
             if (parent.hasNode(name)) {
                 log.debug(
                     "importSystemView: Node {} for XML {} already exists, nothing to to",
@@ -773,7 +771,7 @@
                                              final String path) {
         URL rootNodeDescriptor = null;
 
-        for (Map.Entry<String, ImportProvider> e : importProviders.entrySet()) {
+        for (Map.Entry<String, ImportProvider> e : this.contentCreator.getImportProviders().entrySet())
{
             if (e.getValue() != null) {
                 rootNodeDescriptor = bundle.getEntry(path + ROOT_DESCRIPTOR
                     + e.getKey());
@@ -798,7 +796,7 @@
      * Imports mixin nodes and properties (and optionally child nodes) of the
      * root node.
      */
-    private URL importRootNode(Session session, Bundle bundle, String path, PathEntry configuration)
+    private URL importRootNode(Session session, Bundle bundle, String path)
     throws RepositoryException {
         final Descriptor descriptor = getRootNodeDescriptor(bundle, path);
         // no root descriptor found
@@ -810,7 +808,7 @@
         try {
 
             ins = descriptor.rootNodeDescriptor.openStream();
-            this.contentCreator.init(configuration, session.getRootNode(), null);
+            this.contentCreator.prepareParsing(session.getRootNode(), null);
             descriptor.nodeReader.parse(ins, this.contentCreator);
 
             return descriptor.rootNodeDescriptor;
@@ -830,14 +828,7 @@
     }
 
     private String toPlainName(String name) {
-        String providerExt = null;
-        final Iterator<String> ipIter = importProviders.keySet().iterator();
-        while (providerExt == null && ipIter.hasNext()) {
-            final String ext = ipIter.next();
-            if (name.endsWith(ext)) {
-                providerExt = ext;
-            }
-        }
+        final String providerExt = this.contentCreator.getImportProviderExtension(name);
         if (providerExt != null) {
             return name.substring(0, name.length() - providerExt.length());
         }

Modified: incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/PathEntry.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/PathEntry.java?rev=670670&r1=670669&r2=670670&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/PathEntry.java
(original)
+++ incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/PathEntry.java
Mon Jun 23 09:42:47 2008
@@ -21,6 +21,7 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.StringTokenizer;
 
 import org.apache.sling.commons.osgi.ManifestHeader;
 import org.osgi.framework.Bundle;
@@ -55,12 +56,12 @@
     public static final String CHECKIN_DIRECTIVE = "checkin";
 
     /**
-     * The expand directive specifying whether the available {@link ImportProvider}s
-     * should be used during content loading. This is a boolean value that
-     * defaults to true.
+     * The ignore import providers directive specifying whether the available {@link ImportProvider}s
+     * should be used during content loading. This is a string value that defaults to the
empty
+     * string..
      * @since 2.0.4
      */
-    public static final String EXPAND_DIRECTIVE = "expand";
+    public static final String IGNORE_IMPORT_PROVIDERS_DIRECTIVE = "ignoreImportProviders";
 
     /** The path for the initial content. */
     private final String path;
@@ -74,8 +75,8 @@
     /** Should versionable nodes be checked in? */
     private final boolean checkin;
 
-    /** Should archives be expanded? @since 2.0.4 */
-    private final boolean expand;
+    /** Which import providers should be ignored? @since 2.0.4 */
+    private final List<String> ignoreImportProviders;
 
     /**
      * Target path where initial content will be loaded. If it´s null then
@@ -138,11 +139,13 @@
         }
 
         // expand directive
-        final String expandValue = entry.getDirectiveValue(EXPAND_DIRECTIVE);
-        if ( expandValue != null ) {
-            this.expand = Boolean.valueOf(expandValue);
-        } else {
-            this.expand = true;
+        this.ignoreImportProviders = new ArrayList<String>();
+        final String expandValue = entry.getDirectiveValue(IGNORE_IMPORT_PROVIDERS_DIRECTIVE);
+        if ( expandValue != null && expandValue.length() > 0 ) {
+            final StringTokenizer st = new StringTokenizer(expandValue, ",");
+            while ( st.hasMoreTokens() ) {
+                this.ignoreImportProviders.add(st.nextToken());
+            }
         }
     }
 
@@ -162,8 +165,11 @@
         return this.checkin;
     }
 
-    public boolean isExpand() {
-        return this.expand;
+    public boolean isIgnoredImportProvider(String extension) {
+        if ( extension.startsWith(".") ) {
+            extension = extension.substring(1);
+        }
+        return this.ignoreImportProviders.contains(extension);
     }
 
     public String getTarget() {

Modified: incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ZipReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ZipReader.java?rev=670670&r1=670669&r2=670670&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ZipReader.java
(original)
+++ incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ZipReader.java
Mon Jun 23 09:42:47 2008
@@ -29,10 +29,14 @@
 
 
 /**
- * The <code>JsonReader</code> TODO
+ * The <code>ZipReader</code> TODO
+ *
+ * @since 2.0.4
  */
 class ZipReader implements ContentReader {
 
+    private static final String NT_FOLDER = "nt:folder";
+
     static final ImportProvider ZIP_PROVIDER = new ImportProvider() {
         private ZipReader zipReader;
 
@@ -56,10 +60,10 @@
     };
 
     /** Is this a jar reader? */
-    private final boolean jarReader;
+    //private final boolean jarReader;
 
     public ZipReader(boolean jarReader) {
-        this.jarReader = jarReader;
+        //this.jarReader = jarReader;
     }
 
     /**
@@ -67,7 +71,7 @@
      */
     public void parse(InputStream ins, ContentCreator creator)
     throws IOException, RepositoryException {
-        creator.createNode(null, "nt:folder", null);
+        creator.createNode(null, NT_FOLDER, null);
         final ZipInputStream zis = new ZipInputStream(ins);
         final InputStream dataIS = new CloseShieldInputStream(zis);
         ZipEntry entry;
@@ -78,7 +82,7 @@
                     String name = entry.getName();
                     int pos = name.lastIndexOf('/');
                     if ( pos != -1 ) {
-                        creator.switchCurrentNode(name.substring(0, pos), "nt:folder");
+                        creator.switchCurrentNode(name.substring(0, pos), NT_FOLDER);
                     }
                     creator.createFileAndResourceNode(name, dataIS, null, entry.getTime());
                     creator.finishNode();



Mime
View raw message