You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ak...@apache.org on 2006/03/04 01:02:07 UTC

svn commit: r382973 - in /incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework: ./ src/main/java/org/apache/felix/framework/

Author: akarasulu
Date: Fri Mar  3 16:02:06 2006
New Revision: 382973

URL: http://svn.apache.org/viewcvs?rev=382973&view=rev
Log:
changes ...

 o created felix specific installation layout bean which extends the standard
   layout bean for all daemonized applications/servers using the plugin
 o added DaemonService (heh did not know whether to call it Service or Daemon)
   which can be renamed later for the daemon application's lifecycle methods
   and to be used by the main as well as the bootstrappers
 o modified Main to now use this DaemonService instead of starting Felix itself


Added:
    incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/DaemonService.java   (with props)
    incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/FelixLayout.java   (with props)
Modified:
    incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/pom.xml
    incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Main.java

Modified: incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/pom.xml
URL: http://svn.apache.org/viewcvs/incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/pom.xml?rev=382973&r1=382972&r2=382973&view=diff
==============================================================================
--- incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/pom.xml (original)
+++ incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/pom.xml Fri Mar  3 16:02:06 2006
@@ -24,6 +24,11 @@
       <artifactId>kxml</artifactId>
       <version>2.0</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.directory.daemon</groupId>
+      <artifactId>daemon-bootstrappers</artifactId>
+      <version>1.0-RC1</version>
+    </dependency>
   </dependencies>
   <build>
     <plugins>

