Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.impl.thread » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.concurrent.impl.thread;
   18   
   19   import java.util.ArrayList;
   20   import java.util.HashMap;
   21   import java.util.List;
   22   import java.util.Map;
   23   
   24   import org.apache.geronimo.concurrent.ManagedContext;
   25   import org.apache.geronimo.concurrent.ManagedContextHandler;
   26   import org.apache.geronimo.concurrent.thread.ManagedRunnable;
   27   import org.apache.geronimo.gbean.AbstractName;
   28   import org.apache.geronimo.gbean.AbstractNameQuery;
   29   import org.apache.geronimo.kernel.Kernel;
   30   import org.apache.geronimo.kernel.KernelRegistry;
   31   import org.apache.geronimo.kernel.lifecycle.LifecycleListener;
   32   
   33   public class StandaloneManagedThreadFactory extends TrackingManagedThreadFactory {
   34     
   35       private ManagedContextHandler contextHandler;
   36       private ModuleLifecycleListener moduleLifecyleListener;  
   37       private ManagedContext managedContext;
   38      
   39       public StandaloneManagedThreadFactory(GeronimoManagedThreadFactory threadFactory,
   40                                             ManagedContextHandler contextHandler, 
   41                                             AbstractName moduleName) {
   42           super(threadFactory);
   43           this.contextHandler = contextHandler;
   44           this.moduleLifecyleListener = ModuleLifecycleListener.getModuleLifecycleListener(moduleName);
   45           this.moduleLifecyleListener.addThreadFactory(this);
   46           
   47           // capture context
   48           this.managedContext = ManagedContext.captureContext(this.contextHandler);
   49       }
   50       
   51       @Override
   52       public Thread newThread(Runnable runnable) {
   53           if (!isRunning()) {
   54               throw new IllegalArgumentException(
   55                   "Component that created this thread factory is no longer running");
   56           }
   57                   
   58           Runnable managedRunnable = new ManagedRunnable(runnable, this.managedContext, true);
   59           Thread thread = super.newThread(managedRunnable);
   60           return thread;
   61       }
   62   
   63       protected boolean isRunning() {
   64           return this.moduleLifecyleListener.isRunning();
   65       }
   66             
   67       protected void shutdown() {
   68           interruptThreads();
   69       }
   70           
   71       private static class ModuleLifecycleListener implements LifecycleListener {
   72   
   73           private static final Map<AbstractName, ModuleLifecycleListener> listeners = 
   74               new HashMap<AbstractName, ModuleLifecycleListener>();
   75           
   76           private final AbstractName moduleName;
   77           private boolean running;
   78           private final List<StandaloneManagedThreadFactory> threadFactories = 
   79               new ArrayList<StandaloneManagedThreadFactory>();
   80           
   81           public ModuleLifecycleListener(AbstractName moduleName) {
   82               this.moduleName = moduleName;
   83               this.running = true;
   84           }
   85                   
   86           public synchronized void addThreadFactory(StandaloneManagedThreadFactory threadFactory) {
   87               if (this.running) {
   88                   this.threadFactories.add(threadFactory);
   89               }
   90           }
   91           
   92           public synchronized void shutdown() {
   93               for (StandaloneManagedThreadFactory threadFactory : this.threadFactories) {
   94                   try {
   95                       threadFactory.shutdown();
   96                   } catch (Exception e) {
   97                       // ignore
   98                   }
   99               }
  100               this.threadFactories.clear();
  101           }
  102           
  103           public boolean isRunning() {
  104               return this.running;
  105           }
  106           
  107           public AbstractName getModuleName() {
  108               return this.moduleName;
  109           }
  110           
  111           public void failed(AbstractName arg0) {
  112               this.running = false;
  113           }
  114   
  115           public void loaded(AbstractName arg0) {
  116               this.running = false;
  117           }
  118   
  119           public void running(AbstractName arg0) {
  120               this.running = true;
  121           }
  122   
  123           public void starting(AbstractName arg0) {
  124               this.running = false;
  125           }
  126   
  127           public void stopped(AbstractName arg0) {
  128               this.running = false;            
  129           }
  130   
  131           public void stopping(AbstractName arg0) {
  132               this.running = false;
  133               shutdown();
  134           }
  135   
  136           public void unloaded(AbstractName arg0) { 
  137               this.running = false;
  138               shutdown();
  139               removeModuleLifecycleListener(this.moduleName);
  140           }
  141           
  142           private static synchronized ModuleLifecycleListener getModuleLifecycleListener(AbstractName moduleName) {
  143               ModuleLifecycleListener listener = listeners.get(moduleName);
  144               if (listener == null) {
  145                   listener = new ModuleLifecycleListener(moduleName);
  146                   
  147                   // register listener with Kernel
  148                   AbstractNameQuery query = new AbstractNameQuery(moduleName);
  149                   Kernel kernel = KernelRegistry.getSingleKernel();
  150                   kernel.getLifecycleMonitor().addLifecycleListener(listener, query);
  151                   
  152                   listeners.put(moduleName, listener);
  153               }                       
  154               return listener;
  155           }
  156                   
  157           private static synchronized void removeModuleLifecycleListener(AbstractName moduleName) {
  158               listeners.remove(moduleName);
  159           }
  160           
  161       }
  162           
  163   }

Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.impl.thread » [javadoc | source]