You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/06/16 11:35:42 UTC

svn commit: r668093 - in /servicemix/sandbox/gshell-web/gshell-gwt: ./ src/org/apache/servicemix/gshellweb/ src/org/apache/servicemix/gshellweb/client/ src/org/apache/servicemix/gshellweb/server/

Author: gnodet
Date: Mon Jun 16 02:35:42 2008
New Revision: 668093

URL: http://svn.apache.org/viewvc?rev=668093&view=rev
Log:
Add simple RPC servlet to run commands on the server side

Added:
    servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellService.java
    servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellServiceAsync.java
    servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/server/
    servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/server/ShellServiceImpl.java
Modified:
    servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-compile
    servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-shell
    servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/WebConsole.gwt.xml
    servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/Console.java

Modified: servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-compile
URL: http://svn.apache.org/viewvc/servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-compile?rev=668093&r1=668092&r2=668093&view=diff
==============================================================================
--- servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-compile (original)
+++ servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-compile Mon Jun 16 02:35:42 2008
@@ -1,3 +1,3 @@
 #!/bin/sh
 APPDIR=`dirname $0`;
-java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:/Users/chirino/opt/gwt-mac-1.5.0/gwt-user.jar:/Users/chirino/opt/gwt-mac-1.5.0/gwt-dev-mac.jar" com.google.gwt.dev.GWTCompiler -out "$APPDIR/www" "$@" org.apache.servicemix.gshellweb.WebConsole;
+java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:$APPDIR/target/classes:/Users/gnodet/work/apps/gwt-mac-1.5.0/gwt-user.jar:/Users/gnodet/work/apps/gwt-mac-1.5.0/gwt-dev-mac.jar" com.google.gwt.dev.GWTCompiler -out "$APPDIR/www" "$@" org.apache.servicemix.gshellweb.WebConsole;

Modified: servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-shell
URL: http://svn.apache.org/viewvc/servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-shell?rev=668093&r1=668092&r2=668093&view=diff
==============================================================================
--- servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-shell (original)
+++ servicemix/sandbox/gshell-web/gshell-gwt/WebConsole-shell Mon Jun 16 02:35:42 2008
@@ -1,3 +1,3 @@
 #!/bin/sh
 APPDIR=`dirname $0`;
-java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:/Users/chirino/opt/gwt-mac-1.5.0/gwt-user.jar:/Users/chirino/opt/gwt-mac-1.5.0/gwt-dev-mac.jar" com.google.gwt.dev.GWTShell -out "$APPDIR/www" "$@" org.apache.servicemix.gshellweb.WebConsole/WebConsole.html;
+java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/bin:$APPDIR/target/classes:/Users/gnodet/work/apps/gwt-mac-1.5.0/gwt-user.jar:/Users/gnodet/work/apps/gwt-mac-1.5.0/gwt-dev-mac.jar" com.google.gwt.dev.GWTShell -out "$APPDIR/www" "$@" org.apache.servicemix.gshellweb.WebConsole/WebConsole.html;

Modified: servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/WebConsole.gwt.xml
URL: http://svn.apache.org/viewvc/servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/WebConsole.gwt.xml?rev=668093&r1=668092&r2=668093&view=diff
==============================================================================
--- servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/WebConsole.gwt.xml (original)
+++ servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/WebConsole.gwt.xml Mon Jun 16 02:35:42 2008
@@ -31,7 +31,10 @@
 
       <!-- Specify the app entry point class.                         -->
       <entry-point class='org.apache.servicemix.gshellweb.client.WebConsole'/>
-    
+
+      <!-- Servlets -->
+      <servlet path='/shell' class='org.apache.servicemix.gshellweb.server.ShellServiceImpl'/>
+
       <!-- Specify the application specific style sheet.              -->
       <stylesheet src='WebConsole.css' />
 	

Modified: servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/Console.java
URL: http://svn.apache.org/viewvc/servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/Console.java?rev=668093&r1=668092&r2=668093&view=diff
==============================================================================
--- servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/Console.java (original)
+++ servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/Console.java Mon Jun 16 02:35:42 2008
@@ -17,9 +17,12 @@
 package org.apache.servicemix.gshellweb.client;
 
 import java.util.ArrayList;
+import java.util.List;
 
 import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.WindowResizeListener;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.google.gwt.user.client.rpc.ServiceDefTarget;
 import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.Grid;
 import com.google.gwt.user.client.ui.HTML;
@@ -27,6 +30,7 @@
 import com.google.gwt.user.client.ui.PopupPanel;
 import com.google.gwt.user.client.ui.TextBox;
 import com.google.gwt.user.client.ui.Widget;
