onami-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ioca...@apache.org
Subject svn commit: r1449012 - in /incubator/onami/trunk/autobind: core/src/main/java/org/apache/onami/autobind/annotations/features/ core/src/main/java/org/apache/onami/autobind/utils/ integrations/enterprise/src/main/java/org/apache/onami/autobind/enterprise/
Date Fri, 22 Feb 2013 12:08:37 GMT
Author: iocanel
Date: Fri Feb 22 12:08:37 2013
New Revision: 1449012

URL: http://svn.apache.org/r1449012
Log:
[ONAMI-82] Add a utility for loading classes. Replace Class.forName calls.

Added:
    incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/utils/ClassLoadingUtils.java
Modified:
    incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/annotations/features/AutoBindingFeature.java
    incubator/onami/trunk/autobind/integrations/enterprise/src/main/java/org/apache/onami/autobind/enterprise/BeansXMLModule.java

Modified: incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/annotations/features/AutoBindingFeature.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/annotations/features/AutoBindingFeature.java?rev=1449012&r1=1449011&r2=1449012&view=diff
==============================================================================
--- incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/annotations/features/AutoBindingFeature.java
(original)
+++ incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/annotations/features/AutoBindingFeature.java
Fri Feb 22 12:08:37 2013
@@ -44,6 +44,7 @@ import org.apache.onami.autobind.install
 import org.apache.onami.autobind.scanner.features.BindingScannerFeature;
 
 import com.google.inject.BindingAnnotation;
+import org.apache.onami.autobind.utils.ClassLoadingUtils;
 
 @Singleton
 public class AutoBindingFeature
@@ -165,7 +166,7 @@ public class AutoBindingFeature
             Class<? extends Annotation> annotation;
             try
             {
-                annotation = (Class<? extends Annotation>) Class.forName( key );
+                annotation = (Class<? extends Annotation>) ClassLoadingUtils.loadClass(key);
                 if ( annotation.isAnnotationPresent( GuiceAnnotation.class ) )
                 {
                     filtered.remove( key );

Added: incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/utils/ClassLoadingUtils.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/utils/ClassLoadingUtils.java?rev=1449012&view=auto
==============================================================================
--- incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/utils/ClassLoadingUtils.java
(added)
+++ incubator/onami/trunk/autobind/core/src/main/java/org/apache/onami/autobind/utils/ClassLoadingUtils.java
Fri Feb 22 12:08:37 2013
@@ -0,0 +1,67 @@
+/*
+ * 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.onami.autobind.utils;
+
+import com.google.common.base.Optional;
+
+public final class ClassLoadingUtils {
+
+    private ClassLoadingUtils() {
+        //Utility Class
+    }
+
+    /**
+     * Load a class from the current class loader or the thread context class loader.
+     * The default behavior is to load the class using the class loader of ClassLoadingUtils
and falls back
+     * to the thread context class loader if the class was not found.
+     * @param className
+     * @return
+     * @throws ClassNotFoundException
+     */
+    public static Class loadClass(String className) throws ClassNotFoundException {
+        Optional<ClassLoader> currentCloassLoader = Optional.of(ClassLoadingUtils.class.getClassLoader());
+        Optional<ClassLoader> threadContextClassLoader = Optional.<ClassLoader>fromNullable(Thread.currentThread().getContextClassLoader());
+        Optional<Class> clazz = tryLoad(className, currentCloassLoader).or(tryLoad(className,
threadContextClassLoader));
+        if (!clazz.isPresent()) {
+            throw new ClassNotFoundException("Could not load class:" + className);
+        } else {
+            return clazz.get();
+        }
+    }
+
+    /**
+     * Try loading a {@link Class} with specified name from the specified {@link ClassLoader}.
+     *
+     * @param className   The name of the class.
+     * @param classLoader The class loader.
+     * @return Returns an Optional class.
+     */
+    private static Optional<Class> tryLoad(String className, Optional<ClassLoader>
classLoader) {
+        if (!classLoader.isPresent()) {
+            return Optional.absent();
+        } else {
+            try {
+                return Optional.<Class>of(classLoader.get().loadClass(className));
+            } catch (ClassNotFoundException ex) {
+                return Optional.absent();
+            }
+        }
+    }
+}

Modified: incubator/onami/trunk/autobind/integrations/enterprise/src/main/java/org/apache/onami/autobind/enterprise/BeansXMLModule.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/autobind/integrations/enterprise/src/main/java/org/apache/onami/autobind/enterprise/BeansXMLModule.java?rev=1449012&r1=1449011&r2=1449012&view=diff
==============================================================================
--- incubator/onami/trunk/autobind/integrations/enterprise/src/main/java/org/apache/onami/autobind/enterprise/BeansXMLModule.java
(original)
+++ incubator/onami/trunk/autobind/integrations/enterprise/src/main/java/org/apache/onami/autobind/enterprise/BeansXMLModule.java
Fri Feb 22 12:08:37 2013
@@ -40,6 +40,7 @@ import org.apache.onami.autobind.scanner
 
 import com.google.inject.Binder;
 import com.google.inject.Module;
+import org.apache.onami.autobind.utils.ClassLoadingUtils;
 
 
 @GuiceModule(stage=BindingStage.INTERNAL)
@@ -80,7 +81,7 @@ public class BeansXMLModule implements M
 				List<String> classes = alternatives.getClasses();
 				for(String className : classes){
 					try {
-						Class<Object> clazz = (Class<Object>) Class.forName(className);
+						Class<Object> clazz = (Class<Object>) ClassLoadingUtils.loadClass(className);
 						Annotation[] annotations = clazz.getAnnotations();
 						Map<String, Annotation> map = new HashMap<String, Annotation>();
 						for(Annotation annotation : annotations){
@@ -98,7 +99,7 @@ public class BeansXMLModule implements M
 				classes = interceptors.getClasses();
 				for(String className : classes){
 					try {
-						Class<Object> clazz = (Class<Object>) Class.forName(className);
+						Class<Object> clazz = (Class<Object>) ClassLoadingUtils.loadClass(className);
 						Annotation[] annotations = clazz.getAnnotations();
 						Map<String, Annotation> map = new HashMap<String, Annotation>();
 						for(Annotation annotation : annotations){



Mime
View raw message