You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by un...@apache.org on 2005/04/26 00:37:44 UTC

svn commit: r164677 - /incubator/jackrabbit/trunk/contrib/examples/src/java/org/apache/jackrabbit/examples/FSImport.java

Author: uncled
Date: Mon Apr 25 15:37:43 2005
New Revision: 164677

URL: http://svn.apache.org/viewcvs?rev=164677&view=rev
Log:
File system import example

Added:
    incubator/jackrabbit/trunk/contrib/examples/src/java/org/apache/jackrabbit/examples/FSImport.java   (with props)

Added: incubator/jackrabbit/trunk/contrib/examples/src/java/org/apache/jackrabbit/examples/FSImport.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/examples/src/java/org/apache/jackrabbit/examples/FSImport.java?rev=164677&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/examples/src/java/org/apache/jackrabbit/examples/FSImport.java (added)
+++ incubator/jackrabbit/trunk/contrib/examples/src/java/org/apache/jackrabbit/examples/FSImport.java Mon Apr 25 15:37:43 2005
@@ -0,0 +1,169 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.examples;
+
+import org.apache.jackrabbit.core.jndi.RegistryHelper;
+
+import javax.jcr.*;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.File;
+import java.io.IOException;
+import java.util.Hashtable;
+import java.util.Calendar;
+
+/**
+ * The File System import example class.
+ */
+public class FSImport {
+
+    /**
+     * Run the Filesystem Import example.
+     *
+     * @param args command line arguments
+     */
+    public static void main(String[] args) {
+        try {
+            Repository repository = getRepository();
+            SimpleCredentials creds = new SimpleCredentials("username", "password".toCharArray());
+            Session session = repository.login(creds);
+            Node root = session.getRootNode();
+
+            if (args.length>0) {
+                File startingpoint=new File(args[0]);
+                importFolder (root.addNode(startingpoint.getName(), "nt:folder"), startingpoint);
+                session.save();
+                dump(root.getNode(startingpoint.getName()));
+            } else {
+                System.err.println("Please specify a starting point for the file system import into the repository.");
+            }
+
+
+        } catch (Exception e) {
+            System.err.println(e);
+        }
+    }
+
+    /**
+     * Creates a Repository instance to be used by the example class.
+     *
+     * @return repository instance
+     * @throws Exception on errors
+     */
+    private static Repository getRepository() throws Exception {
+        String configFile = "repotest/repository.xml";
+        String repHomeDir = "repotest";
+
+        Hashtable env = new Hashtable();
+        env.put(Context.INITIAL_CONTEXT_FACTORY,
+                "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
+        env.put(Context.PROVIDER_URL, "localhost");
+        InitialContext ctx = new InitialContext(env);
+
+        RegistryHelper.registerRepository(ctx, "repo", configFile, repHomeDir, true);
+        return (Repository) ctx.lookup("repo");
+    }
+
+    /**
+     * Imports a File.
+     *
+     * @param parentnode Parent Repository Node
+     * @param file File to be imported
+     * @throws RepositoryException, IOException on errors
+     */
+
+    public static void importFile(Node parentnode, File file) throws RepositoryException, IOException {
+
+        // add some other means to extract mime type from filename
+        String mimeType = "application/octet-stream";
+
+        Node fileNode = parentnode.addNode(file.getName(), "nt:file");
+        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
+        resNode.setProperty("jcr:mimeType", mimeType);
+        resNode.setProperty("jcr:encoding", "");
+        resNode.setProperty("jcr:data", new FileInputStream(file));
+        Calendar lastModified = Calendar.getInstance();
+        lastModified.setTimeInMillis(file.lastModified());
+        resNode.setProperty("jcr:lastModified", lastModified);
+        System.out.println(fileNode.getPath());
+   }
+
+
+    /**
+     * Import a Folder.
+     *
+     * @param parentnode Parent Repository Node
+     * @param directory Directory to be traversed
+     * @throws RepositoryException, IOException on errors
+     */
+
+    private static void importFolder(Node parentnode, File directory) throws RepositoryException, IOException  {
+        File[] direntries =directory.listFiles();
+        System.out.println(parentnode.getPath());
+        for (int i=0; i<direntries.length; i++) {
+            File direntry = direntries[i];
+            if (direntry.isDirectory()) {
+                Node childnode = parentnode.addNode(direntry.getName(),"nt:folder");
+                importFolder(childnode, direntry);
+            } else {
+                importFile(parentnode, direntry);
+            }
+        }
+    }
+
+    /**
+     * Dumps the contents of the given node to standard output.
+     *
+     * @param node the node to be dumped
+     * @throws RepositoryException on repository errors
+     */
+    public static void dump(Node node) throws RepositoryException {
+        System.out.println(node.getPath());
+
+        PropertyIterator properties = node.getProperties();
+        while (properties.hasNext()) {
+            Property property = properties.nextProperty();
+            System.out.print(property.getPath() + "=");
+            if (property.getDefinition().isMultiple()) {
+                Value[] values = property.getValues();
+                for (int i = 0; i < values.length; i++) {
+                    if (i > 0) {
+                        System.out.println(",");
+                    }
+                    System.out.println(values[i].getString());
+                }
+            } else {
+                if (property.getType()==PropertyType.BINARY) {
+                    System.out.print("<binary>");
+                }  else {
+                    System.out.print(property.getString());
+                }
+
+            }
+            System.out.println();
+        }
+
+        NodeIterator nodes = node.getNodes();
+        while (nodes.hasNext()) {
+            Node child = nodes.nextNode();
+            dump(child);
+        }
+    }
+
+}

Propchange: incubator/jackrabbit/trunk/contrib/examples/src/java/org/apache/jackrabbit/examples/FSImport.java
------------------------------------------------------------------------------
    svn:eol-style = native