Skip to content

Commit

Permalink
Make EJB example work, hacky version of EJB interceptor & postconstru…
Browse files Browse the repository at this point in the history
…ct interception (want a better way of identifying which bean to postConstruct)

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@556 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Dec 18, 2008
1 parent 636f062 commit 947aa2c
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 33 deletions.
3 changes: 3 additions & 0 deletions examples/translator/webbeans-translator-ear/pom.xml
Expand Up @@ -73,6 +73,9 @@
<contextRoot>/webbeans-translator</contextRoot>
</webModule>
</modules>
<jboss>
<loader-repository>webbeans.jboss.org:loader=webbeans-translator</loader-repository>
</jboss>
</configuration>
</plugin>
</plugins>
Expand Down
Expand Up @@ -11,4 +11,6 @@ public interface TranslatorController

public String getTranslatedText();

public void remove();

}
@@ -1,5 +1,6 @@
package org.jboss.webbeans.examples.translator;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.webbeans.Current;
import javax.webbeans.Named;
Expand Down Expand Up @@ -37,4 +38,10 @@ public String getTranslatedText()
return translatedText;
}

@Remove
public void remove()
{

}

}
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">

<interceptors>
<interceptor>
<interceptor-class>org.jboss.webbeans.ejb.SessionBeanInterceptor</interceptor-class>
</interceptor>
</interceptors>

<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>org.jboss.webbeans.ejb.SessionBeanInterceptor</interceptor-class>
</interceptor-binding>
</assembly-descriptor>

</ejb-jar>
31 changes: 20 additions & 11 deletions examples/translator/webbeans-translator-war/WebContent/home.xhtml
Expand Up @@ -10,20 +10,29 @@
<h1>Translate your text into Latin!</h1>
<h:form id="NumberGuessMain">

<div style="vertical-align: middle;">
Your text:
<h:inputTextarea id="text" value="#{translator.translate}" required="true" rows="5" cols="80">
<f:validateLongRange maximum="#{game.biggest}" minimum="#{game.smallest}"/>
</h:inputTextarea>
</div>

<table>
<tr align="center" style="font-weight: bold" >
<td>
Your text
</td>
<td>
Translation
</td>
</tr>
<tr>
<td>
<h:inputTextarea id="text" value="#{translator.text}" required="true" rows="5" cols="80" />
</td>
<td>
<h:outputText value="#{translator.translatedText}" />
</td>
</tr>
</table>
<div>
<h:commandButton id="GuessButton" value="Guess" action="#{game.check}"/>
<h:commandButton id="button" value="Translate" action="#{translator.translate}"/>
</div>

<div>
<h:outputText value="#{translator.translatedText}" />
</div>

</h:form>
</ui:define>
</ui:composition>
Expand Down
13 changes: 13 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -26,7 +26,9 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.webbeans.AmbiguousDependencyException;
Expand Down Expand Up @@ -84,6 +86,7 @@ public class ManagerImpl implements Manager
private ProxyPool proxyPool;
// The registered beans
private List<Bean<?>> beans;
private Map<Class<?>, Bean<?>> beanMap;
// The registered decorators
private Set<Decorator> decorators;
// The registered interceptors
Expand All @@ -100,6 +103,7 @@ public class ManagerImpl implements Manager
public ManagerImpl()
{
this.beans = new CopyOnWriteArrayList<Bean<?>>();
this.beanMap = new ConcurrentHashMap<Class<?>, Bean<?>>();
this.resolver = new Resolver(this);
this.proxyPool = new ProxyPool();
this.decorators = new HashSet<Decorator>();
Expand Down Expand Up @@ -273,9 +277,18 @@ public void setBeans(Set<AbstractBean<?, ?>> beans)
synchronized (beans)
{
this.beans = new CopyOnWriteArrayList<Bean<?>>(beans);
for (AbstractBean<?, ?> bean : beans)
{
beanMap.put(bean.getType(), bean);
}
resolver.clear();
}
}

public Map<Class<?>, Bean<?>> getBeanMap()
{
return beanMap;
}

/**
* The beans registered with the Web Bean manager. For internal use
Expand Down
Expand Up @@ -268,11 +268,7 @@ public T create()
{
DependentContext.INSTANCE.setActive(true);
T instance = (T) manager.getInstanceByType(DefaultEnterpriseBeanLookup.class).lookup(ejbDescriptor);
bindDecorators();
bindInterceptors();
injectEjbAndCommonFields();
injectBoundFields(instance, manager);
callInitializers(instance);
// TODO Return enterprise proxy
return instance;
}
finally
Expand Down Expand Up @@ -402,4 +398,22 @@ public String toString()
return buffer.toString();
}

public void postConstruct(T instance)
{
try
{
DependentContext.INSTANCE.setActive(true);
bindDecorators();
bindInterceptors();
injectEjbAndCommonFields();
injectBoundFields(instance, manager);
callInitializers(instance);
}
finally
{
DependentContext.INSTANCE.setActive(false);
}

}

}
Expand Up @@ -75,14 +75,15 @@ public static <T> T lookup(EjbDescriptor<T> ejbDescriptor)
throw new RuntimeException("EJB must have local interface " + ejbDescriptor);
}
String jndiName = ejbDescriptor.getLocalBusinessInterfaces().iterator().next().getJndiName();
String s = jndiName.substring(0, jndiName.lastIndexOf("-"));
try
{
// TODO Implement enterprise proxies and select the correct jndiName
return (T) JNDI.lookup(jndiName);
return (T) JNDI.lookup(s);
}
catch (Exception e)
{
throw new CreationException("could not find the EJB in JNDI", e);
throw new CreationException("could not find the name in JNDI " + s, e);
}
}

Expand Down
@@ -0,0 +1,26 @@
package org.jboss.webbeans.ejb;

import javax.annotation.PostConstruct;
import javax.interceptor.InvocationContext;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.bean.EnterpriseBean;

public class SessionBeanInterceptor
{

@PostConstruct
public void postConstruct(InvocationContext invocationContext)
{
Class<?> beanClass = invocationContext.getTarget().getClass();
// TODO Don't like this
Bean<?> bean = CurrentManager.rootManager().getBeanMap().get(beanClass);
if (bean instanceof EnterpriseBean)
{
EnterpriseBean<Object> enterpriseBean = (EnterpriseBean<Object>) bean;
enterpriseBean.postConstruct(invocationContext.getTarget());
}
}

}
Expand Up @@ -46,6 +46,14 @@ public interface AnnotatedField<T> extends AnnotatedItem<T, Field>
*/
public void inject(Object instance, Manager manager);

