How I can use Java Reflection API within EJB 3 entity? - reflection

I do the following steps with eclipse 3.5 ,JBoss 4.2 ,EJB3 but I face class not found exception
1.compiling this code to foo.jar file
package mypackage.foo;
import myejbpackage.ejb.fooInterface;
class foo implements fooInterface{
#override
void helloWorld(){System.out.print("HelloWorld");}
}
Note that fooInterface interface in is written inside the used EJB
2.using reflection , I take an instance from this class, also with the same ejb
package myejbpackage.ejb;
class fooCaller{
void call(){
Class foo= Class.forName("mypackage.foo.foo");
fooInterface iDataBackupWriter =(fooInterface) foo.newInstance();
fooInterface.helloWorld();
}
}
3.then calling it within stateless ejb3
package myejbpackage.ejb;
test(){
System.Out.Write("before calling");
new fooCaller().call();
}
4.then deploying to Jboss 4.2 and and puting foo.jar in default/lib
5.then calling the ejb 3 method.using simple client
it print :
"before calling"
and the following exception occurs in eclipse console
javax.ejb.EJBException: java.lang.RuntimeException: java.lang.NoClassDefFoundError: myejbpackage/ejb/fooInterface; nested exception is: java.lang.RuntimeException: java.lang.NoClassDefFoundError: myejbpackage/ejb/fooInterface
java.lang.RuntimeException: java.lang.NoClassDefFoundError:myejbpackage/ejb/fooInterface
Any suggestion ?
1.Is this exception from JBOOS, why ?
1.Where I should put the foo.jar to bee seen with the ejb3 jar ?
thanks in advance

there was a circular dependency which generate this problem

Related

System.InvalidOperationException when trying to mock a dependency and register with an IoC container