Added: incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/DaemonService.java
URL: http://svn.apache.org/viewcvs/incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/DaemonService.java?rev=382973&view=auto
==============================================================================
--- incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/DaemonService.java (added)
+++ incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/DaemonService.java Fri Mar  3 16:02:06 2006
@@ -0,0 +1,242 @@
+package org.apache.felix.framework;
+
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Properties;
+
+import org.apache.directory.daemon.DaemonApplication;
+import org.apache.directory.daemon.InstallationLayout;
+import org.apache.felix.framework.util.MutablePropertyResolverImpl;
+import org.apache.felix.framework.util.StringMap;
+
+
+/**
+ * NOTE: Does not set system properties which are done via a daemon ui, some
+ * init script, or a main() application entry point.
+ */
+public class DaemonService implements DaemonApplication
+{
+    /**
+     * The system property name used to specify an URL to the configuration
+     * property file to be used for the created the framework instance.
+     */
+    public static final String CONFIG_PROPERTIES_PROP = "felix.config.properties";
+    /**
+     * The default name used for the configuration properties file.
+     */
+    public static final String CONFIG_PROPERTIES_FILE_VALUE = "config.properties";
+
+    private Felix instance;
+    private Properties configationProperties;
+    private FelixLayout layout;
+
+
+    public void init( InstallationLayout suppliedLayout, String[] args ) throws Exception
+    {
+        if ( !( suppliedLayout instanceof FelixLayout ) )
+        {
+            this.layout = new FelixLayout( suppliedLayout );
+        }
+        else
+        {
+            this.layout = ( FelixLayout ) suppliedLayout;
+        }
+
+        instance = new Felix();
+        configationProperties = readConfigProperties();
+    }
+
+
+    public void start()
+    {
+        instance.start( new MutablePropertyResolverImpl( new StringMap( configationProperties, false ) ), null );
+    }
+
+
+    public void stop( String[] arg0 ) throws Exception
+    {
+        instance.shutdown();
+    }
+
+
+    public void destroy()
+    {
+    }
+    
+    
+    public Properties getConfigurationProperties()
+    {
+        return ( Properties ) configationProperties.clone();
+    }
+
+
+    /**
+     * <p>
+     * Reads the configuration properties in the configuration property
+     * file associated with the framework installation; these properties are
+     * only accessible to the framework and are intended for configuration
+     * purposes. By default, the configuration property file is located in
+     * the same directory as the <tt>felix.jar</tt> file and is called
+     * "<tt>config.properties</tt>". This may be changed by setting the
+     * "<tt>felix.config.properties</tt>" system property to an
+     * arbitrary URL.
+     * </p>
+     * @return A <tt>Properties</tt> instance or <tt>null</tt> if there was an error.
+     */
+    private Properties readConfigProperties()
+    {
+        // The config properties file is either present in a default 
+        // location using the layout, or is specified by a system property
+        // Try to load it from one of these places.
+
+        // See if the property URL was specified as a property.
+        URL propURL = null;
+        String custom = System.getProperty( CONFIG_PROPERTIES_PROP );
+        if ( custom != null )
+        {
+            try
+            {
+                propURL = new URL( custom );
+            }
+            catch ( MalformedURLException ex )
+            {
+                System.err.print( "Main: " + ex );
+                return null;
+            }
+        }
+        else
+        {
+            try
+            {
+                propURL = layout.getConfigurationFile().toURL();
+            }
+            catch ( MalformedURLException ex )
+            {
+                System.err.print( "Main: " + ex );
+                return null;
+            }
+        }
+
+        // Read the properties file.
+        Properties props = new Properties();
+        InputStream is = null;
+        try
+        {
+            is = propURL.openConnection().getInputStream();
+            props.load( is );
+            is.close();
+        }
+        catch ( FileNotFoundException ex )
+        {
+            // Ignore file not found.
+        }
+        catch ( Exception ex )
+        {
+            System.err.println( "Error loading config properties from " + propURL );
+            System.err.println( "Main: " + ex );
+            
+            try
+            {
+                if ( is != null ) 
+                {
+                    is.close();
+                }
+            }
+            catch ( IOException ex2 )
+            {
+                // Nothing we can do.
+            }
+            
+            return null;
+        }
+
+        // Perform variable substitution for system properties.
+        for ( Enumeration e = props.propertyNames(); e.hasMoreElements(); /* EMPTY */ )
+        {
+            String name = ( String ) e.nextElement();
+            props.setProperty( name, substVars( ( String ) props.getProperty( name ) ) );
+        }
+
+        return props;
+    }
+
+    
+    private static final String DELIM_START = "${";
+    private static final char DELIM_STOP = '}';
+    private static final int DELIM_START_LEN = 2;
+    private static final int DELIM_STOP_LEN = 1;
+
+
+    /**
+     * <p>
+     * This method performs system property variable substitution on the
+     * specified string value. If the specified string contains the syntax
+     * <tt>${&lt;system-prop-name&gt;}</tt>, then the corresponding system
+     * property value is substituted for the marker.
+     * </p>
+     * 
+     * @param val
+     *            The string on which to perform system property substitution.
+     * @return The value of the specified string after system property
+     *         substitution.
+     * @throws IllegalArgumentException
+     *             If there was a syntax error in the system property variable
+     *             marker syntax.
+     */
+    public static String substVars( String val ) throws IllegalArgumentException
+    {
+        StringBuffer sbuf = new StringBuffer();
+
+        if ( val == null )
+        {
+            return val;
+        }
+
+        int i = 0;
+        int j, k;
+
+        while ( true )
+        {
+            j = val.indexOf( DELIM_START, i );
+            if ( j == -1 )
+            {
+                if ( i == 0 )
+                {
+                    return val;
+                }
+                else
+                {
+                    sbuf.append( val.substring( i, val.length() ) );
+                    return sbuf.toString();
+                }
+            }
+            else
+            {
+                sbuf.append( val.substring( i, j ) );
+                k = val.indexOf( DELIM_STOP, j );
+                if ( k == -1 )
+                {
+                    throw new IllegalArgumentException( '"' + val
+                        + "\" has no closing brace. Opening brace at position " + j + '.' );
+                }
+                else
+                {
+                    j += DELIM_START_LEN;
+                    String key = val.substring( j, k );
+                    // Try system properties.
+                    String replacement = System.getProperty( key, null );
+                    if ( replacement != null )
+                    {
+                        sbuf.append( replacement );
+                    }
+                    i = k + DELIM_STOP_LEN;
+                }
+            }
+        }
+    }
+}

