[jboss-osgi-commits] JBoss-OSGI SVN: r95995 - in projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi: internal and 1 other directory.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Wed Nov 4 08:14:14 EST 2009


Author: thomas.diesler at jboss.com
Date: 2009-11-04 08:14:13 -0500 (Wed, 04 Nov 2009)
New Revision: 95995

Added:
   projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/internal/OSGiBootstrapBean.java
Modified:
   projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/OSGiBootstrap.java
Log:
Hide bootstrap implemenation detail

Modified: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/OSGiBootstrap.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/OSGiBootstrap.java	2009-11-04 10:54:33 UTC (rev 95994)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/framework/OSGiBootstrap.java	2009-11-04 13:14:13 UTC (rev 95995)
@@ -23,31 +23,12 @@
 
 //$Id$
 
-import static org.jboss.osgi.spi.OSGiConstants.OSGI_HOME;
-import static org.jboss.osgi.spi.OSGiConstants.OSGI_SERVER_HOME;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Properties;
-
-import org.jboss.osgi.spi.util.ServiceLoader;
+import org.jboss.osgi.spi.internal.OSGiBootstrapBean;
 import org.kohsuke.args4j.CmdLineException;
 import org.kohsuke.args4j.CmdLineParser;
-import org.kohsuke.args4j.Option;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.launch.Framework;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
- * The OSGiBootstrap provides an {@link OSGiFramework} through a {@link OSGiBootstrapProvider}.
+ * The OSGiBootstrap is the entry point to obtain an {@link OSGiBootstrapProvider}.
  * 
  * A OSGiBootstrapProvider is discovered in two stages.
  * 
@@ -63,27 +44,12 @@
  */
 public class OSGiBootstrap
 {
-   private static Logger log;
-
-   private static final String JAVA_PROTOCOL_HANDLERS = "java.protocol.handler.pkgs";
-   private static final String JBOSS_BIND_ADDRESS = "jboss.bind.address";
-   private static final String OSGI_SERVER_NAME = "osgi.server.name";
-
-   @Option(name = "-c", aliases = { "--server-name" }, usage = "The runtime profile to start. (-c minimal)", required = false)
-   public String serverName = "default";
-
-   @Option(name = "-b", aliases = { "--bind-address" }, usage = "The network address various services can bind to (-b 127.0.0.1)", required = false)
-   public String bindAddress = "localhost";
-
-   private String osgiHome;
-   private String osgiServerHome;
-
-   /*
+   /**
     * The main entry point
     */
    public static void main(String[] args)
    {
-      OSGiBootstrap bean = new OSGiBootstrap();
+      OSGiBootstrapBean bean = new OSGiBootstrapBean();
       CmdLineParser parser = new CmdLineParser(bean);
       try
       {
@@ -97,172 +63,11 @@
       }
    }
 
-   public void run()
-   {
-      initBootstrap();
-
-      OSGiBootstrapProvider bootProvider = getBootstrapProvider();
-      Framework framework = bootProvider.getFramework();
-
-      Runtime runtime = Runtime.getRuntime();
-      runtime.addShutdownHook(new ShutdownThread(framework));
-
-      Thread thread = new StartupThread(framework);
-      thread.start();
-   }
-
-   private void initBootstrap()
-   {
-      osgiHome = System.getProperty(OSGI_HOME);
-      if (osgiHome == null)
-         throw new IllegalStateException("Cannot obtain system property: '" + OSGI_HOME + "'");
-
-      osgiServerHome = osgiHome + "/server/" + serverName;
-
-      // Replace the context class loader with one that contains the server conf dir 
-      File serverConfDir = new File(osgiServerHome + "/conf");
-      if (serverConfDir.exists())
-      {
-         ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
-         URLClassLoader confLoader = new URLClassLoader(new URL[] { toURL(serverConfDir) }, ctxLoader);
-         Thread.currentThread().setContextClassLoader(confLoader);
-      }
-
-      // This property must be set before the logger is obtained
-      System.setProperty(OSGI_SERVER_HOME, osgiServerHome);
-      log = LoggerFactory.getLogger(OSGiBootstrap.class);
-
-      Properties defaults = new Properties();
-      defaults.setProperty(OSGI_SERVER_NAME, serverName);
-      defaults.setProperty(OSGI_SERVER_HOME, osgiServerHome);
-      defaults.setProperty(JBOSS_BIND_ADDRESS, bindAddress);
-      defaults.setProperty(JAVA_PROTOCOL_HANDLERS, "org.jboss.net.protocol|org.jboss.virtual.protocol");
-
-      log.debug("JBoss OSGi System Properties");
-      log.debug("   " + OSGI_SERVER_HOME + "=" + osgiServerHome);
-
-      Enumeration<?> defaultNames = defaults.propertyNames();
-      while (defaultNames.hasMoreElements())
-      {
-         String propName = (String)defaultNames.nextElement();
-         String sysValue = System.getProperty(propName);
-         if (sysValue == null)
-         {
-            String propValue = defaults.getProperty(propName);
-            System.setProperty(propName, propValue);
-            log.debug("   " + propName + "=" + propValue);
-         }
-      }
-   }
-
-   /*
+   /**
     * Get an instance of an OSGiBootstrapProvider.
     */
    public static OSGiBootstrapProvider getBootstrapProvider()
    {
-      if (log == null)
-         log = LoggerFactory.getLogger(OSGiBootstrap.class);
-      
-      OSGiBootstrapProvider provider = null;
-
-      List<OSGiBootstrapProvider> providers = ServiceLoader.loadServices(OSGiBootstrapProvider.class);
-      for (OSGiBootstrapProvider aux : providers)
-      {
-         try
-         {
-            aux.configure();
-            provider = aux;
-            break;
-         }
-         catch (Exception ex)
-         {
-            log.debug("Cannot configure [" + aux.getClass().getName() + "]", ex);
-         }
-      }
-
-      if (provider == null)
-      {
-         provider = new PropertiesBootstrapProvider();
-         log.debug("Using default: " + PropertiesBootstrapProvider.class.getName());
-      }
-
-      return provider;
+      return OSGiBootstrapBean.getBootstrapProvider();
    }
-
-   private URL toURL(File file)
-   {
-      try
-      {
-         return file.toURL();
-      }
-      catch (MalformedURLException e)
-      {
-         throw new IllegalArgumentException("Invalid file: " + file);
-      }
-   }
-
-   class StartupThread extends Thread
-   {
-      private Framework framework;
-
-      public StartupThread(Framework framework)
-      {
-         this.framework = framework;
-      }
-
-      public void run()
-      {
-         // Start the framework
-         long beforeStart = System.currentTimeMillis();
-         try
-         {
-            framework.start();
-         }
-         catch (BundleException ex)
-         {
-            throw new IllegalStateException("Cannot start framework", ex);
-         }
-         
-         float diff = (System.currentTimeMillis() - beforeStart) / 1000f;
-         log.info("JBossOSGi Runtime booted in " + diff + "sec");
-         
-         Reader br = new InputStreamReader(System.in);
-         try
-         {
-            int inByte = br.read();
-            while (inByte != 0)
-            {
-               inByte = br.read();
-            }
-         }
-         catch (IOException ioe)
-         {
-            // ignore user input
-         }
-      }
-   }
-
-   class ShutdownThread extends Thread
-   {
-      private Framework framework;
-
-      public ShutdownThread(Framework framework)
-      {
-         this.framework = framework;
-      }
-
-      public void run()
-      {
-         log.info("Initiating shutdown ...");
-         try
-         {
-            framework.stop();
-         }
-         catch (BundleException ex)
-         {
-            log.error("Cannot stop framework", ex);
-         }
-         log.info("Shutdown complete");
-      }
-   }
 }
