Return-Path: Delivered-To: apmail-incubator-sling-commits-archive@locus.apache.org Received: (qmail 98453 invoked from network); 23 Jun 2008 13:47:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Jun 2008 13:47:57 -0000 Received: (qmail 91470 invoked by uid 500); 23 Jun 2008 13:46:29 -0000 Delivered-To: apmail-incubator-sling-commits-archive@incubator.apache.org Received: (qmail 90639 invoked by uid 500); 23 Jun 2008 13:46:27 -0000 Mailing-List: contact sling-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: sling-dev@incubator.apache.org Delivered-To: mailing list sling-commits@incubator.apache.org Received: (qmail 89587 invoked by uid 99); 23 Jun 2008 13:46:22 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jun 2008 06:46:22 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jun 2008 13:27:07 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id ABCE923889C2; Mon, 23 Jun 2008 06:27:26 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r670562 - in /incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal: ContentCreator.java ContentLoader.java ContentLoaderService.java Loader.java Date: Mon, 23 Jun 2008 13:27:26 -0000 To: sling-commits@incubator.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080623132726.ABCE923889C2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cziegeler Date: Mon Jun 23 06:27:25 2008 New Revision: 670562 URL: http://svn.apache.org/viewvc?rev=670562&view=rev Log: SLING-548: Provide methods for creating file nodes and to change the current context node. 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/ContentLoaderService.java incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/Loader.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=670562&r1=670561&r2=670562&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 06:27:25 2008 @@ -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 @@ * @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 @@ 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; } 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=670562&r1=670561&r2=670562&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 06:27:25 2008 @@ -26,6 +26,7 @@ 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 @@ 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 @@ /** * @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 @@ if ( this.rootNode == null ) { this.rootNode = node; } + return node; } + return null; } /** @@ -364,4 +376,73 @@ 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; + } + } Modified: incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java?rev=670562&r1=670561&r2=670562&view=diff ============================================================================== --- incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java (original) +++ incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoaderService.java Mon Jun 23 06:27:25 2008 @@ -60,7 +60,7 @@ 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 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=670562&r1=670561&r2=670562&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 06:27:25 2008 @@ -59,9 +59,6 @@ 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 @@ private Map importProviders; - private ContentLoader contentCreator = new ContentLoader(); + private final ContentLoader contentCreator; // bundles whose registration failed and should be retried private List delayedBundles; public Loader(ContentLoaderService jcrContentHelper) { this.jcrContentHelper = jcrContentHelper; + this.contentCreator = new ContentLoader(jcrContentHelper); this.delayedBundles = new LinkedList(); importProviders = new LinkedHashMap(); @@ -365,7 +363,7 @@ // 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 @@ * @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; - } + 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; } - // ensure sensible last modification date - if (lastModified <= 0) { - lastModified = System.currentTimeMillis(); - } - - 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(); } /**