[jboss-cvs] JBossAS SVN: r92897 - projects/webbeans-ri-int/trunk/ejb/src/main/java/org/jboss/webbeans/integration/ejb.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Aug 27 19:18:10 EDT 2009
Author: pete.muir at jboss.org
Date: 2009-08-27 19:18:10 -0400 (Thu, 27 Aug 2009)
New Revision: 92897
Added:
projects/webbeans-ri-int/trunk/ejb/src/main/java/org/jboss/webbeans/integration/ejb/SessionBeanInterceptor.java
Log:
ioops
Added: projects/webbeans-ri-int/trunk/ejb/src/main/java/org/jboss/webbeans/integration/ejb/SessionBeanInterceptor.java
===================================================================
--- projects/webbeans-ri-int/trunk/ejb/src/main/java/org/jboss/webbeans/integration/ejb/SessionBeanInterceptor.java (rev 0)
+++ projects/webbeans-ri-int/trunk/ejb/src/main/java/org/jboss/webbeans/integration/ejb/SessionBeanInterceptor.java 2009-08-27 23:18:10 UTC (rev 92897)
@@ -0,0 +1,155 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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.jboss.webbeans.integration.ejb;
+
+import java.io.Serializable;
+import java.lang.reflect.Field;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.annotation.Resource;
+import javax.ejb.SessionContext;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.interceptor.InvocationContext;
+
+import org.jboss.ejb3.BeanContext;
+import org.jboss.ejb3.Container;
+import org.jboss.ejb3.EJBContextImpl;
+import org.jboss.ejb3.Ejb3Registry;
+import org.jboss.ejb3.stateful.StatefulSessionContextImpl;
+import org.jboss.webbeans.ejb.spi.EjbDescriptor;
+import org.jboss.webbeans.manager.api.WebBeansManager;
+
+/**
+ * Interceptor for handling EJB post-construct tasks
+ *
+ * @author Pete Muir
+ */
+public class SessionBeanInterceptor implements Serializable
+{
+ private static final long serialVersionUID = 7327757031821596782L;
+
+ private static final Field statefulGuidField;
+ private static final Field beanContextField;
+
+ static
+ {
+ try
+ {
+ statefulGuidField = StatefulSessionContextImpl.class.getDeclaredField("containerGuid");
+ statefulGuidField.setAccessible(true);
+ beanContextField = EJBContextImpl.class.getDeclaredField("beanContext");
+ beanContextField.setAccessible(true);
+ }
+ catch (SecurityException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (NoSuchFieldException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Resource
+ private SessionContext sessionContext;
+
+ @Resource(mappedName="java:app/BeanManager")
+ private WebBeansManager beanManager;
+
+ private CreationalContext<Object> creationalContext;
+ private InjectionTarget<Object> injectionTarget;
+
+ /**
+ * Gets the underlying target and calls the post-construct method
+ *
+ * @param invocationContext The invocation context
+ * @throws Exception
+ */
+ @PostConstruct
+ public void postConstruct(InvocationContext invocationContext) throws Exception
+ {
+ String ejbName = getEjbName();
+ EjbDescriptor<Object> descriptor = beanManager.getEjbDescriptor(ejbName);
+ injectionTarget = beanManager.createInjectionTarget(descriptor);
+ Bean<Object> bean = beanManager.getBean(descriptor);
+ creationalContext = beanManager.createCreationalContext(bean);
+ injectionTarget.inject(invocationContext.getTarget(), creationalContext);
+ invocationContext.proceed();
+ }
+
+ /**
+ * Gets the underlying target and calls the pre-destroy method
+ *
+ * @param invocationContext The invocation context
+ * @throws Exception
+ */
+ @PreDestroy
+ public void preDestroy(InvocationContext invocationContext) throws Exception
+ {
+ creationalContext.release();
+ invocationContext.proceed();
+ }
+
+ private String getEjbName()
+ {
+ if (sessionContext instanceof StatefulSessionContextImpl)
+ {
+ try
+ {
+
+ String guid = (String) statefulGuidField.get(sessionContext);
+ Container container = Ejb3Registry.getContainer(guid);
+ return container.getEjbName();
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ else if (sessionContext instanceof EJBContextImpl<?, ?>)
+ {
+ try
+ {
+ BeanContext<?> beanContext = (BeanContext<?>) beanContextField.get(sessionContext);
+ return beanContext.getContainer().getEjbName();
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ else
+ {
+ throw new IllegalStateException("Unable to extract ejb name from SessionContext " + sessionContext);
+ }
+
+ }
+
+}
+
+
\ No newline at end of file
Property changes on: projects/webbeans-ri-int/trunk/ejb/src/main/java/org/jboss/webbeans/integration/ejb/SessionBeanInterceptor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
More information about the jboss-cvs-commits
mailing list