incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r891673 - in /sling/trunk/bundles/commons/threads: ./ src/main/java/org/apache/sling/commons/threads/ src/main/java/org/apache/sling/commons/threads/impl/ src/main/resources/OSGI-INF/ src/main/resources/OSGI-INF/metatype/
Date Thu, 17 Dec 2009 12:38:36 GMT
Author: cziegeler
Date: Thu Dec 17 12:38:10 2009
New Revision: 891673

URL: http://svn.apache.org/viewvc?rev=891673&view=rev
Log:
SLING-1244 : Redesign thread pool management

Added:
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ModifiableThreadPoolConfig.java   (with props)
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/Activator.java   (with props)
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java   (with props)
    sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/
    sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/
    sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.properties   (with props)
    sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.xml   (with props)
Modified:
    sling/trunk/bundles/commons/threads/pom.xml
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolConfig.java
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolManager.java
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java

Modified: sling/trunk/bundles/commons/threads/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/pom.xml?rev=891673&r1=891672&r2=891673&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/pom.xml (original)
+++ sling/trunk/bundles/commons/threads/pom.xml Thu Dec 17 12:38:10 2009
@@ -29,7 +29,7 @@
 
     <artifactId>org.apache.sling.commons.threads</artifactId>
     <packaging>bundle</packaging>
-    <version>2.0.5-SNAPSHOT</version>
+    <version>3.0.0-SNAPSHOT</version>
 
     <name>Apache Sling Thread Support</name>
     <description>
@@ -54,8 +54,11 @@
                 <extensions>true</extensions>
                 <configuration>
                     <instructions>
+                        <Bundle-Activator>
+                            org.apache.sling.commons.threads.impl.Activator
+                        </Bundle-Activator>
                         <Export-Package>
-                            org.apache.sling.commons.threads;version=${pom.version}
+                            org.apache.sling.commons.threads;version=3.0.0
                         </Export-Package>
                         <Private-Package>
                             org.apache.sling.commons.threads.impl
@@ -81,6 +84,10 @@
     <dependencies>
         <dependency>
             <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
         </dependency>
         <dependency>

