Index: shell/commands/src/main/java/org/apache/karaf/shell/commands/EditAction.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- shell/commands/src/main/java/org/apache/karaf/shell/commands/EditAction.java (revision ) +++ shell/commands/src/main/java/org/apache/karaf/shell/commands/EditAction.java (revision ) @@ -0,0 +1,161 @@ +package org.apache.karaf.shell.commands; + +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.felix.gogo.commands.Argument; +import org.apache.felix.gogo.commands.Command; +import org.apache.karaf.shell.console.AbstractAction; +import org.jledit.ConsoleEditor; +import org.jledit.EditorFactory; +import org.jledit.utils.Closeables; + +@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 { + Closeables.closeQuitely(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 { + Closeables.closeQuitely(fis); + } + } + + if (is != null) { + Closeables.closeQuitely(is); + } + + if (os != null) { + Closeables.closeQuitely(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; + } +} Index: shell/commands/pom.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- shell/commands/pom.xml (revision b4ea852b302d5ddf8156cd229c7da4a84a85696d) +++ shell/commands/pom.xml (revision ) @@ -43,6 +43,10 @@ org.apache.karaf.shell.console + org.jledit + core + + org.apache.felix org.apache.felix.gogo.runtime Index: shell/console/pom.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- shell/console/pom.xml (revision b4ea852b302d5ddf8156cd229c7da4a84a85696d) +++ shell/console/pom.xml (revision ) @@ -45,6 +45,10 @@ jansi + org.jledit + core + + org.osgi org.osgi.core @@ -127,11 +131,13 @@ org.apache.karaf.shell.console*;version=${project.version}, org.fusesource.jansi;version=${jansi.version};-split-package:=merge-first, jline*;version=${jline.version}, + org.jledit*;version=${jledit.version}, org.fusesource.hawtjni*;version=1.0;-split-package:=merge-first org.fusesource.jansi.internal;-split-package:=merge-first, org.apache.karaf.util*;-split-package:=merge-first, + org.mozilla.universalchardet*;-split-package:=merge-first, META-INF.native.*;-split-package:=merge-first Index: shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml (revision b4ea852b302d5ddf8156cd229c7da4a84a85696d) +++ shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml (revision ) @@ -46,6 +46,8 @@ + + org.apache.felix.service.command.Function @@ -101,6 +103,8 @@ + + Index: shell/console/src/main/resources/META-INF/services/org/jledit/simple IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- shell/console/src/main/resources/META-INF/services/org/jledit/simple (revision ) +++ shell/console/src/main/resources/META-INF/services/org/jledit/simple (revision ) @@ -0,0 +1,1 @@ +org.jledit.simple.SimpleConsoleEditor \ No newline at end of file Index: pom.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- pom.xml (revision b4ea852b302d5ddf8156cd229c7da4a84a85696d) +++ pom.xml (revision ) @@ -165,6 +165,7 @@ 1.0.0 1.9 2.9 + 0.1.1 3.2.3 1.2.17 2.0.9 @@ -791,6 +792,11 @@ org.fusesource.jansi jansi ${jansi.version} + + + org.jledit + core + ${jledit.version} org.apache.maven.artifact Index: shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (revision b4ea852b302d5ddf8156cd229c7da4a84a85696d) +++ shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (revision ) @@ -91,6 +91,11 @@ + + + + + @@ -111,5 +116,6 @@ +