I have a somewhat strange problem with Arquillian tests and CDI injections:
This bean works perfectly fine when it's being tested:
#RequestScoped
public class Greeter {
#Inject
protected GreetingProvider greetingProvider;
public String doGreeting(String greet) {
System.out.println("Information: greetingProvider=" + greetingProvider);
return String.format("%s, %s!", greetingProvider.getGreetingWord(), greet);
}
}
with the only exception, that in the following test case the method "testGreetingProviderInGreeterIsNotNull()" always fails
#RunWith(Arquillian.class)
public class GreeterTest {
#Deployment
public static WebArchive createDeployment() {
WebArchive deployment = ShrinkWrap.create(WebArchive.class)
.addClass(Greeter.class)
.addClass(GreetingProvider.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") ;
return deployment;
}
#Inject
private Greeter greeter;
#Test
#InSequence(1)
public void testGreeterIsInjectedProperly() {
assertNotNull(greeter);
}
#Test
#InSequence(2)
public void getGreetingTest() {
String name = "John";
String expected = "Hello, " + name + "!";
String actual = greeter.doGreeting(name);
assertEquals(expected, actual);
}
#Test
#InSequence(3)
public void testGreetingProviderInGreeterIsNotNull() {
assertNotNull(greeter.greetingProvider);
}
}
I do not understand why, cause if the field greetingProvider were null also the second test method should fail.
What's my error?
The full source code is available on GitHub:
https://github.com/t3chris/ArquillianInjectionTests
I believe, but not 100% sure, it's because you're missing an arquillian.xml in your src/test/resources folder that should force the default protocol to Servlet 3. By default, in AS7/EAP6 the protocol is their JMX protocol. When you use their JMX protocol the RequestScope is not active for the interaction.
Your arquillian.xml should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<defaultProtocol type="Servlet 3.0" />
</arquillian>
Related
I'm working on Liferay PortletMVC, I injected the protected MVCActionCommand mvcActionCommand; with the #Reference(target = "(component.name=String.Here)", unbind = "-") to do some functions stuff inside the doProcessAction() method of my MVCActionCommand component.
My question is how can I set the component.name in the target of #Reference attribute, Should I put any String or should I put a defined one ?
component.name target the MVC command class name. So you need to provide the class name including it's package name.
To inject the instance variable mvcActionCommand, you can use something like this:
#Reference(target = "(component.name=com.test.service.impl.TestServiceImp)",
unbind = "-")
public void setMvcActionCommand(MVCActionCommand mvcActionCommand) {
this.mvcActionCommand = mvcActionCommand;
}
I wrote a full example here:
JAVA:
#Component(
immediate = true,
property = {
"javax.portlet.name=YOU_COMPONENT_NAME",
"mvc.command.name=/hello"
},
service = MVCActionCommand.class
)
public class LoginTestCommand extends BaseMVCActionCommand {
protected MVCActionCommand mvcActionCommand;
#Reference(target = "(component.name=com.liferay.login.web.internal.portlet.action.LoginMVCActionCommand)",
unbind = "-")
public void setMvcActionCommand(MVCActionCommand mvcActionCommand) {
this.mvcActionCommand = mvcActionCommand;
}
#Override
protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
System.out.println( "Login account doProcessAction" );
mvcActionCommand.processAction(actionRequest, actionResponse);
}
}
JSP:
<portlet:actionURL var="sayHelloURL" name="/hello">
<portlet:param name="mvcActionCommand" value="/hello" />
</portlet:actionURL>
Check this example too on Github.
See this TUTORIAL for more details about Overriding MVC Commands.
I have been using Tomcat 6.0.26-6.0.35 for several years with JSF 2 Mojarra, various versions up to 2.1.2 which I have been using for some months. I have several request-scoped and session-scoped beans with code like this:
private #Resource(name="jdbc/cLabs", mappedName="jdbc/cLabs") DataSource cLabs;
which has been correctly injected in every version of Tomcat 6 I've used. I also have other types of #Resource that doesn't work either, so it isn't just DataSource resources. I've tried switching to Tomcat 7.0.27 and suddenly none of these constructs works any more. The resource is not injected. I also have other types of #Resource that doesn't work either, so it isn't just DataSource resources. However in each case the resource named does exist, and can be looked up via e.g.
new InitialContext().lookup("java:comp/env/jdbc/cLabs");
[They are defined by elements in context.xml]
This of course is a royal PITA as I spent some time a year or two ago replacing the latter with the former. Is there some other magic spell I have to weave with Tomcat 7 to make it work again?
Note that resources are injected correctly into Servlets, so it isn't completely broken. Some interaction between Tomcat and JSF.
My guess is something is causing org.apache.catalina.core.DefaultInstanceManager to ignore annotations because faces requires the #PostConstruct method to be processed apart from #Resource fields. I have created a workaround that works for fields annotated with #Resource.
Add the following to web.xml:
<context-param>
<param-name>com.sun.faces.injectionProvider</param-name>
<param-value>com.example.faces.Tomcat7InjectionProvider</param-value>
</context-param>
And add the class to your source:
package com.example.faces;
import java.lang.reflect.Field;
import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContext;
import org.apache.catalina.util.Introspection;
import com.sun.faces.spi.InjectionProviderException;
import com.sun.faces.vendor.WebContainerInjectionProvider;
public class Tomcat7InjectionProvider extends WebContainerInjectionProvider {
public Tomcat7InjectionProvider(ServletContext servletContext) {
}
#Override
public void inject(Object managedBean) throws InjectionProviderException {
if (managedBean != null) {
// see org.apache.catalina.core.DefaultInstanceManager
Field[] fields = Introspection.getDeclaredFields(managedBean.getClass());
for (Field field : fields) {
// field may be private
field.setAccessible(true);
if (field.isAnnotationPresent(Resource.class)) {
Resource annotation = null;
try {
annotation = field.getAnnotation(Resource.class);
Context ctx = new InitialContext();
Object resource = ctx.lookup("java:comp/env/" + annotation.name());
field.set(managedBean, resource);
} catch (Exception e) {
throw new InjectionProviderException("cannot find resource " + annotation.name(), e);
}
}
}
}
}
}
Answering my own question, an improved version of #JeffE's answer. The basic problem is:
A Tomcat6InjectionProvider was provided with JSF 2.0 but was removed at some point.
The default WebContainerInjectionProvider doesn't process #Resource annotations, as JeffE points out.
You can overcome this without web.xml context-entries as follows:
Create a file called META-INF/services/com.sun.faces.spi.injectionprovider and add the following line to it:
com.sun.faces.vendor.Tomcat7InjectionProvider:org.apache.catalina.core.DefaultInstanceManager
The meaning of this line is that if the second class is present in the deployment, the first class is used as the injection provider. The second class above is part of Tomcat 7.
Compile the following class.
This version contains numerous improvements over JeffE's version. Specifically:
it processes superclasses, as required by the #Resource and #Resources Javadoc
it processes #Resource and #Resources annotations at the class level
it processes methods annotated with #Resource, as required by the #Resource Javadoc
it handles empty or missing name attributes of #Resources correctly, as required by the #Resource Javadoc
it restores the Field's original access
it has no dependencies on Tomcat classes.
Adjust the package name above if you change its package name.
package com.sun.faces.vendor;
import com.sun.faces.spi.DiscoverableInjectionProvider;
import com.sun.faces.spi.InjectionProviderException;
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
/**
* #author Jeff E
* #author Esmond Pitt Improvements named above.
*
* #see javax.annotation.Resource
*
* #see <a href="http://stackoverflow.com/a/21978577/207421">This StackOverflow
* answer, although what org.apache.catalina.util.Introspection may be and where
* it lives remains a mystery.</a>
*/
public class Tomcat7InjectionProvider
extends DiscoverableInjectionProvider
{
private Logger logger = Logger.getLogger(this.getClass().getName());
private ServletContext servletContext;
private WebContainerInjectionProvider delegate = new WebContainerInjectionProvider();
public Tomcat7InjectionProvider(ServletContext servletContext)
{
logger.config("constructed");
this.servletContext = servletContext;
}
#Override
public void inject(Object managedBean) throws InjectionProviderException
{
logger.log(Level.CONFIG, "managedBean={0}", new Object[]{managedBean.getClass().getName()});
Class<?> clazz = managedBean.getClass();
do
{
List<Resource> classResources = new LinkedList<>();
// Process class-level #Resources and #Resource
if (clazz.isAnnotationPresent(Resources.class))
{
Resources annotation = clazz.getAnnotation(Resources.class);
for (Resource resource : annotation.value())
{
classResources.add(resource);
}
}
if (clazz.isAnnotationPresent(Resource.class))
{
Resource annotation = clazz.getAnnotation(Resource.class);
classResources.add(annotation);
}
for (Resource annotation : classResources)
{
String name = annotation.name();
// Make sure the resource exists.
try
{
Context ctx = new InitialContext();
Object resource = ctx.lookup("java:comp/env/" + name);
}
catch (NamingException exc)
{
throw new InjectionProviderException("checking class resource " + annotation.name()+" of "+clazz.getName(), exc);
}
}
// Process fields with #Resource
// see org.apache.catalina.core.DefaultInstanceManager
// Field[] fields = Introspection.getDeclaredFields(managedBean.getClass());
Field[] fields = managedBean.getClass().getDeclaredFields();
for (Field field : fields)
{
if (field.isAnnotationPresent(Resource.class))
{
Resource annotation = field.getAnnotation(Resource.class);
String name = annotation.name();
logger.log(Level.CONFIG, "injecting #Resource(name=\"{2}\") into {0}.{1}", new Object[]
{
managedBean.getClass().getName(), field.getName(), name
});
try
{
Context ctx = new InitialContext();
Object resource;
if (name != null && name.length() > 0)
{
resource = ctx.lookup("java:comp/env/" + name);
}
else
{
resource = ctx.lookup(clazz.getName() + "/" + field.getName());
}
// field may be private
boolean accessibility = field.isAccessible();
try
{
field.setAccessible(true);
field.set(managedBean, resource);
}
finally
{
field.setAccessible(accessibility);
}
}
catch (NamingException | IllegalAccessException exc)
{
throw new InjectionProviderException("injecting resource " + annotation.name()+" into "+clazz.getName()+"."+field.getName(), exc);
}
}
}
// Process methods with #Resource
for (Method method : clazz.getDeclaredMethods())
{
if (method.isAnnotationPresent(Resource.class)
&& method.getName().startsWith("set")
&& method.getName().length() > 3
&& method.getReturnType() == void.class
&& method.getParameterTypes().length == 1)
{
// It's a setter with #Resource
Resource annotation = method.getAnnotation(Resource.class);
String name = annotation.name();
logger.log(Level.CONFIG, "injecting #Resource(name=\"{2}\") via {0}.{1}", new Object[]
{
managedBean.getClass().getName(), method.getName(), name
});
try
{
Context ctx = new InitialContext();
Object resource;
if (name != null && name.length() > 0)
{
resource = ctx.lookup("java:comp/env/" + name);
}
else
{
name = method.getName().substring(3);
name = name.substring(0,1).toLowerCase()+name.substring(1);
resource = ctx.lookup(clazz.getName() + "/" + name);
}
// method may be private
boolean accessibility = method.isAccessible();
try
{
method.setAccessible(true);
method.invoke(managedBean, resource);
}
finally
{
method.setAccessible(accessibility);
}
}
catch (NamingException | IllegalAccessException | InvocationTargetException exc)
{
throw new InjectionProviderException("injecting resource " + annotation.name()+" via "+clazz.getName()+"."+method.getName(), exc);
}
}
}
} while ((clazz = clazz.getSuperclass()) != Object.class);
}
#Override
public void invokePostConstruct(Object managedBean) throws InjectionProviderException
{
logger.log(Level.CONFIG, "managedBean={0}", new Object[]{managedBean});
delegate.invokePostConstruct(managedBean);
}
#Override
public void invokePreDestroy(Object managedBean) throws InjectionProviderException
{
logger.log(Level.CONFIG, "managedBean={0}", new Object[]{managedBean});
delegate.invokePreDestroy(managedBean);
}
}
E&OE
Another possibility, yet I would think unlikely; is you are using metadata-complete="true" in your web.xml or web-fragment.xml file(s).
Defined metadata-complete:
The metadata-complete attribute defines whether this deployment descriptor and other related deployment
descriptors for this module (e.g., web service descriptors) are complete, or whether the class files available
to this module and packaged with this application should be examined for annotations that specify
deployment information. If metadata-complete is set to "true", the deployment tool must ignore any
annotations that specify deployment information, which might be present in the class files of the
application. If metadata-complete is not specified or is set to "false", the deployment tool must examine the
class files of the application for annotations, as specified by the specifications.
Example web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ResourceTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Sorry I'm unable to comment due to rep or I would of asked for more clarification on what errors/etc you are seeing. That being said I have tested with both Tomcat 7.0.27 and Tomcat 7.0.41 with java full version "1.6.0_51-b11-457" and was able to use #Resource.
Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/Sample" auth="Container"
type="javax.sql.DataSource" username="nbuser" password="nbuser"
driverClassName="org.apache.derby.jdbc.ClientDriver"
url="jdbc:derby://localhost/Sample"
maxActive="8" maxIdle="4"/>
</Context>
ResourceTest.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
#WebServlet("/ResourceTest")
public class ResourceTest extends HttpServlet {
private static final long serialVersionUID = 1L;
#Resource(name="jdbc/Sample")
private DataSource ds;
public ResourceTest() {
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.println("<body>");
try {
//Context initCtx = new InitialContext();
//Context envCtx = (Context) initCtx.lookup("java:comp/env");
//DataSource ds = (DataSource) envCtx.lookup("jdbc/Sample");
Connection conn = ds.getConnection();
Statement s = conn.createStatement();
s.execute("Select * From \"NBUSER\".\"Friends\"");
ResultSet rs = s.getResultSet();
while (rs.next()) {
out.println(rs.getString("NAME") + " is my friend.");
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
out.println("</body>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
I am in a Jboss AS 7 environment. My application's /admIn/* path is protected by a security-constraint which requires form based authentication. Security domain is database backed.
It's ok but now I want to display a "good morning " in each page's header.
I'm looking for some sort of getLoggedUsername() or getPrincipal() function but I can't find it.
Please post a reference to the official docs if any.
Thank you.
You should be able to use JAAS. Which is what JBoss 7 ought to be using.
The calling principal will be stored in a SessionContext which you can obtain by telling JBoss it's a resource.
#Resource
private SessionContext context;
public void myAwesomeMethod() {
String currentUser = context.getCallerPrincipal().getName();
}
If for some reason the Injection doesn't work on a Stateless bean, you can look up the EJBContext direct.
#Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/EJBContext");
System.out.println("look up EJBContext by standard name: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
}
This snippet was obtained from 4 ways to obtain EJBContext.
I have a service that needs to invoke a runnable class.
Here are the lines of code that are being used in my service.
#Autowired
private LinkBrc2MemberProfile brcTask;
// Background Task.
SimpleAsyncTaskExecutor sate = new SimpleAsyncTaskExecutor();
sate.createThread(new LinkBrc2MemberProfile(user));
Here is my Runnable class
#Service
public class LinkBrc2MemberProfile implements Runnable {
private final Logger log = LoggerFactory.getLogger(LinkBrc2MemberProfile.class);
#Autowired
private LoyaltyDao dao;
private Member member;
public LinkBrc2MemberProfile() {
super();
}
public LinkBrc2MemberProfile(Member member) {
this.member = member;
}
public void run() {
log.debug("*** Member User Name: " + member.getString("USER_NAME"));
String emailAddress = member.getString("USER_NAME");
Map<String, Object> map = dao.findBrcByEmailAddress( emailAddress );
log.debug("==========================================================");
if( ! map.isEmpty() ) {
try {
//a.CUSTOMER_ID, a.EMAIL_ADDRESS, b.card_no
String customerId = (String) map.get("CUSTOMER_ID");
String brcCardNumber = (String) map.get("CARD_NO");
log.debug("\ncustomerId: " + customerId + " brcCardNumber: " + brcCardNumber);
if(!brcCardNumber.equals("")) {
// Add the Be Rewarded Card.
HashMap<String, String> userAttributes = new HashMap<String, String>();
String brcNumber = member.getString("BREWARDED_CARD_NO");
if (brcNumber.equals("")) {
userAttributes.put("BREWARDED_CARD_NO", brcCardNumber);
try {
member.putAll(userAttributes);
} catch (Exception e) {
String errorMessage = "Unable to save user's BRC information due to: " + e.getMessage();
log.error("{}", errorMessage);
}
}
}
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
I'm not seeing any errors in the log but at the same time it does not appear to be invoking the Runnable class. Am I missing an annotation somewhere? Are there any good examples that you can point me to, the only ones I have found use XML files to configure the runnable class I would like to use annotations. Thanks in Advance.
I've updated my service to do the following.
Please help, my DAO is NULL so it looks like my #Autowired in my Runnable class is not wiring it in.
I've added the following bean to my bean-config.xml file.
<bean id="brcType" class="com.ws.ocp.service.LinkBrc2MemberProfile" scope="prototype"/>
I removed my #Autowired annotation and added the following to my service class.
ClassPathResource rsrc = new ClassPathResource("bean-config.xml");
XmlBeanFactory factory = new XmlBeanFactory(rsrc);
LinkBrc2MemberProfile brcTask = (LinkBrc2MemberProfile) factory.getBean("brcType");
SimpleAsyncTaskExecutor sate = new SimpleAsyncTaskExecutor();
// Set Member attribute
brcTask.setMember(user);
// Executer
sate.execute(brcTask);
Why is my dao still null?
The runnable will throw a NullPointerException, since you create it yourself (using the new operator), instead of letting Spring create it. This obviously means that the autowired DAO attribute won't be autowired, which will lead to a NPE when calling dao.findBrcByEmailAddress(...).
You should get your Runnable instance from the bean factory (as a prototype), set its member attribute, and then submit it to the executor.
To answer your question of how to properly use a Prototype-Bean, this is my favorite way:
#Component
abstract class MyBean {
/* Factory method that will be installed by Spring */
#Lookup
protected abstract YourPrototypeBean createBean();
void someCode() {
YourPrototypeBean bean = createBean();
}
}
Since it's a factory method, you can create as many instances as you like.
How can I access a jsf managed bean (say, icefaces) from a blazeds client?
Will it be possible to share the same session information also ? (for eg, if I have a page with a jsf/icefaces component and a swf client - can they use the same session?)
first of all you have to implement the FlexFactory in your own Factory:
http://livedocs.adobe.com/blazeds/1/blazeds_devguide/factory_2.html
I change the SpringFactory a little bit so you can access to the current instance of the given bean:
public class BeanFactory implements FlexFactory {
private static final String SOURCE = "source";
public void initialize(String id, ConfigMap configMap) {}
public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
BeanFactoryInstance instance = new BeanFactoryInstance(this, id, properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
return instance;
}
public Object lookup(FactoryInstance inst) {
BeanFactoryInstance factoryInstance = (BeanFactoryInstance) inst;
return factoryInstance.lookup();
}
static class BeanFactoryInstance extends FactoryInstance {
BeanFactoryInstance(BeanFactory factory, String id, ConfigMap properties) {
super(factory, id, properties);
}
public String toString() {
return "BeanFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();
}
public Object lookup() {
HttpServletRequest hsr = FlexContext.getHttpRequest();
String beanName = getSource();
try
{
Object o = hsr.getSession().getAttribute(beanName);
return o;
}
catch (Exception e)
{
ServiceException se = new ServiceException();
String msg = "Java Bean '" + beanName + "' does not exist.";
se.setMessage(msg);
se.setRootCause(e);
se.setDetails(msg);
se.setCode("Server.Processing");
throw se;
}
}
}}
in the service-config.xml (WEB-INF/flex folder) you have to register this factory:
<factories>
<factory id="beanFactory" class="packageName.BeanFactory"/>
</factories>
then you have to register the factory in your destination in the remoting-config.xml like this:
<destination id="remoteService">
<properties>
<factory>beanFactory</factory>
<source>beanName</source>
<scope>session</scope>
</properties>
</destination>
so what is this BeanFactory doing:
when you want to access per remote from flex to java or to the jee-application, you declare a remoteobject in flex with the destination "remoteService" which is configured in the remoting-config.xml. the moment you access from flex to java by calling a server-sided-method, the beanfactory looks after the current instance of the bean you declare in the remoting-config.xml by getting the Request over the FlexContext:
HttpServletRequest hsr = FlexContext.getHttpRequest();
now you get the session and with the beanName the instance by callinghsr.getSession().getAttribute(beanName)
this is only working with application and session beans and only if jsf instantiated the bean before the BeanFactory want to access the bean...
when you want to integrate a swf-file with icefaces you should take the ice:outputMedia-Tag an set the player-attribute to "flash"
if you work with eclipse to develop your jee-application and you integrate the tomcat in eclipse you can set the server root folder in the properties of your flex-project to the tomcat folder (flex builder):
(Sorry not time for making this looks good ;) )
now you can start the tomcat server directly in eclipse and you can debug on flex and java too :)
hope this helps!