This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.jcr.contentloader-2.0.4-incubator in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-contentloader.git
commit b042f5d41b7e6b4815b777bcb40d417edbd13c2e Author: Carsten Ziegeler <[email protected]> AuthorDate: Mon Jun 23 13:27:25 2008 +0000 SLING-548: Provide methods for creating file nodes and to change the current context node. git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/jcr/contentloader@670562 13f79535-47bb-0310-9956-ffa450edef68 --- .../jcr/contentloader/internal/ContentCreator.java | 40 ++++++++++- .../jcr/contentloader/internal/ContentLoader.java | 83 +++++++++++++++++++++- .../internal/ContentLoaderService.java | 2 +- .../sling/jcr/contentloader/internal/Loader.java | 57 ++++++--------- 4 files changed, 143 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java index bd72de7..3e9acaf 100644 --- a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java +++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java @@ -18,6 +18,9 @@ */ package org.apache.sling.jcr.contentloader.internal; +import java.io.InputStream; + +import javax.jcr.Node; import javax.jcr.RepositoryException; /** @@ -38,7 +41,7 @@ interface ContentCreator { * @param mixinNodeTypes The mixin node types or null. * @throws RepositoryException If anything goes wrong. */ - void createNode(String name, + Node createNode(String name, String primaryNodeType, String[] mixinNodeTypes) throws RepositoryException; @@ -95,4 +98,39 @@ interface ContentCreator { void createProperty(String name, Object[] values) throws RepositoryException; + + /** + * Create a file and a resource node. + * After the nodes have been created, the current node is the resource node. + * So this method call should be followed by two calls to {@link #finishNode()} + * to be on the same level as before the file creation. + * @param name The name of the file node + * @param data The data of the file + * @param mimeType The mime type or null + * @param lastModified The last modified or -1 + * @throws RepositoryException + */ + void createFileAndResourceNode(String name, + InputStream data, + String mimeType, + long lastModified) + throws RepositoryException; + + /** + * Switch the current node to the path (which must be relative + * to the root node of the import). + * If the path does not exist and a node type is supplied, + * the nodes are created with the given node type. + * If the path does not exist and node type is null, false is + * returned. + * Switching is only allowed if the current node is the root node + * of the import. + * When the changes to the node are finished, {@link #finishNode()} + * must be callsed. + * @param subPath The relative path + * @param newNodeType Node typ for newly created nodes. + * @throws RepositoryException + */ + boolean switchCurrentNode(String subPath, String newNodeType) + throws RepositoryException; } diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java index 415b5a7..fe2e544 100644 --- a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java +++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Stack; +import java.util.StringTokenizer; import javax.jcr.Item; import javax.jcr.Node; @@ -56,6 +57,15 @@ public class ContentLoader implements ContentCreator { private boolean isRootNodeImport; + // default content type for createFile() + private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream"; + + private final ContentLoaderService jcrContentHelper; + + public ContentLoader(ContentLoaderService jcrContentHelper) { + this.jcrContentHelper = jcrContentHelper; + } + /** * Initialize this component. * If the defaultRootName is null, we are in ROOT_NODE import mode. @@ -90,7 +100,7 @@ public class ContentLoader implements ContentCreator { /** * @see org.apache.sling.jcr.contentloader.internal.ContentCreator#createNode(java.lang.String, java.lang.String, java.lang.String[]) */ - public void createNode(String name, + public Node createNode(String name, String primaryNodeType, String[] mixinNodeTypes) throws RepositoryException { @@ -147,7 +157,9 @@ public class ContentLoader implements ContentCreator { if ( this.rootNode == null ) { this.rootNode = node; } + return node; } + return null; } /** @@ -364,4 +376,73 @@ public class ContentLoader implements ContentCreator { return (item.isNode()) ? (Node) item : null; } + + /** + * @see org.apache.sling.jcr.contentloader.internal.ContentCreator#createFileAndResourceNode(java.lang.String, java.io.InputStream, java.lang.String, long) + */ + public void createFileAndResourceNode(String name, + InputStream data, + String mimeType, + long lastModified) + throws RepositoryException { + int lastSlash = name.lastIndexOf('/'); + name = (lastSlash < 0) ? name : name.substring(lastSlash + 1); + final Node parentNode = this.parentNodeStack.peek(); + + // if node already exists but should be overwritten, delete it + if (this.configuration.isOverwrite() && parentNode.hasNode(name)) { + parentNode.getNode(name).remove(); + } else if (parentNode.hasNode(name)) { + return; + } + + // ensure content type + if (mimeType == null) { + mimeType = jcrContentHelper.getMimeType(name); + if (mimeType == null) { + jcrContentHelper.log.info( + "createFile: Cannot find content type for {}, using {}", + name, DEFAULT_CONTENT_TYPE); + mimeType = DEFAULT_CONTENT_TYPE; + } + } + + // ensure sensible last modification date + if (lastModified <= 0) { + lastModified = System.currentTimeMillis(); + } + + this.createNode(name, "nt:file", null); + this.createNode("jcr:content", "nt:resource", null); + this.createProperty("jcr:mimeType", mimeType); + this.createProperty("jcr:lastModified", lastModified); + this.createProperty("jcr:data", data); + } + + /** + * @see org.apache.sling.jcr.contentloader.internal.ContentCreator#switchCurrentNode(java.lang.String, java.lang.String) + */ + public boolean switchCurrentNode(String subPath, String newNodeType) + throws RepositoryException { + if ( this.parentNodeStack.size() > 1 ) { + throw new RepositoryException("Switching the current node is not allowed."); + } + if ( subPath.startsWith("/") ) { + subPath = subPath.substring(1); + } + final StringTokenizer st = new StringTokenizer(subPath, "/"); + Node node = this.parentNodeStack.peek(); + while ( st.hasMoreTokens() ) { + final String token = st.nextToken(); + if ( !node.hasNode(token) ) { + if ( newNodeType == null ) { + return false; + } + node = node.addNode(token, newNodeType); + } + } + this.parentNodeStack.push(node); + return true; + } + } diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java index 347ce30..341fda5 100644 --- a/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java +++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java @@ -60,7 +60,7 @@ public class ContentLoaderService implements SynchronousBundleListener { public static final String BUNDLE_CONTENT_NODE = "/var/sling/bundle-content"; /** default log */ - private final Logger log = LoggerFactory.getLogger(getClass()); + final Logger log = LoggerFactory.getLogger(getClass()); /** * The JCR Repository we access to resolve resources diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java index 9e5efe8..fd55242 100644 --- a/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java +++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.java @@ -59,9 +59,6 @@ public class Loader { public static final String ROOT_DESCRIPTOR = "/ROOT"; - // default content type for createFile() - private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream"; - /** default log */ private final Logger log = LoggerFactory.getLogger(Loader.class); @@ -69,13 +66,14 @@ public class Loader { private Map<String, ImportProvider> importProviders; - private ContentLoader contentCreator = new ContentLoader(); + private final ContentLoader contentCreator; // bundles whose registration failed and should be retried private List<Bundle> delayedBundles; public Loader(ContentLoaderService jcrContentHelper) { this.jcrContentHelper = jcrContentHelper; + this.contentCreator = new ContentLoader(jcrContentHelper); this.delayedBundles = new LinkedList<Bundle>(); importProviders = new LinkedHashMap<String, ImportProvider>(); @@ -365,7 +363,7 @@ public class Loader { // otherwise just place as file try { - createFile(parent, file); + createFile(configuration, parent, file); } catch (IOException ioe) { log.warn("Cannot create file node for {}", file, ioe); } @@ -475,39 +473,26 @@ public class Loader { * @throws IOException * @throws RepositoryException */ - private void createFile(Node parent, URL source) throws IOException, - RepositoryException { - String name = getName(source.getPath()); - if (parent.hasNode(name)) { - return; - } - - URLConnection conn = source.openConnection(); - long lastModified = conn.getLastModified(); - String type = conn.getContentType(); - InputStream data = conn.getInputStream(); - - // ensure content type - if (type == null) { - type = jcrContentHelper.getMimeType(name); - if (type == null) { - log.info( - "createFile: Cannot find content type for {}, using {}", - source.getPath(), DEFAULT_CONTENT_TYPE); - type = DEFAULT_CONTENT_TYPE; - } - } - - // ensure sensible last modification date - if (lastModified <= 0) { - lastModified = System.currentTimeMillis(); + private void createFile(PathEntry configuration, Node parent, URL source) + throws IOException, RepositoryException { + final String srcPath = source.getPath(); + int pos = srcPath.lastIndexOf("/"); + final String name = getName(source.getPath()); + final String path; + if ( pos == -1 ) { + path = name; + } else { + path = srcPath.substring(0, pos + 1) + name; } - Node file = parent.addNode(name, "nt:file"); - Node content = file.addNode("jcr:content", "nt:resource"); - content.setProperty("jcr:mimeType", type); - content.setProperty("jcr:lastModified", lastModified); - content.setProperty("jcr:data", data); + this.contentCreator.init(configuration, parent, name); + final URLConnection conn = source.openConnection(); + final long lastModified = conn.getLastModified(); + final String type = conn.getContentType(); + final InputStream data = conn.getInputStream(); + this.contentCreator.createFileAndResourceNode(path, data, type, lastModified); + this.contentCreator.finishNode(); + this.contentCreator.finishNode(); } /** -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
