1 /* 2 * Copyright (c) 2005-2007 Creative Sphere Limited. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * 10 * Creative Sphere - initial API and implementation 11 * 12 */ 13 package org.abstracthorizon.extend.server.deployment.danube; 14 15 import org.abstracthorizon.danube.connection.Connection; 16 import org.abstracthorizon.danube.connection.ConnectionHandler; 17 import org.abstracthorizon.extend.server.deployment.Module; 18 19 import java.io.IOException; 20 21 /** 22 * This class represents thread context handler. This connection handler 23 * sets given module's class loader as context class loader for the thread 24 * it operaters under and then invokes further handler. It is helpful since 25 * rest of the chain executed after this connection handler have thread 26 * with context class loader as of given module's that loaded them. 27 * 28 * @author Daniel Sendula 29 */ 30 public class ThreadContextHandler implements ConnectionHandler { 31 32 /** Module this thread is executing under */ 33 protected Module module; 34 35 /** Handler to be invoked */ 36 protected ConnectionHandler handler; 37 38 /** Constructor */ 39 public ThreadContextHandler(ConnectionHandler handler, Module module) { 40 this.module = module; 41 this.handler = handler; 42 } 43 44 /** 45 * This method creates sets context path to be same as context path 46 * up to here plus this component's path. Component's path is reset 47 * to "<code>/<code>" 48 * 49 * @param connection socket connection 50 * @throws IOException io exception 51 */ 52 public void handleConnection(Connection connection) { 53 ClassLoader cl = module.getClassLoader(); 54 if (cl != null) { 55 Thread.currentThread().setContextClassLoader(cl); 56 } 57 handler.handleConnection(connection); 58 } 59 }