Permalink
Browse files

[maven-release-plugin] copy for tag camel-2.8.0

git-svn-id: https://svn.apache.org/repos/asf/camel/tags/camel-2.8.0@1148091 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
2 parents f4c00bb + a76e637 commit 9d5fd0d12094fc6967834baf5270bbfa18a1c9b4 @hzbarcea hzbarcea committed Jul 18, 2011
Showing 10,635 changed files with 1,159 additions and 921,915 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
@@ -5,7 +5,7 @@
=========================================================================
Apache Camel
-Copyright 2007-2010 The Apache Software Foundation
+Copyright 2007-2011 The Apache Software Foundation
This product includes software developed by
The Apache Software Foundation (http://www.apache.org/).
@@ -40,6 +40,11 @@ public NoSuchBeanException(String name, Throwable cause) {
this.name = name;
}
+ public NoSuchBeanException(String name, String message, Throwable cause) {
+ super(message, cause);
+ this.name = name;
+ }
+
public String getName() {
return name;
}
@@ -21,6 +21,7 @@
import java.util.List;
import java.util.Map;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.spi.Registry;
/**
@@ -44,9 +45,18 @@ public void addRegistry(Registry registry) {
public <T> T lookup(String name, Class<T> type) {
T answer = null;
for (Registry registry : registryList) {
- answer = registry.lookup(name, type);
- if (answer != null) {
- break;
+ try {
+ answer = registry.lookup(name, type);
+ if (answer != null) {
+ break;
+ }
+ } catch (Throwable e) {
+ // do not double wrap the exception
+ if (e instanceof NoSuchBeanException) {
+ throw (NoSuchBeanException) e;
+ }
+ throw new NoSuchBeanException(name, "Cannot lookup: " + name + " from registry: " + registry
+ + " with expected type: " + type + " due: " + e.getMessage(), e);
}
}
return answer;
@@ -24,6 +24,7 @@
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.Registry;
@@ -43,8 +44,20 @@ public JndiRegistry(Context context) {
}
public <T> T lookup(String name, Class<T> type) {
- Object value = lookup(name);
- return type.cast(value);
+ Object answer = lookup(name);
+
+ // just to be safe
+ if (answer == null) {
+ return null;
+ }
+
+ try {
+ return type.cast(answer);
+ } catch (Throwable e) {
+ String msg = "Found bean: " + name + " in JNDI Context: " + context
+ + " of type: " + answer.getClass().getName() + " expected type was: " + type;
+ throw new NoSuchBeanException(name, msg, e);
+ }
}
public Object lookup(String name) {
@@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.Map;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.spi.Registry;
/**
@@ -33,8 +34,20 @@ public Object lookup(String name) {
}
public <T> T lookup(String name, Class<T> type) {
- Object o = lookup(name);
- return type.cast(o);
+ Object answer = lookup(name);
+
+ // just to be safe
+ if (answer == null) {
+ return null;
+ }
+
+ try {
+ return type.cast(answer);
+ } catch (Throwable e) {
+ String msg = "Found bean: " + name + " in SimpleRegistry: " + this
+ + " of type: " + answer.getClass().getName() + " expected type was: " + type;
+ throw new NoSuchBeanException(name, msg, e);
+ }
}
public <T> Map<String, T> lookupByType(Class<T> type) {
@@ -21,9 +21,11 @@
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URL;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import static java.lang.reflect.Modifier.isAbstract;
@@ -41,12 +43,23 @@
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * A class which will auto-discover converter objects and methods to pre-load
- * the registry of converters on startup
+ * A class which will auto-discover {@link Converter} objects and methods to pre-load
+ * the {@link TypeConverterRegistry} of converters on startup.
+ * <p/>
+ * This implementation supports scanning for type converters in JAR files. The {@link #META_INF_SERVICES}
+ * contains a list of packages or FQN class names for {@link Converter} classes. The FQN class names
+ * is loaded first and directly by the class loader.
+ * <p/>
+ * The {@link PackageScanClassResolver} is being used to scan packages for {@link Converter} classes and
+ * this procedure is slower than loading the {@link Converter} classes directly by its FQN class name.
+ * Therefore its recommended to specify FQN class names in the {@link #META_INF_SERVICES} file.
+ * Likewise the procedure for scanning using {@link PackageScanClassResolver} may require custom implementations
+ * to work in various containers such as JBoss, OSGi, etc.
*
* @version
*/
@@ -77,21 +90,36 @@ public void load(TypeConverterRegistry registry) throws TypeConverterLoaderExcep
// if we only have camel-core on the classpath then we have already pre-loaded all its type converters
// but we exposed the "org.apache.camel.core" package in camel-core. This ensures there is at least one
// packageName to scan, which triggers the scanning process. That allows us to ensure that we look for
- // type converters in all the JARs.
+ // META-INF/services in all the JARs.
if (packageNames.length == 1 && "org.apache.camel.core".equals(packageNames[0])) {
LOG.debug("No additional package names found in classpath for annotated type converters.");
// no additional package names found to load type converters so break out
return;
}
- LOG.trace("Found converter packages to scan: {}", packageNames);
- Set<Class<?>> classes = resolver.findAnnotated(Converter.class, packageNames);
- if (classes == null || classes.isEmpty()) {
- throw new TypeConverterLoaderException("Cannot find any type converter classes from the following packages: " + Arrays.asList(packageNames));
+ // now filter out org.apache.camel.core as its not needed anymore (it was just a dummy)
+ packageNames = filterUnwantedPackage("org.apache.camel.core", packageNames);
+
+ // filter out package names which can be loaded as a class directly so we avoid package scanning which
+ // is much slower and does not work 100% in all runtime containers
+ Set<Class<?>> classes = new HashSet<Class<?>>();
+ packageNames = filterPackageNamesOnly(resolver, packageNames, classes);
+ if (!classes.isEmpty()) {
+ LOG.info("Loaded " + classes.size() + " @Converter classes");
}
- LOG.info("Found " + packageNames.length + " packages with " + classes.size() + " @Converter classes to load");
+ // if there is any packages to scan and load @Converter classes, then do it
+ if (packageNames != null && packageNames.length > 0) {
+ LOG.trace("Found converter packages to scan: {}", packageNames);
+ Set<Class<?>> scannedClasses = resolver.findAnnotated(Converter.class, packageNames);
+ if (scannedClasses.isEmpty()) {
+ throw new TypeConverterLoaderException("Cannot find any type converter classes from the following packages: " + Arrays.asList(packageNames));
+ }
+ LOG.info("Found " + packageNames.length + " packages with " + scannedClasses.size() + " @Converter classes to load");
+ classes.addAll(scannedClasses);
+ }
+ // load all the found classes into the type converter registry
for (Class type : classes) {
if (LOG.isTraceEnabled()) {
LOG.trace("Loading converter class: {}", ObjectHelper.name(type));
@@ -105,6 +133,57 @@ public void load(TypeConverterRegistry registry) throws TypeConverterLoaderExcep
}
/**
+ * Filters the given list of packages and returns an array of <b>only</b> package names.
+ * <p/>
+ * This implementation will check the given list of packages, and if it contains a class name,
+ * that class will be loaded directly and added to the list of classes. This optimizes the
+ * type converter to avoid excessive file scanning for .class files.
+ *
+ * @param resolver the class resolver
+ * @param packageNames the package names
+ * @param classes to add loaded @Converter classes
+ * @return the filtered package names
+ */
+ protected String[] filterPackageNamesOnly(PackageScanClassResolver resolver, String[] packageNames, Set<Class<?>> classes) {
+ if (packageNames == null || packageNames.length == 0) {
+ return packageNames;
+ }
+
+ // optimize for CorePackageScanClassResolver
+ if (resolver.getClassLoaders().isEmpty()) {
+ return packageNames;
+ }
+
+ // the filtered packages to return
+ List<String> packages = new ArrayList<String>();
+
+ // try to load it as a class first
+ for (String name : packageNames) {
+ // must be a FQN class name by having an upper case letter
+ if (StringHelper.hasUpperCase(name)) {
+ for (ClassLoader loader : resolver.getClassLoaders()) {
+ try {
+ Class<?> clazz = loader.loadClass(name);
+ LOG.trace("Loaded {} as class {}", name, clazz);
+ classes.add(clazz);
+ // class founder, so no need to load it with another class loader
+ break;
+ } catch (Throwable e) {
+ // ignore as its not a class (will be package scan afterwards)
+ packages.add(name);
+ }
+ }
+ } else {
+ // ignore as its not a class (will be package scan afterwards)
+ packages.add(name);
+ }
+ }
+
+ // return the packages which is not FQN classes
+ return packages.toArray(new String[packages.size()]);
+ }
+
+ /**
* Finds the names of the packages to search for on the classpath looking
* for text files on the classpath at the {@link #META_INF_SERVICES} location.
*
@@ -282,4 +361,25 @@ protected boolean isValidFallbackConverterMethod(Method method) {
|| (parameterTypes.length == 4 && Exchange.class.isAssignableFrom(parameterTypes[1]))
&& (TypeConverterRegistry.class.isAssignableFrom(parameterTypes[parameterTypes.length - 1])));
}
+
+ /**
+ * Filters the given list of packages
+ *
+ * @param name the name to filter out
+ * @param packageNames the packages
+ * @return he packages without the given name
+ */
+ protected static String[] filterUnwantedPackage(String name, String[] packageNames) {
+ // the filtered packages to return
+ List<String> packages = new ArrayList<String>();
+
+ for (String s : packageNames) {
+ if (!name.equals(s)) {
+ packages.add(s);
+ }
+ }
+
+ return packages.toArray(new String[packages.size()]);
+ }
+
}
@@ -383,7 +383,7 @@ protected TypeConverter doLookup(Class<?> toType, Class<?> fromType, boolean isS
/**
* Loads the core type converters which is mandatory to use Camel
*/
- protected void loadCoreTypeConverters() throws Exception {
+ public void loadCoreTypeConverters() throws Exception {
int before = typeMappings.size();
// load all the type converters from camel-core
@@ -56,6 +56,7 @@
import org.apache.camel.spi.TypeConverterRegistry;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
@@ -295,34 +296,76 @@ public void load(TypeConverterRegistry registry) throws TypeConverterLoaderExcep
PackageScanFilter test = new AnnotatedWithPackageScanFilter(Converter.class, true);
Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
Set<String> packages = getConverterPackages(bundle.getEntry(META_INF_TYPE_CONVERTER));
+
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Found {} {} packages: {}", new Object[]{packages.size(), META_INF_TYPE_CONVERTER, packages});
+ }
+ // if we only have camel-core on the classpath then we have already pre-loaded all its type converters
+ // but we exposed the "org.apache.camel.core" package in camel-core. This ensures there is at least one
+ // packageName to scan, which triggers the scanning process. That allows us to ensure that we look for
+ // META-INF/services in all the JARs.
+ if (packages.size() == 1 && "org.apache.camel.core".equals(packages.iterator().next())) {
+ LOG.debug("No additional package names found in classpath for annotated type converters.");
+ // no additional package names found to load type converters so break out
+ return;
+ }
+
+ // now filter out org.apache.camel.core as its not needed anymore (it was just a dummy)
+ packages.remove("org.apache.camel.core");
+
for (String pkg : packages) {
+
+ if (StringHelper.hasUpperCase(pkg)) {
+ // its a FQN class name so load it directly
+ LOG.trace("Loading {} class", pkg);
+ try {
+ Class clazz = bundle.loadClass(pkg);
+ if (test.matches(clazz)) {
+ classes.add(clazz);
+ }
+ // the class could be found and loaded so continue to next
+ continue;
+ } catch (Throwable t) {
+ // Ignore
+ LOG.trace("Failed to load " + pkg + " class due " + t.getMessage() + ". This exception will be ignored.", t);
+ }
+ }
+
+ // its not a FQN but a package name so scan for classes in the bundle
Enumeration<URL> e = bundle.findEntries("/" + pkg.replace('.', '/'), "*.class", true);
while (e != null && e.hasMoreElements()) {
String path = e.nextElement().getPath();
String externalName = path.substring(path.charAt(0) == '/' ? 1 : 0, path.indexOf('.')).replace('/', '.');
+ LOG.trace("Loading {} class", externalName);
try {
Class clazz = bundle.loadClass(externalName);
if (test.matches(clazz)) {
- classes.add(bundle.loadClass(externalName));
+ classes.add(clazz);
}
} catch (Throwable t) {
// Ignore
+ LOG.trace("Failed to load " + externalName + " class due " + t.getMessage() + ". This exception will be ignored.", t);
}
}
}
+
+ // load the classes into type converter registry
LOG.info("Found {} @Converter classes to load", classes.size());
for (Class type : classes) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Loading converter class: {}", ObjectHelper.name(type));
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Loading converter class: {}", ObjectHelper.name(type));
}
loadConverterMethods(registry, type);
}
+
+ // register fallback converters
URL fallbackUrl = bundle.getEntry(META_INF_FALLBACK_TYPE_CONVERTER);
if (fallbackUrl != null) {
TypeConverter tc = createInstance("FallbackTypeConverter", fallbackUrl, registry.getInjector());
registry.addFallbackTypeConverter(tc, false);
}
- // Clear info
+
+ // now clear the maps so we do not hold references
visitedClasses.clear();
visitedURIs.clear();
}
Oops, something went wrong.

0 comments on commit 9d5fd0d

Please sign in to comment.