Author: alexsmirnov
Date: 2010-06-09 20:30:15 -0400 (Wed, 09 Jun 2010)
New Revision: 17590
Added:
root/core/branches/jsr-330/jsr330-impl/
root/core/branches/jsr-330/jsr330-impl/pom.xml
root/core/branches/jsr-330/jsr330-impl/src/
root/core/branches/jsr-330/jsr330-impl/src/main/
root/core/branches/jsr-330/jsr330-impl/src/main/java/
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/
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/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/DynamicBinding.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/ProviderWrapper.java
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/Target.java
root/core/branches/jsr-330/jsr330-impl/src/test/
root/core/branches/jsr-330/jsr330-impl/src/test/java/
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/
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/TestConstructorInjection.java
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestFieldInjection.java
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestInterface.java
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestMethodInjection.java
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestProviderInjection.java
Log:
add lightweight jsr-330 implementation
Property changes on: root/core/branches/jsr-330/jsr330-impl
___________________________________________________________________
Name: svn:ignore
+ target
.settings
.project
.classpath
.clover
Added: root/core/branches/jsr-330/jsr330-impl/pom.xml
===================================================================
--- root/core/branches/jsr-330/jsr330-impl/pom.xml (rev 0)
+++ root/core/branches/jsr-330/jsr330-impl/pom.xml 2010-06-10 00:30:15 UTC (rev 17590)
@@ -0,0 +1,43 @@
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.richfaces</groupId>
+ <artifactId>jsr330-impl</artifactId>
+ <version>4.0.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>jsr330-impl</name>
+ <url>http://maven.apache.org</url>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <!--
http://maven.apache.org/plugins/maven-compiler-plugin/ -->
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.inject</groupId>
+ <artifactId>javax.inject</artifactId>
+ <version>1</version>
+ </dependency>
+ </dependencies>
+</project>
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Binding.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,11 @@
+package org.richfaces.jsr330;
+
+import javax.inject.Provider;
+
+public interface Binding extends Provider {
+
+ public Object get();
+
+ void init(InjectorImpl injectorImpl);
+
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/BindingModule.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,7 @@
+package org.richfaces.jsr330;
+
+public interface BindingModule {
+
+ public void configure(InjectorImpl injector);
+
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyException.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,68 @@
+/*
+ * $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 DependencyException extends RuntimeException {
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ */
+ public DependencyException() {
+ // TODO Auto-generated constructor stub
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param message
+ */
+ public DependencyException(String message) {
+ super(message);
+ // TODO Auto-generated constructor stub
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param cause
+ */
+ public DependencyException(Throwable cause) {
+ super(cause);
+ // TODO Auto-generated constructor stub
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param message
+ * @param cause
+ */
+ public DependencyException(String message, Throwable cause) {
+ super(message, cause);
+ // TODO Auto-generated constructor stub
+ }
+
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DependencyInjector.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,35 @@
+/*
+ * $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 interface DependencyInjector {
+
+ public <T> T create(Class<T> type);
+
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/DynamicBinding.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,186 @@
+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;
+ }
+ }
+
+ 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;
+ }
+
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/InjectorImpl.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,95 @@
+package org.richfaces.jsr330;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.inject.Provider;
+
+public class InjectorImpl implements DependencyInjector {
+
+
+ private final Map<Target,Binding> bindings = new HashMap<Target,
Binding>();
+
+ @SuppressWarnings("unchecked")
+ public <T> T create(Class<T> type) {
+ Target target = Target.create(type);
+ return (T) getBinding(target).get();
+ }
+
+ public void init(BindingModule ... modules){
+ for (BindingModule bindingModule : modules) {
+ bindingModule.configure(this);
+ }
+ initProviders();
+ }
+
+ 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 initProviders() {
+ Collection<Binding> values = new
ArrayList<Binding>(bindings.values());
+ for (Binding binding : values) {
+ binding.init(this);
+ }
+
+ }
+
+ public Binding getBinding(Target type) {
+ if (bindings.containsKey(type)) {
+ return bindings.get(type);
+ } else if(type.isConcrete()){
+ // Concrete classes can be created without configuration.
+ Binding binding = registerImplementation(type, type.getRawType());
+ binding.init(this);
+ return binding;
+ } else {
+ throw new DependencyException("Type "+type+" has not been
registered");
+ }
+ }
+
+ public Binding registerProvider(Target type, Provider<?> provider){
+ StaticBinding binding = new StaticBinding(provider);
+ registerProviderWrapper(type, binding);
+ return binding;
+ }
+
+ public Binding registerProvider(Target type, Class<? extends Provider<?>>
provider){
+ DynamicBinding binding = new DynamicBinding(provider);
+ registerProviderWrapper(type, binding);
+ return binding;
+ }
+
+ private void registerProviderWrapper(Target type, Binding binding) {
+ bindings.put(type.toProvider(), binding);
+ ProviderWrapper wrapper = new ProviderWrapper(binding);
+ bindings.put(type, wrapper);
+ }
+
+ public Binding registerImplementation(Target type, Class<?> implementation){
+ DynamicBinding provider = new DynamicBinding(implementation);
+ registerImplementationProvider(type, provider);
+ return provider;
+ }
+
+ public Binding registerInstance(Target type, Object instance){
+ StaticBinding provider = new StaticBinding(instance);
+ registerImplementationProvider(type, provider);
+ return provider;
+ }
+
+ private void registerImplementationProvider(Target type, Binding provider) {
+ bindings.put(type, provider);
+ bindings.put(type.toProvider(), new StaticBinding(provider));
+ }
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ObjectFactory.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,40 @@
+package org.richfaces.jsr330;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Implementation independent factory for JSR-330 beans.
+ *
+ */
+public final class ObjectFactory {
+ /**
+ * <p class="changed_added_4_0">
+ * </p>
+ */
+ private static final Map<ClassLoader, DependencyInjector> INSTANCES =
+ new ConcurrentHashMap<ClassLoader, DependencyInjector>();
+
+ private ObjectFactory() {
+ }
+
+ public static <T> T create(Class<T> target) {
+ return getInjector().create(target);
+ }
+
+ private static DependencyInjector getInjector() {
+ return INSTANCES.get(getCurrentLoader());
+ }
+
+ private static ClassLoader getCurrentLoader() {
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+ if (null == contextClassLoader) {
+ contextClassLoader = ObjectFactory.class.getClassLoader();
+ }
+ return contextClassLoader;
+ }
+
+ public static void setInjector(DependencyInjector injector) {
+ INSTANCES.put(getCurrentLoader(), injector);
+ }
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/ProviderWrapper.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -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;
+
+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() {
+ // TODO Auto-generated method stub
+ return getProvider().get();
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.jsr330.Binding#getProvider()
+ */
+ private Provider<?> getProvider() {
+ // TODO Auto-generated method stub
+ return (Provider<?>) binding.get();
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.jsr330.Binding#init(org.richfaces.jsr330.InjectorImpl)
+ */
+ public void init(InjectorImpl injectorImpl) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/StaticBinding.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,48 @@
+/*
+ * $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
+ }
+
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/main/java/org/richfaces/jsr330/Target.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,170 @@
+/*
+ * $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.lang.annotation.Annotation;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Arrays;
+
+import javax.inject.Named;
+import javax.inject.Provider;
+
+/**
+ * <p class="changed_added_4_0">
+ * </p>
+ *
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public class Target {
+
+ private static final Class<?>[] EMPTY_TYPES = new Class<?>[]{};
+
+ private final String name;
+
+ private final Type type;
+
+ private Class<?> rawType;
+
+ private Type[] actualTypeArguments;
+
+ public Target(Type type) {
+ this(type,null);
+ }
+
+ public Target(Type type, String name) {
+ 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(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(type);
+ }
+
+ public Target toProvider() {
+ Target target = new Target(type, name);
+ target.rawType = Provider.class;
+ target.actualTypeArguments = new Type[]{this.type};
+ return target;
+ }
+
+ public boolean isConcrete() {
+ return !Modifier.isAbstract((getRawType()).getModifiers());
+ }
+
+ public Class<?> getRawType(){
+ return rawType;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Arrays.hashCode(this.actualTypeArguments);
+ result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
+ result = prime * result + ((this.rawType == null) ? 0 :
this.rawType.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ Target other = (Target) obj;
+ if (!Arrays.equals(this.actualTypeArguments, other.actualTypeArguments)) {
+ return false;
+ }
+ if (this.name == null) {
+ if (other.name != null) {
+ return false;
+ }
+ } else if (!this.name.equals(other.name)) {
+ return false;
+ }
+ if (!this.rawType.equals(other.rawType)) {
+ return false;
+ }
+ return true;
+ }
+
+ public static Type[] getActualTypeArguments(Type type) {
+ if(type instanceof Class<?>){
+ return EMPTY_TYPES;
+ } else if(type instanceof ParameterizedType){
+ return ((ParameterizedType) type).getActualTypeArguments();
+ } else {
+ throw new DependencyException("Unknown type "+type);
+ }
+ }
+
+ public static Class<?> getRawClass(Type type){
+ if(type instanceof Class<?>){
+ return (Class<?>)type;
+ } else if(type instanceof ParameterizedType){
+ return getRawClass(((ParameterizedType) type).getRawType());
+ } else {
+ throw new DependencyException("Unknown type "+type);
+ }
+
+ }
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @return the actualTypeArguments
+ */
+ public Type[] getActualTypeArguments() {
+ return this.actualTypeArguments;
+ }
+
+
+}
Added:
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
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/InjectorTest.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,77 @@
+package org.richfaces.jsr330;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+
+public class InjectorTest {
+
+ @Test
+ public void injectConstructor() throws Exception {
+ 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");
+ }
+
+ });
+ TestInterface testInterface = injector.create(TestInterface.class);
+ assertTrue(testInterface instanceof TestConstructorInjection);
+ assertEquals("bar",testInterface.hello());
+ }
+
+ @Test
+ public void injectField() throws Exception {
+ 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");
+ }
+
+ });
+ TestInterface testInterface = injector.create(TestInterface.class);
+ assertTrue(testInterface instanceof TestFieldInjection);
+ assertEquals("bar",testInterface.hello());
+ }
+
+ @Test
+ public void injectMethod() throws Exception {
+ 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");
+ }
+
+ });
+ TestInterface testInterface = injector.create(TestInterface.class);
+ assertTrue(testInterface instanceof TestMethodInjection);
+ assertEquals("bar",testInterface.hello());
+ }
+ @Test
+ public void injectProvider() throws Exception {
+ 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");
+ }
+
+ });
+ TestInterface testInterface = injector.create(TestInterface.class);
+ assertTrue(testInterface instanceof TestProviderInjection);
+ assertEquals("bar",testInterface.hello());
+ }
+
+}
Added:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestConstructorInjection.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestConstructorInjection.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestConstructorInjection.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,47 @@
+/*
+ * $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.Inject;
+import javax.inject.Named;
+
+/**
+ * <p class="changed_added_4_0"></p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public class TestConstructorInjection implements TestInterface {
+
+ final String param;
+
+ @Inject
+ public TestConstructorInjection( @Named("foo") String param) {
+ this.param = param;
+ }
+
+ public String hello() {
+ return param;
+ }
+
+}
Added:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestFieldInjection.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestFieldInjection.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestFieldInjection.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,47 @@
+/*
+ * $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.Inject;
+import javax.inject.Named;
+
+/**
+ * <p class="changed_added_4_0"></p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public class TestFieldInjection implements TestInterface {
+
+ @Inject
+ @Named("foo")
+ String param;
+
+ public TestFieldInjection( ) {
+ }
+
+ public String hello() {
+ return param;
+ }
+
+}
Added:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestInterface.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestInterface.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestInterface.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,35 @@
+/*
+ * $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 interface TestInterface {
+
+ public String hello();
+
+}
Added:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestMethodInjection.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestMethodInjection.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestMethodInjection.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -0,0 +1,51 @@
+/*
+ * $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.Inject;
+import javax.inject.Named;
+
+/**
+ * <p class="changed_added_4_0"></p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public class TestMethodInjection implements TestInterface {
+
+ String param;
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param param the param to set
+ */
+ @Inject
+ public void setParam( @Named("foo") String param) {
+ this.param = param;
+ }
+
+ public String hello() {
+ return param;
+ }
+
+}
Added:
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestProviderInjection.java
===================================================================
---
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestProviderInjection.java
(rev 0)
+++
root/core/branches/jsr-330/jsr330-impl/src/test/java/org/richfaces/jsr330/TestProviderInjection.java 2010-06-10
00:30:15 UTC (rev 17590)
@@ -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.Inject;
+import javax.inject.Named;
+import javax.inject.Provider;
+
+/**
+ * <p class="changed_added_4_0"></p>
+ * @author asmirnov(a)exadel.com
+ *
+ */
+public class TestProviderInjection implements TestInterface {
+
+ String param;
+
+ /**
+ * <p class="changed_added_4_0"></p>
+ * @param param the param to set
+ */
+ @Inject
+ public void setParam( @Named("foo") Provider<String> param) {
+ this.param = param.get();
+ }
+
+ public String hello() {
+ return param;
+ }
+
+}