Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.handlers » [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.handlers;
   18   
   19   import java.util.Map;
   20   
   21   import org.apache.commons.logging.Log;
   22   import org.apache.commons.logging.LogFactory;
   23   import org.apache.geronimo.concurrent.ManagedContextHandler;
   24   
   25   /*
   26    * Managed context handler that for thread classloader.
   27    */
   28   public class ClassLoaderContextHandler implements ManagedContextHandler {
   29   
   30       private final static Log LOG = LogFactory.getLog(ClassLoaderContextHandler.class);
   31       
   32       private final static String OLD_CLASSLOADER = 
   33           ClassLoaderContextHandler.class.getName() + ".oldClassloader";
   34       
   35       private final static String NEW_CLASSLOADER = 
   36           ClassLoaderContextHandler.class.getName() + ".newClassloader";
   37           
   38       public void saveContext(Map<String, Object> context) {
   39           LOG.debug("saveContext");
   40           
   41           Thread thread = Thread.currentThread();
   42           context.put(NEW_CLASSLOADER, 
   43                       thread.getContextClassLoader());                    
   44       }
   45   
   46       public void setContext(Map<String, Object> threadContext) {
   47           LOG.debug("setContext");
   48           
   49           Thread thread = Thread.currentThread();
   50           
   51           // save existing classloader
   52           threadContext.put(OLD_CLASSLOADER, 
   53                             thread.getContextClassLoader());
   54           
   55           // set new classloader
   56           ClassLoader classLoader = (ClassLoader)threadContext.get(NEW_CLASSLOADER);
   57           thread.setContextClassLoader(classLoader);
   58       }
   59   
   60       public void unsetContext(Map<String, Object> threadContext) {
   61           LOG.debug("unsetContext");
   62           
   63           Thread thread = Thread.currentThread();
   64           
   65           // restore old classloader
   66           ClassLoader classLoader = (ClassLoader)threadContext.get(OLD_CLASSLOADER);
   67           thread.setContextClassLoader(classLoader);;       
   68       }
   69     
   70   }

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