From: erik Date: Thu, 15 Jul 2010 13:48:57 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.3~81 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=00d2c044a1766367a5e7f639025afe99ca557448;p=utils --- diff --git a/support/cdi/src/main/java/org/wamblee/inject/Injectable.java b/support/cdi/src/main/java/org/wamblee/inject/Injectable.java deleted file mode 100644 index 0c881d9b..00000000 --- a/support/cdi/src/main/java/org/wamblee/inject/Injectable.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.inject; - -import java.util.ServiceLoader; - - -/** - * This abstract base class performs injection at construction. Be sure not to - * initialize fields of derived classes to null as these will override the - * initializations of this base class. - * - * Use this by subclassing through {@link #Injectable()). - * - * To use this class, the {@link ServiceLoader} mechanism must be used to locate - * a {@link InjectorFactory}. The first implementation that is found will be - * used for injection. - * - * @author Erik Brakkee - */ -public abstract class Injectable { - - private static final SimpleInjector INJECTOR = new SimpleInjector( - InjectorFactoryBuilder.getInjectorFactory()); - - /** - * Inheritance style constructor. - */ - protected Injectable() { - INJECTOR.inject(this); - } -} diff --git a/support/cdi/src/main/java/org/wamblee/inject/Injector.java b/support/cdi/src/main/java/org/wamblee/inject/Injector.java deleted file mode 100644 index e42cc5f7..00000000 --- a/support/cdi/src/main/java/org/wamblee/inject/Injector.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.inject; - -/** - * Interface used to perform injection with. An injector instance knows how to perform injection in - * one or more specific classes. - * - * @author Erik Brakkee - */ -public interface Injector { - - /** - * Injects beans into a given component of the required class. - * - * @param aComponent - * Component to inject into. - * @throws IllegalArgumentException In case the argument passed is not supported for injection - * @throws NullPointerException In case the argument passed is null. - */ - void inject(Object aComponent); - -} \ No newline at end of file diff --git a/support/cdi/src/main/java/org/wamblee/inject/InjectorCache.java b/support/cdi/src/main/java/org/wamblee/inject/InjectorCache.java deleted file mode 100644 index 24a0f2c4..00000000 --- a/support/cdi/src/main/java/org/wamblee/inject/InjectorCache.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.inject; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.wamblee.cdi.CdiInjector; - -/** - * Cache of {@link CdiInjector}s for efficiency to avoid duplicate analysis of a - * given class. - * - * @author Erik Brakkee - */ -public class InjectorCache { - - private Map injectors; - - private InjectorFactory injectorFactory; - - /** - * Constructs an empty cache. - * - * @param aMgr - * Bean manager. - */ - public InjectorCache(InjectorFactory aInjectorFactory) { - injectorFactory = aInjectorFactory; - injectors = new ConcurrentHashMap(); - } - - /** - * Gets the injector for a given class. This returns a cached injector or - * creates a new injector and caches it. - * - * @param aClass - * Class to find injector for. - * @return Injector. - */ - public Injector getInjector(Class aClass) { - Injector injector = injectors.get(aClass.getName()); - if (injector == null) { - // create and add injector - // NOTE: in rare circumstances this will lead to parallel - // creation of - // an injector for the same class. However, only one of them - // will be the final one - // in the map. There are no side effects of this duplicate - // creation of injectors. - injector = injectorFactory.create(aClass); - injectors.put(aClass.getName(), injector); - } - return injector; - } -} diff --git a/support/cdi/src/main/java/org/wamblee/inject/InjectorFactory.java b/support/cdi/src/main/java/org/wamblee/inject/InjectorFactory.java deleted file mode 100644 index 84f7a66b..00000000 --- a/support/cdi/src/main/java/org/wamblee/inject/InjectorFactory.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.inject; - - -/** - * Injector factory used. This creates an injector that is appropriate for a certain class. - * May be subclassed for testing or other advanced usage (even replacing CDI with another - * injection framework). - * - * Implementations of this calss must have a default no-arg constructor. - * - * @author Erik Brakkee - */ -public interface InjectorFactory { - - /** - * Creates an injector. - * @return Injector fot he given class. - * @throws IllegalArgumentException In case the given class cannot be used. - */ - Injector create(Class aClass); - -} diff --git a/support/cdi/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java b/support/cdi/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java deleted file mode 100644 index d6005005..00000000 --- a/support/cdi/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.inject; - -import java.util.NoSuchElementException; -import java.util.ServiceLoader; - -/** - * Utility for obtaining an implementation of the injector factory using - * {@link ServiceLoader}. - * - * @author Erik Brakkee - */ -public class InjectorFactoryBuilder { - - /** - * Gets the injector factory by using the first one found using - * {@link ServiceLoader}. - * - * @return InjectorFactory. - */ - public static InjectorFactory getInjectorFactory() { - ServiceLoader factories = ServiceLoader - .load(InjectorFactory.class); - try { - return (InjectorFactory) factories.iterator().next(); - } catch (NoSuchElementException e) { - throw new RuntimeException("Can not find InjectorFactory to use"); - } - } -} diff --git a/support/cdi/src/main/java/org/wamblee/inject/SimpleInjector.java b/support/cdi/src/main/java/org/wamblee/inject/SimpleInjector.java deleted file mode 100644 index 61bb5560..00000000 --- a/support/cdi/src/main/java/org/wamblee/inject/SimpleInjector.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.inject; - -import java.util.ServiceLoader; - - -/** - * Singleton injector access. This should be used as main entry point for - * injection. A different {@link InjectorFactory} can be plugged in for testing. - * - * Given the following class: - *
 
- * class Pojo {
- *   @EJB
- *   private Service service; 
- *   
- *   ...
- * }
- * 
- * injecting the EJB into a POJO using Contexts and Dependency Injection is accomplished as follows: - *
- *   Pojo pojo = new Pojo(); 
- *   SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory()); 
- *   injector.inject(pojo);
- * 
- * - * Note that it is recommended to cache the injector because the injector does caching - * of the types that it injects into. Caching the injector makes sure that a class is not - * analysed again for annotation every time injection is used. - * - * The {@link InjectorFactory} to be used can also be found using a {@link ServiceLoader} discovery - * mechanism as provided by {@link InjectorFactoryBuilder}. - * - * @author Erik Brakkee - */ -public class SimpleInjector { - - private InjectorCache cache; - - /** - * Constructs the injector. - * @param aFactory Factory to use. - */ - public SimpleInjector(InjectorFactory aFactory) { - cache = new InjectorCache(aFactory); - } - - /** - * Injects into a given object. - * - * @param aObject - * Object to inject into. - */ - public void inject(Object aObject) { - cache.getInjector(aObject.getClass()).inject(aObject); - } -}