use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class NonModuleInjectionServices method aroundInject.
@Override
public <T> void aroundInject(InjectionContext<T> injectionContext) {
try {
ServiceLocator serviceLocator = Globals.getDefaultHabitat();
ComponentEnvManager compEnvManager = serviceLocator.getService(ComponentEnvManager.class);
JndiNameEnvironment componentEnv = compEnvManager.getCurrentJndiNameEnvironment();
Object target = injectionContext.getTarget();
String targetClass = target.getClass().getName();
if (componentEnv == null) {
// throw new IllegalStateException("No valid EE environment for injection of " + targetClass);
System.err.println("No valid EE environment for injection of " + targetClass);
injectionContext.proceed();
return;
}
injectionManager.injectInstance(target, componentEnv, false);
injectionContext.proceed();
} catch (InjectionException ie) {
throw new IllegalStateException(ie.getMessage(), ie);
}
}
use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class InjectionManagerImpl method _inject.
/**
* Internal injection operation. componentId is only specified if componentId-specific lookup operation should be used.
*/
private void _inject(final Class clazz, final Object instance, String componentId, List<InjectionCapable> injectableResources) throws InjectionException {
for (InjectionCapable next : injectableResources) {
try {
String lookupName = next.getComponentEnvName();
if (!lookupName.startsWith("java:")) {
lookupName = "java:comp/env/" + lookupName;
}
Object injectedValue = null;
// do a loop here
for (InjectionTarget target : next.getInjectionTargets()) {
// we can just jump to the next target
if (!clazz.getName().equals(target.getClassName()))
continue;
// otherwise we might get called for the wrong module in the EAR file
if (injectedValue == null) {
injectedValue = (componentId != null) ? glassfishNamingManager.lookup(componentId, lookupName) : glassfishNamingManager.getInitialContext().lookup(lookupName);
}
if (target.isFieldInjectable()) {
final Field f = getField(target, clazz);
if (Modifier.isStatic(f.getModifiers()) && (instance != null)) {
throw new InjectionException(localStrings.getLocalString("injection-manager.illegal-use-of-static-field", "Illegal use of static field on class that only supports instance-based injection: {0}", f));
}
if ((instance == null) && !Modifier.isStatic(f.getModifiers())) {
throw new InjectionException(localStrings.getLocalString("injection-manager.appclient-injected-field-must-be-static", "Injected field: {0} on Application Client class: {1} must be declared static", f, clazz));
}
if (_logger.isLoggable(Level.FINE)) {
_logger.fine(localStrings.getLocalString("injection-manager.injecting-dependency-field", "Injecting dependency with logical name: {0} into field: {1} on class: {2}", next.getComponentEnvName(), f, clazz));
}
final Object value = injectedValue;
// allow for private/protected field access.
if (System.getSecurityManager() != null) {
java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {
public java.lang.Object run() throws Exception {
f.set(instance, value);
return null;
}
});
} else {
f.set(instance, value);
}
} else if (target.isMethodInjectable()) {
final Method m = getMethod(next, target, clazz);
if (Modifier.isStatic(m.getModifiers()) && (instance != null)) {
throw new InjectionException(localStrings.getLocalString("injection-manager.illegal-use-of-static-method", "Illegal use of static method on class that only supports instance-based injection: {0}", m));
}
if ((instance == null) && !Modifier.isStatic(m.getModifiers())) {
throw new InjectionException(localStrings.getLocalString("injection-manager.appclient-injected-method-must-be-static", "Injected method: {0} on Application Client class: {1} must be declared static", m, clazz));
}
if (_logger.isLoggable(Level.FINE)) {
_logger.fine(localStrings.getLocalString("injection-manager.injecting-dependency-method", "Injecting dependency with logical name: {0} into method: {1} on class: {2}", next.getComponentEnvName(), m, clazz));
}
final Object value = injectedValue;
if (System.getSecurityManager() != null) {
// Wrap actual value insertion in doPrivileged to
// allow for private/protected field access.
java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {
public java.lang.Object run() throws Exception {
m.invoke(instance, new Object[] { value });
return null;
}
});
} else {
m.invoke(instance, new Object[] { value });
}
}
}
} catch (Throwable t) {
Throwable cause = (t instanceof InvocationTargetException) ? ((InvocationTargetException) t).getCause() : t;
String msg = localStrings.getLocalString("injection-manager.exception-to-inject", "Exception attempting to inject {0} into {1}: {2}", next, clazz, cause.getMessage());
_logger.log(Level.FINE, msg, t);
throw new InjectionException(msg, cause);
}
}
}
use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class InjectionManagerImpl method getPostConstructMethod.
private Method getPostConstructMethod(InjectionInfo injInfo, Class resourceClass) throws InjectionException {
Method m = injInfo.getPostConstructMethod();
if (m == null) {
String postConstructMethodName = injInfo.getPostConstructMethodName();
// This does not include super-classes.
for (Method next : resourceClass.getDeclaredMethods()) {
// methods with no arguments.
if (next.getName().equals(postConstructMethodName) && (next.getParameterTypes().length == 0)) {
m = next;
injInfo.setPostConstructMethod(m);
break;
}
}
}
if (m == null) {
throw new InjectionException(localStrings.getLocalString("injection-manager.postconstruct-not-found", "InjectionManager exception. PostConstruct method: {0} not found in class: {1}", injInfo.getPostConstructMethodName(), injInfo.getClassName()));
}
return m;
}
use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class InjectionManagerImpl method injectInstance.
public void injectInstance(Object instance, String componentId, boolean invokePostConstruct) throws InjectionException {
ComponentInvocation invocation = invocationMgr.getCurrentInvocation();
if (invocation == null) {
throw new InjectionException(localStrings.getLocalString("injection-manager.null-invocation-context", "Null invocation context"));
}
JndiNameEnvironment componentEnvironment = compEnvManager.getJndiNameEnvironment(componentId);
if (componentEnvironment == null) {
throw new InjectionException(localStrings.getLocalString("injection-manager.no-descriptor-registered-for-component", "No descriptor registered for componentId: {0}", componentId));
}
inject(instance.getClass(), instance, componentEnvironment, componentId, invokePostConstruct);
}
use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class InjectionManagerImpl method getPreDestroyMethod.
private Method getPreDestroyMethod(InjectionInfo injInfo, Class resourceClass) throws InjectionException {
Method m = injInfo.getPreDestroyMethod();
if (m == null) {
String preDestroyMethodName = injInfo.getPreDestroyMethodName();
// This does not include super-classses.
for (Method next : resourceClass.getDeclaredMethods()) {
// methods with no arguments.
if (next.getName().equals(preDestroyMethodName) && (next.getParameterTypes().length == 0)) {
m = next;
injInfo.setPreDestroyMethod(m);
break;
}
}
}
if (m == null) {
throw new InjectionException(localStrings.getLocalString("injection-manager.predestroy-not-found", "InjectionManager exception. PreDestroy method: {0} not found in class: {1}", injInfo.getPreDestroyMethodName(), injInfo.getClassName()));
}
return m;
}
Aggregations