\ No newline at end of file

Added: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/internal/OSGiBootstrapBean.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/internal/OSGiBootstrapBean.java	                        (rev 0)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/internal/OSGiBootstrapBean.java	2009-11-04 13:14:13 UTC (rev 95995)
@@ -0,0 +1,238 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.osgi.spi.internal;
+
+//$Id$
+
+import static org.jboss.osgi.spi.OSGiConstants.OSGI_HOME;
+import static org.jboss.osgi.spi.OSGiConstants.OSGI_SERVER_HOME;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Properties;
+
+import org.jboss.osgi.spi.framework.OSGiBootstrap;
+import org.jboss.osgi.spi.framework.OSGiBootstrapProvider;
+import org.jboss.osgi.spi.framework.PropertiesBootstrapProvider;
+import org.jboss.osgi.spi.util.ServiceLoader;
+import org.kohsuke.args4j.Option;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.launch.Framework;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An internal bean that collabrates with {@link OSGiBootstrap}.
+ * 
+ * @author thomas.diesler at jboss.com
+ * @since 04-Nov-2008
+ */
+public class OSGiBootstrapBean
+{
+   private static Logger log;
+
+   private static final String JAVA_PROTOCOL_HANDLERS = "java.protocol.handler.pkgs";
+   private static final String JBOSS_BIND_ADDRESS = "jboss.bind.address";
+   private static final String OSGI_SERVER_NAME = "osgi.server.name";
+
+   @Option(name = "-c", aliases = { "--server-name" }, usage = "The runtime profile to start. (-c minimal)", required = false)
+   public String serverName = "default";
+
+   @Option(name = "-b", aliases = { "--bind-address" }, usage = "The network address various services can bind to (-b 127.0.0.1)", required = false)
+   public String bindAddress = "localhost";
+
+   private String osgiHome;
+   private String osgiServerHome;
+
+   public void run()
+   {
+      initBootstrap();
+
+      OSGiBootstrapProvider bootProvider = getBootstrapProvider();
+      Framework framework = bootProvider.getFramework();
+
+      Runtime runtime = Runtime.getRuntime();
+      runtime.addShutdownHook(new ShutdownThread(framework));
+
+      Thread thread = new StartupThread(framework);
+      thread.start();
+   }
+
+   private void initBootstrap()
+   {
+      osgiHome = System.getProperty(OSGI_HOME);
+      if (osgiHome == null)
+         throw new IllegalStateException("Cannot obtain system property: '" + OSGI_HOME + "'");
+
+      osgiServerHome = osgiHome + "/server/" + serverName;
+
+      // Replace the context class loader with one that contains the server conf dir 
+      File serverConfDir = new File(osgiServerHome + "/conf");
+      if (serverConfDir.exists())
+      {
+         ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
+         URLClassLoader confLoader = new URLClassLoader(new URL[] { toURL(serverConfDir) }, ctxLoader);
+         Thread.currentThread().setContextClassLoader(confLoader);
+      }
+
+      // This property must be set before the logger is obtained
+      System.setProperty(OSGI_SERVER_HOME, osgiServerHome);
+      log = LoggerFactory.getLogger(OSGiBootstrapBean.class);
+
+      Properties defaults = new Properties();
+      defaults.setProperty(OSGI_SERVER_NAME, serverName);
+      defaults.setProperty(OSGI_SERVER_HOME, osgiServerHome);
+      defaults.setProperty(JBOSS_BIND_ADDRESS, bindAddress);
+      defaults.setProperty(JAVA_PROTOCOL_HANDLERS, "org.jboss.net.protocol|org.jboss.virtual.protocol");
+
+      log.debug("JBoss OSGi System Properties");
+      log.debug("   " + OSGI_SERVER_HOME + "=" + osgiServerHome);
+
+      Enumeration<?> defaultNames = defaults.propertyNames();
+      while (defaultNames.hasMoreElements())
+      {
+         String propName = (String)defaultNames.nextElement();
+         String sysValue = System.getProperty(propName);
+         if (sysValue == null)
+         {
+            String propValue = defaults.getProperty(propName);
+            System.setProperty(propName, propValue);
+            log.debug("   " + propName + "=" + propValue);
+         }
+      }
+   }
+
+   public static OSGiBootstrapProvider getBootstrapProvider()
+   {
+      if (log == null)
+         log = LoggerFactory.getLogger(OSGiBootstrap.class);
+      
+      OSGiBootstrapProvider provider = null;
+
+      List<OSGiBootstrapProvider> providers = ServiceLoader.loadServices(OSGiBootstrapProvider.class);
+      for (OSGiBootstrapProvider aux : providers)
+      {
+         try
+         {
+            aux.configure();
+            provider = aux;
+            break;
+         }
+         catch (Exception ex)
+         {
+            log.debug("Cannot configure [" + aux.getClass().getName() + "]", ex);
+         }
+      }
+
+      if (provider == null)
+      {
+         provider = new PropertiesBootstrapProvider();
+         log.debug("Using default: " + PropertiesBootstrapProvider.class.getName());
+      }
+
+      return provider;
+   }
+   
+   private URL toURL(File file)
+   {
+      try
+      {
+         return file.toURL();
+      }
+      catch (MalformedURLException e)
+      {
+         throw new IllegalArgumentException("Invalid file: " + file);
+      }
+   }
+
+   class StartupThread extends Thread
+   {
+      private Framework framework;
+
+      public StartupThread(Framework framework)
+      {
+         this.framework = framework;
+      }
+
+      public void run()
+      {
+         // Start the framework
+         long beforeStart = System.currentTimeMillis();
+         try
+         {
+            framework.start();
+         }
+         catch (BundleException ex)
+         {
+            throw new IllegalStateException("Cannot start framework", ex);
+         }
+         
+         float diff = (System.currentTimeMillis() - beforeStart) / 1000f;
+         log.info("JBossOSGi Runtime booted in " + diff + "sec");
+         
+         Reader br = new InputStreamReader(System.in);
+         try
+         {
+            int inByte = br.read();
+            while (inByte != 0)
+            {
+               inByte = br.read();
+            }
+         }
+         catch (IOException ioe)
+         {
+            // ignore user input
+         }
+      }
+   }
+
+   class ShutdownThread extends Thread
+   {
+      private Framework framework;
+
+      public ShutdownThread(Framework framework)
+      {
+         this.framework = framework;
+      }
+
+      public void run()
+      {
+         log.info("Initiating shutdown ...");
+         try
+         {
+            framework.stop();
+         }
+         catch (BundleException ex)
+         {
+            log.error("Cannot stop framework", ex);
+         }
+         log.info("Shutdown complete");
+      }
+   }
+}
\ No newline at end of file


Property changes on: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/internal/OSGiBootstrapBean.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF



More information about the jboss-osgi-commits mailing list