felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From db...@apache.org
Subject svn commit: r943947 [2/2] - in /felix/trunk/gogo: ./ commands/ commands/src/test/java/org/apache/felix/gogo/commands/ console/ console/src/main/java/org/apache/felix/gogo/console/ console/src/main/java/org/apache/felix/gogo/options/ console/src/main/ja...
Date Thu, 13 May 2010 17:32:35 GMT
Added: felix/trunk/gogo/console/src/main/java/org/apache/felix/gogo/shell/Telnet.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/console/src/main/java/org/apache/felix/gogo/shell/Telnet.java?rev=943947&view=auto
==============================================================================
--- felix/trunk/gogo/console/src/main/java/org/apache/felix/gogo/shell/Telnet.java (added)
+++ felix/trunk/gogo/console/src/main/java/org/apache/felix/gogo/shell/Telnet.java Thu May
13 17:32:34 2010
@@ -0,0 +1,185 @@
+/*
+ * 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.felix.gogo.shell;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.List;
+
+import org.apache.felix.gogo.options.Option;
+import org.apache.felix.gogo.options.Options;
+import org.osgi.service.command.CommandProcessor;
+import org.osgi.service.command.CommandSession;
+
+/*
+ * a very simple Telnet server.
+ * real remote access should be via ssh.
+ */
+public class Telnet implements Runnable
+{
+    private static final int defaultPort = 2019;
+    private final CommandProcessor processor;
+    private ServerSocket server;
+    private Thread thread;
+    private boolean quit;
+    private int port;
+
+    public Telnet(CommandProcessor procesor)
+    {
+        this.processor = procesor;
+    }
+
+    public void telnetd(String[] argv) throws IOException
+    {
+        final String[] usage = { "telnetd - start simple telnet server",
+                "Usage: telnetd [-p port] start | stop | status",
+                "  -p --port=PORT           listen port (default=" + defaultPort + ")",
+                "  -? --help                show help" };
+
+        Option opt = Options.compile(usage).parse(argv);
+        List<String> args = opt.args();
+
+        if (opt.isSet("help") || args.isEmpty())
+        {
+            opt.usage();
+            return;
+        }
+
+        String command = args.get(0);
+
+        if ("start".equals(command))
+        {
+            if (server != null)
+            {
+                throw new IllegalStateException("telnetd is already running on port "
+                    + port);
+            }
+            port = opt.getNumber("port");
+            start();
+            status();
+        }
+        else if ("stop".equals(command))
+        {
+            if (server == null)
+            {
+                throw new IllegalStateException("telnetd is not running.");
+            }
+            stop();
+        }
+        else if ("status".equals(command))
+        {
+            status();
+        }
+        else
+        {
+            throw opt.usageError("bad command: " + command);
+        }
+    }
+
+    private void status()
+    {
+        if (server != null)
+        {
+            System.out.println("telnetd is running on port " + port);
+        }
+        else
+        {
+            System.out.println("telnetd is not running.");
+        }
+    }
+
+    private void start() throws IOException
+    {
+        quit = false;
+        server = new ServerSocket(port);
+        thread = new Thread(this, "gogo telnet");
+        thread.start();
+    }
+
+    private void stop() throws IOException
+    {
+        quit = true;
+        server.close();
+        server = null;
+        thread.interrupt();
+    }
+
+    public void run()
+    {
+        try
+        {
+            while (!quit)
+            {
+                final Socket socket = server.accept();
+                PrintStream out = new PrintStream(socket.getOutputStream());
+                final CommandSession session = processor.createSession(
+                    socket.getInputStream(), out, out);
+
+                Thread handler = new Thread()
+                {
+                    public void run()
+                    {
+                        try
+                        {
+                            session.execute("gosh --login --noshutdown");
+                        }
+                        catch (Exception e)
+                        {
+                            e.printStackTrace();
+                        }
+                        finally
+                        {
+                            session.close();
+                            try
+                            {
+                                socket.close();
+                            }
+                            catch (IOException e)
+                            {
+                            }
+                        }
+                    }
+                };
+                handler.start();
+            }
+        }
+        catch (IOException e)
+        {
+            if (!quit)
+            {
+                e.printStackTrace();
+            }
+        }
+        finally
+        {
+            try
+            {
+                if (server != null)
+                {
+                    server.close();
+                }
+            }
+            catch (IOException e)
+            {
+            }
+        }
+    }
+}

