JSF2 with EJB3 and JPA - how best to package - jsf-2

I'm trying to teach myself a bit about EJB3 and JPA by creating some functionality that uses JSF2 as a front end, stateless EJB3 session beans to handle transactions and JPA to persist entities.
Ideally I'd like the EJB app to be on a remote server, and the JSF2 war file on a separate server and communicate remotely - thus simulating a distributed project. But I'd like them to share the same JPA entity classes (as it seems kind of pointless converting them into other DTO objects just for front-end use - so it makes sense to me if the JPA project is in a project of its own
What I'm having trouble understanding is how to best package and deploy the various components.
Would it be best to deploy:
1) on server one: an EAR file containing the JPA jar file and the EJB module on one server. But if I do this where should the persistence.xml file be located - in the jpa jar file or in the ejb module?
and
2) on server two: a war file with the JSF application and the JPA jar file in the WEB-INF lib of this war. In this case, I assume the EJB interfaces will also need to be externalized into a jar file and included in the web-inf/lib?
or am I thinking in completely the wrong way?
Would appreciate any thoughts on what the best practices should be to achieve what I'm trying to do? Apologies in advance if I'm doing it all wrong...it's a learning experience!

First JPA objects are managed by persistence context hence i dont recommend to pass around that object to web layer. You may use JAXB objects to be sent to your web layer. JPA entity objects should be read and set to JAXB object fields and return the same.
Its faster and easier than pojo mapping frameworks.

Related

How do i build and application that works as both OSGI bundles and a WAR

I am developing and application as a series of OSGI bundles which will run on Karaf.
The Bundles has a fair bit of interoperability, both exposing a bunch of services, and consuming services from each other.
However, I would very much like to be able to build a WAR file of the application alongside the bundles. There are a few clients that quite simply won't allow an OSGI container on their servers.
So my question is, what is the best way to separate the OSGI service logic from the application logic? The activators are no trouble, as they are just unused in the WAR deployment, but i have a lot of calls that retrives services from the bundlecontext, like bundleContext.getServiceReference(stuffISortOfNeed.class)scattered around my code. bundleContext is currently a static object set by the activator.
This won't do in a WAR container that knows nothing of OSGI.
Is it possible to somehow hide the BundleContext and the getServiceReference calls away from the actual application? Preferably i would love an injection-like approach where i could define my services with #Annotations and define and injector for OSGI and one for PlainOldJava.
To really have POJOs you could use Blueprint XML to register your Web Application Bundle (WAB).
A good example can be found in the pax web project.
By using XML over annotations you only have an extra resource file that can be ignored if you assemble you bundles as libs in a war file, no extra dependencies on specific annotations.

How to use Grails Domain objects in a Java project?

I have a Grails application with outsourced (in a separate Grails Plugin) domain objects and would like to use the domain objects in a Java application.
If this is possible, how to do that?
Thanks
In order to use them as domain classes, you need GORM-plugin to be installed somehow in your java code. Without the plugin the domain classes lose their "active record" aspect and can be used only as POJOs.
I can barely imagine how to do that w/o lot of pain
You need to define your custom hbm.xml hibernate mapping files. See hibernate doc

How to reuse Grails application's domain classes?

I have already create a Grails application. For some reasons I need to create another console application for someone to modify database data.
Is it possible to package Grails application as a JAR library, so that the console application can reuse those domain classes?
Or, I add/create some classes in Grails application and package it as a JAR and run as console application?
If no better answer, probably I will use the batch-launcher plugin to do that.
You can put the domain classes in a JAR and tell Grails that these are your domain classes by adding a Hibernate XML file (in grails-app/conf/hibernate) that refers to the classes in this JAR. You can use this JAR in any other Java/Groovy application, but obviously they'll only have the persistence methods (dynamic finders, save(), etc.) when used in a Grails app.

Why do we need beans.xml while working with JSF web application?

I started web development using JSF, after studying JSP and Servlets for a while.
When you create a JSF web application you always(may be often but I am not sure if it always or not) have to create beans.xml and you don't write anything in it. But, if that file doesn't exist the JSF web app will not work.
What is the reason behind that?
Why we need that file?
Please, detailed explanation.
Adding to the answer of Micheal; CDI is not only useful in combination with JSF because of the injection support, but also because of its support to be used with EL (expression language). This is a feature JSF heavily depends upon.
In fact, CDI beans can almost entirely replace JSF managed beans and therefor you'll find lots of examples using them and a good amount of JSF books advising their use. For JSF applications CDI beans have e.g. the following advantages:
Can inject smaller scopes into larger scopes. E.g. request scoped GET parameters can be injected into a session scoped bean. This is not possible with JSF managed beans.
Can take advantage of the conversation scope; a scope that spawns multiple different pages.
Very unfortunate is that in JSF 2.0 and 2.1 the extremely handy view scope is not supported by CDI beans by default, although extensions like Seam can add these. (update: in JSF 2.2 there's a new view scope that does work with CDI beans)
All in all the confusion between JSF Managed beans and CDI beans is such that there's a JSF spec issue for this, see http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-976
The presence of a beans.xml file signals the servlet container that the application uses JSR-299 (Contexts and Dependency Injection) - without it, the application's classes will not be scanned for CDI annotations either and dependency injection will not be performed. If you don't use CDI (e.g. you only use plain JSF managed beans), you don't need beans.xml. If you do, you can use the file (instead of annotations) to define CDI configurations - this allows you, for example, to provide a test configuration where some dependencies are replaced with mocks.
From Getting Started with Contexts and Dependency Injection and JSF 2.x
When the application is deployed the server looks for CDI managed beans. In a Java EE 7 application, the classes on the path are scanned for CDI annotations by default. In a Java EE 6 application, the classes are scanned for CDI annotations if the module contains a beans.xml file.

Is it possible to add Grails MVC classes at deployment time?

I'm writing a Grails app which I'd like 3rd parties to augment at runtime. Ideally they would be able to add a JAR/WAR to the webapp directory which contains new domain, controller and service classes, new views, and other content.
Is there a simple way to do this within grails? Would it be simplest to create a startup script which copies the new classes etc. into the relevant directories and then updates grails.xml and web.xml?
You will be able to do this in version 2 of grails in which plugins will be also OSGI plugins http://jira.codehaus.org/browse/GRAILS/fixforversion/15421
It seems that the Grails plugins will actually fit quite well for this: http://www.grails.org/Understanding+Plugins
A plugin can do just about anything... One thing a plugin cannot do though is modify the web-app/WEB-INF/web.xml or web-app/WEB-INF/applicationContext.xml files. A plugin can participate in web.xml generation, but not modify the file or provide a replacement. A plugin can NEVER change the applicationContext.xml file, but can provide runtime bean definitions

Resources