Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.tuscany.sca.contribution.java.impl;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
A Model Resolver for ClassReferences.

Version:
$Rev: 986740 $ $Date: 2010-08-18 18:28:56 +0300 (Wed, 18 Aug 2010) $
 
 public class ClassLoaderModelResolver extends URLClassLoader implements ModelResolver {
     private ProcessorContext context;
     private Map<StringModelResolverimportResolvers = new HashMap<StringModelResolver>();
     
     // a space to pass back the contribution that was used to resolve
     // a class via an import
 
     private static ClassLoader parentClassLoader(Contribution contribution) {
         if (contribution.getClassLoader() != null) {
             return contribution.getClassLoader();
         }
         ClassLoader parentClassLoader = ServiceDiscovery.getInstance().getContextClassLoader();
         return parentClassLoader;
     }
 
     private static URL[] getContributionURLs(final Contribution contributionthrows IOException {
         if (contribution.getClassLoader() != null) {
             // Do not include the contribution url
             return new URL[0];
         }
         List<URLurls = new ArrayList<URL>();
         urls.add(new URL(contribution.getLocation()));
         urls.addAll(ContributionHelper.getNestedJarUrls(contribution));
         return urls.toArray(new URL[urls.size()]);
     }
 
     public ClassLoaderModelResolver(final Contribution contributionFactoryExtensionPoint modelFactoriesthrows IOException {
         super(getContributionURLs(contribution), parentClassLoader(contribution));
         this. = new WeakReference<Contribution>(contribution);
         // Index Java import resolvers by package name
         Map<StringList<ModelResolver>> resolverMap = new HashMap<StringList<ModelResolver>>();
         for (Import import_this..get().getImports()) {
             if (import_ instanceof JavaImport) {
                 JavaImport javaImport = (JavaImport)import_;
                 List<ModelResolverresolvers = resolverMap.get(javaImport.getPackage());
                 if (resolvers == null) {
                     resolvers = new ArrayList<ModelResolver>();
                     resolverMap.put(javaImport.getPackage(), resolvers);
                 }
                 resolvers.add(javaImport.getModelResolver());
             }
         }
 
         // Create a delegating model resolver for each imported package
         for (Map.Entry<StringList<ModelResolver>> entryresolverMap.entrySet()) {
             .put(entry.getKey(), new DefaultDelegatingModelResolver(entry.getValue()));
         }
     }
 
     public void addModel(Object resolvedProcessorContext context) {
        throw new IllegalStateException();
    }
    public Object removeModel(Object resolvedProcessorContext context) {
        throw new IllegalStateException();
    }
    public <T> T resolveModel(Class<T> modelClass, T unresolvedProcessorContext context) {
        if (!(unresolved instanceof ClassReference)) {
            return unresolved;
        }
        try {
            this. = context;
             = .get();
            
            // Load the class and return a class reference for it
            String className = ((ClassReference)unresolved).getClassName();
            Class<?> clazz = Class.forName(classNamefalsethis);
            ClassReference classReference = new ClassReference(clazz);
            classReference.setContributionContainingClass();
             = null;
            return modelClass.cast(classReference);
        } catch (ClassNotFoundException e) {
            return unresolved;
        }
    }
    @Override
    public URL findResource(String name) {
        //TODO delegate to the Java import resolvers
        URL url = super.findResource(name);
        return url;
    }
    @Override
    public Enumeration<URLfindResources(String namethrows IOException {
        //TODO delegate to the Java import resolvers
        //Enumeration<URL> importedResources;
        Enumeration<URLresources = super.findResources(name);
        List<URLallResources = new ArrayList<URL>();
        //for (; importedResources.hasMoreElements(); ) {
        //    allResources.add(importedResources.nextElement());
        //}
        for (; resources.hasMoreElements(); ) {
            allResources.add(resources.nextElement());
        }
        return Collections.enumeration(allResources);
    }
    @Override
    protected Class<?> findClass(String namethrows ClassNotFoundException {
        // Extract the package name
        int d = name.lastIndexOf('.');
        String packageName;
        if (d != -1) {
            packageName = name.substring(0, d);
        } else {
            packageName = null;
        }
        // First try to load the class using the Java import resolvers
        ModelResolver importResolver = .get(packageName);
        if (importResolver != null) {
            ClassReference classReference = importResolver.resolveModel(ClassReference.classnew ClassReference(name), );
            if (!classReference.isUnresolved()) {
                 = classReference.getContributionContainingClass();
                return classReference.getJavaClass();
            }
        }
        // Next, try to load the class from the current contribution
        Class<?> clazz = super.findClass(name);
        return clazz;
    }
New to GrepCode? Check out our FAQ X