directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: r209428 [2/2] - in /directory/sandbox/trunk/osgi-spec/trunk/configuration/src: ./ main/ main/java/ main/java/org/ main/java/org/apache/ main/java/org/apache/configuration/ main/java/org/apache/configuration/store/
Date Wed, 06 Jul 2005 08:02:10 GMT
Added: directory/sandbox/trunk/osgi-spec/trunk/configuration/src/main/java/org/apache/configuration/UpdateQueue.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-spec/trunk/configuration/src/main/java/org/apache/configuration/UpdateQueue.java?rev=209428&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-spec/trunk/configuration/src/main/java/org/apache/configuration/UpdateQueue.java
(added)
+++ directory/sandbox/trunk/osgi-spec/trunk/configuration/src/main/java/org/apache/configuration/UpdateQueue.java
Wed Jul  6 01:02:09 2005
@@ -0,0 +1,210 @@
+/*
+ *   Copyright 2005 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.configuration;
+
+import java.util.Vector;
+
+import org.osgi.service.cm.ConfigurationException;
+
+/**
+ * This class is responsible for dispatching configurations to ManagedService(Factories).
+ * It is also responsible for calling <code>ConfigurationPlugins</code>.
+ */
+final class UpdateQueue implements Runnable
+{
+    /**
+     * The PluginManager to use.
+     */
+    private PluginManager pm;
+
+    /**
+     * The thread running this object.
+     */
+    private Thread thread;
+
+    /**
+     * The thread running this object.
+     */
+    private final Object threadLock = new Object();
+
+    /**
+     * The queue of updates.
+     */
+    private Vector queue = new Vector();
+
+    /**
+     * Construct an UpdateQueue given a BundleContext.
+     * 
+     * @param tracker The BundleContext to use.
+     */
+    UpdateQueue( PluginManager pm )
+    {
+        this.pm = pm;
+    }
+
+    /**
+     * Overide of Thread.run().
+     */
+    public void run()
+    {
+        while ( true )
+        {
+            if ( doUpdateQueueLogging() )
+            {
+                Activator.LOG.info( "[UpdateQueue] Getting next Update from queue" );
+            }
+            
+            Update update = dequeue();
+            
+            if ( update == null )
+            {
+                if ( doUpdateQueueLogging() )
+                {
+                    Activator.LOG.info( "[UpdateQueue] Got null Update from queue" );
+                }
+                return;
+            }
+            
+            if ( doUpdateQueueLogging() )
+            {
+                Activator.LOG.info( "[UpdateQueue] Got an Update from queue" );
+            }
+            try
+            {
+                if (doUpdateQueueLogging())
+                {
+                    Activator.LOG.info( "[UpdateQueue] Calling Update.doUpdate" );
+                }
+                
+                update.doUpdate(pm);
+                
+                if (doUpdateQueueLogging())
+                {
+                    Activator.LOG.info( "[UpdateQueue] Update.doUpdate returned" );
+                }
+            }
+            catch (ConfigurationException ce)
+            {
+                Activator.LOG.error("[CM] Error in configuration for " + update.pid, ce);
+            }
+            catch (Exception e)
+            {
+                Activator.LOG.error("[CM] Error while updating " + update.pid, e);
+            }
+        }
+    }
+
+    /**
+     * Add an entry to the end of the queue.
+     * 
+     * @param update The Update to add to the queue.
+     * @throws java.lang.Exception If given a null argument.
+     */
+    public synchronized void enqueue( Update update )
+    {
+        if ( update == null )
+        {
+            throw new IllegalArgumentException(
+                    "ConfigurationDispatcher.enqueue(Update) needs a non-null argument."
);
+        }
+        
+        if ( doUpdateQueueLogging() )
+        {
+            Activator.LOG.info( "[UpdateQueue] Adding update for " + update.pid + " to queue"
);
+        }
+        
+        queue.addElement( update );
+        attachNewThreadIfNeccesary();
+        notifyAll();
+    }
+
+    /**
+     * Get and remove the next entry from the queue.  If the queue is empty
+     * this method waits until an entry is available.
+     * 
+     * @return The Hashtable entry removed from the queue.
+     */
+    private synchronized Update dequeue()
+    {
+        if ( queue.isEmpty() )
+        {
+            try
+            {
+                if ( doUpdateQueueLogging() )
+                {
+                    Activator.LOG.info( "[UpdateQueue] Queue is empty. Waiting 5000 ms" );
+                }
+                wait( 5000 );
+            }
+            catch (InterruptedException ignored)
+            {
+            }
+        }
+        if ( queue.isEmpty() )
+        {
+            if ( doUpdateQueueLogging() )
+            {
+                Activator.LOG.info( "[UpdateQueue] Queue is still empty. Detaching thread."
);
+            }
+            
+            detachCurrentThread();
+            return null;
+        }
+        
+        Update u = (Update) queue.elementAt(0);
+        queue.removeElementAt(0);
+        return u;
+    }
+
+    void attachNewThreadIfNeccesary()
+    {
+        synchronized ( threadLock )
+        {
+            if ( thread == null )
+            {
+                if ( doUpdateQueueLogging() )
+                {
+                    Activator.LOG.info( "[UpdateQueue] Attaching new thread." );
+                }
+                
+                thread = new Thread( this );
+                thread.setDaemon( true );
+                thread.start();
+            }
+        }
+    }
+
+    void detachCurrentThread()
+    {
+        synchronized ( threadLock )
+        {
+            if ( doUpdateQueueLogging() )
+            {
+                Activator.LOG.info( "[UpdateQueue] Detaching thread because queue is empty."
);
+            }
+            
+            thread = null;
+        }
+    }
+
+    boolean doUpdateQueueLogging()
+    {
+        return true;
+    }
+}
+