/**
* Injects an instance
*
* @param instance The instance to inject
* @param manager The Web Beans manager
*/
public void injectIntoInstance(Object instance, Manager manager);

/**
* Injects an instance
*
Expand Down
Expand Up @@ -126,6 +126,11 @@ public void inject(Object instance, Manager manager)
{
Reflections.setAndWrap(getDelegate(), instance, getValue(manager));
}

public void injectIntoInstance(Object instance, Manager manager)
{
Reflections.setAndWrap(getName(), instance, getValue(manager));
}

@SuppressWarnings("unchecked")
public T get(Object instance)
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.jboss.webbeans.bootstrap.WebBeansBootstrap;
import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
import org.jboss.webbeans.contexts.ApplicationContext;
import org.jboss.webbeans.contexts.DependentContext;
import org.jboss.webbeans.contexts.RequestContext;
import org.jboss.webbeans.contexts.SessionContext;
import org.jboss.webbeans.log.LogProvider;
Expand Down Expand Up @@ -105,6 +106,7 @@ public static void endSession(HttpSession session)
public static void beginRequest(HttpServletRequest request)
{
SessionContext.INSTANCE.setBeanMap(new SessionBeanMap(request.getSession()));
DependentContext.INSTANCE.setActive(true);
}

/**
Expand All @@ -114,6 +116,7 @@ public static void beginRequest(HttpServletRequest request)
*/
public static void endRequest(HttpServletRequest request)
{
DependentContext.INSTANCE.setActive(false);
RequestContext.INSTANCE.destroy();
SessionContext.INSTANCE.setBeanMap(null);
}
Expand Down
21 changes: 6 additions & 15 deletions webbeans-ri/src/main/java/org/jboss/webbeans/util/JNDI.java
Expand Up @@ -44,7 +44,7 @@ public static InitialContext getInitialContext(Hashtable<String, String> props)
{
if (props==null)
{
throw new IllegalStateException("JNDI properties not initialized, Seam was not started correctly");
throw new IllegalStateException("JNDI properties not initialized");
}

if (log.isDebugEnabled())
Expand All @@ -66,7 +66,10 @@ public static InitialContext getInitialContext(Hashtable<String, String> props)

public static InitialContext getInitialContext() throws NamingException
{
if (initialContext == null) initInitialContext();
if (initialContext == null)
{
initInitialContext();
}

return initialContext;
}
Expand All @@ -75,22 +78,10 @@ private static synchronized void initInitialContext() throws NamingException
{
if (initialContext == null)
{
initialContext = getInitialContext(initialContextProperties);
initialContext = getInitialContext(new Hashtable<String, String>());
}
}

public static void setInitialContextProperties(Hashtable initialContextProperties)
{
initialContextProperties = initialContextProperties;
initialContext = null;
}

public static Hashtable getInitialContextProperties()
{
return initialContextProperties;
}


/**
* Looks up a object in JNDI
*
Expand Down
31 changes: 31 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java
Expand Up @@ -516,6 +516,37 @@ public static void setAndWrap(Field field, Object target, Object value)
throw new ExecutionException("Error setting field " + field.getName() + " on " + field.getDeclaringClass(), e);
}
}

/**
* Sets value of a field and wraps exceptions
*
* @param field The field to set on
* @param target The instance to set on
* @param value The value to set
*/
public static void setAndWrap(String fieldName, Object target, Object value)
{
try
{
target.getClass().getField(fieldName).set(target, value);
}
catch (IllegalArgumentException e)
{
throw new ExecutionException("Error setting field " + fieldName + " on " + target.getClass(), e);
}
catch (IllegalAccessException e)
{
throw new ExecutionException("Error setting field " + fieldName + " on " + target.getClass(), e);
}
catch (SecurityException e)
{
throw new ExecutionException("Error setting field " + fieldName + " on " + target.getClass(), e);
}
catch (NoSuchFieldException e)
{
throw new ExecutionException("Error setting field " + fieldName + " on " + target.getClass(), e);
}
}

/**
* Gets value of a field and wraps exceptions
Expand Down

0 comments on commit 947aa2c

Please sign in to comment.