karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ioca...@apache.org
Subject svn commit: r1434642 - in /karaf/trunk: ./ assemblies/features/framework/ shell/commands/ shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/ shell/commands/src/main/resources/OSGI-INF/blueprint/
Date Thu, 17 Jan 2013 12:36:29 GMT
Author: iocanel
Date: Thu Jan 17 12:36:28 2013
New Revision: 1434642

URL: http://svn.apache.org/viewvc?rev=1434642&view=rev
Log:
[KARAF-2044] Add shell:edit command.

Added:
    karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/EditAction.java
Modified:
    karaf/trunk/assemblies/features/framework/pom.xml
    karaf/trunk/pom.xml
    karaf/trunk/shell/commands/pom.xml
    karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml

Modified: karaf/trunk/assemblies/features/framework/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/assemblies/features/framework/pom.xml?rev=1434642&r1=1434641&r2=1434642&view=diff
==============================================================================
--- karaf/trunk/assemblies/features/framework/pom.xml (original)
+++ karaf/trunk/assemblies/features/framework/pom.xml Thu Jan 17 12:36:28 2013
@@ -75,6 +75,10 @@
             <groupId>jline</groupId>
             <artifactId>jline</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.jledit</groupId>
+            <artifactId>core</artifactId>
+        </dependency>
 
         <!-- listed in source feature.xml with explicit startlevel -->
         <dependency>

Modified: karaf/trunk/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/pom.xml?rev=1434642&r1=1434641&r2=1434642&view=diff
==============================================================================
--- karaf/trunk/pom.xml (original)
+++ karaf/trunk/pom.xml Thu Jan 17 12:36:28 2013
@@ -197,6 +197,7 @@
         <gemini.blueprint.version>1.0.0.RELEASE</gemini.blueprint.version>
         <jansi.version>1.9</jansi.version>
         <jline.version>2.9</jline.version>
+        <jledit.version>0.1.2</jledit.version>
         <jsw.version>3.2.3</jsw.version>
         <log4j.version>1.2.17</log4j.version>
         <maven.version>2.0.9</maven.version>
@@ -1174,6 +1175,11 @@
                 <version>${jansi.version}</version>
             </dependency>
             <dependency>
+                <groupId>org.jledit</groupId>
+                <artifactId>core</artifactId>
+                <version>${jledit.version}</version>
+            </dependency>
+            <dependency>
                 <groupId>org.apache.maven.artifact</groupId>
                 <artifactId>maven-artifact</artifactId>
                 <version>3.0-alpha-1</version>

Modified: karaf/trunk/shell/commands/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/pom.xml?rev=1434642&r1=1434641&r2=1434642&view=diff
==============================================================================
--- karaf/trunk/shell/commands/pom.xml (original)
+++ karaf/trunk/shell/commands/pom.xml Thu Jan 17 12:36:28 2013
@@ -46,7 +46,10 @@
             <groupId>org.apache.karaf.shell</groupId>
             <artifactId>org.apache.karaf.shell.table</artifactId>
         </dependency>
-
+        <dependency>
+            <groupId>org.jledit</groupId>
+            <artifactId>core</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.gogo.runtime</artifactId>

