001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.aop.deployment;
023:
024: import org.jboss.aop.AspectManager;
025: import org.jboss.aop.Domain;
026: import org.jboss.aop.classpool.AOPScopedClassLoaderHelper;
027: import org.jboss.mx.loading.HeirarchicalLoaderRepository3;
028: import org.jboss.mx.loading.LoaderRepository;
029: import org.jboss.mx.loading.RepositoryClassLoader;
030:
031: /**
032: *
033: * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
034: * @version $Revision$
035: */
036: public class JBossScopedClassLoaderHelper implements
037: AOPScopedClassLoaderHelper {
038: public ClassLoader ifScopedDeploymentGetScopedParentUclForCL(
039: ClassLoader loader) {
040: ClassLoader parent = loader;
041: //The web classloader will be a child of the unified classloader - find out if that is scoped
042: while (parent != null) {
043: if (isScopedClassLoader(parent)) {
044: return parent;
045: }
046: if (parent instanceof RepositoryClassLoader) {
047: //We were a repository classloader, but not scoped - ignore the parents like a sulky teenager
048: return null;
049: }
050: parent = parent.getParent();
051: }
052: return null;
053: }
054:
055: public static boolean isScopedClassLoader(ClassLoader loader) {
056: boolean scoped = false;
057: if (loader instanceof RepositoryClassLoader) {
058: LoaderRepository repository = ((RepositoryClassLoader) loader)
059: .getLoaderRepository();
060: if (repository instanceof HeirarchicalLoaderRepository3) {
061: scoped = true;
062: HeirarchicalLoaderRepository3 hlr = (HeirarchicalLoaderRepository3) repository;
063: boolean parentFirst = hlr.getUseParentFirst();
064: }
065: }
066: return scoped;
067: }
068:
069: public ClassLoader getTopLevelJBossClassLoader() {
070: ClassLoader loader = Thread.currentThread()
071: .getContextClassLoader();
072: RepositoryClassLoader topRcl = null;
073: while (loader != null) {
074: if (loader instanceof RepositoryClassLoader) {
075: topRcl = (RepositoryClassLoader) loader;
076: }
077: loader = loader.getParent();
078: }
079: return topRcl;
080: }
081:
082: public Domain getScopedClassLoaderDomain(ClassLoader cl,
083: AspectManager parent) {
084: boolean parentDelegation = true;
085: if (cl instanceof RepositoryClassLoader) {
086: HeirarchicalLoaderRepository3 repository = (HeirarchicalLoaderRepository3) ((RepositoryClassLoader) cl)
087: .getLoaderRepository();
088: parentDelegation = repository.getUseParentFirst();
089: }
090: String name = String.valueOf(System.identityHashCode(cl));
091: return new ScopedClassLoaderDomain(cl, name, parentDelegation,
092: parent, false);
093: }
094:
095: public Object getLoaderRepository(ClassLoader loader) {
096: ClassLoader cl = ifScopedDeploymentGetScopedParentUclForCL(loader);
097: if (cl != null) {
098: return ((RepositoryClassLoader) cl).getLoaderRepository();
099: }
100: return null;
101: }
102: }
|