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, 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(invocation.getComponentId());
if (componentEnvironment == null) {
throw new InjectionException(localStrings.getLocalString("injection-manager.no-descriptor-registered-for-invocation", "No descriptor registered for current invocation: {0}", invocation));
}
inject(instance.getClass(), instance, componentEnvironment, null, invokePostConstruct);
}
use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class J2EEInstanceListener method handleAfterEvent.
private void handleAfterEvent(InstanceEvent event, InstanceEvent.EventType eventType) {
Wrapper wrapper = event.getWrapper();
Context context = (Context) wrapper.getParent();
if (!(context instanceof WebModule)) {
return;
}
WebModule wm = (WebModule) context;
Object instance;
if (eventType == InstanceEvent.EventType.AFTER_FILTER_EVENT) {
instance = event.getFilter();
} else {
instance = event.getServlet();
}
if (instance == null) {
return;
}
// Emit monitoring probe event
if (instance instanceof Servlet) {
if (eventType == InstanceEvent.EventType.AFTER_INIT_EVENT) {
wm.servletInitializedEvent(wrapper.getName());
} else if (eventType == InstanceEvent.EventType.AFTER_DESTROY_EVENT) {
wm.servletDestroyedEvent(wrapper.getName());
}
}
// EE invocation context
try {
if (eventType == InstanceEvent.EventType.AFTER_DESTROY_EVENT && !DefaultServlet.class.equals(instance.getClass()) && !JspServlet.class.equals(instance.getClass())) {
injectionMgr.destroyManagedObject(instance, false);
}
} catch (InjectionException ie) {
String msg = _rb.getString(LogFacade.EXCEPTION_DURING_HANDLE_EVENT);
msg = MessageFormat.format(msg, new Object[] { eventType, wm });
_logger.log(Level.SEVERE, msg, ie);
}
ComponentInvocation inv = new WebComponentInvocation(wm, instance);
try {
// if postinvoke failed in this event the invocation stack would get corrupted by double-remove
if (event.getException() == null || !(event.getException() instanceof PostInvokeHandlingException)) {
im.postInvoke(inv);
}
} catch (Exception ex) {
String msg = _rb.getString(LogFacade.EXCEPTION_DURING_HANDLE_EVENT);
msg = MessageFormat.format(msg, new Object[] { eventType, wm });
PostInvokeHandlingException rethrown = new PostInvokeHandlingException(msg, ex);
if (event.getException() != null) {
rethrown.addSuppressed(event.getException());
}
throw rethrown;
} finally {
if (eventType == InstanceEvent.EventType.AFTER_DESTROY_EVENT) {
if (tm != null) {
tm.componentDestroyed(instance, inv);
}
} else if (eventType == InstanceEvent.EventType.AFTER_FILTER_EVENT || eventType == InstanceEvent.EventType.AFTER_SERVICE_EVENT) {
// Emit monitoring probe event
if (eventType == InstanceEvent.EventType.AFTER_SERVICE_EVENT) {
ServletResponse response = event.getResponse();
int status = -1;
if (response != null && response instanceof HttpServletResponse) {
status = ((HttpServletResponse) response).getStatus();
}
wm.afterServiceEvent(wrapper.getName(), status);
}
// BEGIN IASRI# 4646060
if (im.getCurrentInvocation() == null) {
// END IASRI# 4646060
try {
// clear security context
Realm ra = context.getRealm();
if (ra != null && (ra instanceof RealmInitializer)) {
// cleanup not only securitycontext but also PolicyContext
((RealmInitializer) ra).logout();
}
} catch (Exception ex) {
String msg = _rb.getString(LogFacade.EXCEPTION_DURING_HANDLE_EVENT);
msg = MessageFormat.format(msg, new Object[] { eventType, wm });
_logger.log(Level.SEVERE, msg, ex);
}
if (tm != null) {
try {
if (tm.getTransaction() != null) {
tm.rollback();
}
tm.cleanTxnTimeout();
} catch (Exception ex) {
}
}
}
if (tm != null) {
tm.componentDestroyed(instance, inv);
}
}
}
}
use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class GlassFishInjectionProvider method getComponentEnvironment.
// --------------------------------------------------------- Private Methods
/**
* <p>
* This is based off of code in <code>InjectionManagerImpl</code>.
* </p>
*
* @return <code>JndiNameEnvironment</code>
* @throws InjectionException
* if we're unable to obtain the <code>JndiNameEnvironment</code>
*/
private JndiNameEnvironment getComponentEnvironment() throws InjectionException {
ComponentInvocation invocation = invocationManager.getCurrentInvocation();
if (invocation == null) {
throw new InjectionException("null invocation context");
}
if (invocation.getInvocationType() != SERVLET_INVOCATION) {
throw new InjectionException("Wrong invocation type");
}
JndiNameEnvironment componentEnvironment = (JndiNameEnvironment) invocation.getJndiEnvironment();
if (componentEnvironment == null) {
throw new InjectionException("No descriptor registered for " + " current invocation : " + invocation);
}
return componentEnvironment;
}
use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class GlassFishInjectionProvider method getPostConstructMethod.
/**
* <p>
* This is based off of code in <code>InjectionManagerImpl</code>.
* </p>
*
* @param injectionInfo
* InjectionInfo
* @param resourceClass
* target class
* @return a Method marked with the @PostConstruct annotation
* @throws InjectionException
* if an error occurs
*/
private Method getPostConstructMethod(InjectionInfo injectionInfo, Class<? extends Object> resourceClass) throws InjectionException {
Method postConstructMethod = injectionInfo.getPostConstructMethod();
if (postConstructMethod == null) {
String postConstructMethodName = injectionInfo.getPostConstructMethodName();
// This does not include super-classses.
for (Method declaredMethod : resourceClass.getDeclaredMethods()) {
// methods with no arguments.
if (declaredMethod.getName().equals(postConstructMethodName) && declaredMethod.getParameterTypes().length == 0) {
postConstructMethod = declaredMethod;
injectionInfo.setPostConstructMethod(postConstructMethod);
break;
}
}
}
if (postConstructMethod == null) {
throw new InjectionException("InjectionManager exception. PostConstruct method " + injectionInfo.getPostConstructMethodName() + " could not be found in class " + injectionInfo.getClassName());
}
return postConstructMethod;
}
use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.
the class InjectionServicesImpl method aroundInject.
@Override
public <T> void aroundInject(InjectionContext<T> injectionContext) {
try {
ServiceLocator serviceLocator = Globals.getDefaultHabitat();
ComponentEnvManager compEnvManager = serviceLocator.getService(ComponentEnvManager.class);
EjbContainerServices containerServices = serviceLocator.getService(EjbContainerServices.class);
JndiNameEnvironment componentEnv = compEnvManager.getCurrentJndiNameEnvironment();
if (componentEnv == null) {
InvocationManager invMgr = serviceLocator.getService(InvocationManager.class);
if (invMgr.getCurrentInvocation() != null) {
componentEnv = (JndiNameEnvironment) invMgr.<ComponentInvocation>getCurrentInvocation().getJNDIEnvironment();
}
}
ManagedBeanDescriptor mbDesc = null;
JndiNameEnvironment injectionEnv = (JndiNameEnvironment) bundleContext;
AnnotatedType annotatedType = injectionContext.getAnnotatedType();
Class targetClass = annotatedType.getJavaClass();
String targetClassName = targetClass.getName();
Object target = injectionContext.getTarget();
if (isInterceptor(targetClass) && (componentEnv != null && !componentEnv.equals(injectionEnv))) {
// Resources injected into interceptors must come from the environment in which the interceptor is
// intercepting, not the environment in which the interceptor resides (for everything else!)
// Must use the injectionEnv to get the injection info to determine where in jndi to look for the objects to inject.
// must use the current jndi component env to lookup the objects to inject
injectionManager.inject(targetClass, target, injectionEnv, null, false);
} else {
if (componentEnv == null) {
// throw new IllegalStateException("No valid EE environment for injection of " + targetClassName);
System.err.println("No valid EE environment for injection of " + targetClassName);
injectionContext.proceed();
return;
}
if (componentEnv instanceof EjbDescriptor) {
EjbDescriptor ejbDesc = (EjbDescriptor) componentEnv;
if (containerServices.isEjbManagedObject(ejbDesc, targetClass)) {
injectionEnv = componentEnv;
} else {
if (bundleContext instanceof EjbBundleDescriptor) {
// Check if it's a @ManagedBean class within an ejb-jar. In that case,
// special handling is needed to locate the EE env dependencies
mbDesc = bundleContext.getManagedBeanByBeanClass(targetClassName);
}
}
}
if (mbDesc != null) {
injectionManager.injectInstance(target, mbDesc.getGlobalJndiName(), false);
} else {
if (injectionEnv instanceof EjbBundleDescriptor) {
// set the environment of the ejb bundle.
if (target == null) {
injectionManager.injectClass(targetClass, compEnvManager.getComponentEnvId(injectionEnv), false);
} else {
injectionManager.injectInstance(target, compEnvManager.getComponentEnvId(injectionEnv), false);
}
} else {
if (target == null) {
injectionManager.injectClass(targetClass, injectionEnv, false);
} else {
injectionManager.injectInstance(target, injectionEnv, false);
}
}
}
}
injectionContext.proceed();
} catch (InjectionException ie) {
throw new IllegalStateException(ie.getMessage(), ie);
}
}
Aggregations