Skip to content

Commit

Permalink
Bah, really commit
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@552 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Dec 18, 2008
1 parent 9f2a9cf commit 9e7a5ff
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 83 deletions.
2 changes: 1 addition & 1 deletion webbeans-ri/.settings/org.eclipse.jdt.core.prefs
@@ -1,4 +1,4 @@
#Tue Dec 16 18:53:08 GMT 2008
#Tue Dec 16 18:57:16 GMT 2008
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.source=1.5
Expand Down
9 changes: 9 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -49,6 +49,7 @@
import org.jboss.webbeans.bean.AbstractBean;
import org.jboss.webbeans.bean.proxy.ProxyPool;
import org.jboss.webbeans.contexts.ContextMap;
import org.jboss.webbeans.ejb.EjbDescriptorCache;
import org.jboss.webbeans.event.EventManager;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotatedMethod;
Expand Down Expand Up @@ -87,6 +88,8 @@ public class ManagerImpl implements Manager
private Set<Decorator> decorators;
// The registered interceptors
private Set<Interceptor> interceptors;

private EjbDescriptorCache ejbDescriptorCache;

/**
* Constructor
Expand All @@ -103,6 +106,7 @@ public ManagerImpl()
this.interceptors = new HashSet<Interceptor>();
this.contextMap = new ContextMap();
this.eventManager = new EventManager(this);
this.ejbDescriptorCache = new EjbDescriptorCache();

List<Class<? extends Annotation>> defaultEnabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
defaultEnabledDeploymentTypes.add(0, Standard.class);
Expand Down Expand Up @@ -629,6 +633,11 @@ public Resolver getResolver()
{
return resolver;
}

public EjbDescriptorCache getEjbDescriptorCache()
{
return ejbDescriptorCache;
}

/**
* Gets a string representation
Expand Down
Expand Up @@ -31,14 +31,13 @@
import javax.webbeans.Observes;
import javax.webbeans.Produces;
import javax.webbeans.Specializes;
import javax.webbeans.manager.EnterpriseBeanLookup;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bootstrap.spi.EjbDescriptor;
import org.jboss.webbeans.bootstrap.spi.MethodDescriptor;
import org.jboss.webbeans.contexts.DependentContext;
import org.jboss.webbeans.ejb.EjbDescriptorCache;
import org.jboss.webbeans.ejb.DefaultEnterpriseBeanLookup;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.introspector.AnnotatedParameter;
Expand Down Expand Up @@ -83,7 +82,11 @@ public EnterpriseBean(Class<T> type, ManagerImpl manager)
protected void init()
{
super.init();
Iterable<EjbDescriptor<T>> ejbDescriptors = EjbDescriptorCache.instance().get(getType());
Iterable<EjbDescriptor<T>> ejbDescriptors = manager.getEjbDescriptorCache().get(getType());
if (ejbDescriptors == null)
{
throw new DefinitionException("Not an EJB " + toString());
}
for (EjbDescriptor<T> ejbDescriptor : ejbDescriptors)
{
if (this.ejbDescriptor == null)
Expand Down Expand Up @@ -160,7 +163,8 @@ private void checkSpecialization()
{
return;
}
if (EjbDescriptorCache.instance().containsKey(getType().getSuperclass()))
// TODO Should also check the bean type it does contain!
if (!manager.getEjbDescriptorCache().containsKey(getType().getSuperclass()))
{
throw new DefinitionException("Annotation defined specializing EJB must have EJB superclass");
}
Expand All @@ -171,29 +175,38 @@ private void checkSpecialization()
*/
protected void initRemoveMethod()
{
if (!ejbDescriptor.isStateful())
{
// Nothing to do for stateless enterprise beans;
return;
}

// >1 @Destructor
if (getAnnotatedItem().getAnnotatedMethods(Destructor.class).size() > 1)
{
throw new DefinitionException("Multiple @Destructor methods not allowed on " + getAnnotatedItem());
}


if (getAnnotatedItem().getAnnotatedMethods(Destructor.class).size() == 1)
{
AnnotatedMethod<?> destructorMethod = getAnnotatedItem().getAnnotatedMethods(Destructor.class).iterator().next();
for (MethodDescriptor removeMethod : ejbDescriptor.getRemoveMethods())
{
AnnotatedMethod<?> annotatedRemoveMethod = getAnnotatedItem().getMethod(removeMethod);
if (annotatedRemoveMethod != null && annotatedRemoveMethod.equals(destructorMethod))
{
this.removeMethod = destructorMethod;
return;
}
}
throw new DefinitionException("Method annotated @Destructor is not an EJB remove method on " + toString());
}
// <1 (0) @Destructors
Set<MethodDescriptor> noArgsRemoveMethods = getNoArgsRemoveMethods(ejbDescriptor);
if (noArgsRemoveMethods.size() == 1)
{
this.removeMethod = annotatedItem.getMethod(noArgsRemoveMethods.iterator().next());
return;
}

if (!getScopeType().equals(Dependent.class))
{
throw new DefinitionException("Only @Dependent scoped enterprise beans can be without remove methods");
throw new DefinitionException("Only @Dependent scoped enterprise beans can be without remove methods " + toString());
}

}
Expand All @@ -210,16 +223,6 @@ private static Set<MethodDescriptor> getNoArgsRemoveMethods(EjbDescriptor<?> ejb
}
return noArgsRemoveMethods;
}

private void checkDestructorMethods()
{
// TODO Check that any method annotated @Destructor is actually in the remove method list
/*
if (removeMethod.isAnnotationPresent(Destructor.class) && !removeMethod.isAnnotationPresent(EJB.REMOVE_ANNOTATION))
{
throw new DefinitionException("Methods marked @Destructor must also be marked @Remove on " + removeMethod.getName());
}*/
}

