org.jvnet.hk2.config.InjectionManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2007-2017 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://oss.oracle.com/licenses/CDDL+GPL-1.1
* or LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.hk2.config;
import java.lang.reflect.*;
import java.lang.annotation.Annotation;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.hk2.api.MultiException;
/**
* InjectionManager is responsible for injecting resources into a component.
* Injection targets are identified by the injection resolver type attribute.
*
* @author Jerome Dochez
*/
@SuppressWarnings("unchecked")
public class InjectionManager {
/**
* Initializes the component by performing injection.
*
* @param component component instance to inject
* @param onBehalfOf the inhabitant to do injection on behalf of
* @param targets the injection resolvers to resolve all injection points
* @throws ComponentException
* if injection failed for some reason.
*/
public void inject(Object component, InjectionResolver... targets) {
syncDoInject(component, component.getClass(), targets);
}
/**
* Initializes the component by performing injection.
*
* @param component component instance to inject
* @param onBehalfOf the inhabitant to do injection on behalf of
* @param es the ExecutorService to use in order to handle the work load
* @param targets the injection resolvers to resolve all injection points
* @throws ComponentException
* if injection failed for some reason.
*/
public void inject(Object component, ExecutorService es, InjectionResolver... targets) {
try {
syncDoInject(component, component.getClass(), targets);
/*if (null == es) {
syncDoInject(component, component.getClass(), targets);
} else {
syncDoInject(component, component.getClass(), targets);
// syncDoInject(new InjectContext(component, onBehalfOf, component.getClass(), es, targets));
}*/
} catch (Exception e) {
// we do this to bolster debugging
if (e instanceof MultiException) {
throw (MultiException) e;
}
throw new MultiException(e);
}
}
protected static class InjectContext {
public final Object component;
//public final Class> type;
public final ExecutorService es;
public final InjectionResolver[] targets;
public InjectContext(final Object component,
//final Class type,
final ExecutorService es,
final InjectionResolver[] targets) {
assert(null != component);
this.component = component;
//this.type = type;
this.es = es;
this.targets = targets;
}
}
/**
* Initializes the component by performing injection.
*
* @param component component instance to inject
* @param type component class
* @param targets the injection resolvers to resolve all injection points
* @throws ComponentException
* if injection failed for some reason.
*/
public void inject(Object component,
Class type,
InjectionResolver... targets) {
syncDoInject(component, type, targets);
}
/**
* Initializes the component by performing injection.
*
* @param component component instance to inject
* @param onBehalfOf the inhabitant to do injection on behalf of
* @param type component class
* @param targets the injection resolvers to resolve all injection points
* @throws ComponentException
* if injection failed for some reason.
*/
protected void syncDoInject(Object component,
Class type,
InjectionResolver... targets) {
assert component!=null;
try {
Class currentClass = type;
while (currentClass!=null && Object.class != currentClass) {
// get the list of the instances variable
for (Field field : currentClass.getDeclaredFields()) {
Annotation nonOptionalAnnotation=null;
boolean injected = false;
for (InjectionResolver target : targets) {
Annotation inject = field.getAnnotation(target.type);
if (inject == null) continue;
Type genericType = field.getGenericType();
Class fieldType = field.getType();
try {
Object value = target.getValue(component, field, genericType, fieldType);
if (value != null) {
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Field run() {
field.setAccessible(true);
return field;
}
});
field.set(component, value);
injected = true;
break;
} else {
if (!target.isOptional(field, inject)) {
nonOptionalAnnotation = inject;
}
}
} catch (MultiException e) {
error_injectionException(target, inject, field, e);
} catch (IllegalAccessException e) {
error_injectionException(target, inject, field, e);
} catch (RuntimeException e) {
error_injectionException(target, inject, field, e);
} catch (Exception ex) {
error_injectionException(target, inject, field, ex);
}
}
// exhausted all injection managers,
if (!injected && nonOptionalAnnotation!=null) {
throw new UnsatisfiedDependencyException(field, nonOptionalAnnotation);
}
}
for (Method method : currentClass.getDeclaredMethods()) {
for (InjectionResolver target : targets) {
Annotation inject = method.getAnnotation(target.type);
if (inject == null) continue;
Method setter = target.getSetterMethod(method, inject);
if (setter.getReturnType() != void.class) {
if (Collection.class.isAssignableFrom(setter.getReturnType())) {
injectCollection(component, setter,
target.getValue(component, method, null, setter.getReturnType()));
continue;
}
error_InjectMethodIsNotVoid(method);
}
Class>[] paramTypes = setter.getParameterTypes();
Type[] genericParamTypes = setter.getGenericParameterTypes();
if (allowInjection(method, paramTypes)) {
try {
if (1 == paramTypes.length) {
Object value = target.getValue(component, method, genericParamTypes[0], paramTypes[0]);
if (value != null) {
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Method run() {
setter.setAccessible(true);
return setter;
}
});
setter.invoke(component, value);
} else {
if (!target.isOptional(method, inject)) {
throw new UnsatisfiedDependencyException(method, inject);
}
}
} else {
// multi params
AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Method run() {
setter.setAccessible(true);
return setter;
}
});
Type gparamType[] = setter.getGenericParameterTypes();
Object params[] = new Object[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
Object value = target.getValue(component, method, gparamType[i], paramTypes[i]);
if (value != null) {
params[i] = value;
} else {
if (!target.isOptional(method, inject)) {
throw new UnsatisfiedDependencyException(method, inject);
}
}
}
setter.invoke(component, params);
}
} catch (MultiException e) {
error_injectionException(target, inject, setter, e);
} catch (IllegalAccessException e) {
error_injectionException(target, inject, setter, e);
} catch (InvocationTargetException e) {
error_injectionException(target, inject, setter, e);
} catch (RuntimeException e) {
error_injectionException(target, inject, setter, e);
}
}
}
}
currentClass = currentClass.getSuperclass();
}
} catch (final LinkageError e) {
// reflection could trigger additional classloading and resolution, so it can cause linkage error.
// report more information to assist diagnosis.
// can't trust component.toString() as the object could be in an inconsistent state.
final Class> cls = type;
AccessController.doPrivileged(new PrivilegedAction