Author: alexsmirnov
Date: 2010-06-15 16:07:22 -0400 (Tue, 15 Jun 2010)
New Revision: 17627
Added:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binder.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BinderImpl.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binders.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicProvider.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Initializable.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorConfig.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceException.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceLoader.java
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/ServiceLoaderTest.java
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestServiceImpl.java
root/core/branches/jsr-330/jsr330-impl/src/test/resources/
root/core/branches/jsr-330/jsr330-impl/src/test/resources/META-INF/
root/core/branches/jsr-330/jsr330-impl/src/test/resources/META-INF/services/
root/core/branches/jsr-330/jsr330-impl/src/test/resources/META-INF/services/java.util.List
Removed:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binding.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicBinding.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ProviderWrapper.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/StaticBinding.java
Modified:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BindingModule.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyException.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyInjector.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorImpl.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ObjectFactory.java
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Target.java
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/InjectorTest.java
Log:
Finish implementation and services loader.
Added:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binder.java
===================================================================
--- root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binder.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binder.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,52 @@
+/*
+ * $Id$
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.jsr330;
+
+import javax.inject.Provider;
+
+/**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public interface Binder<T> {
+
+
+ public Binder<T> to(Class<? extends T> implementation);
+
+ public Binder<T> toInstance(T value);
+
+ public Binder<T> toProvider(Class<? extends Provider<T>>
provaderImplementation);
+
+ public Binder<T> toProviderInstance(Provider<T> provider);
+
+ public Binder<T> toService();
+
+ public Binder<T> toService(Class<? extends T> defaultImplementation);
+
+ public Binder<T> asSingleton();
+
+}
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binder.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BinderImpl.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BinderImpl.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BinderImpl.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,213 @@
+/*
+ * $Id$
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.jsr330;
+
+import java.util.Collection;
+
+import javax.inject.Provider;
+
+/**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ * @param <T>
+ */
+public final class BinderImpl<T> implements Binder<T> {
+
+ private final Class<T> target;
+
+ private volatile T value;
+
+ private Provider<T> provider;
+
+ private Provider<Provider<T>> providerOfProvider;
+
+ private boolean singleton;
+
+ private volatile BinderImpl<Provider<T>> providerBinder;
+
+ private boolean initialized = false;
+
+ private boolean providerSingleton;
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param target the target to set
+ */
+ public BinderImpl(Class<T> target) {
+ this.target = target;
+ }
+
+ public T get() {
+ checkInitialized();
+ if (null != value) {
+ return value;
+ } else {
+ return getProvider().get();
+ }
+ }
+
+ public BinderImpl<Provider<T>> asProviderBinder() {
+ checkInitialized();
+ if (null == providerBinder) {
+ createProviderBinding();
+ }
+ return providerBinder;
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.jsr330.Binder#to(java.lang.Class)
+ */
+ public Binder<T> to(Class<? extends T> implementation) {
+ this.provider = new DynamicProvider<T>(implementation);
+ return this;
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.jsr330.Binder#toInstance(java.lang.Object)
+ */
+ public Binder<T> toInstance(T value) {
+ checkNotInitialized();
+ this.value = value;
+ this.provider = new Provider<T>() {
+
+ public T get() {
+ return BinderImpl.this.value;
+ }
+ };
+ return this;
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.jsr330.Binder#toProvider(java.lang.Class)
+ */
+ @SuppressWarnings("unchecked")
+ public Binder<T> toProvider(Class<? extends Provider<T>>
providerImplementation) {
+ checkNotInitialized();
+ this.providerOfProvider = new DynamicProvider(providerImplementation);
+ return this;
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.jsr330.Binder#toProviderInstance(javax.inject.Provider)
+ */
+ public Binder<T> toProviderInstance(Provider<T> provider) {
+ this.provider = provider;
+ return this;
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.jsr330.Binder#asSingleton()
+ */
+ public Binder<T> asSingleton() {
+ this.singleton = true;
+ return this;
+ }
+
+ private void checkInitialized() {
+ if(!initialized){
+ throw new DependencyException("Dependency injection implementation has
not been initialized");
+ }
+ }
+
+ private void checkNotInitialized() {
+ if(initialized){
+ throw new DependencyException("Dependency injection implementation has
already been initialized");
+ }
+ }
+
+ Provider<T> getProvider() {
+ return null == this.provider ? this.providerOfProvider.get() : this.provider;
+ }
+
+ void init(Binders injector) throws DependencyException {
+ checkNotInitialized();
+ if (null == value && null == provider && null ==
providerOfProvider) {
+ throw new DependencyException("binding not has not been set");
+ }
+ if (null != provider && provider instanceof Initializable) {
+ this.singleton |=((Initializable) provider).init(injector);
+ }
+ if (null != providerOfProvider && providerOfProvider instanceof
Initializable) {
+ this.providerSingleton |=((Initializable)
providerOfProvider).init(injector);
+ }
+ this.initialized = true;
+ }
+
+ void createInstances(Binders injector) throws DependencyException {
+ checkInitialized();
+ if(providerSingleton && null == provider){
+ this.provider = providerOfProvider.get();
+ }
+ if(singleton && null == value ){
+ this.value= getProvider().get();
+ }
+ createProviderBinding();
+ }
+
+ @SuppressWarnings("unchecked")
+ private void createProviderBinding() {
+ BinderImpl<Provider<T>> providerBinder = new
BinderImpl(Provider.class);
+ if (null != provider) {
+ providerBinder.toInstance(provider);
+ } else if(null != providerOfProvider){
+ providerBinder.toProviderInstance(providerOfProvider);
+ }
+ providerBinder.initialized=true;
+ this.providerBinder = providerBinder;
+ }
+
+ public Binder<T> toService() {
+ Collection<Class<? extends T>> service = loadService();
+ if(service.size()>0){
+ this.to(service.iterator().next());
+ } else {
+ throw new DependencyException("No implementation found for service
"+target.getName());
+ }
+ return this;
+ }
+
+ private Collection<Class<? extends T>> loadService() {
+ if(null == target){
+ throw new DependencyException("Binder does not configured
correctly");
+ }
+ try {
+ return ServiceLoader.loadServiceClasses(target);
+ } catch (ServiceException e) {
+ throw new DependencyException("Error loading service ",e);
+ }
+ }
+
+ public Binder<T> toService(Class<? extends T> defaultImplementation) {
+ Collection<Class<? extends T>> service = loadService();
+ if(service.size()>0){
+ this.to(service.iterator().next());
+ } else {
+ this.to(defaultImplementation);
+ }
+ return this;
+ }
+}
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BinderImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binders.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binders.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binders.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,12 @@
+package org.richfaces.jsr330;
+
+/**
+ * <p class="changed_added_4_0"></p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public interface Binders {
+
+ public BinderImpl<?> getBinding(Target type);
+
+}
\ No newline at end of file
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binders.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binding.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binding.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binding.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -1,13 +0,0 @@
-package org.richfaces.jsr330;
-
-import javax.inject.Provider;
-
-public interface Binding extends Provider {
-
- public Object get();
-
- void init(InjectorImpl injectorImpl);
-
- public abstract Binding getProvider();
-
-}
Modified:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BindingModule.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BindingModule.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BindingModule.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -1,7 +1,12 @@
package org.richfaces.jsr330;
+/**
+ * <p class="changed_added_4_0">User-provided configuration
module.</p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
public interface BindingModule {
- public void configure(InjectorImpl injector);
+ public void configure(InjectorConfig injector);
}
Modified:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyException.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyException.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyException.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -24,17 +24,18 @@
package org.richfaces.jsr330;
/**
- * <p class="changed_added_4_0"></p>
+ * <p class="changed_added_4_0">Dependency injection errors.</p>
* @author asmirnov(a)exadel.com
*
*/
+@SuppressWarnings("serial")
public class DependencyException extends RuntimeException {
/**
* <p class="changed_added_4_0"></p>
*/
public DependencyException() {
- // TODO Auto-generated constructor stub
+
}
/**
@@ -43,7 +44,6 @@
*/
public DependencyException(String message) {
super(message);
- // TODO Auto-generated constructor stub
}
/**
@@ -52,7 +52,6 @@
*/
public DependencyException(Throwable cause) {
super(cause);
- // TODO Auto-generated constructor stub
}
/**
@@ -62,7 +61,6 @@
*/
public DependencyException(String message, Throwable cause) {
super(message, cause);
- // TODO Auto-generated constructor stub
}
}
Modified:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyInjector.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyInjector.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyInjector.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -34,4 +34,6 @@
public <T> T create(Class<T> type,String name);
+ public void inject(Object value);
+
}
Deleted:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicBinding.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicBinding.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicBinding.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -1,190 +0,0 @@
-package org.richfaces.jsr330;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.inject.Inject;
-import javax.inject.Provider;
-import javax.inject.Singleton;
-
-public class DynamicBinding implements Binding, Provider {
-
- private final Class<?> type;
-
- private Constructor<?> constructor;
-
- private List<Binding> constructorArguments;
-
- private Map<Field, Binding> injectedFields;
-
- private Map<Method, List<Binding>> injectedMethods;
-
- private boolean singleton;
-
- private volatile Object value;
-
- public DynamicBinding(Class<?> type) {
- this.type = type;
- }
-
- void findInjectorMethods(InjectorImpl injectorImpl) throws Exception {
- injectedMethods = new HashMap<Method, List<Binding>>();
- Method[] methods = this.type.getMethods();
- for (Method method : methods) {
- if (method.isAnnotationPresent(Inject.class)) {
- Type[] parameterTypes = method.getGenericParameterTypes();
- Annotation[][] parameterAnnotations = method.getParameterAnnotations();
- injectedMethods.put(method, getParameterBindings(injectorImpl,
parameterTypes, parameterAnnotations));
- }
- }
- }
-
- void findInjectedFields(InjectorImpl injectorImpl) throws Exception {
- Field[] declaredFields = this.type.getDeclaredFields();
- this.injectedFields = new HashMap<Field, Binding>();
- for (Field field : declaredFields) {
- if (field.isAnnotationPresent(Inject.class)) {
- Type fieldType = field.getGenericType();
- if (fieldType instanceof Class) {
- Class<?> fieldClass = (Class<?>) fieldType;
- this.injectedFields.put(field,
injectorImpl.getBinding(Target.create(fieldClass, field
- .getAnnotations())));
- }
- }
- }
- }
-
- void findConstructor(InjectorImpl injectorImpl) throws Exception {
- Constructor<?>[] constructors = this.type.getConstructors();
- for (Constructor<?> constructor : constructors) {
- if (constructor.isAnnotationPresent(Inject.class)) {
- if (null != this.constructor) {
- throw new DependencyException("More then one constructor have
@Inject annotation " + this.type);
- }
- this.constructor = constructor;
- Type[] parameterTypes = constructor.getGenericParameterTypes();
- Annotation[][] parameterAnnotations =
constructor.getParameterAnnotations();
- this.constructorArguments = getParameterBindings(injectorImpl,
parameterTypes, parameterAnnotations);
- }
- }
- if (null == constructor) {
- constructor = (Constructor<?>) this.type.getConstructor();
- this.constructorArguments = Collections.emptyList();
- }
- }
-
- List<Binding> getParameterBindings(InjectorImpl injectorImpl, Type[]
parameterTypes,
- Annotation[][] parameterAnnotations) {
- List<Binding> arguments = new
ArrayList<Binding>(parameterTypes.length);
- int i = 0;
- for (Type parameterType : parameterTypes) {
- arguments.add(injectorImpl.getBinding(Target.create(parameterType,
parameterAnnotations[i])));
- i++;
- }
- return arguments;
- }
-
- public Object get() {
- if (isSingleton()) {
- if (null == value) {
- synchronized (this) {
- value = createInstance();
- }
- }
- return value;
- } else {
- Object instance = createInstance();
- return instance;
- }
- }
-
- public Binding getProvider() {
- return new StaticBinding(this);
- }
-
- void invokeInjectionMethods(Object instance) throws Exception {
- for (Method method : injectedMethods.keySet()) {
- method.invoke(instance, getArgumentValues(injectedMethods.get(method)));
- }
-
- }
-
- void injectFields(Object instance) throws Exception {
- for (Field field : injectedFields.keySet()) {
- boolean accessible = field.isAccessible();
- if (!accessible) {
- field.setAccessible(true);
- }
- field.set(instance, injectedFields.get(field).get());
- if (!accessible) {
- field.setAccessible(accessible);
- }
- }
- }
-
- Object createInstance() {
- try {
- Object[] arguments = getArgumentValues(this.constructorArguments);
- Object instance = constructor.newInstance(arguments);
- injectFields(instance);
- invokeInjectionMethods(instance);
- return instance;
- } catch (Exception e) {
- throw new DependencyException(e);
- }
- }
-
- Object[] getArgumentValues(List<Binding> argumentBinding) {
- Object[] arguments = new Object[argumentBinding.size()];
- int i = 0;
- for (Binding provider : argumentBinding) {
- arguments[i++] = provider.get();
- }
- return arguments;
- }
-
- public void init(InjectorImpl injectorImpl) {
- try {
- findConstructor(injectorImpl);
- findInjectedFields(injectorImpl);
- findInjectorMethods(injectorImpl);
- if (type.isAnnotationPresent(Singleton.class)) {
- this.singleton = true;
- }
- } catch (Exception e) {
- throw new DependencyException(e);
- }
- // TODO Auto-generated method stub
-
- }
-
- /**
- * <p class="changed_added_4_0">
- * </p>
- *
- * @return the singleton
- */
- public boolean isSingleton() {
- return this.singleton;
- }
-
- /**
- * <p class="changed_added_4_0">
- * </p>
- *
- * @param singleton
- * the singleton to set
- */
- public void setSingleton(boolean singleton) {
- this.singleton = singleton;
- }
-
-}
Copied:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicProvider.java
(from rev 17623,
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicBinding.java)
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicProvider.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicProvider.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,158 @@
+package org.richfaces.jsr330;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.inject.Inject;
+import javax.inject.Provider;
+import javax.inject.Singleton;
+
+/**
+ * <p class="changed_added_4_0">Dynamicaly </p>
+ * @author asmirnov(a)exadel.com
+ *
+ * @param <T>
+ */
+public class DynamicProvider<T> implements Provider<T>, Initializable {
+
+ private final Class<? extends T> type;
+
+ private Constructor<T> constructor;
+
+ private List<BinderImpl<?>> constructorArguments;
+
+ private Map<Field, BinderImpl<?>> injectedFields;
+
+ private Map<Method, List<BinderImpl<?>>> injectedMethods;
+
+ public DynamicProvider(Class<? extends T> type) {
+ this.type = type;
+ }
+
+ void findInjectorMethods(Binders injectorImpl) throws Exception {
+ injectedMethods = new HashMap<Method, List<BinderImpl<?>>>();
+ Method[] methods = this.type.getMethods();
+ for (Method method : methods) {
+ if (method.isAnnotationPresent(Inject.class)) {
+ Type[] parameterTypes = method.getGenericParameterTypes();
+ Annotation[][] parameterAnnotations = method.getParameterAnnotations();
+ injectedMethods.put(method, getParameterBindings(injectorImpl,
parameterTypes, parameterAnnotations));
+ }
+ }
+ }
+
+ void findInjectedFields(Binders injectorImpl) throws Exception {
+ Field[] declaredFields = this.type.getDeclaredFields();
+ this.injectedFields = new HashMap<Field, BinderImpl<?>>();
+ for (Field field : declaredFields) {
+ if (field.isAnnotationPresent(Inject.class)) {
+ Type fieldType = field.getGenericType();
+ if (fieldType instanceof Class<?>) {
+ Class<?> fieldClass = (Class<?>) fieldType;
+ this.injectedFields.put(field,
injectorImpl.getBinding(Target.createNamed(fieldClass, field
+ .getAnnotations())));
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ void findConstructor(Binders injectorImpl) throws Exception {
+ Constructor<T>[] constructors = this.type.getConstructors();
+ for (Constructor<T> constructor : constructors) {
+ if (constructor.isAnnotationPresent(Inject.class)) {
+ if (null != this.constructor) {
+ throw new DependencyException("More then one constructor have
@Inject annotation " + this.type);
+ }
+ this.constructor = constructor;
+ Type[] parameterTypes = constructor.getGenericParameterTypes();
+ Annotation[][] parameterAnnotations =
constructor.getParameterAnnotations();
+ this.constructorArguments = getParameterBindings(injectorImpl,
parameterTypes, parameterAnnotations);
+ }
+ }
+ if (null == constructor) {
+ constructor = (Constructor<T>) this.type.getConstructor();
+ this.constructorArguments = Collections.emptyList();
+ }
+ }
+
+ List<BinderImpl<?>> getParameterBindings(Binders injectorImpl, Type[]
parameterTypes,
+ Annotation[][] parameterAnnotations) {
+ List<BinderImpl<?>> arguments = new
ArrayList<BinderImpl<?>>(parameterTypes.length);
+ int i = 0;
+ for (Type parameterType : parameterTypes) {
+ arguments.add(injectorImpl.getBinding(Target.createNamed(parameterType,
parameterAnnotations[i])));
+ i++;
+ }
+ return arguments;
+ }
+
+
+ void invokeInjectionMethods(Object instance) throws Exception {
+ for (Method method : injectedMethods.keySet()) {
+ method.invoke(instance, getArgumentValues(injectedMethods.get(method)));
+ }
+
+ }
+
+ void injectFields(Object instance) throws Exception {
+ for (Field field : injectedFields.keySet()) {
+ boolean accessible = field.isAccessible();
+ if (!accessible) {
+ field.setAccessible(true);
+ }
+ field.set(instance, injectedFields.get(field).get());
+ if (!accessible) {
+ field.setAccessible(accessible);
+ }
+ }
+ }
+
+ public T get() {
+ try {
+ Object[] arguments = getArgumentValues(this.constructorArguments);
+ T instance = constructor.newInstance(arguments);
+ inject(instance);
+ return instance;
+ } catch (Exception e) {
+ throw new DependencyException(e);
+ }
+ }
+
+ void inject(T instance) throws Exception {
+ injectFields(instance);
+ invokeInjectionMethods(instance);
+ }
+
+ Object[] getArgumentValues(List<BinderImpl<?>> argumentBinding) {
+ Object[] arguments = new Object[argumentBinding.size()];
+ int i = 0;
+ for (BinderImpl<?> provider : argumentBinding) {
+ arguments[i++] = provider.get();
+ }
+ return arguments;
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.jsr330.Initializable#init(org.richfaces.jsr330.Binders)
+ */
+ public boolean init(Binders injectorImpl) {
+ try {
+ findConstructor(injectorImpl);
+ findInjectedFields(injectorImpl);
+ findInjectorMethods(injectorImpl);
+ return type.isAnnotationPresent(Singleton.class);
+ } catch (Exception e) {
+ throw new DependencyException(e);
+ }
+ }
+
+}
Added:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Initializable.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Initializable.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Initializable.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,12 @@
+package org.richfaces.jsr330;
+
+/**
+ * <p class="changed_added_4_0">Classes that require initialization
should implement this interface</p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public interface Initializable {
+
+ public boolean init(Binders injectorImpl);
+
+}
\ No newline at end of file
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Initializable.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorConfig.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorConfig.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorConfig.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,19 @@
+package org.richfaces.jsr330;
+
+/**
+ * <p class="changed_added_4_0">
+ * Implementation provides instance of this interface to let user register services and
its implementations.
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public interface InjectorConfig {
+
+ public <T> Binder<T> register(Target type);
+
+ public <T> Binder<T> register(Class<T> type, Class<?>...
typeArguments);
+
+ public <T> Binder<T> register(String name, Class<T> type,
Class<?>... typeArguments);
+
+}
\ No newline at end of file
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorConfig.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorImpl.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorImpl.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorImpl.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -7,93 +7,122 @@
import javax.inject.Provider;
-public class InjectorImpl implements DependencyInjector {
+/**
+ * <p class="changed_added_4_0">
+ * Lightweight implementation for JSR-330 dependency injection. Supports only {@link
javax.inject.Named} annotations, no
+ * scopes except {@link javax.inject.Singleton}
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public class InjectorImpl implements DependencyInjector, Binders, InjectorConfig {
+ private final Map<Target, BinderImpl<?>> bindings = new
ConcurrentHashMap<Target, BinderImpl<?>>();
- private final Map<Target,Binding> bindings = new ConcurrentHashMap<Target,
Binding>();
-
@SuppressWarnings("unchecked")
- public <T> T create(Class<T> type,String name) {
- Target target = Target.create(type,name);
+ public <T> T create(Class<T> type, String name) {
+ Target target = Target.create(name, type);
return (T) getBinding(target).get();
}
-
+
public <T> T create(Class<T> type) {
return create(type, null);
}
- public void init(BindingModule ... modules){
+ public void init(BindingModule... modules) {
+ register(DependencyInjector.class).toInstance(this);
for (BindingModule bindingModule : modules) {
bindingModule.configure(this);
}
initProviders();
+ initSingletons();
}
- public void initSingletons() {
- Collection<Binding> values = new
ArrayList<Binding>(bindings.values());
- for (Binding binding : values) {
- if (binding instanceof DynamicBinding) {
- DynamicBinding dynamicBinding = (DynamicBinding) binding;
- if(dynamicBinding.isSingleton()){
- dynamicBinding.get();
- }
- }
+ private void initSingletons() {
+ Collection<BinderImpl<?>> values = new
ArrayList<BinderImpl<?>>(bindings.values());
+ for (BinderImpl<?> binding : values) {
+ binding.createInstances(this);
}
}
-
+
private void initProviders() {
- Collection<Binding> values = new
ArrayList<Binding>(bindings.values());
- for (Binding binding : values) {
+ Collection<BinderImpl<?>> values = new
ArrayList<BinderImpl<?>>(bindings.values());
+ for (BinderImpl<?> binding : values) {
binding.init(this);
}
-
+
}
- public Binding getBinding(Target type) {
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.jsr330.Binders#getBinding(org.richfaces.jsr330.Target)
+ */
+ @SuppressWarnings("unchecked")
+ public BinderImpl<?> getBinding(Target type) {
if (bindings.containsKey(type)) {
return bindings.get(type);
- } else if(type.isConcrete()){
+ } else if (type.isConcrete()) {
// Concrete classes can be created without configuration.
- Binding binding = registerImplementation(type, type.getRawType());
+ BinderImpl binding = new BinderImpl(type.getRawType());
+ binding.to(type.getRawType());
binding.init(this);
+ binding.createInstances(this);
+ registerBinding(type, binding);
return binding;
- } else if(type.isProvider()){
- return getBinding(type.toProviderTarget()).getProvider();
+ } else if (type.isProvider()) {
+ return getBinding(type.toProviderTarget()).asProviderBinder();
} else {
- throw new DependencyException("Type "+type+" has not been
registered");
+ throw new DependencyException("Type " + type + " has not been
registered");
}
}
- public Binding registerProviderInstance(Target type, Provider<?> provider){
- StaticBinding binding = new StaticBinding(provider);
- registerProviderBinding(type, binding);
- return binding;
+ @SuppressWarnings("unchecked")
+ public void inject(Object value) {
+ Target target = Target.create(value.getClass());
+ Provider<?> provider = getBinding(target).getProvider();
+ if (provider instanceof DynamicProvider) {
+ try {
+ ((DynamicProvider) provider).inject(value);
+ } catch (Exception e) {
+ throw new DependencyException("Cannot inject dependencies into
object " + value, e);
+ }
+ }
+
}
- public Binding registerProviderImplementation(Target type, Class<? extends
Provider<?>> provider){
- DynamicBinding binding = new DynamicBinding(provider);
- registerProviderBinding(type, binding);
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.jsr330.InjectorConfig#register(org.richfaces.jsr330.Target)
+ */
+ @SuppressWarnings("unchecked")
+ public <T> Binder<T> register(Target type) {
+ BinderImpl<T> binding = new BinderImpl(type.getRawType());
+ registerBinding(type, binding);
return binding;
}
- private void registerProviderBinding(Target type, Binding binding) {
- Binding wrapper = new ProviderWrapper(binding);
- bindings.put(type, wrapper);
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.jsr330.InjectorConfig#register(java.lang.Class,
java.lang.Class)
+ */
+ public <T> Binder<T> register(Class<T> type, Class<?>...
typeArguments) {
+ return register(Target.create(type, typeArguments));
}
- public Binding registerImplementation(Target type, Class<?> implementation){
- DynamicBinding provider = new DynamicBinding(implementation);
- registerImplementationBinding(type, provider);
- return provider;
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.jsr330.InjectorConfig#register(java.lang.String,
java.lang.Class, java.lang.Class)
+ */
+ public <T> Binder<T> register(String name, Class<T> type,
Class<?>... typeArguments) {
+ return register(Target.create(name, type, typeArguments));
}
- public Binding registerInstance(Target type, Object instance){
- StaticBinding provider = new StaticBinding(instance);
- registerImplementationBinding(type, provider);
- return provider;
+ private void registerBinding(Target type, BinderImpl<?> binder) {
+ bindings.put(type, binder);
}
-
- private void registerImplementationBinding(Target type, Binding provider) {
- bindings.put(type, provider);
- }
}
Modified:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ObjectFactory.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ObjectFactory.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ObjectFactory.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -26,6 +26,9 @@
return getInjector().create(target);
}
+ public static void inject(Object value){
+ getInjector().inject(value);
+ }
private static DependencyInjector getInjector() {
return INSTANCES.get(getCurrentLoader());
}
Deleted:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ProviderWrapper.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ProviderWrapper.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ProviderWrapper.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -1,62 +0,0 @@
-/*
- * $Id$
- *
- * License Agreement.
- *
- * Rich Faces - Natural Ajax for Java Server Faces (JSF)
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.richfaces.jsr330;
-
-import javax.inject.Provider;
-
-/**
- * <p class="changed_added_4_0"></p>
- * @author asmirnov(a)exadel.com
- *
- */
-public class ProviderWrapper implements Binding {
-
- private final Binding binding;
-
- public ProviderWrapper(Binding binding) {
- this.binding = binding;
- }
-
- /* (non-Javadoc)
- * @see org.richfaces.jsr330.Binding#get()
- */
- public Object get() {
- return ((Provider) getProvider().get()).get();
- }
-
- /* (non-Javadoc)
- * @see org.richfaces.jsr330.Binding#getProvider()
- */
- public Binding getProvider() {
- return binding;
- }
-
- /* (non-Javadoc)
- * @see org.richfaces.jsr330.Binding#init(org.richfaces.jsr330.InjectorImpl)
- */
- public void init(InjectorImpl injectorImpl) {
- this.binding.init(injectorImpl);
- }
-
-}
Added:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceException.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceException.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceException.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,65 @@
+/*
+ * $Id$
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.jsr330;
+
+/**
+ * <p class="changed_added_4_0"></p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+@SuppressWarnings("serial")
+public class ServiceException extends Exception {
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ */
+ public ServiceException() {
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param message
+ */
+ public ServiceException(String message) {
+ super(message);
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param cause
+ */
+ public ServiceException(Throwable cause) {
+ super(cause);
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param message
+ * @param cause
+ */
+ public ServiceException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceException.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceLoader.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceLoader.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceLoader.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,195 @@
+/*
+ * $Id$
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.jsr330;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+/**
+ * <p class="changed_added_4_0">
+ * This class loads services from files placed to the META-INF/services in classpath.
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public final class ServiceLoader {
+
+ private static final String META_INF_SERVICES = "META-INF/services/";
+ private static final Pattern LEGAL_JAVA_NAME =
Pattern.compile("^(([A-Za-z0-9_])+\\.)+[A-Z]([A-Za-z0-9_]*)$");
+
+ private ServiceLoader() {
+
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * Load and instantiate all service implementations.
+ * </p>
+ *
+ * @param <S>
+ * @param serviceClass
+ * @return
+ * @throws ServiceException
+ */
+ public static <S> Collection<S> loadServices(Class<S> serviceClass)
throws ServiceException {
+ Collection<Class<? extends S>> serviceClasses =
loadServiceClasses(serviceClass);
+ List<S> instances = new ArrayList<S>();
+ try {
+ for (Class<? extends S> implementationClass : serviceClasses) {
+ instances.add(implementationClass.newInstance());
+ }
+
+ } catch (InstantiationException e) {
+ throw new ServiceException("Cannot instantiate service class, does it
have default constructor ?", e);
+ } catch (IllegalAccessException e) {
+ throw new ServiceException("Cannot instantiate service class, illegal
access", e);
+ }
+ return instances;
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * Load service implementation classes.
+ * </p>
+ *
+ * @param <S>
+ * @param serviceClass
+ * @return
+ * @throws ServiceException
+ */
+ public static <S> Collection<Class<? extends S>>
loadServiceClasses(Class<S> serviceClass) throws ServiceException {
+ ClassLoader classLoader = getClassLoader(serviceClass);
+ Set<String> names = new HashSet<String>();
+ Enumeration<URL> resources;
+ try {
+ resources = classLoader.getResources(META_INF_SERVICES +
serviceClass.getName());
+ while (resources.hasMoreElements()) {
+ names.addAll(parse(resources.nextElement()));
+ }
+ } catch (IOException e) {
+ throw new ServiceException("Error load service descriptions", e);
+ }
+ List<Class<? extends S>> instanceClasses = new
ArrayList<Class<? extends S>>();
+ for (String className : names) {
+ instanceClasses.add(loadClass(serviceClass, classLoader, className));
+ }
+ return instanceClasses;
+
+ }
+
+ static Collection<String> parse(URL url) throws ServiceException, IOException
{
+ InputStream inputStream = null;
+ try {
+ URLConnection connection = url.openConnection();
+ try {
+ connection.setUseCaches(false);
+ } catch (IllegalArgumentException e) {
+ // Do nothing.
+ }
+ Set<String> names = new HashSet<String>();
+ inputStream = connection.getInputStream();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,
"utf-8"));
+ String line;
+ while (null != (line = reader.readLine())) {
+ parseLine(line, names);
+ }
+ return names;
+ } finally {
+ if (null != inputStream) {
+ inputStream.close();
+ }
+ }
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * Parse a single line from service description. Skips empty lines and comments
started with #
+ * </p>
+ *
+ * @param line
+ * @param names
+ * @throws ServiceException
+ */
+ static void parseLine(String line, Collection<String> names) throws
ServiceException {
+ String name;
+ int commentIndex = line.indexOf('#');
+ if (commentIndex >= 0) {
+ name = line.substring(0, commentIndex);
+ } else {
+ name = line;
+ }
+ name = name.trim();
+ if (name.length() > 0) {
+ if (LEGAL_JAVA_NAME.matcher(name).matches()) {
+ names.add(name);
+ } else {
+ throw new ServiceException("Invalid java class name [" + line +
"]");
+ }
+ }
+ }
+
+ /**
+ * <p class="changed_added_4_0">
+ * Get class loader
+ * </p>
+ *
+ * @param <S>
+ * @param serviceClass
+ * @return context class loader or loader with which service class has been loaded.
+ */
+ private static <S> ClassLoader getClassLoader(Class<S> serviceClass) {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ if (null == classLoader) {
+ classLoader = serviceClass.getClassLoader();
+ }
+ return classLoader;
+ }
+
+ private static <S> Class<? extends S> loadClass(Class<S>
serviceClass, ClassLoader classLoader, String className)
+ throws ServiceException {
+ try {
+ Class<?> implementationClass = classLoader.loadClass(className);
+ if (serviceClass.isAssignableFrom(implementationClass)) {
+ return implementationClass.asSubclass(serviceClass);
+ } else {
+ throw new ServiceException("Class " + className + " in not
the instance of " + serviceClass.getName());
+ }
+ } catch (ClassNotFoundException e) {
+ throw new ServiceException("Class " + className + " not
found", e);
+ }
+ }
+
+}
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ServiceLoader.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/StaticBinding.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/StaticBinding.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/StaticBinding.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -1,52 +0,0 @@
-/*
- * $Id$
- *
- * License Agreement.
- *
- * Rich Faces - Natural Ajax for Java Server Faces (JSF)
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.richfaces.jsr330;
-
-
-/**
- * <p class="changed_added_4_0"></p>
- * @author asmirnov(a)exadel.com
- *
- */
-public class StaticBinding implements Binding {
-
- private final Object value;
-
- public StaticBinding(Object value) {
- this.value = value;
- }
-
- public Object get() {
- return value;
- }
-
- public void init(InjectorImpl injectorImpl) {
- // do nothing
- }
-
- public Binding getProvider() {
- return new StaticBinding(this);
- }
-
-}
Modified:
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Target.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Target.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Target.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -52,25 +52,31 @@
private Type[] actualTypeArguments;
public Target(Type type) {
- this(type,null);
+ this(null,type);
}
- public Target(Type type, String name) {
+ public Target(String name, Type type) {
this.type = type;
this.name = name;
this.rawType = getRawClass(type);
this.actualTypeArguments = getActualTypeArguments(type);
}
- public static Target create(Class<?> type, String name) {
- return new Target(type, name);
+ public static Target create(Class<?> type, Class<?> ...typeArguments) {
+ return create(null, type, typeArguments);
}
- public static Target create(Type type, Annotation... annotations) {
+ public static Target create(String name, Class<?> type, Class<?>
...typeArguments) {
+ Target target = new Target(name, type);
+ target.actualTypeArguments = typeArguments;
+ return target;
+ }
+
+ public static Target createNamed(Type type, Annotation... annotations) {
if (null != annotations) {
for (Annotation annotation : annotations) {
if (annotation instanceof Named) {
- return new Target(type, ((Named) annotation).value());
+ return new Target(((Named) annotation).value(), type);
}
}
@@ -79,7 +85,7 @@
}
public Target toProvider() {
- Target target = new Target(type, name);
+ Target target = new Target(name, type);
target.rawType = Provider.class;
target.actualTypeArguments = new Type[]{this.type};
return target;
@@ -87,7 +93,7 @@
public Target toProviderTarget(){
if(isProvider()){
- return new Target(this.actualTypeArguments[0],name);
+ return new Target(name,this.actualTypeArguments[0]);
} else {
throw new DependencyException("Type "+this+" not is a Provider
type");
}
Modified:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/InjectorTest.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/InjectorTest.java 2010-06-15
15:50:35 UTC (rev 17626)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/InjectorTest.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -12,10 +12,10 @@
InjectorImpl injector = new InjectorImpl();
injector.init(new BindingModule(){
- public void configure(InjectorImpl injector) {
- injector.registerImplementation(Target.create(TestInterface.class),
TestConstructorInjection.class);
- injector.registerInstance(Target.create(String.class, "foo"),
"bar");
- injector.registerInstance(Target.create(String.class, "bar"),
"baz");
+ public void configure(InjectorConfig injector) {
+
injector.register(TestInterface.class).to(TestConstructorInjection.class);
+ injector.register("foo",
String.class).toInstance("bar");
+ injector.register("bar",
String.class).toInstance("baz");
}
});
@@ -29,10 +29,10 @@
InjectorImpl injector = new InjectorImpl();
injector.init(new BindingModule(){
- public void configure(InjectorImpl injector) {
- injector.registerImplementation(Target.create(TestInterface.class),
TestFieldInjection.class);
- injector.registerInstance(Target.create(String.class, "foo"),
"bar");
- injector.registerInstance(Target.create(String.class, "bar"),
"baz");
+ public void configure(InjectorConfig injector) {
+ injector.register(TestInterface.class).to(TestFieldInjection.class);
+ injector.register("foo",
String.class).toInstance("bar");
+ injector.register("bar",
String.class).toInstance("baz");
}
});
@@ -46,10 +46,10 @@
InjectorImpl injector = new InjectorImpl();
injector.init(new BindingModule(){
- public void configure(InjectorImpl injector) {
- injector.registerImplementation(Target.create(TestInterface.class),
TestMethodInjection.class);
- injector.registerInstance(Target.create(String.class, "foo"),
"bar");
- injector.registerInstance(Target.create(String.class, "bar"),
"baz");
+ public void configure(InjectorConfig injector) {
+ injector.register(TestInterface.class).to(TestMethodInjection.class);
+ injector.register("foo",
String.class).toInstance("bar");
+ injector.register("bar",
String.class).toInstance("baz");
}
});
@@ -63,10 +63,10 @@
InjectorImpl injector = new InjectorImpl();
injector.init(new BindingModule(){
- public void configure(InjectorImpl injector) {
- injector.registerImplementation(Target.create(TestInterface.class),
TestProviderInjection.class);
- injector.registerInstance(Target.create(String.class, "foo"),
"bar");
- injector.registerInstance(Target.create(String.class, "bar"),
"baz");
+ public void configure(InjectorConfig injector) {
+ injector.register(TestInterface.class).to(TestProviderInjection.class);
+ injector.register("foo",
String.class).toInstance("bar");
+ injector.register("bar",
String.class).toInstance("baz");
}
});
@@ -80,10 +80,10 @@
InjectorImpl injector = new InjectorImpl();
injector.init(new BindingModule(){
- public void configure(InjectorImpl injector) {
- injector.registerImplementation(Target.create(TestInterface.class),
TestProviderInjection.class);
- injector.registerProviderInstance(Target.create(String.class,
"foo"), new FooProvider());
- injector.registerProviderImplementation(Target.create(String.class,
"bar"), BarProvider.class);
+ public void configure(InjectorConfig injector) {
+ injector.register(TestInterface.class).to(TestProviderInjection.class);
+ injector.register("foo", String.class).toProviderInstance(new
FooProvider());
+ injector.register("bar",
String.class).toProvider(BarProvider.class);
}
});
Added:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/ServiceLoaderTest.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/ServiceLoaderTest.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/ServiceLoaderTest.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,156 @@
+/*
+ * $Id$
+ *
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.jsr330;
+
+import static org.junit.Assert.*;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public class ServiceLoaderTest {
+
+ private static final String JAVA_UTIL_LIST = "java.util.List";
+
+ private ClassLoader currentLoader;
+
+ @Before
+ public void setUp() {
+ currentLoader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+ }
+
+ @After
+ public void thearDown() {
+ Thread.currentThread().setContextClassLoader(currentLoader);
+ }
+
+ /**
+ * Test method for {@link
org.richfaces.jsr330.ServiceLoader#loadServices(java.lang.Class)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testLoadServices() throws Exception {
+ Collection<List> services = ServiceLoader.loadServices(List.class);
+ assertEquals(1, services.size());
+ }
+
+ /**
+ * Test method for {@link
org.richfaces.jsr330.ServiceLoader#loadServiceClasses(java.lang.Class)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testLoadServiceClasses() throws Exception {
+ Collection<Class<? extends List>> serviceClasses =
ServiceLoader.loadServiceClasses(List.class);
+ assertEquals(1, serviceClasses.size());
+ assertEquals(TestServiceImpl.class, serviceClasses.iterator().next());
+ }
+
+ /**
+ * Test method for {@link org.richfaces.jsr330.ServiceLoader#parse(java.net.URL)}.
+ *
+ * @throws Exception
+ * @throws ServiceException
+ */
+ @Test
+ public void testParse() throws Exception {
+ URL url =
this.getClass().getResource("/META-INF/services/java.util.List");
+ Collection<String> collection = ServiceLoader.parse(url);
+ assertEquals(1, collection.size());
+ assertEquals(TestServiceImpl.class.getName(), collection.iterator().next());
+ }
+
+ /**
+ * Test method for {@link
org.richfaces.jsr330.ServiceLoader#parseLine(java.lang.String, java.util.Set)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testParseLine() throws Exception {
+ String line = "\t" + JAVA_UTIL_LIST + " ";
+ ArrayList<String> names = new ArrayList<String>();
+ ServiceLoader.parseLine(line, names);
+
+ assertEquals(1, names.size());
+ assertEquals(JAVA_UTIL_LIST, names.get(0));
+ }
+
+ /**
+ * Test method for {@link
org.richfaces.jsr330.ServiceLoader#parseLine(java.lang.String, java.util.Set)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testParseLine1() throws Exception {
+ String line = TestServiceImpl.class.getName();
+ ArrayList<String> names = new ArrayList<String>();
+ ServiceLoader.parseLine(line, names);
+
+ assertEquals(1, names.size());
+ assertEquals(TestServiceImpl.class.getName(), names.get(0));
+ }
+
+ /**
+ * Test method for {@link
org.richfaces.jsr330.ServiceLoader#parseLine(java.lang.String, java.util.Set)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testParseLineWithComment() throws Exception {
+ String line = JAVA_UTIL_LIST + " # comment";
+ ArrayList<String> names = new ArrayList<String>();
+ ServiceLoader.parseLine(line, names);
+
+ assertEquals(1, names.size());
+ assertEquals(JAVA_UTIL_LIST, names.get(0));
+ }
+
+ /**
+ * Test method for {@link
org.richfaces.jsr330.ServiceLoader#parseLine(java.lang.String, java.util.Set)}.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testParseLineComment() throws Exception {
+ String line = " # comment";
+ ArrayList<String> names = new ArrayList<String>();
+ ServiceLoader.parseLine(line, names);
+
+ assertEquals(0, names.size());
+ }
+}
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/ServiceLoaderTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestServiceImpl.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestServiceImpl.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestServiceImpl.java 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,125 @@
+package org.richfaces.jsr330;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+public class TestServiceImpl implements List<String> {
+
+ public boolean add(String o) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public void add(int index, String element) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public boolean addAll(Collection<? extends String> c) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean addAll(int index, Collection<? extends String> c) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public void clear() {
+ // TODO Auto-generated method stub
+
+ }
+
+ public boolean contains(Object o) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean containsAll(Collection<?> c) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public String get(int index) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int indexOf(Object o) {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public boolean isEmpty() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public Iterator<String> iterator() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int lastIndexOf(Object o) {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public ListIterator<String> listIterator() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ListIterator<String> listIterator(int index) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public boolean remove(Object o) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public String remove(int index) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public boolean removeAll(Collection<?> c) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public boolean retainAll(Collection<?> c) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public String set(int index, String element) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int size() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public List<String> subList(int fromIndex, int toIndex) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Object[] toArray() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public <T> T[] toArray(T[] a) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
Property changes on:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestServiceImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
root/core/branches/jsr-330/jsr330-impl/src/test/resources/META-INF/services/java.util.List
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/resources/META-INF/services/java.util.List
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/resources/META-INF/services/java.util.List 2010-06-15
20:07:22 UTC (rev 17627)
@@ -0,0 +1,3 @@
+# test implementation of List
+org.richfaces.jsr330.TestServiceImpl # comment
+