You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by br...@apache.org on 2013/06/13 11:15:33 UTC

svn commit: r1492589 - in /ace/sandbox/bramk/org.apache.ace.cli: .settings/org.eclipse.jdt.core.prefs src/org/apache/ace/cli/Main.java src/org/apache/ace/cli/command/CopyDirectory.java src/org/apache/ace/cli/repository/AceObrRepository.java

Author: bramk
Date: Thu Jun 13 09:15:32 2013
New Revision: 1492589

URL: http://svn.apache.org/r1492589
Log:
[sandbox] Added initial copy directory command

Added:
    ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/command/CopyDirectory.java
Removed:
    ace/sandbox/bramk/org.apache.ace.cli/.settings/org.eclipse.jdt.core.prefs
Modified:
    ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/Main.java
    ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/repository/AceObrRepository.java

Modified: ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/Main.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/Main.java?rev=1492589&r1=1492588&r2=1492589&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/Main.java (original)
+++ ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/Main.java Thu Jun 13 09:15:32 2013
@@ -31,6 +31,7 @@ import java.util.Map.Entry;
 import java.util.Properties;
 
 import org.apache.ace.cli.command.AbstractCommand;
+import org.apache.ace.cli.command.CopyDirectory;
 import org.apache.ace.cli.command.CopyResources;
 import org.apache.ace.cli.command.CreateArtifacts;
 import org.apache.ace.cli.command.CreateDistribution;