Propchange: incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/DaemonService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/FelixLayout.java
URL: http://svn.apache.org/viewcvs/incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/FelixLayout.java?rev=382973&view=auto
==============================================================================
--- incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/FelixLayout.java (added)
+++ incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/FelixLayout.java Fri Mar  3 16:02:06 2006
@@ -0,0 +1,65 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.framework;
+
+
+import java.io.File;
+import org.apache.directory.daemon.InstallationLayout;
+
+
+/**
+ * A felix specific installation layout.
+ *
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class FelixLayout extends InstallationLayout
+{
+    public FelixLayout( InstallationLayout layout )
+    {
+        super( layout.getBaseDirectory() );
+    }
+
+
+    public FelixLayout( File baseDirectory )
+    {
+        super( baseDirectory );
+    }
+
+
+    public FelixLayout( String baseDirectoryPath )
+    {
+        super( baseDirectoryPath );
+    }
+
+
+    public File getBundleDirectory()
+    {
+        return new File( super.baseDirectory, "bundle" );
+    }
+    
+    
+    public File getConfigurationFile()
+    {
+        return super.getConfigurationFile( "config.properties" );
+    }
+    
+
+    public File getSystemPropertiesFile()
+    {
+        return new File( super.baseDirectory, "system.properties" );
+    }
+}

Propchange: incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/FelixLayout.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Main.java
URL: http://svn.apache.org/viewcvs/incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Main.java?rev=382973&r1=382972&r2=382973&view=diff
==============================================================================
--- incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Main.java (original)
+++ incubator/felix/sandbox/akarasulu/mavenized/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Main.java Fri Mar  3 16:02:06 2006
@@ -22,9 +22,8 @@
 import java.util.Enumeration;
 import java.util.Properties;
 
+import org.apache.directory.daemon.InstallationLayout;
 import org.apache.felix.framework.cache.DefaultBundleCache;
-import org.apache.felix.framework.util.MutablePropertyResolverImpl;
-import org.apache.felix.framework.util.StringMap;
 
 /**
  * <p>
@@ -48,17 +47,6 @@
      * The default name used for the system properties file.
     **/
     public static final String SYSTEM_PROPERTIES_FILE_VALUE = "system.properties";