Added: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ModifiableThreadPoolConfig.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ModifiableThreadPoolConfig.java?rev=891673&view=auto
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ModifiableThreadPoolConfig.java (added)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ModifiableThreadPoolConfig.java Thu Dec 17 12:38:10 2009
@@ -0,0 +1,298 @@
+/*
+ * 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.sling.commons.threads;
+
+import java.util.concurrent.ThreadFactory;
+
+/**
+ * This is a modifiable thread pool configuration that can be instantiated
+ * and then configured to create a new thread pool.
+ *
+ * The default values for this configuration are:
+ * - min pool size: 5
+ * - max pool size: 5
+ * - queue size: -1
+ * - keep alive time: 60000
+ * - block policy: RUN
+ * - shutdown graceful: false
+ * - shutdown wait time: -1
+ * - priority: NORM
+ * - daemon: false
+ * - factory: null (= default jvm thread factory)
+ */
+public final class ModifiableThreadPoolConfig implements ThreadPoolConfig {
+
+    /** Configuration property for the min pool size. */
+    public static final String PROPERTY_MIN_POOL_SIZE = "minPoolSize";
+    /** Configuration property for the max pool size. */
+    public static final String PROPERTY_MAX_POOL_SIZE = "maxPoolSize";
+    /** Configuration property for the queue size. */
+    public static final String PROPERTY_QUEUE_SIZE = "queueSize";
+    /** Configuration property for the keep alive time. */
+    public static final String PROPERTY_KEEP_ALIVE_TIME = "keepAliveTime";
+    /** Configuration property for the block policy. */
+    public static final String PROPERTY_BLOCK_POLICY = "blockPolicy";
+    /** Configuration property for the shutdown graceful flag. */
+    public static final String PROPERTY_SHUTDOWN_GRACEFUL = "shutdownGraceful";
+    /** Configuration property for the shutdown wait time. */
+    public static final String PROPERTY_SHUTDOWN_WAIT_TIME = "shutdownWaitTime";
+    /** Configuration property for the priority. */
+    public static final String PROPERTY_PRIORITY = "priority";
+    /** Configuration property for the daemon flag. */
+    public static final String PROPERTY_DAEMON = "daemon";
+    /** Configuration property for the thread pool name. */
+    public static final String PROPERTY_NAME = "name";
+
+    /** The min pool size. */
+    private int minPoolSize = 5;
+
+    /** The max pool size. */
+    private int maxPoolSize = 5;
+
+    /** The queue size */
+    private int queueSize = -1;
+
+    /** The keep alive time. */
+    private long  keepAliveTime = 60000L;
+
+    /** The thread pool policy. Default is RUN. */
+    private ThreadPoolPolicy blockPolicy = ThreadPoolPolicy.RUN;
+
+    /** Try to shutdown gracefully? */
+    private  boolean shutdownGraceful = false;
+
+    /** Wait time during shutdown. */
+    private  int shutdownWaitTimeMs = -1;
+
+    /** Optional thread factory. */
+    private  ThreadFactory factory;
+
+    /** Thread priority. */
+    private  ThreadPriority   priority = ThreadPriority.NORM;
+
+    /** Create daemon threads? */
+    private  boolean isDaemon = false;
+
+    /**
+     * Create a new default configuration.
+     */
+    public ModifiableThreadPoolConfig() {
+        // nothing to do
+    }
+
+    /**
+     * Clone an existing configuration
+     * @param copy The config to clone
+     */
+    public ModifiableThreadPoolConfig(final ThreadPoolConfig copy) {
+        if ( copy != null ) {
+            this.minPoolSize = copy.getMinPoolSize();
+            this.maxPoolSize = copy.getMaxPoolSize();
+            this.queueSize = copy.getQueueSize();
+            this.keepAliveTime = copy.getKeepAliveTime();
+            this.blockPolicy = copy.getBlockPolicy();
+            this.shutdownGraceful = copy.isShutdownGraceful();
+            this.shutdownWaitTimeMs = copy.getShutdownWaitTimeMs();
+            this.factory = copy.getFactory();
+            this.priority = copy.getPriority();
+            this.isDaemon = copy.isDaemon();
+        }
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#getMinPoolSize()
+     */
+    public int getMinPoolSize() {
+        return minPoolSize;
+    }
+
+    /**
+     * Set the min pool size.
+     * @param minPoolSize New min pool size.
+     */
+    public void setMinPoolSize(final int minPoolSize) {
+        this.minPoolSize = minPoolSize;
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#getMaxPoolSize()
+     */
+    public int getMaxPoolSize() {
+        return maxPoolSize;
+    }
+
+    /**
+     * Set the max pool size.
+     * @param maxPoolSize New max pool size.
+     */
+    public void setMaxPoolSize(final int maxPoolSize) {
+        this.maxPoolSize = maxPoolSize;
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#getQueueSize()
+     */
+    public int getQueueSize() {
+        return queueSize;
+    }
+
+    /**
+     * Set the queue size.
+     * @param queueSize New queue size.
+     */
+    public void setQueueSize(final int queueSize) {
+        this.queueSize = queueSize;
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#getKeepAliveTime()
+     */
+    public long getKeepAliveTime() {
+        return keepAliveTime;
+    }
+
+    /**
+     * Set the keep alive time.
+     * @param keepAliveTime New keep alive time.
+     */
+    public void setKeepAliveTime(final long keepAliveTime) {
+        this.keepAliveTime = keepAliveTime;
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#getBlockPolicy()
+     */
+    public ThreadPoolPolicy getBlockPolicy() {
+        return blockPolicy;
+    }
+
+    /**
+     * Set the block policy.
+     * @param blockPolicy The new block policy.
+     * @throws IllegalArgumentException If blockPolicy is null.
+     */
+    public void setBlockPolicy(final ThreadPoolPolicy blockPolicy) {
+        this.blockPolicy = blockPolicy;
+        if ( blockPolicy == null ) {
+            throw new IllegalArgumentException("Policy must not be null.");
+        }
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#isShutdownGraceful()
+     */
+    public boolean isShutdownGraceful() {
+        return shutdownGraceful;
+    }
+
+    /**
+     * Set if the pool should be shutdown graceful.
+     * @param shutdownGraceful The shutdown graceful setting.
+     */
+    public void setShutdownGraceful(final boolean shutdownGraceful) {
+        this.shutdownGraceful = shutdownGraceful;
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#getShutdownWaitTimeMs()
+     */
+    public int getShutdownWaitTimeMs() {
+        return shutdownWaitTimeMs;
+    }
+
+    /**
+     * Set the shutdown wait time.
+     * @param shutdownWaitTimeMs The new shutdown wait time.
+     */
+    public void setShutdownWaitTimeMs(final int shutdownWaitTimeMs) {
+        this.shutdownWaitTimeMs = shutdownWaitTimeMs;
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#getFactory()
+     */
+    public ThreadFactory getFactory() {
+        return factory;
+    }
+
+    /**
+     * Set the thread factory.
+     * @param factory The thread factory to be used or <code>null</code> to use
+     *                the default thread factory.
+     */
+    public void setFactory(final ThreadFactory factory) {
+        this.factory = factory;
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#getPriority()
+     */
+    public ThreadPriority getPriority() {
+        return priority;
+    }
+
+    /**
+     * Set the thread priority.
+     * @param priority The thread priority.
+     * @throws IllegalArgumentException If priority is null.
+     */
+    public void setPriority(final ThreadPriority priority) {
+        if ( priority == null ) {
+            throw new IllegalArgumentException("Priority must not be null.");
+        }
+        this.priority = priority;
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolConfig#isDaemon()
+     */
+    public boolean isDaemon() {
+        return isDaemon;
+    }
+
+    /**
+     * Set the daemon handling.
+     * @param isDaemon The daemon setting.
+     */
+    public void setDaemon(final boolean isDaemon) {
+        this.isDaemon = isDaemon;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if ( obj instanceof ModifiableThreadPoolConfig ) {
+            final ModifiableThreadPoolConfig o = (ModifiableThreadPoolConfig)obj;
+            return this.minPoolSize == o.minPoolSize
+                && this.maxPoolSize == o.maxPoolSize
+                && this.queueSize == o.queueSize
+                && this.keepAliveTime == o.keepAliveTime
+                && this.blockPolicy.equals(o.blockPolicy)
+                && this.shutdownGraceful == o.shutdownGraceful
+                && this.shutdownWaitTimeMs == o.shutdownWaitTimeMs
+                && this.priority.equals(o.priority)
+                && this.isDaemon == o.isDaemon;
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        // we actually don't need hash code, but we don't want to violate the
+        // contract with equals
+        return this.blockPolicy.hashCode();
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ModifiableThreadPoolConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ModifiableThreadPoolConfig.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ModifiableThreadPoolConfig.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java?rev=891673&r1=891672&r2=891673&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPool.java Thu Dec 17 12:38:10 2009
@@ -17,10 +17,8 @@
 package org.apache.sling.commons.threads;
 
 /**
- * The ThreadPool interface allows to start runnables by
+ * The thread pool interface allows to start runnables by
  * getting threads from a managed pool.
- *
- * @version $Id$
  */
 public interface ThreadPool {
 
@@ -36,9 +34,7 @@
     String getName();
 
     /**
-     * Shut down the thread pool.
+     * The thread pool configuration.
      */
-    void shutdown();
-
     ThreadPoolConfig getConfiguration();
 }

Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolConfig.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolConfig.java?rev=891673&r1=891672&r2=891673&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolConfig.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolConfig.java Thu Dec 17 12:38:10 2009
@@ -19,11 +19,9 @@
 import java.util.concurrent.ThreadFactory;
 
 /**
- * The ThreadPool configuration.
- *
- * @version $Id$
+ * The thread pool configuration.
  */
-public final class ThreadPoolConfig {
+public interface ThreadPoolConfig {
 
     /** The thread pool policies. */
     public enum ThreadPoolPolicy {
@@ -39,164 +37,65 @@
         MAX
     };
 
-    /** The min pool size. */
-    private int minPoolSize = 5;
+    /**
+     * Return the minimum pool size.
+     * @return The minimum pool size.
+     */
+    int getMinPoolSize();
+
+    /**
+     * Return the maximum pool size
+     * @return The maximum pool size.
+     */
+    int getMaxPoolSize();
+
+    /**
+     * Return the queue size.
+     * @return The queue size.
+     */
+    int getQueueSize();
+
+    /**
+     * Return the keep alive time.
+     * @return The keep alive time.
+     */
+    long getKeepAliveTime();
+
+    /**
+     * Return the block policy.
+     * @return The block policy.
+     */
+    ThreadPoolPolicy getBlockPolicy();
 
-    /** The max pool size. */
-    private int maxPoolSize = 5;
+    /**
+     * Should this pool shutdown graceful.
+     * @return <code>true</code> if the pool should shutdown graceful.
+     */
+    boolean isShutdownGraceful();
 
-    /** The queue size */
-    private int queueSize = -1;
+    /**
+     * Return the shutdown wait time in ms. A value below 1 means
+     * no waiting at shutdown.
+     * @return The shutdown wait time in ms.
+     */
+    int getShutdownWaitTimeMs();
 
-    /** The keep alive time. */
-    private long  keepAliveTime = 60000L;
-
-    /** The thread pool policy. Default is RUN. */
-    private ThreadPoolPolicy blockPolicy = ThreadPoolPolicy.RUN;
-
-    private  boolean shutdownGraceful = false;
-
-    private  int shutdownWaitTimeMs = -1;
-
-    private  ThreadFactory factory;
-
-    private  ThreadPriority   priority = ThreadPriority.NORM;
-
-    private  boolean isDaemon = false;
-
-    /** Can this configuration still be changed? */
-    private boolean isWritable = true;
-
-    /**
-     * Create a new default configuration.
-     */
-    public ThreadPoolConfig() {
-        // nothing to do
-    }
-
-    /**
-     * Clone an existing configuration
-     * @param copy The config to clone
-     */
-    public ThreadPoolConfig(ThreadPoolConfig copy) {
-        this.minPoolSize = copy.minPoolSize;
-        this.maxPoolSize = copy.maxPoolSize;
-        this.queueSize = copy.queueSize;
-        this.keepAliveTime = copy.keepAliveTime;
-        this.blockPolicy = copy.blockPolicy;
-        this.shutdownGraceful = copy.shutdownGraceful;
-        this.shutdownWaitTimeMs = copy.shutdownWaitTimeMs;
-        this.factory = copy.factory;
-        this.priority = copy.priority;
-        this.isDaemon = copy.isDaemon;
-    }
-
-    protected void checkWritable() {
-        if ( !isWritable ) {
-            throw new IllegalStateException("ThreadPoolConfig is read-only.");
-        }
-    }
-
-    /**
-     * Make the configuration read-only.
-     */
-    public void makeReadOnly() {
-        this.isWritable = false;
-    }
-
-    public int getMinPoolSize() {
-        return minPoolSize;
-    }
-
-    public void setMinPoolSize(int minPoolSize) {
-        this.checkWritable();
-        this.minPoolSize = minPoolSize;
-    }
-
-    public int getMaxPoolSize() {
-        return maxPoolSize;
-    }
-
-    public void setMaxPoolSize(int maxPoolSize) {
-        this.checkWritable();
-        this.maxPoolSize = maxPoolSize;
-    }
-
-    public int getQueueSize() {
-        return queueSize;
-    }
-
-    public void setQueueSize(int queueSize) {
-        this.checkWritable();
-        this.queueSize = queueSize;
-    }
-
-    public long getKeepAliveTime() {
-        return keepAliveTime;
-    }
-
-    public void setKeepAliveTime(long keepAliveTime) {
-        this.checkWritable();
-        this.keepAliveTime = keepAliveTime;
-    }
-
-    public ThreadPoolPolicy getBlockPolicy() {
-        return blockPolicy;
-    }
-
-    public void setBlockPolicy(ThreadPoolPolicy blockPolicy) {
-        this.checkWritable();
-        this.blockPolicy = blockPolicy;
-        if ( blockPolicy == null ) {
-            throw new IllegalArgumentException("Policy must not be null.");
-        }
-    }
-
-    public boolean isShutdownGraceful() {
-        return shutdownGraceful;
-    }
-
-    public void setShutdownGraceful(boolean shutdownGraceful) {
-        this.checkWritable();
-        this.shutdownGraceful = shutdownGraceful;
-    }
-
-    public int getShutdownWaitTimeMs() {
-        return shutdownWaitTimeMs;
-    }
-
-    public void setShutdownWaitTimeMs(int shutdownWaitTimeMs) {
-        this.checkWritable();
-        this.shutdownWaitTimeMs = shutdownWaitTimeMs;
-    }
-
-    public ThreadFactory getFactory() {
-        return factory;
-    }
-
-    public void setFactory(ThreadFactory factory) {
-        this.checkWritable();
-        this.factory = factory;
-    }
-
-    public ThreadPriority getPriority() {
-        return priority;
-    }
-
-    public void setPriority(ThreadPriority priority) {
-        this.checkWritable();
-        if ( priority == null ) {
-            throw new IllegalArgumentException("Priority must not be null.");
-        }
-        this.priority = priority;
-    }
-
-    public boolean isDaemon() {
-        return isDaemon;
-    }
-
-    public void setDaemon(boolean isDaemon) {
-        this.checkWritable();
-        this.isDaemon = isDaemon;
-    }
+    /**
+     * Return the thread pool factory. A value of null means the
+     * default jvm thread pool factory is used.
+     * @return The thread pool factory or <code>null</code>
+     */
+    ThreadFactory getFactory();
+
+    /**
+     * Return the priority for the threads.
+     * @return The priority for the threads.
+     */
+    ThreadPriority getPriority();
+
+    /**
+     * Return if daemon threads should be created.
+     * @return <code>true</code> if daemon threads should be created.
+     */
+    boolean isDaemon();
 }
\ No newline at end of file

Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolManager.java?rev=891673&r1=891672&r2=891673&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolManager.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/ThreadPoolManager.java Thu Dec 17 12:38:10 2009
@@ -20,7 +20,6 @@
 /**
  * The <cod>ThreadPoolManager</code> manages thread pools.
  *
- * @version $Id$
  */
 public interface ThreadPoolManager {
 
@@ -28,29 +27,27 @@
     String DEFAULT_THREADPOOL_NAME = "default";
 
     /**
-     * Add a new pool.
-     * If a pool with the same name already exists, the new pool is not added
-     * and false is returned.
-     * @param pool The pool.
-     * @return True if the pool could be added, false otherwise.
-     */
-    boolean add(ThreadPool pool);
-
-    /**
      * Get a thread pool.
-     * If there is no thread pool with the given name, the default thread
-     * pool is returned.
+     * If there is no thread pool with the given name, a new thread pool with
+     * the default configuration is created and returned.
+     * A thread pool must be released when not used anymore with the
+     * {@link #release(ThreadPool)} method.
      * @param name The name of the thread pool or null for the default pool.
+     * @return A thread pool.
      */
     ThreadPool get(String name);
 
     /**
-     * Create a new thread pool.
-     * If a pool with the same name already exists, no new pool is created
-     * and <code>null</code> is returned.
-     * @param name Name must not be null.
+     * Create a new thread pool with this configuration.
+     * A thread pool must be released when not used anymore with the
+     * {@link #release(ThreadPool)} method.
      * @param config The thread pool configuration.
+     * @return A new thread pool.
+     */
+    ThreadPool create(ThreadPoolConfig config);
+
+    /**
+     * Release the thread pool again.
      */
-    ThreadPool create(String name,
-                      ThreadPoolConfig config);
+    void release(ThreadPool pool);
 }

Added: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/Activator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/Activator.java?rev=891673&view=auto
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/Activator.java (added)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/Activator.java Thu Dec 17 12:38:10 2009
@@ -0,0 +1,74 @@
+/*
+ * 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.sling.commons.threads.impl;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.sling.commons.threads.ThreadPoolManager;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ManagedServiceFactory;
+
+/**
+ * This activator registers the thread pool manager.
+ */
+public class Activator implements BundleActivator {
+
+    /** The service registration for the thread pool manager. */
+    private ServiceRegistration serviceReg;
+
+    /** The thread pool manager. */
+    private DefaultThreadPoolManager service;
+
+    /** The bundle context. */
+    private BundleContext bundleContext;
+
+    /**
+     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+     */
+    public void start(BundleContext context) {
+        this.bundleContext = context;
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Thread Pool Manager");
+        props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        props.put(Constants.SERVICE_PID, DefaultThreadPool.class.getName() + ".factory");
+        this.service = new DefaultThreadPoolManager(this.bundleContext, props);
+        this.serviceReg = this.bundleContext.registerService(new String[] {ThreadPoolManager.class.getName(),
+                ManagedServiceFactory.class.getName()}, service, props);
+    }
+
+    /**
+     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+     */
+    public void stop(BundleContext context) {
+        if ( this.serviceReg != null ) {
+            this.serviceReg.unregister();
+            this.serviceReg = null;
+        }
+
+        if ( this.service != null ) {
+            this.service.destroy();
+            this.service = null;
+        }
+        this.bundleContext = null;
+    }
+}

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/Activator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/Activator.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/Activator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java?rev=891673&r1=891672&r2=891673&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPool.java Thu Dec 17 12:38:10 2009
@@ -25,6 +25,7 @@
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.sling.commons.threads.ModifiableThreadPoolConfig;
 import org.apache.sling.commons.threads.ThreadPool;
 import org.apache.sling.commons.threads.ThreadPoolConfig;
 import org.apache.sling.commons.threads.ThreadPoolManager;
@@ -35,8 +36,6 @@
 /**
  * The DefaultThreadPool class implements the {@link ThreadPool} interface.
  * Instances of this class are managed by the {@link ThreadPoolManager}.
- *
- * @version $Id$
  */
 public class DefaultThreadPool
     implements ThreadPool {
@@ -50,7 +49,7 @@
     /** The executor. */
     protected ThreadPoolExecutor executor;
 
-    protected final ThreadPoolConfig configuration;
+    protected final ModifiableThreadPoolConfig configuration;
 
     /**
      * Create a new thread pool.
@@ -58,9 +57,7 @@
      *               is used
      */
     public DefaultThreadPool(final String name,
-                             ThreadPoolConfig origConfig) {
-        this.logger.info("ThreadPool [{}] initializing ...", name);
-
+                             final ThreadPoolConfig origConfig) {
         // name
         if ( name != null ) {
             this.name = name;
@@ -68,13 +65,15 @@
             this.name = DefaultThreadPoolManager.DEFAULT_THREADPOOL_NAME;
         }
 
-        this.configuration = new ThreadPoolConfig(origConfig);
+        this.logger.info("Initializing thread pool [{}]  ...", this.name);
+
+        this.configuration = new ModifiableThreadPoolConfig(origConfig);
 
         // factory
         final ThreadFactory delegateThreadFactory;
         if (this.configuration.getFactory() == null) {
-            logger.warn("No ThreadFactory is configured. Will use JVM default thread factory."
-                + ExtendedThreadFactory.class.getName());
+            logger.debug("Thread pool [{}] ; No ThreadFactory is configured. Will use JVM default thread factory: {}",
+                    this.name, ExtendedThreadFactory.class.getName());
             delegateThreadFactory = Executors.defaultThreadFactory();
         } else {
             delegateThreadFactory = this.configuration.getFactory();
@@ -82,7 +81,7 @@
         // Min pool size
         if (this.configuration.getMinPoolSize() < 1) {
             this.configuration.setMinPoolSize(1);
-            this.logger.warn("min-pool-size < 1 for pool \"" + name + "\". Set to 1");
+            this.logger.warn("min-pool-size < 1 for pool \"" + this.name + "\". Set to 1");
         }
         // Max pool size
         if ( this.configuration.getMaxPoolSize() < 0 ) {
@@ -95,7 +94,7 @@
         // Keep alive time
         if (this.configuration.getKeepAliveTime() < 0) {
             this.configuration.setKeepAliveTime(1000);
-            this.logger.warn("keep-alive-time-ms < 0 for pool \"" + name + "\". Set to 1000");
+            this.logger.warn("keep-alive-time-ms < 0 for pool \"" + this.name + "\". Set to 1000");
         }
 
         // Queue
@@ -132,8 +131,7 @@
                 queue,
                 threadFactory,
                 handler);
-        this.configuration.makeReadOnly();
-        this.logger.info("ThreadPool [{}] initialized.", name);
+        this.logger.info("Thread pool [{}] initialized.", name);
     }
 
     /**
@@ -173,9 +171,10 @@
     }
 
     /**
-     * @see org.apache.sling.commons.threads.ThreadPool#shutdown()
+     * Shut down the threadpool.
      */
     public void shutdown() {
+        this.logger.info("Shutting down thread pool [{}] ...", name);
         if ( this.executor != null ) {
             if (this.configuration.isShutdownGraceful()) {
                 this.executor.shutdown();
@@ -186,16 +185,17 @@
             try {
                 if (this.configuration.getShutdownWaitTimeMs() > 0) {
                     if (!this.executor.awaitTermination(this.configuration.getShutdownWaitTimeMs(), TimeUnit.MILLISECONDS)) {
-                        logger.warn("running commands have not terminated within "
+                        logger.warn("Running commands have not terminated within "
                             + this.configuration.getShutdownWaitTimeMs()
                             + "ms. Will shut them down by interruption");
                         this.executor.shutdownNow();
                     }
                 }
             } catch (final InterruptedException ie) {
-                this.logger.error("Cannot shutdown ThreadPool", ie);
+                this.logger.error("Cannot shutdown thread pool [" + this.name + "]", ie);
             }
             this.executor = null;
         }
+        this.logger.info("Thread pool [{}] is shut down.", this.name);
     }
 }

Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java?rev=891673&r1=891672&r2=891673&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java Thu Dec 17 12:38:10 2009
@@ -16,118 +16,345 @@
  */
 package org.apache.sling.commons.threads.impl;
 
+import java.io.IOException;
+import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.Map;
+import java.util.UUID;
 
+import org.apache.sling.commons.threads.ModifiableThreadPoolConfig;
 import org.apache.sling.commons.threads.ThreadPool;
 import org.apache.sling.commons.threads.ThreadPoolConfig;
 import org.apache.sling.commons.threads.ThreadPoolManager;
-import org.osgi.service.component.ComponentContext;
+import org.apache.sling.commons.threads.ThreadPoolConfig.ThreadPoolPolicy;
+import org.apache.sling.commons.threads.ThreadPoolConfig.ThreadPriority;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * The DefaultThreadPoolManager implements the {@link ThreadPoolManager} interface
- * and is responsible to manage {@link ThreadPool}s.
- *
- * @scr.component metatype="no"
- * @scr.service interface="org.apache.sling.commons.threads.ThreadPoolManager"
- *
- * @version $Id$
+ * and is responsible for managing {@link ThreadPool}s.
  */
-public class DefaultThreadPoolManager implements ThreadPoolManager {
+public class DefaultThreadPoolManager
+    implements ThreadPoolManager, ManagedServiceFactory {
 
     /** By default we use the logger for this class. */
-    protected Logger logger = LoggerFactory.getLogger(getClass());
+    protected final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
 
     /** The managed thread pools */
-    protected final Map<String, ThreadPool> pools = new HashMap<String, ThreadPool>();
+    protected final Map<String, Entry> pools = new HashMap<String, Entry>();
+
+    /** The properties. */
+    protected final Dictionary<String, Object> properties;
+
+    /** The bundle context. */
+    protected final BundleContext bundleContext;
+
+    /** Service tracker for the config admin. */
+    protected final ServiceTracker configAdminTracker;
 
     /**
-     * Activate this component.
+     * Constructor and activate this component.
      */
-    protected void activate(ComponentContext context) throws Exception {
-        this.logger.info("Starting thread pool manager.");
-        final ThreadPool defaultPool = new DefaultThreadPool(
-                    DEFAULT_THREADPOOL_NAME,
-                    new ThreadPoolConfig());
-        synchronized ( this.pools ) {
-            this.pools.put(defaultPool.getName(), defaultPool);
-        }
-        this.logger.info("Thread pool manager startet with default pool.");
+    public DefaultThreadPoolManager(final BundleContext bc, final Dictionary<String, Object> props) {
+        this.properties = props;
+        this.bundleContext = bc;
+        this.configAdminTracker = new ServiceTracker(bc, ConfigurationAdmin.class.getName(), null);
+        this.configAdminTracker.open();
+        this.logger.info("Startet Apache Sling Thread Pool Manager: {}", getPid());
     }
 
     /**
      * Deactivate this component.
      */
-    protected void deactivate(ComponentContext context) throws Exception {
-        this.logger.info("Stopping thread pool manager.");
+    public void destroy() {
         this.logger.debug("Disposing all thread pools");
 
-        synchronized ( this.pools ) {
-            for (ThreadPool pool : this.pools.values()) {
-                this.logger.debug("Shutting down thread pool {}", pool.getName());
-
-                pool.shutdown();
+        this.configAdminTracker.close();
 
-                this.logger.debug("Thread pool " + pool.getName() + " is shut down.");
+        synchronized ( this.pools ) {
+            for (final Entry entry : this.pools.values()) {
+                entry.shutdown();
             }
             this.pools.clear();
         }
-        this.logger.info("Thread pool manager stopped.");
+        this.logger.info("Stopped Apache Sling Thread Pool Manager.");
     }
 
     /**
-     * @see org.apache.sling.commons.threads.ThreadPoolManager#add(org.apache.sling.commons.threads.ThreadPool)
+     * Helper method to get the pid
      */
-    public boolean add(ThreadPool pool) {
-        synchronized ( this.pools ) {
-            if (null != pools.get(pool.getName())) {
-                return false;
-            }
-            pools.put(pool.getName(), pool);
+    private String getPid() {
+        // as the activator put a string in the props we know that this is a string
+        return this.properties.get(Constants.SERVICE_PID).toString();
+    }
+
+    /**
+     * Create a dictionary with configuration properties for the configuration
+     */
+    private Dictionary<String, Object> getProperties(final String poolName, final ThreadPoolConfig config) {
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(ModifiableThreadPoolConfig.PROPERTY_MIN_POOL_SIZE, config.getMinPoolSize());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_MAX_POOL_SIZE, config.getMaxPoolSize());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_QUEUE_SIZE, config.getQueueSize());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_KEEP_ALIVE_TIME, config.getKeepAliveTime());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_BLOCK_POLICY, config.getBlockPolicy().toString());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_SHUTDOWN_GRACEFUL, config.isShutdownGraceful());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_SHUTDOWN_WAIT_TIME, config.getShutdownWaitTimeMs());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_PRIORITY, config.getPriority().toString());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_DAEMON, config.isDaemon());
+        props.put(ModifiableThreadPoolConfig.PROPERTY_NAME, poolName);
+
+        return props;
+    }
+
+    /**
+     * Create a thread pool configuration from a config admin configuration
+     */
+    private ThreadPoolConfig createConfig(final Dictionary<String, Object> props) {
+        final ModifiableThreadPoolConfig config = new ModifiableThreadPoolConfig();
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_MIN_POOL_SIZE) != null ) {
+            config.setMinPoolSize((Integer)props.get(ModifiableThreadPoolConfig.PROPERTY_MIN_POOL_SIZE));
+        }
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_MAX_POOL_SIZE) != null ) {
+            config.setMaxPoolSize((Integer)props.get(ModifiableThreadPoolConfig.PROPERTY_MAX_POOL_SIZE));
         }
-        return true;
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_QUEUE_SIZE) != null ) {
+            config.setQueueSize((Integer)props.get(ModifiableThreadPoolConfig.PROPERTY_QUEUE_SIZE));
+        }
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_KEEP_ALIVE_TIME) != null ) {
+            config.setKeepAliveTime((Long)props.get(ModifiableThreadPoolConfig.PROPERTY_KEEP_ALIVE_TIME));
+        }
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_BLOCK_POLICY) != null ) {
+            config.setBlockPolicy(ThreadPoolPolicy.valueOf(props.get(ModifiableThreadPoolConfig.PROPERTY_BLOCK_POLICY).toString()));
+        }
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_SHUTDOWN_GRACEFUL) != null ) {
+            config.setShutdownGraceful((Boolean)props.get(ModifiableThreadPoolConfig.PROPERTY_SHUTDOWN_GRACEFUL));
+        }
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_SHUTDOWN_WAIT_TIME) != null ) {
+            config.setShutdownWaitTimeMs((Integer)props.get(ModifiableThreadPoolConfig.PROPERTY_SHUTDOWN_WAIT_TIME));
+        }
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_PRIORITY) != null ) {
+            config.setPriority(ThreadPriority.valueOf(props.get(ModifiableThreadPoolConfig.PROPERTY_PRIORITY).toString()));
+        }
+        if ( props.get(ModifiableThreadPoolConfig.PROPERTY_DAEMON) != null ) {
+            config.setDaemon((Boolean)props.get(ModifiableThreadPoolConfig.PROPERTY_DAEMON));
+        }
+        return config;
     }
 
     /**
      * @see org.apache.sling.commons.threads.ThreadPoolManager#get(java.lang.String)
      */
-    public ThreadPool get(String name) {
-        if ( name == null ) {
-            name = DEFAULT_THREADPOOL_NAME;
-        }
+    public ThreadPool get(final String name) {
+        final String poolName = (name == null ? DEFAULT_THREADPOOL_NAME : name);
+        Entry entry = null;
         synchronized (this.pools) {
-            ThreadPool pool = this.pools.get(name);
-            if ( pool == null && !(name.equals(DEFAULT_THREADPOOL_NAME))) {
-                this.logger.info("Requested pool {} is not available, returning default pool.", name);
-                pool = this.pools.get(DEFAULT_THREADPOOL_NAME);
+            entry = this.pools.get(poolName);
+            if ( entry == null ) {
+                this.logger.debug("Creating new pool with name {}", poolName);
+                final ModifiableThreadPoolConfig config = new ModifiableThreadPoolConfig();
+                // check for config admin
+                final ConfigurationAdmin ca = (ConfigurationAdmin) this.configAdminTracker.getService();
+                if ( ca != null ) {
+                    try {
+                        final Configuration caConfig = ca.createFactoryConfiguration(getPid());
+                        caConfig.update(this.getProperties(poolName, config));
+                        entry = new Entry(caConfig.getProperties().get(Constants.SERVICE_PID).toString(), config, poolName);
+                    } catch (IOException e) {
+                        // there is not much we can do if we get an io exception
+                        // just log and continue
+                        this.logger.error("Unable to create configuration for thread pool " + poolName, e);
+                    }
+                } else {
+                    this.logger.error("Configuration admin is not available.");
+                }
+                // sanity check - if CA is not available or a problem occured during persisting
+                // we don't have an entry
+                if ( entry == null ) {
+                    entry = new Entry(null, config, poolName);
+                }
+                this.pools.put(poolName, entry);
             }
-            return pool;
+            return entry.incUsage();
+
         }
     }
 
     /**
-     * @see org.apache.sling.commons.threads.ThreadPoolManager#create(java.lang.String, org.apache.sling.commons.threads.ThreadPoolConfig)
+     * @see org.apache.sling.commons.threads.ThreadPoolManager#release(org.apache.sling.commons.threads.ThreadPool)
      */
-    public ThreadPool create(String name,
-                             ThreadPoolConfig config) {
-        if ( name == null ) {
-            throw new IllegalArgumentException("Name must not be null.");
+    public void release(ThreadPool pool) {
+        if ( pool instanceof ThreadPoolFacade ) {
+            synchronized ( this.pools ) {
+                final Entry entry = this.pools.get(pool.getName());
+                if ( entry != null ) {
+                    entry.decUsage();
+                }
+            }
         }
+
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPoolManager#create(org.apache.sling.commons.threads.ThreadPoolConfig)
+     */
+    public ThreadPool create(ThreadPoolConfig config) {
         if ( config == null ) {
             throw new IllegalArgumentException("Config must not be null.");
         }
+        final String name = "ThreadPool-" + UUID.randomUUID().toString();
+        final Entry entry = new Entry(null, config, name);
+        synchronized ( this.pools ) {
+            this.pools.put(name, entry);
+        }
+        return entry.incUsage();
+    }
+
+    /**
+     * @see org.osgi.service.cm.ManagedServiceFactory#getName()
+     */
+    public String getName() {
+        return this.properties.get(Constants.SERVICE_DESCRIPTION).toString();
+    }
+
+    /**
+     * @see org.osgi.service.cm.ManagedServiceFactory#updated(java.lang.String, java.util.Dictionary)
+     */
+    @SuppressWarnings("unchecked")
+    public void updated(String pid, Dictionary properties)
+    throws ConfigurationException {
+        final String name = (String) properties.get(ModifiableThreadPoolConfig.PROPERTY_NAME);
+        if ( name == null || name.length() == 0 ) {
+            throw new ConfigurationException(ModifiableThreadPoolConfig.PROPERTY_NAME, "Property is missing or empty.");
+        }
+        this.logger.debug("Updating {} with {}", pid, properties);
         synchronized ( this.pools ) {
-            ThreadPool pool = this.pools.get(name);
-            if ( pool != null ) {
-                // pool already exists
-                return null;
+            Entry foundEntry = null;
+            // we have to search the config by using the pid!
+            for (final Entry entry : this.pools.values()) {
+                if ( pid.equals(entry.getPid()) ) {
+                    foundEntry = entry;
+                    break;
+                }
+            }
+            final ThreadPoolConfig config = this.createConfig(properties);
+            if ( foundEntry != null ) {
+                // if the name changed - we have to reregister(!)
+                if ( !name.equals(foundEntry.getName()) ) {
+                    this.pools.remove(foundEntry.getName());
+                    this.pools.put(name, foundEntry);
+                }
+                // update
+                foundEntry.update(config, name);
+            } else {
+                // create
+                this.pools.put(name, new Entry(pid, config, name));
             }
-            pool = new DefaultThreadPool(name, config);
-            this.pools.put(name, pool);
-            return pool;
         }
     }
 
+    /**
+     * @see org.osgi.service.cm.ManagedServiceFactory#deleted(java.lang.String)
+     */
+    public void deleted(String pid) {
+        this.logger.debug("Deleting " + pid);
+        // we just remove the thread pool from our list of pools and readd it
+        // as an anonymous pool with default config(!) if it is used
+        synchronized ( this.pools ) {
+            Entry foundEntry = null;
+            // we have to search the config by using the pid!
+            for (final Entry entry : this.pools.values()) {
+                if ( pid.equals(entry.getPid()) ) {
+                    foundEntry = entry;
+                    break;
+                }
+            }
+            if ( foundEntry != null ) {
+                this.pools.remove(foundEntry.getName());
+                if ( foundEntry.isUsed() ) {
+                    // we register this with a new name
+                    final String name = "ThreadPool-" + UUID.randomUUID().toString();
+                    foundEntry.update(new ModifiableThreadPoolConfig(), name);
+                    this.pools.put(name, foundEntry);
+                }
+            }
+        }
+    }
+
+    private static final class Entry {
+        /** The configuration pid. (might be null for anonymous pools.*/
+        private final String pid;
+
+        /** Usage count. */
+        private volatile int count;
+
+        /** The configuration for the pool. */
+        private volatile ThreadPoolConfig config;
+
+        /** The name of the pool. */
+        private volatile String name;
+
+        /** The corresponding pool - might be null if unused. */
+        private volatile ThreadPoolFacade pool;
+
+        public Entry(final String pid, final ThreadPoolConfig config, final String name) {
+            this.pid = pid;
+            this.config = config;
+            this.name = name;
+        }
+
+        public String getPid() {
+            return this.pid;
+        }
+
+        public void shutdown() {
+            if ( this.pool != null ) {
+                this.pool.shutdown();
+                this.pool = null;
+            }
+        }
+
+        public ThreadPoolFacade incUsage() {
+            if ( pool == null ) {
+                pool = new ThreadPoolFacade(new DefaultThreadPool(name, this.config));
+            }
+            this.count++;
+            return pool;
+        }
+
+        public void decUsage() {
+            this.count--;
+            if ( this.count == 0 ) {
+                this.shutdown();
+            }
+        }
+
+        public void update(final ThreadPoolConfig config, final String name) {
+            if ( this.pool != null ) {
+                this.pool.setName(name);
+                if ( !this.config.equals(config) ) {
+                    this.pool.setPool(new DefaultThreadPool(name, config));
+                }
+            }
+            this.config = config;
+            this.name = name;
+        }
+
+        public String getName() {
+            return this.name;
+        }
+
+        public boolean isUsed() {
+            return this.count > 0;
+        }
+    }
 }

Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java?rev=891673&r1=891672&r2=891673&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ExtendedThreadFactory.java Thu Dec 17 12:38:10 2009
@@ -25,7 +25,6 @@
  * This class is responsible to create new Thread instances.
  * It's a very basic implementation.
  *
- * @version $Id$
  */
 public final class ExtendedThreadFactory implements ThreadFactory {
 

Added: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java?rev=891673&view=auto
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java (added)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java Thu Dec 17 12:38:10 2009
@@ -0,0 +1,84 @@
+/*
+ * 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.sling.commons.threads.impl;
+
+import org.apache.sling.commons.threads.ThreadPool;
+import org.apache.sling.commons.threads.ThreadPoolConfig;
+
+
+/**
+ * This is a facade which is delivered to clients. It allows
+ * to change the thread pool behind the facacde without affecting
+ * the client.
+ */
+public final class ThreadPoolFacade implements ThreadPool {
+
+    /** The thread pool */
+    private volatile DefaultThreadPool delegatee;
+
+    /** The name of the pool */
+    private String name;
+
+    public ThreadPoolFacade(final DefaultThreadPool pool) {
+        this.delegatee = pool;
+        this.name = pool.getName();
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPool#execute(java.lang.Runnable)
+     */
+    public void execute(Runnable runnable) {
+        this.delegatee.execute(runnable);
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPool#getConfiguration()
+     */
+    public ThreadPoolConfig getConfiguration() {
+        return this.delegatee.getConfiguration();
+    }
+
+    /**
+     * @see org.apache.sling.commons.threads.ThreadPool#getName()
+     */
+    public String getName() {
+        return this.name;
+    }
+
+    /**
+     * Shutdown this thread pool.
+     */
+    public void shutdown() {
+        this.delegatee.shutdown();
+    }
+
+    /**
+     * Update name
+     */
+    public void setName(final String name) {
+        this.name = name;
+    }
+
+    /**
+     * Update pool
+     */
+    public void setPool(final DefaultThreadPool pool) {
+        final DefaultThreadPool oldPool = this.delegatee;
+        this.delegatee = pool;
+        oldPool.shutdown();
+    }
+}

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/ThreadPoolFacade.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=891673&view=auto
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.properties (added)
+++ sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.properties Thu Dec 17 12:38:10 2009
@@ -0,0 +1,51 @@
+#
+#  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.
+#
+
+factory.config.name = Apache Sling Thread Pool Configuration
+factory.config.description = Configuration for a named thread pool.
+
+name.name=Name
+name.description=The unique name for this thread pool.
+
+minPoolSize.name=Min Pool Size
+minPoolSize.description=The minimum pool size.
+
+maxPoolSize.name=Max Pool Size
+maxPoolSize.description=The maximum pool size.
+
+queueSize.name=Queue Size
+queueSize.description=The queue size or -1 for an unlimited queue size.
+
+keepAliveTime.name=Keep Alive Time
+keepAliveTime.description=The keep alive time.
+
+blockPolicy.name=Block Policy
+blockPolicy.description=The block policy.
+
+shutdownGraceful.name=Shutdown Graceful
+shutdownGraceful.description=Should the pool be shutdown gracefully?
+
+daemon.name=Daemon Threads
+daemon.description=Should the pool create daemon threads?
+
+shutdownWaitTime.name=Shutdown Wait Time
+shutdownWaitTime.description=The shutdown wait time. A value below 1 means no wait time.
+
+priority.name=Priority
+priority.description=The default priority for the threads.

Propchange: sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.properties
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.xml?rev=891673&view=auto
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.xml (added)
+++ sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.xml Thu Dec 17 12:38:10 2009
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<metatype:MetaData
+    xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0"
+    localization="metatype">
+
+    <metatype:OCD
+        id="org.apache.sling.commons.threads.impl.DefaultThreadPool.factory"
+        name="%factory.config.name"
+        description="%factory.config.description">
+        <metatype:AD id="name"
+            type="String" name="%name.name"
+            description="%name.description" required="true"/>
+        <metatype:AD id="minPoolSize"
+            type="Integer" default="5" name="%minPoolSize.name"
+            description="%minPoolSize.description"/>
+        <metatype:AD id="maxPoolSize"
+            type="Integer" default="5" name="%queueSize.name"
+            description="%maxPoolSize.description" />
+        <metatype:AD id="queueSize"
+            type="Integer" default="-1" name="%queueSize.name"
+            description="%queueSize.description" />
+        <metatype:AD id="keepAliveTime"
+            type="Long" default="60000" name="%keepAliveTime.name"
+            description="%keepAliveTime.description" />
+        <metatype:AD id="blockPolicy"
+            type="String" default="RUN" name="%blockPolicy.name"
+            description="%blockPolicy.description">
+            <metatype:Option value="ABORT" label="Abort" />
+            <metatype:Option value="DISCARD" label="Discard" />
+            <metatype:Option value="DISCARDOLDEST" label="Discard Oldest" />
+            <metatype:Option value="RUN" label="Run" />
+        </metatype:AD>
+        <metatype:AD id="shutdownGraceful"
+            type="Boolean" default="false" name="%shutdownGraceful.name"
+            description="%shutdownGraceful.description" />
+        <metatype:AD id="daemon"
+            type="Integer" default="-1" name="%daemon.name"
+            description="%daemon.description" />
+        <metatype:AD id="shutdownWaitTime"
+            type="Boolean" default="false" name="%shutdownWaitTime.name"
+            description="%shutdownWaitTime.description" />
+        <metatype:AD id="priority"
+            type="String" default="NORM" name="%priority.name"
+            description="%priority.description" >
+            <metatype:Option value="NORM" label="Norm" />
+            <metatype:Option value="MIN" label="Min" />
+            <metatype:Option value="MAX" label="Max" />
+        </metatype:AD>
+    </metatype:OCD>
+    <metatype:Designate
+        pid="org.apache.sling.commons.threads.impl.DefaultThreadPool.factory"
+        factoryPid="org.apache.sling.commons.threads.impl.DefaultThreadPool.factory">
+        <metatype:Object
+            ocdref="org.apache.sling.commons.threads.impl.DefaultThreadPool.factory" />
+    </metatype:Designate>
+</metatype:MetaData>
\ No newline at end of file

Propchange: sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.xml
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: sling/trunk/bundles/commons/threads/src/main/resources/OSGI-INF/metatype/metatype.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message