+import com.google.gwt.core.client.GWT;
 
 public class Console extends PopupPanel {
 
@@ -39,9 +43,18 @@
     }
 
     CommandListener commandListener;
-    ArrayList<String> commandHistory = new ArrayList<String>();
+    List<String> commandHistory = new ArrayList<String>();
     int historyIndex=0;
     private boolean showing;
+    private ShellServiceAsync shell;
+    private State state;
+    private String sessionId;
+
+    private enum State {
+        PromptLogin,
+        PromptPassword,
+        PromptCommand
+    };
     
     public Console() {
 
@@ -104,7 +117,63 @@
                 input.getElement().scrollIntoView();
             }
         });
-                
+
+    }
+
+    private void createShell() {
+        println("Creating shell service...");
+        shell = GWT.create(ShellService.class);
+        ServiceDefTarget endpoint = (ServiceDefTarget) shell;
+        String moduleRelativeURL = GWT.getModuleBaseURL() + "shell";
+        endpoint.setServiceEntryPoint(moduleRelativeURL);
+        println("Logging in...");
+        try {
+            shell.login("smx", "smx", new AsyncCallback<String>() {
+                public void onFailure(Throwable throwable) {
+                    println("Login failed: " + throwable);
+                }
+
+                public void onSuccess(String o) {
+                    println("Login succeeded: " + o);
+                    sessionId = o;
+                    state = State.PromptCommand;
+                    pollNextLineOut();
+                    pollNextLineErr();
+                }
+            });
+        } catch (Throwable t) {
+            println("Error " + t);
+        }
+    }
+
+    private void pollNextLineOut() {
+        shell.getNextLineOut(sessionId, new AsyncCallback<String>() {
+            public void onFailure(Throwable throwable) {
+                println("getNextLineOut error: " + throwable);
+            }
+
+            public void onSuccess(String s) {
+                if (s != null) {
+                    println("out: " + s);
+                }
+                pollNextLineOut();
+            }
+        });
+    }
+
+    private void pollNextLineErr() {
+        shell.getNextLineErr(sessionId, new AsyncCallback<String>() {
+            public void onFailure(Throwable throwable) {
+                println("pollNextLineErr error: " + throwable);
+            }
+
+            public void onSuccess(String s) {
+                if (s != null) {
+                    println("err: " + s);
+                }
+                pollNextLineErr();
+            }
+        });
     }
 
     protected void onTab() {
@@ -113,18 +182,31 @@
     }
 
     protected void onEnter() {
-        
+        if (state != State.PromptCommand) {
+            println("No session");
+            return;
+        }
+
         String command = input.getText();
         input.setText("");
         commandHistory.add(command);
         
         println(prompt.getHTML()+command);
-        
+
+        shell.setNextLineIn(sessionId, command, new AsyncCallback() {
+            public void onFailure(Throwable throwable) {
+                println("Unable to send command: " + throwable);
+            }
+            public void onSuccess(Object o) {
+            }
+        });
+        /*
         if( commandListener!=null ) {
             commandListener.onCommand(this, command);
         } else {
             println("Internal Error.. no command listener configured.");
         }
+        */
         
     }
 
@@ -179,7 +261,9 @@
         input.getElement().scrollIntoView();
         input.setFocus(true);
         showing=true;
-        
+        if (shell == null) {
+            createShell();
+        }
     }
     
     @Override