@@ -61,6 +62,7 @@ public class Main {
 
     private final static List<AbstractCommand> COMMANDS = new ArrayList<AbstractCommand>();
     static {
+        COMMANDS.add(new CopyDirectory());
         COMMANDS.add(new ListResources());
         COMMANDS.add(new DeleteResources());
         COMMANDS.add(new CopyResources());
@@ -123,6 +125,9 @@ public class Main {
         if (commandLine.hasOption("f")) {
             configuration.put("filter", commandLine.getOptionValue("f"));
         }
+        if (commandLine.hasOption("d")) {
+            configuration.put("directory", commandLine.getOptionValue("d"));
+        }
 
         if (commandLine.hasOption("assign-name")) {
             configuration.put("assign-name", commandLine.getOptionValue("assign-name"));
@@ -159,6 +164,7 @@ public class Main {
         options.addOption("h", "help", false, "print this message");
         options.addOption("f", "filter", true, "<[<namespace>:]<filter>>, default namespace is osgi.identity");
         options.addOption("r", "repository", true, "<source|target>, default is target");
+        options.addOption("d", "directory", true, "source directory for copy-directory");
 
         options.addOption(OptionBuilder
             .withLongOpt("assign-name").hasArg()

Added: ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/command/CopyDirectory.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/command/CopyDirectory.java?rev=1492589&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/command/CopyDirectory.java (added)
+++ ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/command/CopyDirectory.java Thu Jun 13 09:15:32 2013
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.ace.cli.command;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Stack;
+
+import org.apache.ace.cli.repository.AceObrRepository;
+
+import aQute.bnd.deployer.repository.FixedIndexedRepo;
+
+/**
+ * Command that recursively copies resources from a local directory to a target ACE repository.
+ * 
+ * 
+ * Notes:
+ * <ol>
+ * <li>Standard BND repository put is bypassed because ACE OBR needs filename for non-bundles</li>
+ * <li>This just tries an upload per file that may fail due to the fact a resource already exists.</li>
+ * <li>Locally we do not have a Repository so can not check for existence or location</li>
+ * <li>ACE OBR does not return location upon conflict so we can not delete it based on this</li>
+ * <li>Because locally we do not have a Repository we can not check for existence in the target repository.</li>
+ * </ol>
+ * 
+ * FIXME We should generate a local repository index first in order to be able to address the notes.
+ * 
+ */
+public class CopyDirectory extends AbstractCommand {
+
+    @Override
+    public String getName() {
+        return "copy-directory";
+    }
+
+    @Override
+    public String getDescription() {
+        return "copy directory recursively to the target repository (no filter support)";
+    }
+
+    @Override
+    public void doCommand(String command) throws Exception {
+
+        FixedIndexedRepo targetRepo = getTargetRepository();
+        if (!targetRepo.canWrite()) {
+            throw new IllegalArgumentException("The target repository is not writable");
+        }
+        if (!(targetRepo instanceof AceObrRepository)) {
+            throw new IllegalArgumentException("The target repository is not an ACE OBR");
+        }
+        String dirName = getConfiguration().get("directory");
+        if (dirName == null) {
+            throw new IllegalArgumentException("Copying a directory requires valid -d");
+        }
+        File dirFile = new File(dirName);
+        if (!dirFile.exists() || !dirFile.isDirectory()) {
+            throw new IllegalArgumentException("Copying a directory requires valid -d");
+        }
+
+        AceObrRepository aceRepo = (AceObrRepository) targetRepo;
+
+        int copyCount = 0;
+        int skipCount = 0;
+        printVerbose("Recursively copying directory to target from " + getFilterNamespace() + ":" + getFilter());
+
+        Stack<File> dirs = new Stack<File>();
+        dirs.push(dirFile);
+        while (!dirs.isEmpty()) {
+            File dir = dirs.pop();
+            File[] files = dir.listFiles();
+            for (File file : files) {
+                if (file.isDirectory()) {
+                    dirs.push(file);
+                    continue;
+                }
+
+                // We need a mimetype or Jetty will throw a 500 Form too large
+                String mimeType = "application/jar";
+                if (file.getName().endsWith(".xml")) {
+                    mimeType = "text/xml";
+                }
+                InputStream input = new FileInputStream(file);
+                try {
+                    URI uri = aceRepo.upload(input, file.getName(), mimeType);
+                    System.out.println("Copied " + file.getAbsolutePath() + " => " + uri.toString());
+                    copyCount++;
+                }
+                catch (Exception e) {
+                    System.out.println("Failed to upload " + file.getAbsolutePath());
+                    e.printStackTrace();
+                }
+                finally {
+                    input.close();
+                }
+            }
+        }
+        printVerbose("Copied " + copyCount + " resources (skipped " + skipCount + "existing)");
+    }
+}

Modified: ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/repository/AceObrRepository.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/repository/AceObrRepository.java?rev=1492589&r1=1492588&r2=1492589&view=diff
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/repository/AceObrRepository.java (original)
+++ ace/sandbox/bramk/org.apache.ace.cli/src/org/apache/ace/cli/repository/AceObrRepository.java Thu Jun 13 09:15:32 2013
@@ -71,19 +71,23 @@ public class AceObrRepository extends Fi
             throw new IllegalArgumentException("No stream and/or options specified");
 
         PutResult result = new PutResult();
-        result.artifact = upload(m_endpoint, stream, "", options.type);
+        result.artifact = upload(stream, "", options.type);
 
         reset();
         return result;
     }
 
-    private URI upload(URL aceOBR, InputStream stream, String filename, String mimetype) throws Exception {
+    public URL getEndpoint() {
+        return m_endpoint;
+    }
+
+    public URI upload(InputStream stream, String filename, String mimetype) throws Exception {
 
         OutputStream output = null;
         String location = null;
         try {
 
-            URL url = new URL(aceOBR.toExternalForm() + "?filename=" + filename);
+            URL url = new URL(m_endpoint, "?filename=" + filename);
             URLConnection connection = url.openConnection();
             connection.setDoOutput(true);
             connection.setDoInput(true);
@@ -104,7 +108,7 @@ public class AceObrRepository extends Fi
                 output.write(buffer, 0, count);
                 size += count;
                 if (m_verbose)
-                    System.err.println("Uploaded bytes... " + size);
+                    System.out.println("Uploaded bytes... " + size);
             }
             output.close();