[jboss-user] [EJB 3.0] - Junit failed.
samwan809
do-not-reply at jboss.com
Wed Jul 15 00:35:20 EDT 2009
Hi,
I have the following problem when I tried to establish a JNDI connectoin from a remote machine to the EJB3 JBOSS server thought JNDI.
# ant test
Buildfile: build.xml
compile-test:
[javac] Compiling 4 source files
test:
[junit] Testsuite: Client
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Null Test: Caused an ERROR
[junit] Client (wrong name: com/ip6networks/calling_card_registration/test/Client)
[junit] java.lang.NoClassDefFoundError: Client (wrong name: com/ip6networks/calling_card_registration/test/Client)
[junit] at java.lang.ClassLoader.defineClass1(Native Method)
[junit] at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
[junit] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[junit]
[junit]
[junit] Test Client FAILED
BUILD SUCCESSFUL
Total time: 4 seconds
Main program:
# cat Client.java
package com.ip6networks.calling_card_registration.test;
import com.ip6networks.calling_card_registration.test.Calculator;
import com.ip6networks.calling_card_registration.test.CalculatorRemote;
import com.ip6networks.calling_card_registration.test.ServiceLocator;
public class Client
{
public static void main(String[] args) throws Exception
{
Calculator calculator = getSession();
System.out.println("in Client class");
System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.subtract(1, 1));
}
private static Calculator getSession() throws Exception {
Calculator mgr = null;
mgr = (Calculator) ServiceLocator.getInstance().getService("Calculator");
if (mgr == null) {
//throw new SystemErrorException("Unable to connect to service");
throw new Exception("Unable to connect to service");
} else {
return mgr;
}
}
}
It complained ServiceLocator..... cant be casted to Calculator.
Here is the ServiceLocator program:
# cat ServiceLocator.java
package com.ip6networks.calling_card_registration.test;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
public class ServiceLocator {
private Map<String, java.lang.Object> cache;
private static ServiceLocator ourInstance = new ServiceLocator();
private static final Log logger = LogFactory
.getLog(ServiceLocator.class);
public static ServiceLocator getInstance() {
return ourInstance;
}
private ServiceLocator() {
this.cache = Collections.synchronizedMap(
new HashMap<String, java.lang.Object>());
}
public java.lang.Object getService(String serviceName) {
String serviceHost = "192.168.1.242";
if (this.cache.containsKey(serviceName)) {
return this.cache.get(serviceName);
}
try {
java.lang.Object service = this.getContext(
serviceHost).lookup(
serviceName + "Bean/remote");
cache.put(serviceName, service);
return service;
} catch (Exception e) {
e.printStackTrace();
logger.error("Unable to bind to service "
+ serviceName + ". Hostname: " + serviceHost, e);
return null;
} catch (Throwable e) {
e.printStackTrace();
logger.error("Unable to bind to service "
+ serviceName + ". Hostname: " + serviceHost, e);
return null;
}
}
public java.lang.Object getService(String serviceName, String serviceHost) {
String key = serviceHost + "." + serviceName;
if (this.cache.containsKey(key)) {
return this.cache.get(key);
} else {
try {
java.lang.Object service = this.getContext(
serviceHost).lookup(
serviceName + "Bean/remote");
cache.put(key, service);
return service;
} catch (Exception e) {
logger.error("Unable to bind to service "
+ serviceName + ". Hostname: " + serviceHost);
return null;
}
}
}
private Context getContext(String serviceHost) throws javax.naming.NamingException
{
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
properties.put(Context.PROVIDER_URL, serviceHost + ":1099");
return new InitialContext(properties);
}
}
Thanks
Sam
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4243629#4243629
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4243629
More information about the jboss-user
mailing list