Added: felix/trunk/gogo/console/src/main/resources/gosh_profile
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/console/src/main/resources/gosh_profile?rev=943947&view=auto
==============================================================================
--- felix/trunk/gogo/console/src/main/resources/gosh_profile (added)
+++ felix/trunk/gogo/console/src/main/resources/gosh_profile Thu May 13 17:32:34 2010
@@ -0,0 +1,36 @@
+# default gosh_profile
+# only read if etc/gosh_profile doesn't exist relative to the System property
+# gosh.home or failing that the current directory.
+
+# ensure gogo commands are found first
+SCOPE = gogo:*
+
+# add methods on BundleContext object as commands
+#addcommand context ${.context} (${.context} class)
+# bug: above invokes (String, Object, String) instead of (String, Object, Class)
+addcommand context ${.context}
+
+# add methods on System object as commands
+# FELIX-2335 prevents the use of (bundle 0) loadclass
+addcommand system ((bundle 1) loadclass java.lang.System)
+
+# alias to print full stack trace
+e = { $exception printStackTrace }
+
+## disable console auto-formatting of each result
+#  you will then need to explicitly use the 'format' command
+#  to print the result of commands that don't write to stdout.
+#.Gogo.format = false
+
+## disable printing the formatted result of a command into pipelines
+#.Format.Pipe = false
+
+# set prompt
+prompt = 'g! '
+
+# print welcome message
+try {
+  cat ($0 resolve motd)
+}
+
+# end

Added: felix/trunk/gogo/console/src/main/resources/motd
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/console/src/main/resources/motd?rev=943947&view=auto
==============================================================================
--- felix/trunk/gogo/console/src/main/resources/motd (added)
+++ felix/trunk/gogo/console/src/main/resources/motd Thu May 13 17:32:34 2010
@@ -0,0 +1,4 @@
+_______________
+Welcome to Gogo
+Use 'type' to explore registered commands, 'type -?' for help.
+

Modified: felix/trunk/gogo/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/pom.xml?rev=943947&r1=943946&r2=943947&view=diff
==============================================================================
--- felix/trunk/gogo/pom.xml (original)
+++ felix/trunk/gogo/pom.xml Thu May 13 17:32:34 2010
@@ -26,15 +26,14 @@
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <packaging>pom</packaging>
-    <name>Apache Felix Gogo Shell</name>
-    <description>Apache Felix Gogo Shell</description>
+    <name>Apache Felix Gogo</name>
+    <description>Apache Felix Gogo Bundles</description>
     <groupId>org.apache.felix.gogo</groupId>
     <artifactId>gogo</artifactId>
     <version>0.5.0-SNAPSHOT</version>
 
     <modules>
         <module>runtime</module>
-        <module>launcher</module>
         <module>console</module>
         <module>commands</module>
         <module>felixcommands</module>

Modified: felix/trunk/gogo/runtime/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/pom.xml?rev=943947&r1=943946&r2=943947&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/pom.xml (original)
+++ felix/trunk/gogo/runtime/pom.xml Thu May 13 17:32:34 2010
@@ -24,7 +24,7 @@
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <packaging>bundle</packaging>
-    <name>Apache Felix Gogo Shell Runtime</name>
+    <name>Apache Felix Gogo Runtime</name>
     <artifactId>org.apache.felix.gogo.runtime</artifactId>
     <version>0.5.0-SNAPSHOT</version>
     <dependencies>

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java?rev=943947&r1=943946&r2=943947&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Activator.java Thu
May 13 17:32:34 2010
@@ -18,11 +18,14 @@
  */
 package org.apache.felix.gogo.runtime;
 
-import org.apache.felix.gogo.runtime.osgi.OSGiCommands;
-import org.apache.felix.gogo.runtime.osgi.OSGiConverters;
-import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
-import org.apache.felix.gogo.runtime.shell.CommandProxy;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.apache.felix.gogo.runtime.shell.CommandProcessorImpl;
+import org.apache.felix.gogo.runtime.shell.CommandProxy;
+import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Filter;
@@ -35,11 +38,6 @@ import org.osgi.service.command.Function
 import org.osgi.service.threadio.ThreadIO;
 import org.osgi.util.tracker.ServiceTracker;
 