Added: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/EditAction.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/EditAction.java?rev=1434642&view=auto
==============================================================================
--- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/EditAction.java
(added)
+++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/EditAction.java
Thu Jan 17 12:36:28 2013
@@ -0,0 +1,161 @@
+package org.apache.karaf.shell.commands.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.UUID;
+import java.util.regex.Pattern;
+import jline.Terminal;
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.console.AbstractAction;
+import org.apache.karaf.util.StreamUtils;
+import org.jledit.ConsoleEditor;
+import org.jledit.EditorFactory;
+
+@Command(scope = "shell", name = "edit", description = "Calls a text editor.")
+public class EditAction extends AbstractAction {
+
+        private final Pattern URL_PATTERN = Pattern.compile("[^: ]+:[^ ]+");
+
+        @Argument(index = 0, name = "url", description = "The url of the resource to edit.",
required = true, multiValued = false)
+        private String url;
+
+        private EditorFactory editorFactory;
+
+
+        @Override
+        protected Object doExecute() throws Exception {
+            URLConnection connection = null;
+            InputStream is = null;
+            OutputStream os = null;
+            String path = null;
+            boolean isLocal = true;
+            String sourceUrl = url;
+
+            //If no url format found, assume file url.
+            if (!URL_PATTERN.matcher(sourceUrl).matches()) {
+                File f = new File(sourceUrl);
+                sourceUrl = "file://" + f.getAbsolutePath();
+            }
+
+            URL u = new URL(sourceUrl);
+            //If its not a file url.
+            if (!u.getProtocol().equals("file")) {
+                isLocal = false;
+
+                try {
+                    connection = u.openConnection();
+                    is = connection.getInputStream();
+                } catch (IOException ex) {
+                    System.out.println("Failed to open " + sourceUrl + " for reading.");
+                    return null;
+                }
+                try {
+                    os = connection.getOutputStream();
+                } catch (IOException ex) {
+                    System.out.println("Failed to open " + sourceUrl + " for writing.");
+                    return null;
+                }
+
+                //Copy the resource to a tmp location.
+                FileOutputStream fos = null;
+                try {
+                    path = System.getProperty("karaf.data") + "/editor/" + UUID.randomUUID();
+                    File f = new File(path);
+                    if (!f.exists()) {
+                        if (!f.getParentFile().exists()) {
+                            f.getParentFile().mkdirs();
+                        }
+                    }
+
+                    fos = new FileOutputStream(f);
+                    copy(is, fos);
+                } catch (Exception ex) {
+                    System.out.println("Failed to copy resource from url:" + sourceUrl +
" to tmp file: " + path + "  for editing.");
+                } finally {
+                    StreamUtils.close(fos);
+                }
+            } else {
+                path = u.getFile();
+            }
+
+
+            File file = new File(path);
+            if (!file.exists()) {
+                if (!file.getParentFile().exists()) {
+                    file.getParentFile().mkdirs();
+                }
+            }
+
+            //Call the editor
+            ConsoleEditor editor = editorFactory.create(getTerminal());
+            editor.setTitle("Karaf");
+            editor.open(file, url);
+            editor.setOpenEnabled(false);
+            editor.start();
+
+            //If resource is not local, copy the resource back.
+            if (!isLocal) {
+                FileInputStream fis = new FileInputStream(path);
+                try {
+                    copy(fis, os);
+                } finally {
+                    StreamUtils.close(fis);
+                }
+            }
+
+            if (is != null) {
+                StreamUtils.close(is);
+            }
+
+            if (os != null) {
+                StreamUtils.close(os);
+            }
+            return null;
+        }
+
+        /**
+         * Gets the {@link jline.Terminal} from the current session.
+         *
+         * @return
+         * @throws Exception
+         */
+    private Terminal getTerminal() throws Exception {
+        Object terminalObject = session.get(".jline.terminal");
+        if (terminalObject instanceof Terminal) {
+            return (Terminal) terminalObject;
+
+        }
+        throw new IllegalStateException("Could not get Terminal from CommandSession.");
+    }
+
+    /**
+     * Copies the content of {@link InputStream} to {@link OutputStream}.
+     *
+     * @param input
+     * @param output
+     * @throws IOException
+     */
+    private void copy(final InputStream input, final OutputStream output) throws IOException
{
+        byte[] buffer = new byte[1024 * 16];
+        int n = 0;
+        while (-1 != (n = input.read(buffer))) {
+            output.write(buffer, 0, n);
+            output.flush();
+        }
+    }
+
+    public EditorFactory getEditorFactory() {
+        return editorFactory;
+    }
+
+    public void setEditorFactory(EditorFactory editorFactory) {
+        this.editorFactory = editorFactory;
+    }
+}

Modified: karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1434642&r1=1434641&r2=1434642&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (original)
+++ karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml Thu
Jan 17 12:36:28 2013
@@ -97,6 +97,11 @@
                 <null/>
             </completers>
         </command>
+        <command>
+            <action class="org.apache.karaf.shell.commands.impl.EditAction">
+                <property name="editorFactory" ref="editorFactory"/>
+            </action>
+        </command>
     </command-bundle>
 
    <bean id="commandCompleter" class="org.apache.karaf.shell.console.completer.CommandsCompleter"/>
@@ -117,5 +122,6 @@
     </reference-list>
 
     <reference id="commandProcessor" interface="org.apache.felix.service.command.CommandProcessor"/>
+    <reference id="editorFactory" interface="org.jledit.EditorFactory"/>
  
 </blueprint>



Mime
View raw message