Added: directory/sandbox/trunk/osgi-spec/trunk/configuration/src/main/java/org/apache/configuration/store/SimpleConfigurationStore.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-spec/trunk/configuration/src/main/java/org/apache/configuration/store/SimpleConfigurationStore.java?rev=209428&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-spec/trunk/configuration/src/main/java/org/apache/configuration/store/SimpleConfigurationStore.java
(added)
+++ directory/sandbox/trunk/osgi-spec/trunk/configuration/src/main/java/org/apache/configuration/store/SimpleConfigurationStore.java
Wed Jul  6 01:02:09 2005
@@ -0,0 +1,110 @@
+/*
+ *   Copyright 2005 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.configuration.store;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Properties;
+import java.util.Vector;
+
+import org.apache.configuration.ConfigurationDictionary;
+import org.apache.configuration.ConfigurationStore;
+
+
+public class SimpleConfigurationStore implements ConfigurationStore
+{
+    /**
+     * Default values for JNDI lookups, database table names, etc.
+     */
+    private static Properties configProps;
+    
+    public SimpleConfigurationStore( File storeDir )
+    {
+        System.out.println( "Got store dir " + storeDir );
+        
+        try
+        {
+            File configFile = new File( storeDir, "config.properties" );
+            InputStream is = new FileInputStream( configFile );
+            
+            configProps = new Properties();
+            configProps.load( is );
+        }
+        catch (IOException ioe)
+        {
+            ioe.printStackTrace();
+            System.out.println( "Could not load config.properties." );
+        }
+    }
+    
+    public ConfigurationDictionary delete( String pid ) throws IOException
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+    
+    public void deleteAll( String factoryPid ) throws IOException
+    {
+        // TODO Auto-generated method stub
+    }
+    
+    public String generatePid( String factoryPid ) throws IOException
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+    
+    public Enumeration listPids()
+    {
+        Vector pids = new Vector();
+        pids.add( "org.apache.ntp" );
+        
+        return pids.elements();
+    }
+    
+    public ConfigurationDictionary load( String pid ) throws IOException
+    {
+        System.out.println( "Got config request from " + pid );
+        System.out.println( "Returning dictionary of size " + configProps.size() );
+        
+        return new ConfigurationDictionary( configProps );
+    }
+    
+    public ConfigurationDictionary[] loadAll( String factoryPid )
+            throws IOException
+    {
+        ConfigurationDictionary[] dictionaries = new ConfigurationDictionary[ 1 ];
+        dictionaries[ 0 ] = new ConfigurationDictionary( configProps );
+        
+        System.out.println( "Got config request from " + factoryPid );
+        System.out.println( "Returning dictionary array of size " + dictionaries.length );
+        
+        return dictionaries;
+    }
+    
+    public void store( String pid, String factoryPid, Dictionary configuration )
+            throws IOException
+    {
+        // TODO Auto-generated method stub
+    }
+}
+



Mime
View raw message