felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r790997 - in /felix/trunk/gogo/gogo.launcher: pom.xml src/main/java/org/apache/felix/gogo/launcher/Launcher.java
Date Fri, 03 Jul 2009 19:42:05 GMT
Author: rickhall
Date: Fri Jul  3 19:42:04 2009
New Revision: 790997

URL: http://svn.apache.org/viewvc?rev=790997&view=rev
Log:
Tried to update the launcher to use standard embedding and launching API.

Modified:
    felix/trunk/gogo/gogo.launcher/pom.xml
    felix/trunk/gogo/gogo.launcher/src/main/java/org/apache/felix/gogo/launcher/Launcher.java

Modified: felix/trunk/gogo/gogo.launcher/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/gogo.launcher/pom.xml?rev=790997&r1=790996&r2=790997&view=diff
==============================================================================
--- felix/trunk/gogo/gogo.launcher/pom.xml (original)
+++ felix/trunk/gogo/gogo.launcher/pom.xml Fri Jul  3 19:42:04 2009
@@ -31,7 +31,8 @@
     <dependencies>
         <dependency>
             <groupId>org.apache.felix</groupId>
-            <artifactId>org.osgi.core</artifactId>
+            <artifactId>org.apache.felix.framework</artifactId>
+            <version>1.8.1</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: felix/trunk/gogo/gogo.launcher/src/main/java/org/apache/felix/gogo/launcher/Launcher.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/gogo.launcher/src/main/java/org/apache/felix/gogo/launcher/Launcher.java?rev=790997&r1=790996&r2=790997&view=diff
==============================================================================
--- felix/trunk/gogo/gogo.launcher/src/main/java/org/apache/felix/gogo/launcher/Launcher.java
(original)
+++ felix/trunk/gogo/gogo.launcher/src/main/java/org/apache/felix/gogo/launcher/Launcher.java
Fri Jul  3 19:42:04 2009
@@ -22,7 +22,8 @@
 import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
 import org.apache.felix.gogo.console.stdio.Console;
 import org.osgi.framework.Bundle;
-import org.osgi.service.command.CommandProcessor;
+import org.osgi.framework.launch.FrameworkFactory;
+import org.osgi.framework.launch.Framework;
 import org.osgi.service.command.CommandSession;
 
 import java.io.*;
@@ -40,7 +41,7 @@
     public static void main(String args[]) throws Exception
     {
         StringBuffer sb = new StringBuffer();
-        String framework = null;
+        String fwkClassName = null;
         PrintStream out = System.out;
         InputStream in = System.in;
         boolean console = false;
@@ -50,7 +51,7 @@
             String arg = args[i];
             if (arg.equals("-f"))
             {
-                framework = args[++i];
+                fwkClassName = args[++i];
             }
             else
             {
@@ -87,28 +88,31 @@
             }
         }
 
-        if (framework == null)
+        URL[] urls = classpath.toArray(new URL[classpath.size()]);
+        URLClassLoader cl = new URLClassLoader(urls, Launcher.class.getClassLoader());
+
+        Properties p = new Properties(System.getProperties());
+
+        Framework framework;
+        if (fwkClassName == null)
+        {
+            framework = getFrameworkFactory(cl).newFramework(p);
+        }
+        else
         {
-            System.err.println("No framework set");
-            System.exit(1);
+            Class<?> fw = cl.loadClass(fwkClassName);
+            Constructor<?> c = fw.getConstructor(Map.class, List.class);
+            framework = (Framework) c.newInstance(p);
         }
 
         ThreadIOImpl threadio = new ThreadIOImpl();
         threadio.start();
-        URL[] urls = classpath.toArray(new URL[classpath.size()]);
-        URLClassLoader urlcl = new URLClassLoader(urls, Launcher.class.getClassLoader());
-        Class<?> fw = urlcl.loadClass(framework);
-
-        Constructor<?> c = fw.getConstructor(Map.class, List.class);
-        Properties p = new Properties(System.getProperties());
-        Bundle bundle = (Bundle) c.newInstance(p, null);
 
         OSGiShell shell = new OSGiShell();
         shell.setThreadio(threadio);
-        shell.setBundle(bundle);
+        shell.setBundle(framework);
         shell.start();
 
-
         CommandSession session = shell.createSession(in, out, System.err);
         session.put("shell", shell);
         session.put("threadio", threadio);
@@ -116,7 +120,7 @@
         session.execute(sb);
         out.flush();
 
-        if (bundle.getState() == Bundle.ACTIVE)
+        if (framework.getState() == Bundle.ACTIVE)
         {
         }
         if (console)
@@ -127,6 +131,42 @@
         }
     }
 
+    /**
+     * Simple method to search for META-INF/services for a FrameworkFactory
+     * provider. It simply looks for the first non-commented line and assumes
+     * it is the name of the provider class.
+     * @return a <tt>FrameworkFactory</tt> instance.
+     * @throws Exception if anything goes wrong.
+    **/
+    private static FrameworkFactory getFrameworkFactory(ClassLoader cl)
+        throws Exception
+    {
+        URL url = cl.getResource(
+            "META-INF/services/org.osgi.framework.launch.FrameworkFactory");
+        if (url != null)
+        {
+            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
+            try
+            {
+                for (String s = br.readLine(); s != null; s = br.readLine())
+                {
+                    s = s.trim();
+                    // Try to load first non-empty, non-commented line.
+                    if ((s.length() > 0) && (s.charAt(0) != '#'))
+                    {
+                        return (FrameworkFactory) cl.loadClass(s).newInstance();
+                    }
+                }
+            }
+            finally
+            {
+                if (br != null) br.close();
+            }
+        }
+
+        throw new Exception("Could not find framework factory.");
+    }
+
     private static void classpath(String string) throws MalformedURLException
     {
         StringTokenizer st = new StringTokenizer(string, File.pathSeparator);



Mime
View raw message