Wildfly see resources from external jar in different ears - wildfly

Is it possibile to scan some directories to find all jars and add them as dependencies in ears at runtime?
I'm using wildfly 8.1.0 final.
For example we have two separated ear:
ear1
ear2
And an external jar with some jsf managed beans, facelets, and static content (images, css, js):
jar1
Does exist any way to make all resources in jar1 to be accessible from ear1 and ear2 without put jar1 as module in ear1 or ear2?
I don't know any other way than add jar1 as dependency in ear1 and ear2 and redeploy ear1 and ear2.

Standard Java EE way is create EJB services in war1 module. After this you should use JNDI name lookup in ear1, ear2 and consume EJB interfaces provided by war1. If you need dynamicly add JSF resources (jsf pages, and static files) you should write custom implementation of javax.faces.application.ResourceHandlerWrapper. Your implementation should transfer resource request to war1 module (using EJB interface).
In this case you have weak dependency between ear1,ear2 and war1 modules. Also you can have a set of war modules and some registry with JNDI names or use list method: Wildfly jndi list.
But I am not sure how transfer call to war1 managed bean. I think you should use EJB in war1 module, and write some proxy JSF bean in ear1,ear2.

Related

overriding JNDI names in JBoss EJB3.1

We're using Maven and Artifactory, and therefore our ear files have names like
our-project-ear-0.0.1-20151215.151526-3.ear.
So JNDI names for our EJBs have names like
java:global/our-project-ear/our-project-ejb/AnEjbJar!com.acme.ourproject.SomeEjb.
These names are not just ugly and complex, they also embed temporary suffixes added by Maven/Artifactory.
I thought we could simplify the names via the JBoss-specific #RemoteHomeBinding annotation, but I do not find this annotation in EAP 6.3. Is there still a way to do this? If not, how can I control the JNDI name under which my EJBs are published?
Figured it out myself. The #RemoteBinding, #LocalBinding, #RemoteHomeBinding and #LocalHomeBinding annotations have been phased out in AS7.x. A version of the JBoss instructions for migrating from AS5/6 to AS7 (not the current one) states:
>In AS7 there is no possibility for custom JNDI names of EJB beans and it's not planned for 7.1.
>Therefor the annotation #RemoteBindings and #LocalBindings are not available.
The recommended approach is to use the default bindings. However, custom JNDI names can also be defined via the #EJB annotation, as stated in this Oracle blog:
The developer can select an additional JNDI name that resolves to a particular client view of a session bean by using the #EJB annotation. Starting with Java EE 6, the #EJB name() attribute value can be prefixed with any one of the three portable Java EE namespaces : java:global, java:app, java:module. This has the effect of exporting the dependency into the selected scope.
I think what you are looking for is :
#Ejb(lookup="java:/global/somecustomPath")
As this will not affect where in JNDI the bean is bound but rather where to find it in JNDI.
If I'm right this is because JBOSS7 is a JEE6 application server and JEE6 introduced the concept of Portable Global JNDI names in JEE6.

How to use a Liferay service builder jar in other portlets

I have a Liferay portal project built using the service builder. In generating the portlet a jar is also created and I want to use that jar in other portlets. From posts here and elsewhere the suggested approach is to just place the myPortletName-portlet-services.jar in the WEB-INF/lib folder of the other portlet. I have also seen where the *-portlet-services.jar is placed in the /lib/ext folder of Tomcat. I have also read where the liferay-plugin-package.properties should be updated with the dependency.
I have tried each approach and each produces a result that is tantalizingly close to working. The service builder built jar references a jndi/jdbc global resource that is different than the LR database and when a method is invoked from that jar, I receive a "user lacks privilege or object not found:" error (I have posted about this elsewhere).
My hunch is that the jar has some dependencies on Spring/Hibernate that are not being met. That, or the jndi/jdbc resource isn't "visible" when the jar is placed outside the service builder deployed portal. In any case, it's obvious that the jar's methods are being found (else the dreaded beanLocator error) so it is now simply a DB connection issue.
The question is: Are there some structural dependencies that are not being met when a jar generated by the service builder is placed outside the originating portal in some other portal?
For use a Liferay service builder jar in other portlets
Try:
create ServiceLocator placed in jar in lib of Tomcat (or other aplication server).
set your bean from service builder to service locator by setter as static field in spring context (setter must set static field)
Next implement bean that will use bean from service locator and will delagate calls
simply pass your bean from service builder through class static field placed in tomcat lib (visible everywhere)
place required interfaces also in jar in lib

