Skip to content

Commit

Permalink
a sane implementation of MockNaming :-|
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@927 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Jan 13, 2009
1 parent 18baf3d commit 0d12912
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 84 deletions.
Expand Up @@ -235,7 +235,7 @@ public T create()
{
DependentContext.INSTANCE.setActive(true);
T instance = produceInstance();
//checkReturnValue(instance);
checkReturnValue(instance);
return instance;
}
finally
Expand Down
Expand Up @@ -17,22 +17,16 @@

package org.jboss.webbeans.resource;

import java.util.ArrayList;
import java.util.List;

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

import org.jboss.webbeans.resources.spi.Naming;

/**
* The default naming provider
*
* @author Pete Muir
*/
public class DefaultNaming implements Naming
public class DefaultNaming extends AbstractNaming
{
private static final long serialVersionUID = 1L;
// The initial lookup context
Expand All @@ -58,72 +52,9 @@ public DefaultNaming()
*
* @return The initial context
*/
public InitialContext getInitialContext()
public InitialContext getContext()
{
return initialContext;
}

/**
* Binds in item to JNDI
*
* @param key The key to bind under
* @param value The value to bind
*/
public void bind(String key, Object value)
{
try
{
List<String> parts = splitIntoContexts(key);
Context context = initialContext;
for (int i = 0; i < parts.size() - 1; i++)
{
context = (Context) context.lookup(parts.get(i));
}
context.bind(parts.get(parts.size() - 1), value);
}
catch (NamingException e)
{
throw new ExecutionException("Cannot bind " + value + " to " + key, e);
}
}

/**
* Lookup an item from JNDI
*
* @param name The key
* @param expectedType The expected return type
* @return The found item
*/
@SuppressWarnings("unchecked")
public <T> T lookup(String name, Class<? extends T> expectedType)
{
Object instance;
try
{
instance = initialContext.lookup(name);
}
catch (NamingException e)
{
throw new ExecutionException("Cannot lookup " + name, e);
}
try
{
return (T) instance;
}
catch (ClassCastException e)
{
throw new ExecutionException(instance + " not of expected type " + expectedType, e);
}
}

private static List<String> splitIntoContexts(String key)
{
List<String> parts = new ArrayList<String>();
for (String part : key.split("/"))
{
parts.add(part);
}
return parts;
}

}
Expand Up @@ -23,25 +23,18 @@
import org.jboss.webbeans.context.SessionContext;
import org.jboss.webbeans.context.beanmap.SimpleBeanMap;
import org.jboss.webbeans.ejb.spi.EjbResolver;
import org.jboss.webbeans.resource.DefaultNaming;
import org.jboss.webbeans.resource.AbstractNaming;
import org.jboss.webbeans.resources.spi.Naming;
import org.jboss.webbeans.resources.spi.ResourceLoader;

public class MockBootstrap extends WebBeansBootstrap
{

public static class MockNaming implements Naming
public static class MockNaming extends AbstractNaming
{

private Context context;

private Naming delegate;

public MockNaming()
{
this.delegate = new DefaultNaming();
}

public void setContext(Context context)
{
this.context = context;
Expand All @@ -56,7 +49,7 @@ public void bind(String key, Object value)
{
if (context != null)
{
delegate.bind(key, value);
super.bind(key, value);
}
}

Expand All @@ -67,7 +60,7 @@ public <T> T lookup(String name, Class<? extends T> expectedType)
T instance = overrideLookup(name, expectedType);
if (instance == null)
{
instance = delegate.lookup(name, expectedType);
instance = super.lookup(name, expectedType);
}
return instance;
}
Expand All @@ -83,7 +76,7 @@ private <T> T overrideLookup(String name, Class<? extends T> expectedType)
// JBoss Embedded EJB 3.1 doesn't seem to bind this!
if (name.equals("java:comp/UserTransaction"))
{
final TransactionManager tm = delegate.lookup("java:/TransactionManager", TransactionManager.class);
final TransactionManager tm = super.lookup("java:/TransactionManager", TransactionManager.class);
return (T) new UserTransaction()
{

Expand Down

0 comments on commit 0d12912

Please sign in to comment.