karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject svn commit: r1449721 - /karaf/branches/karaf-2.2.x/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommandFactory.java
Date Mon, 25 Feb 2013 14:31:00 GMT
Author: jbonofre
Date: Mon Feb 25 14:31:00 2013
New Revision: 1449721

URL: http://svn.apache.org/r1449721
Log:
[KARAF-1327] Load command aliases (defined in shell.init.script) in SSH

Modified:
    karaf/branches/karaf-2.2.x/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommandFactory.java

Modified: karaf/branches/karaf-2.2.x/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommandFactory.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommandFactory.java?rev=1449721&r1=1449720&r2=1449721&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommandFactory.java
(original)
+++ karaf/branches/karaf-2.2.x/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommandFactory.java
Mon Feb 25 14:31:00 2013
@@ -18,11 +18,7 @@
  */
 package org.apache.karaf.shell.ssh;
 
-import java.io.Closeable;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PrintStream;
+import java.io.*;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.Map;
@@ -46,6 +42,8 @@ import org.slf4j.LoggerFactory;
 
 public class ShellCommandFactory implements CommandFactory {
 
+    public static final String SHELL_INIT_SCRIPT = "karaf.shell.init.script";
+
     private static final Logger LOGGER = LoggerFactory.getLogger(ShellCommandFactory.class);
 
     private CommandProcessor commandProcessor;
@@ -104,6 +102,8 @@ public class ShellCommandFactory impleme
                     Object result;
                     if (subject != null) {
                         try {
+                            String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
+                            executeScript(scriptFileName, session);
                             result = Subject.doAs(subject, new PrivilegedExceptionAction<Object>()
{
                                 public Object run() throws Exception {
                                     return session.execute(command);
@@ -113,6 +113,8 @@ public class ShellCommandFactory impleme
                             throw e.getException();
                         }
                     } else {
+                        String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
+                        executeScript(scriptFileName, session);
                         result = session.execute(command);
                     }
                     if (result != null)
@@ -191,4 +193,32 @@ public class ShellCommandFactory impleme
             }
         }
     }
+
+    private void executeScript(String scriptFileName, CommandSession session) {
+        if (scriptFileName != null) {
+            Reader r = null;
+            try {
+                File scriptFile = new File(scriptFileName);
+                r = new InputStreamReader(new FileInputStream(scriptFile));
+                CharArrayWriter w = new CharArrayWriter();
+                int n;
+                char[] buf = new char[8192];
+                while ((n = r.read(buf)) > 0) {
+                    w.write(buf, 0, n);
+                }
+                session.execute(new String(w.toCharArray()));
+            } catch (Exception e) {
+                LOGGER.debug("Error in initialization script", e);
+            } finally {
+                if (r != null) {
+                    try {
+                        r.close();
+                    } catch (IOException e) {
+                        // Ignore
+                    }
+                }
+            }
+        }
+    }
+
 }



Mime
View raw message