Who runs the EJB Classes in WAR File

From the Java EE6 documentation, I could see:
To include enterprise bean class files in a WAR module, the class files should be in the WEB-INF/classes directory.
To include a JAR file that contains enterprise beans in a WAR module, add the JAR to the WEB-INF/lib directory of the WAR module.
With this structure, EJBs are part of WAR file. I can have some JSP and Servlet also inside my war file.
I've also read that an Application server contains two containers, one of them is web container that handles all the JSP/Servlets and an app container that handles all the Enterprize beans.
I'm assuming that If I deploy this WAR file (that contains EJB + JSP/Servlets), App-Server will be smart enough to delegate the processing of JSP/Servlets to Web Container and EJBs processing to app containers? Is my assumption correct?
Yes, your assumption in correct.

How to do Application specific Configuration for a Framework

i want to write a Java EE framework for a generic type of applications.
I'm looking for a way to handle application specific config values in my framework.
To give an example:
A component supplies a stateless session bean that handles persistence and i want to configure the name of the datasource that is used in a config file in my application (for example in the web-inf folder of the applications ear).
Now i have X>1 Applications that want to configure X different datasources for their specific persistence management.
Can anybody give me an example how to do that?
Greetings,
Alexander
You should use the JNDI provider thats bundled with the application container. One of its purposes it to access resources, data sources in your case, in a highly dynamic fashion.
For JBoss, setting up a data source involves the following steps:
deploy a *-ds.xml configuration file to the JBoss server's deploy directory. This defines the global JNDI name of the resources. There should be plenty of examples for a lot of databases available on the internet.
add a resource-ref to the jboss-web.xml of any WAR or to the jboss.xml of any EJB jar for any bean that needs the resource. This defines the global JNDI name to local/component JNDI name mapping.
add a resource-ref using the local JNDI name to the web.xml of any WAR or to the ejb-jar.xml of any EJB jar for any bean that needs the resource.
Once those things are in place, you can perform JNDI lookups to access the configured resource.
You can do these things to configure multiple data sources in JBoss and then make one or more of these data sources available to the web applications and EJBs.

Separate JSF and EJB apps

How do I use a deployed EJB app from a separate JSF application?
I'm attempting to separate the two applications and access the EJB through the remote interface. To do this I have two eclipse projects - one contains the EJB and persistence logic, tested independently and works. I then created a JSF project that references the EJB project (so I gain access to the remote interface), however this fails when attempting to either inject the EJB instance or lookup the JNDI name (I've tried several variants to no avail). This is what my JSF backing bean contains:
#EJB(lookup="java:global/LocEJB/LocalityEJB!com.ame.business.LocalityEJBRemote")
private LocalityEJBRemote locality;
This is on Glassfish, and I am only referencing the EJB project and not packaging it with the JSF project. When I do the latter, I receive error initializing EJB container problems on the JSF project. So, how do I access the remote EJB and does the way I'm approaching this make any sense?
Thanks in advance!
Your JSF application has to know about the EJB interfaces (at least they did on EJB 2.0). You're using the Proxy pattern to hide the fact that this is a remote component from your JSF client.
First of all you can not use Local interface if trying to access outside the container. You must use Remote Interface.
You can define your Remote interface in the sun-web.xml or EJB injection in the bean.
sun-web.xml code:
<ejb-ref>
<ejb-ref-name>com.xxx.session.UserRemote</ejb-ref-name>
<jndi-name>corbaname:iiop:127.0.0.xxx:3700#com.xxx.session.UserRemote</jndi-name>
</ejb-ref>
Another thing you must have Remote interfaces in your classpath.