-    /**
-     * The system property name used to specify an URL to the configuration
-     * property file to be used for the created the framework instance.
-    **/
-    public static final String CONFIG_PROPERTIES_PROP = "felix.config.properties";
-    /**
-     * The default name used for the configuration properties file.
-    **/
-    public static final String CONFIG_PROPERTIES_FILE_VALUE = "config.properties";
-
-    private static Felix m_felix = null;
 
     /**
      * <p>
@@ -141,9 +129,24 @@
         // Load system properties.
         Main.loadSystemProperties();
 
+        // Initialize the Felix Daemon/Service
+        DaemonService service = new DaemonService();
+        if ( argv.length > 0 && new File( argv[0] ).isDirectory() )
+        {
+            service.init( new InstallationLayout( argv[0] ), null );
+        }
+        else if ( argv.length > 0 && new File( argv[0] ).isFile() )
+        {
+            service.init( null, argv );
+        }
+        else
+        {
+            service.init( null, null );
+        }
+        
         // Read configuration properties.
-        Properties configProps = Main.readConfigProperties();
-
+        Properties configProps = service.getConfigurationProperties();
+        
         // See if the profile name property was specified.
         String profileName = configProps.getProperty(DefaultBundleCache.CACHE_PROFILE_PROP);
 
@@ -185,13 +188,9 @@
 
         try
         {
-            // Now create an instance of the framework.
-            m_felix = new Felix();
-            m_felix.start(
-                new MutablePropertyResolverImpl(new StringMap(configProps, false)),
-                null);
+            service.start();
         }
-        catch (Exception ex)
+        catch ( Exception ex )
         {
             System.err.println("Could not create framework: " + ex);
             ex.printStackTrace();
@@ -199,6 +198,7 @@
         }
     }
 
+    
     /**
      * <p>
      * Loads the properties in the system property file associated with the
@@ -297,185 +297,7 @@
         for (Enumeration e = props.propertyNames(); e.hasMoreElements(); )
         {
             String name = (String) e.nextElement();
-            System.setProperty(name, substVars((String) props.getProperty(name)));
-        }
-    }
-
-    /**
-     * <p>
-     * Reads the configuration properties in the configuration property
-     * file associated with the framework installation; these properties are
-     * only accessible to the framework and are intended for configuration
-     * purposes. By default, the configuration property file is located in
-     * the same directory as the <tt>felix.jar</tt> file and is called
-     * "<tt>config.properties</tt>". This may be changed by setting the
-     * "<tt>felix.config.properties</tt>" system property to an
-     * arbitrary URL.
-     * </p>
-     * @return A <tt>Properties</tt> instance or <tt>null</tt> if there was an error.
-    **/
-    public static Properties readConfigProperties()
-    {
-        // The config properties file is either specified by a system
-        // property or it is in the same directory as the Felix JAR file.
-        // Try to load it from one of these places.
-
-        // See if the property URL was specified as a property.
-        URL propURL = null;
-        String custom = System.getProperty(CONFIG_PROPERTIES_PROP);
-        if (custom != null)
-        {
-            try
-            {
-                propURL = new URL(custom);
-            }
-            catch (MalformedURLException ex)
-            {
-                System.err.print("Main: " + ex);
-                return null;
-            }
-        }
-        else
-        {
-            // Determine where felix.jar is located by looking at the
-            // system class path.
-            String jarLoc = null;
-            String classpath = System.getProperty("java.class.path");
-            int index = classpath.toLowerCase().indexOf("felix.jar");
-            int start = classpath.lastIndexOf(File.pathSeparator, index) + 1;
-            if (index > start)
-            {
-                jarLoc = classpath.substring(start, index);
-                if (jarLoc.length() == 0)
-                {
-                    jarLoc = ".";
-                }
-            }
-            else
-            {
-                // Can't figure it out so use the current directory as default.
-                jarLoc = System.getProperty("user.dir");
-            }
-
-            try
-            {
-                propURL = new File(jarLoc, CONFIG_PROPERTIES_FILE_VALUE).toURL();
-            }
-            catch (MalformedURLException ex)
-            {
-                System.err.print("Main: " + ex);
-                return null;
-            }
-        }
-
-        // Read the properties file.
-        Properties props = new Properties();
-        InputStream is = null;
-        try
-        {
-            is = propURL.openConnection().getInputStream();
-            props.load(is);
-            is.close();
-        }
-        catch (FileNotFoundException ex)
-        {
-            // Ignore file not found.
-        }
-        catch (Exception ex)
-        {
-            System.err.println(
-                "Error loading config properties from " + propURL);
-            System.err.println("Main: " + ex);
-            try
-            {
-                if (is != null) is.close();
-            }
-            catch (IOException ex2)
-            {
-                // Nothing we can do.
-            }
-            return null;
-        }
-
-        // Perform variable substitution for system properties.
-        for (Enumeration e = props.propertyNames(); e.hasMoreElements(); )
-        {
-            String name = (String) e.nextElement();
-            props.setProperty(name, substVars((String) props.getProperty(name)));
-        }
-
-        return props;
-    }
-
-    private static final String DELIM_START = "${";
-    private static final char DELIM_STOP  = '}';
-    private static final int DELIM_START_LEN = 2;
-    private static final int DELIM_STOP_LEN  = 1;
-
-    /**
-     * <p>
-     * This method performs system property variable substitution on the
-     * specified string value. If the specified string contains the
-     * syntax <tt>${&lt;system-prop-name&gt;}</tt>, then the corresponding
-     * system property value is substituted for the marker.
-     * </p>
-     * @param val The string on which to perform system property substitution.
-     * @return The value of the specified string after system property substitution.
-     * @throws IllegalArgumentException If there was a syntax error in the
-     *         system property variable marker syntax.
-    **/
-    public static String substVars(String val)
-        throws IllegalArgumentException
-    {
-        StringBuffer sbuf = new StringBuffer();
-
-        if (val == null)
-        {
-            return val;
-        }
-
-        int i = 0;
-        int j, k;
-
-        while (true)
-        {
-            j = val.indexOf(DELIM_START, i);
-            if (j == -1)
-            {
-                if (i == 0)
-                {
-                    return val;
-                }
-                else
-                {
-                    sbuf.append(val.substring(i, val.length()));
-                    return sbuf.toString();
-                }
-            }
-            else
-            {
-                sbuf.append(val.substring(i, j));
-                k = val.indexOf(DELIM_STOP, j);
-                if (k == -1)
-                {
-                    throw new IllegalArgumentException(
-                    '"' + val +
-                    "\" has no closing brace. Opening brace at position "
-                    + j + '.');
-                }
-                else
-                {
-                    j += DELIM_START_LEN;
-                    String key = val.substring(j, k);
-                    // Try system properties.
-                    String replacement = System.getProperty(key, null);
-                    if (replacement != null)
-                    {
-                        sbuf.append(replacement);
-                    }
-                    i = k + DELIM_STOP_LEN;
-                }
-            }
+            System.setProperty( name, DaemonService.substVars( ( String ) props.getProperty( name ) ) );
         }
     }
 }