We have an interface...
public interface IService<T> where T : class
{
}
...which I need to mock as part of a component test project and register with a Castle Windsor container.
All I care about is that this interface has an implementation registered with the container to satisfy dependencies in the project.
I've found there's no way to generically mock such an interface (Mock<IService<>>? Mock<T, IService<T>>?) so I created a dummy class...
public class FakeT
{
}
...and registered the servcie like this...
container.Register(Component.For(typeof(IService<>), typeof(Mock<IService<FakeT>>)).LifestyleTransient());
...which at least compiles, but when I run a test, at the point Castle Windsor tries to resolve this dependency it throws the following exception:
[My.Namespace.IService`1[My.Tests.FakeT]] is not a
GenericTypeDefinition. MakeGenericType may only be called on a type
for which Type.IsGenericTypeDefinition is true.'
I'm obviously getting this totally wrong, so hopefully you can help :)
Create a class that implements your interface:
public class SomeClass : IService<FakeT>
{
}
Then register that class and interface:
container.Register(Component.For<IService<FakeT>>().
.ImplementedBy<SomeClass>()
.LifestyleTransient());

Provider org.togglz.slf4j.Slf4jLogProvider not a subtype

I have a library, built with Maven, that uses Spring 4.0.3.RELEASE and Togglz 2.2.0.Final. I'm trying to write a JUnit 4.11 test of my Spring class and running into the following error on the first test that gets executed:
testCreateItem_throwsItemServiceBusinessException(impl.ItemServiceImplTest) Time elapsed: 1.771 sec <<< ERROR!
java.util.ServiceConfigurationError: org.togglz.core.spi.LogProvider:
Provider org.togglz.slf4j.Slf4jLogProvider not a subtype
Here is the relevant java test snippet:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
#PrepareForTest({ ItemServiceImpl.class })
public class ItemServiceImplTest {
#Rule
public TogglzRule togglzRule = TogglzRule.allDisabled(Features.class);
#Rule
public PowerMockRule powerMockRule = new PowerMockRule();
#Test(expected = ItemServiceBusinessException.class)
public void testCreateItem_throwsItemServiceBusinessException() throws Exception {
PowerMockito.doReturn(mockMetricsData).when(serviceUnderTest, START_METRICS_METHOD_NAME, any(MetricsOperationName.class), any(RequestContext.class));
when(mockDao.createItem(any(Item.class), any(RequestContext.class))).thenThrow(dataBusinessException);
serviceUnderTest.createItem(item, context);
verify(mockItemServiceValidator).validate(any(Item.class), any(RequestContext.class));
PowerMockito.verifyPrivate(serviceUnderTest).invoke(START_METRICS_METHOD_NAME, any(MetricsOperationName.class), any(RequestContext.class));
verify(mockDao).createItem(any(Item.class), any(RequestContext.class));
}
}
Subsequent test calls get the following error:
java.lang.NoClassDefFoundError: Could not initialize class org.togglz.junit.TogglzRule
Here are some relevant dependencies I have:
org.mockito:mockito-all=org.mockito:mockito-all:jar:1.9.5:compile,
org.powermock:powermock-module-junit4=org.powermock:powermock-module-junit4:jar:1.5.6:test,org.powermock:powermock-module-junit4-common=org.powermock:powermock-module-junit4-common:jar:1.5.6:test,
org.powermock:powermock-reflect=org.powermock:powermock-reflect:jar:1.5.6:test,
org.powermock:powermock-api-mockito=org.powermock:powermock-api-mockito:jar:1.5.6:test,
org.powermock:powermock-api-support=org.powermock:powermock-api-support:jar:1.5.6:test,
org.powermock:powermock-module-junit4-rule=org.powermock:powermock-module-junit4-rule:jar:1.5.6:test,
org.powermock:powermock-classloading-base=org.powermock:powermock-classloading-base:jar:1.5.6:test,
org.powermock:powermock-core=org.powermock:powermock-core:jar:1.5.6:test,
org.powermock:powermock-classloading-xstream=org.powermock:powermock-classloading-xstream:jar:1.5.6:test,
org.togglz:togglz-core=org.togglz:togglz-core:jar:2.2.0.Final:compile,
org.togglz:togglz-slf4j=org.togglz:togglz-slf4j:jar:2.2.0.Final:compile,
org.togglz:togglz-spring-core=org.togglz:togglz-spring-core:jar:2.2.0.Final:compile,
org.togglz:togglz-testing=org.togglz:togglz-testing:jar:2.2.0.Final:test,
org.togglz:togglz-junit=org.togglz:togglz-junit:jar:2.2.0.Final:test
And I have provided a LogProvider (org.togglz.slf4j.Slf4jLogProvider) via SPI, located at META-INF/serivces/org.togglz.core.spi.LogProvider
This error is baffling as Slf4jLogProvider should be assignable from LogProvider. Sorry for the verbosity, but I wanted to try and show a complete picture. The code in class "under test" is making a call to see if a single feature is enabled inside the create method.
First of all: You don't need to configure the log provider in your application. Including togglz-slf4j on your application path is sufficient because this jar contains the corresponding SPI file.
Could you please check if there are multiple conflicting versions of the Togglz JAR files on your classpath? For example using togglz-core-2.2.0.Final.jar together with togglz-slf4j-2.1.0.Final.jar could result in an error like this.
This can happen if you update Togglz and your IDE didn't remove the old archives. Running a clean build and/or selecting "Update Maven Configuration" on Eclipse will fix this problem.

ClassCastException while call EJB over JNDI

I'm getting an ClassCastException while calling an EJB in an Pojo over JNDI. I use Oracle Weblogic Server 10.3.6 (EJB 3.0).
My structure:
app.ear
lib
Interfaces.jar
MyBeanInterface.java
ejb.jar
MyBeanImpl.java
webapp.war
Client.java
WEB-INF
web.xml
My local Interface:
package mypackage;
#Local
public Interface MyBeanInterface {}
My EJB-Class:
package mypackage;
#Stateless(name = "MyBean")
public class MyBeanImpl implements MyBeanInterface {}
My Client (not an EJB):
MyBeanInterface bean = (MyBeanInterface) new InitialContext().lookup("java:comp/env/ejb/MyBean");
My web.xml
<ejb-local-ref>
<ejb-ref-name>ejb/MyBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>mypackage.MyBeanInterface</local>
</ejb-local-ref>
My Exception:
The Lookup itself works. I get a reference. But when I want to cast with (MyBeanInterface) I get the following error:
Cannot cast an instance of "class mypackage.MyBeanInterface_whjkp6_MyBeanImpl (loaded by instance of weblogic.utils.classloaders.GenericClassLoader(id=28136))" to an instance of "interface mypackage.MyBeanInterface(loaded by instance of weblogic.utils.classloaders.GenericClassLoader(id=28144))
What can I do?
It seems the classes are loaded by different class loaders. Possible options are:
1) Ensure the classes are loaded by same class loader
2) Use reflection
3) Serialize and then deserialize
Refer:
1) cast across classloader?
2) https://community.oracle.com/thread/757133
3) ClassCastException because of classloaders?

EJB Remote Method Invocation with client

I have got a problem with ejb remote method invocation. For testing I created a ejb project with a statless bean and a simple method, declared in a remote interface. I deployed this project in a JBoss 7.1.
Now I want to call the business method hello in my bean from another class, which is not part of the project and not deployed in that jboss server. So if I understand it correct, this is a normal remote method call from a client.
I wrote my HelloWorldClient, declared the properties for the InitialContext(). But when I run this client, I got a NameNotFoundException. For me it seems like the exception is because of a false jndi-name, but I tried a lot to get the correct one.
On the console I got jndi names from my server for the project, but it doesn't work. Could someone help me solve this problem?
Client with context:
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
jndiProps.put(Context.SECURITY_PRINCIPAL, "testuser");
jndiProps.put(Context.SECURITY_CREDENTIALS, "testpasswort");
Context ctx = new InitialContext(jndiProps);
HelloWorldRemote hello = (HelloWorldRemote) ctx.lookup("java:global[/ejbproject1]/ejbproject1/HelloWorldBean[/HelloWorldRemote]");
My Bean:
#Stateless(name="hello")
public class HelloWorldBean implements HelloWorldRemote{
#Override
public String hello(String value) {
System.out.println("Say hello to "+value);
return "Hello " + value;
}
the jndi names from the jboss
18:19:07,464 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-6) JNDI bindings for session bean named hello in deployment unit deployment "ejbproject1.jar" are as follows:
java:global/ejbproject1/hello!interfaces.HelloWorldRemote
java:app/ejbproject1/hello!interfaces.HelloWorldRemote
java:module/hello!interfaces.HelloWorldRemote
java:jboss/exported/ejbproject1/hello!interfaces.HelloWorldRemote
java:global/ejbproject1/hello
java:app/ejbproject1/hello
java:module/hello
and the exception:
javax.naming.NameNotFoundException: global[/ejbproject1]/ejbproject1/HelloWorldBean[/HelloWorldRemote] -- service jboss.naming.context.java.jboss.exported.global[.ejbproject1].ejbproject1.HelloWorldBean[.HelloWorldRemote]
Change from
java:global/ejbproject1/hello!interfaces.HelloWorldRemote
to
java:/ejbproject1/hello!interfaces.HelloWorldRemote
That will work.

What is the relationship between Field 'SYSTEM_PROPERTIES_MODE_ENVIRONMENT' and PropertyPlaceHolderConfigurer?

I am using spring 3.0.6-RELEASE for a basic form based webapp and running into the following error on startup with PropertyPlaceHolderConfigurer. Not sure what's causing this as injecting the property pre-mvc3 was done through beans and now using annotation and it appears quite straight-forward atleast from the docs/other posts.
However the exception during tomcat server startup complains of org.springframework.core.ConstantException: Field 'SYSTEM_PROPERTIES_MODE_ENVIRONMENT' not found in class [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer]
Here is what I have in spring-servlet.xml
<context:property-placeholder location="/WEB-INF/classes/spring.properties"/>
and my controller class
#Controller
#RequestMapping("/home")
public class HomeController {
// #Resource(name="broomManager")
#Autowired
private broomManager broomManager;
private #Value("${spring.broom.maxFileSize}") String inMemoryMaxFileSize;
private #Value("${spring.broom.fileDestination}") String destFilePath;
..
spring.properties
broom.maxFileSize=100000
broom.fileDestination=/Users/foobar/broom/files/
Exception during startup
2012-06-08 02:51:31,900 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0': Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'systemPropertiesModeName' threw exception; nested exception is org.springframework.core.ConstantException: Field 'SYSTEM_PROPERTIES_MODE_ENVIRONMENT' not found in class [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:626)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:467)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:483)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:358)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:325)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3956)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4230)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:831)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:720)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1217)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1306)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1570)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1579)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1559)
at java.lang.Thread.run(Thread.java:680)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'systemPropertiesModeName' threw exception; nested exception is org.springframework.core.ConstantException: Field 'SYSTEM_PROPERTIES_MODE_ENVIRONMENT' not found in class [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 33 more
Probably the following is going on:
Your Spring config files are pointing at the 3.1 schemas.
You are using 3.0.6 JARs.
They need to match.

Resources