Added: servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellService.java
URL: http://svn.apache.org/viewvc/servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellService.java?rev=668093&view=auto
==============================================================================
--- servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellService.java (added)
+++ servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellService.java Mon Jun 16 02:35:42 2008
@@ -0,0 +1,33 @@
+/*
+ * 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.servicemix.gshellweb.client;
+
+import com.google.gwt.user.client.rpc.RemoteService;
+
+public interface ShellService extends RemoteService {
+	
+	String login(String username, String password);
+	
+	void logout(String sessionId);
+	
+	void setNextLineIn(String sessionId, String in);
+	
+	String getNextLineOut(String sessionId);
+	
+	String getNextLineErr(String sessionId);
+	
+}
\ No newline at end of file

Added: servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellServiceAsync.java
URL: http://svn.apache.org/viewvc/servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellServiceAsync.java?rev=668093&view=auto
==============================================================================
--- servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellServiceAsync.java (added)
+++ servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/client/ShellServiceAsync.java Mon Jun 16 02:35:42 2008
@@ -0,0 +1,34 @@
+/*
+ * 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.servicemix.gshellweb.client;
+
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.google.gwt.user.client.rpc.RemoteService;
+
+public interface ShellServiceAsync {
+	
+	void login(String username, String password, AsyncCallback callback);
+	
+	void logout(String sessionId, AsyncCallback callback);
+	
+	void setNextLineIn(String sessionId, String in, AsyncCallback callback);
+	
+	void getNextLineOut(String sessionId, AsyncCallback callback);
+	
+	void getNextLineErr(String sessionId, AsyncCallback callback);
+	
+}
\ No newline at end of file

Added: servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/server/ShellServiceImpl.java
URL: http://svn.apache.org/viewvc/servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/server/ShellServiceImpl.java?rev=668093&view=auto
==============================================================================
--- servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/server/ShellServiceImpl.java (added)
+++ servicemix/sandbox/gshell-web/gshell-gwt/src/org/apache/servicemix/gshellweb/server/ShellServiceImpl.java Mon Jun 16 02:35:42 2008
@@ -0,0 +1,151 @@
+/*
+ * 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.servicemix.gshellweb.server;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.Reader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.PipedOutputStream;
+import java.io.IOException;
+import java.io.BufferedReader;
+
+import javax.servlet.ServletException;
+
+import com.google.gwt.user.server.rpc.RemoteServiceServlet;
+
+import org.apache.servicemix.gshellweb.client.ShellService;
+
+public class ShellServiceImpl extends RemoteServiceServlet implements ShellService {
+
+    private Map<String, Session> sessions = new ConcurrentHashMap<String, Session>();
+    private Timer timer = new Timer(true);
+    private long timeout = 5 * 60 * 1000;
+    private InputStream out;
+    private InputStream err;
+    private BufferedReader outReader;
+    private BufferedReader errReader;
+
+    private PipedOutputStream outPipe;
+    private PipedOutputStream errPipe;
+
+    public void init() throws ServletException {
+        super.init();
+        timer.scheduleAtFixedRate(new TimerTask() {
+            public void run() {
+                processTimeouts();
+            }
+        }, 1000, 1000);
+
+        try {
+            // TODO: bridge the out / err to gshell
+            outPipe = new PipedOutputStream();
+            errPipe = new PipedOutputStream();
+
+            out = new PipedInputStream(outPipe);
+            outReader = new BufferedReader(new InputStreamReader(out));
+            err = new PipedInputStream(errPipe);
+            errReader = new BufferedReader(new InputStreamReader(err));
+        } catch (IOException e) {
+            throw new ServletException(e);
+        }
+    }
+
+    public String login(String username, String password) {
+        Session session = new Session();
+        String sessionId = session.toString();
+        sessions.put(sessionId, session);
+        useSession(sessionId);
+        return sessionId;
+    }
+	
+	public void logout(String sessionId) {
+        sessions.remove(sessionId);
+	}
+	
+	public void setNextLineIn(String sessionId, String in) {
+        System.out.println("setNextLineIn [sessionId=" + sessionId + ", in=" + in + "]");
+        Session session = useSession(sessionId);
+        try {
+            outPipe.write(("Command: " + in + "\n").getBytes());
+        } catch (IOException e) {
+            handleException(sessionId, e);
+        }
+    }
+
+    public String getNextLineOut(String sessionId) {
+        System.out.println("getNextLineOut [sessionId=" + sessionId + "]");
+        Session session = useSession(sessionId);
+        try {
+            return outReader.readLine();
+        } catch (IOException e) {
+            handleException(sessionId, e);
+            return null;
+        }
+    }
+	
+	public String getNextLineErr(String sessionId) {
+        System.out.println("getNextLineErr [sessionId=" + sessionId + "]");
+        Session session = useSession(sessionId);
+        try {
+            return errReader.readLine();
+        } catch (IOException e) {
+            handleException(sessionId, e);
+            return null;
+        }
+	}
+
+    private void handleException(String sessionId, IOException e) {
+        System.err.println("Exception in session: " + sessionId + ": " + e);
+    }
+
+    private Session useSession(String sessionId) {
+        Session session = sessions.get(sessionId);
+        if (session == null) {
+            throw new RuntimeException("Session does not exist or has been closed");
+        }
+        session.updateLastUsed();
+        return session;
+    }
+
+    private void processTimeouts() {
+        long time = System.currentTimeMillis();
+        for (Map.Entry<String, Session> entry : sessions.entrySet()) {
+            if (entry.getValue().getLastUsed() < time - timeout) {
+                System.out.println("Session " + entry.getKey() + " has timed out");
+                sessions.remove(entry.getKey());
+            }
+        }
+    }
+
+    private static class Session {
+        private long lastUsed;
+        public long getLastUsed() {
+            return lastUsed;
+        }
+        public void updateLastUsed() {
+            lastUsed = System.currentTimeMillis();
+        }
+    }
+
+}
+