/**
* Validates the remove method
Expand All @@ -230,7 +233,10 @@ private void checkRemoveMethod()
{
return;
}

else if (ejbDescriptor.isStateless())
{
throw new DefinitionException("Can't define a remove method on SLSBs");
}
if (removeMethod.isAnnotationPresent(Initializer.class))
{
throw new DefinitionException("Remove methods cannot be initializers on " + removeMethod.getName());
Expand Down Expand Up @@ -261,7 +267,7 @@ public T create()
try
{
DependentContext.INSTANCE.setActive(true);
T instance = (T) manager.getInstanceByType(EnterpriseBeanLookup.class).lookup(ejbDescriptor.getEjbName());
T instance = (T) manager.getInstanceByType(DefaultEnterpriseBeanLookup.class).lookup(ejbDescriptor);
bindDecorators();
bindInterceptors();
injectEjbAndCommonFields();
Expand Down
Expand Up @@ -18,9 +18,12 @@
package org.jboss.webbeans.ejb;

import javax.webbeans.CreationException;
import javax.webbeans.Current;
import javax.webbeans.Initializer;
import javax.webbeans.Standard;
import javax.webbeans.manager.EnterpriseBeanLookup;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bootstrap.spi.EjbDescriptor;
import org.jboss.webbeans.util.JNDI;

Expand All @@ -33,6 +36,14 @@
@Standard
public class DefaultEnterpriseBeanLookup implements EnterpriseBeanLookup
{

private ManagerImpl manager;

@Initializer
public DefaultEnterpriseBeanLookup(@Current ManagerImpl manager)
{
this.manager = manager;
}

/**
* Looks up and EJB based on the name
Expand All @@ -46,7 +57,7 @@ public Object lookup(String ejbName)
{
throw new NullPointerException("No EJB name supplied for lookup");
}
return lookup(EjbDescriptorCache.instance().get(ejbName));
return lookup(manager.getEjbDescriptorCache().get(ejbName));
}

/**
Expand All @@ -59,9 +70,15 @@ public Object lookup(String ejbName)
@SuppressWarnings("unchecked")
public static <T> T lookup(EjbDescriptor<T> ejbDescriptor)
{
if (!ejbDescriptor.getLocalBusinessInterfaces().iterator().hasNext())
{
throw new RuntimeException("EJB must have local interface " + ejbDescriptor);
}
String jndiName = ejbDescriptor.getLocalBusinessInterfaces().iterator().next().getJndiName();
try
{
return (T) JNDI.lookup(ejbDescriptor.getEjbName());
// TODO Implement enterprise proxies and select the correct jndiName
return (T) JNDI.lookup(jndiName);
}
catch (Exception e)
{
Expand Down
Expand Up @@ -25,31 +25,13 @@
import org.jboss.webbeans.bootstrap.spi.EjbDescriptor;

/**
* Singleton for accessing EJB descriptors by EJB implementation class or name
* EJB descriptors by EJB implementation class or name
*
* @author Pete Muir
*
*/
public class EjbDescriptorCache
{
// The singleton
private static EjbDescriptorCache instance;

/**
* Accessor for the singleton
*
* @return The instance
*/
public static EjbDescriptorCache instance()
{
return instance;
}

// Static initalizer block
static
{
instance = new EjbDescriptorCache();
}

// EJB name -> EJB descriptor map
private ConcurrentMap<String, EjbDescriptor<?>> ejbsByName;
Expand Down Expand Up @@ -134,5 +116,17 @@ public void addAll(Iterable<EjbDescriptor<?>> ejbDescriptors)
add(ejbDescriptor);
}
}

public void clear()
{
ejbsByBeanClass.clear();
ejbsByName.clear();
}

@Override
public String toString()
{
return ejbsByBeanClass + "\n" + ejbsByName;
}

}
74 changes: 73 additions & 1 deletion webbeans-ri/src/main/java/org/jboss/webbeans/util/JNDI.java
Expand Up @@ -17,6 +17,15 @@

package org.jboss.webbeans.util;

import java.util.Hashtable;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.webbeans.ExecutionException;

import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;


/**
* Provides JNDI access abstraction
Expand All @@ -26,6 +35,62 @@
public class JNDI
{

private static final LogProvider log = Logging.getLogProvider(JNDI.class);
private static Hashtable initialContextProperties;

private static InitialContext initialContext;

public static InitialContext getInitialContext(Hashtable<String, String> props) throws NamingException
{
if (props==null)
{
throw new IllegalStateException("JNDI properties not initialized, Seam was not started correctly");
}

if (log.isDebugEnabled())
{
log.debug("JNDI InitialContext properties:" + props);
}

try {
return props.size()==0 ?
new InitialContext() :
new InitialContext(props);
}
catch (NamingException e) {
log.debug("Could not obtain initial context", e);
throw e;
}

}

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

return initialContext;
}

private static synchronized void initInitialContext() throws NamingException
{
if (initialContext == null)
{
initialContext = getInitialContext(initialContextProperties);
}
}

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

public static Hashtable getInitialContextProperties()
{
return initialContextProperties;
}


/**
* Looks up a object in JNDI
*
Expand All @@ -47,7 +112,14 @@ public static Object lookup(String name)
*/
public static <T> T lookup(String name, Class<? extends T> expectedType)
{
return null;
try
{
return (T) getInitialContext().lookup(name);
}
catch (NamingException e)
{
throw new ExecutionException("Error looking " + name + " up in JNDI", e);
}
}

public static void set(String key, Object value)
Expand Down

0 comments on commit 9e7a5ff

Please sign in to comment.