-import java.util.HashMap;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
 public class Activator implements BundleActivator
 {
     private CommandProcessorImpl processor;
@@ -50,13 +48,10 @@ public class Activator implements Bundle
     private ServiceRegistration processorRegistration;
     private ServiceRegistration threadioRegistration;
     private Map<ServiceReference, ServiceRegistration> felixRegistrations;
-    private OSGiCommands commands;
-    private OSGiConverters converters;
-    private ServiceRegistration convertersRegistration;
     
-    protected CommandProcessorImpl newProcessor(ThreadIO tio)
+    protected CommandProcessorImpl newProcessor(ThreadIO tio, BundleContext context)
     {
-        return new CommandProcessorImpl(threadio);
+        return new CommandProcessorImpl(tio, context);
     }
 
     public void start(final BundleContext context) throws Exception
@@ -66,7 +61,7 @@ public class Activator implements Bundle
         threadioRegistration = context.registerService(ThreadIO.class.getName(),
             threadio, null);
 
-        processor = newProcessor(threadio);
+        processor = newProcessor(threadio, context);
         processorRegistration = context.registerService(CommandProcessor.class.getName(),
             processor, null);
         
@@ -95,17 +90,10 @@ public class Activator implements Bundle
             }
         };
         converterTracker.open();
-
-        // FIXME: optional?
-        commands = new OSGiCommands(context);
-        commands.registerCommands(processor, context.getBundle());
-        converters = new OSGiConverters(context);
-        convertersRegistration = context.registerService(Converter.class.getCanonicalName(),
converters, null);
     }
 
     public void stop(BundleContext context) throws Exception
     {
-        convertersRegistration.unregister();
         processorRegistration.unregister();
         threadioRegistration.unregister();
         

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProcessorImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProcessorImpl.java?rev=943947&r1=943946&r2=943947&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProcessorImpl.java
(original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandProcessorImpl.java
Thu May 13 17:32:34 2010
@@ -29,6 +29,7 @@ import java.util.Set;
 import java.util.TreeSet;
 import java.util.Map.Entry;
 
+import org.osgi.framework.BundleContext;
 import org.osgi.service.command.CommandProcessor;
 import org.osgi.service.command.CommandSession;
 import org.osgi.service.command.Converter;
@@ -39,13 +40,15 @@ public class CommandProcessorImpl implem
 {
     protected final Set<Converter> converters = new HashSet<Converter>();
     protected final Map<String, Object> commands = new LinkedHashMap<String, Object>();
+    protected final BundleContext context;
     protected final ThreadIO threadIO;
 
-    public CommandProcessorImpl(ThreadIO tio)
+    public CommandProcessorImpl(ThreadIO tio, BundleContext context)
     {
         threadIO = tio;
-        addCommand("shell", this, "addCommand");
-        addCommand("shell", this, "removeCommand");
+        this.context = context;
+        addCommand("osgi", this, "addCommand");
+        addCommand("osgi", this, "removeCommand");
     }
 
     public CommandSession createSession(InputStream in, PrintStream out, PrintStream err)
@@ -63,12 +66,17 @@ public class CommandProcessorImpl implem
         converters.remove(c);
     }
     
-    public Set<String> getCommands()
+    Set<String> getCommands()
     {
         return commands.keySet();
     }
+    
+    BundleContext getContext()
+    {
+        return context;
+    }
 
-    public Function getCommand(String name, final Object path)
+    Function getCommand(String name, final Object path)
     {
         int colon = name.indexOf(':');
 

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java?rev=943947&r1=943946&r2=943947&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java
(original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/shell/CommandSessionImpl.java
Thu May 13 17:32:34 2010
@@ -35,6 +35,7 @@ public class CommandSessionImpl implemen
 {
     public static final String VARIABLES = ".variables";
     public static final String COMMANDS = ".commands";
+    public static final String CONTEXT = ".context";
     private static final String COLUMN = "%-20s %s\n";
     
     protected InputStream in;
@@ -90,12 +91,17 @@ public class CommandSessionImpl implemen
         {
             return variables.keySet();
         }
-        
+
         if (COMMANDS.equals(name))
         {
             return processor.getCommands();
         }
 
+        if (CONTEXT.equals(name))
+        {
+            return processor.getContext();
+        }
+
         if (variables.containsKey(name))
         {
             return variables.get(name);

Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/Context.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/Context.java?rev=943947&r1=943946&r2=943947&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/Context.java
(original)
+++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/shell/Context.java
Thu May 13 17:32:34 2010
@@ -36,7 +36,7 @@ public class Context extends CommandProc
 
     public Context()
     {
-        super(threadio);
+        super(threadio, null);
         session = (CommandSessionImpl) createSession(System.in, System.out, System.err);
     }
 



Mime
View raw message