Markus is a Developer Advocate at Red Hat and focuses on JBoss Middleware. He is working with Java EE servers from different vendors since more than 14 years and talks about his favorite topics around Java EE on conferences all over the world. He has been a principle consultant and worked with different customers on all kinds of Java EE related applications and solutions. Beside that he has always been a prolific blogger, writer and tech editor for different Java EE related books. He is the Java Community leader of the German DOAG e.V. and it's representative on the iJUG e.V. As a Java Champion and former ACE Director he is well known in the community. Markus is a DZone MVB and is not an employee of DZone and has posted 193 posts at DZone. You can read more from them at their website. View Full User Profile

Java EE 6 DDD Galleria on WebLogic 12c

03.16.2012
| 4703 views |
  • submit to reddit
You probably followed me with the last Java EE 6 Galleria example posts. The first one was the basic introduction. The second one was about running it on latest GlassFish. Someone of the RedHat guys mentioned, that we should look into bringing this example off from GlassFish. Great ;) Thanks for the nice idea. That is exactly what we are going to do today. I am going to bring the Galleria example to latest WebLogic 12c.

Preparation
Get yourself in the mood for some configuration. You already have the latest NetBeans 7.1 installed and you are going to download the WebLogic 12c ZIP distribution in a second. After you have downloaded the wls1211_dev.zip put it to a location of choice and unzip it. From now on we are going to call this folder the %MW_HOME% folder. Open a command line and setup %JAVA_HOME%, %JAVA_VENDOR% and %MW_HOME% variables in it:
set JAVA_HOME=D:\jdk1.7.0_04
set MW_HOME=D:\temp\wls12zip
set JAVA_VENDOR=Sun
After you have done this one final step is to run the installation configuration script configure.cmd in the MW_HOME directory. This is a one time thing to do.

Setup your WebLogic Domain
Next thing we need is a WebLogic domain. Open a new command line prompt. Setup your environment in the current shell by running the %MW_HOME%\wlserver\server\bin\setWLSEnv.cmd script. Execute the %MW_HOME%\wlserver\common\bin\config.cmd and follow the wizard to create a basic WebLogic Server Domain called test-domain in a folder of your choice (e.g. D:\temp\test-domain). Give a username and password of your choice (e.g. system/system1) and click through the wizard until you have a "finish" button down there. WebLogic needs the Derby client jar file in order to configure and use the database. Copy the derbyclient-10.8.2.2.jar from your m2 repository to the test-domain\lib folder. Now lets start the newly created domain manually by running the startWebLogic.cmd in your newly created domain directory. Verify that everything is up and running by navigating to http://localhost:7001/console and logging in with the credentials from above. Navigate to "Services > Data Sources" and select the "New" button from above the table. Select a "Generic Datasource" and enter a name of your choice (e.g. GalleriaPool) and enter jdbc/galleriaDS as the JNDI-Name. Select Derby as the Database Type and click "next". Select Derby's Driver (Type 4) and click "Next" and "Next" and enter the connection properties (Database: GALLERIATEST, Host: localhost. User and Password: APP" and click "Next". If you like to, you can hit the "Test Configuration" button on top and make sure everything is setup in the right way.
Next the most tricky part. We need a JDBC realm like the one we configured for GlassFish. First difference here is, that we don't actually create a new realm but add an authentication mechanism to the available one. There is a nasty limitation with WebLogic. You can configure as many security realms as you like, but only one can be active at a given time. This stopped myself for a while until I got the tip from Michel Schildmeijer (thanks, btw!). Navigate to "Security Realms" and select "myrealm" from the table. Switch to the Providers tab. Select "New" above the table of the Authentication Providers. Enter "GalleriaAuthenticator" as the name and select "SQLAuthenticator" from the dropdow-box as a type. Click ok. Select the GalleriaAuthenticator and set the Control Flag: SUFFICIENT and save. After that switch to the "Provider Specific" tab. Enter the following:
Data Source Name: GalleriaPool
Password Style Retained: unchecked
Password Algorithm: SHA-512
Password Style: SALTEDHASHED
SQL Get Users Password: SELECT PASSWORD FROM USERS WHERE USERID = ?
SQL Set User Password: UPDATE USERS SET PASSWORD = ? WHERE USERID = ?
SQL User Exists: SELECT USERID FROM USERS WHERE USERID = ?
SQL List Users: SELECT USERID FROM USERS WHERE USERID LIKE ?
SQL Create User: INSERT INTO USERS VALUES ( ? , ? )
SQL Remove User: DELETE FROM USERS WHERE USERID = ?
SQL List Groups: SELECT GROUPID FROM GROUPS WHERE GROUPID LIKE ?
SQL Group Exists: SELECT GROUPID  FROM GROUPS WHERE GROUPID  = ?
SQL Create Group: INSERT INTO GROUPS VALUES ( ? )
SQL Remove Group: DELETE FROM GROUPS WHERE GROUPID  = ?
SQL Is Member: SELECT USERID FROM USERS_GROUPS WHERE GROUPID  = ? AND USERID = ?
SQL List Member Groups: SELECT GROUPID  FROM USERS_GROUPS WHERE USERID  = ?
SQL List Group Members: SELECT USERID FROM USERS_GROUPS WHERE GROUPID = ? AND USERID LIKE ?
SQL Remove Group Memberships: DELETE FROM USERS_GROUPS WHERE GROUPID = ? OR GROUPID = ?
SQL Add Member To Group: INSERT INTO USERS_GROUPS VALUES( ?, ?)
SQL Remove Member From Group: DELETE FROM USERS_GROUPS WHERE GROUPID = ? AND USERID = ?
SQL Remove Group Member: DELETE FROM USERS_GROUPS WHERE GROUPID = ?
Descriptions Supported: unchecked
Save your changes. and go back to the "Providers" tab. Click on the "Reorder" button and push the GalleriaAuthenticator to the top of the list. Click "ok", when done and stop your WebLogic instance. You are free to restart it at any time.

Configure your Projects
Java EE is portable. Right. And you should be able to run the same deployment without any changes on the WebLogic 12c. That is theory. In practice you will have to touch the deployment. Because WebLogic has some issues with Hibernate. And it is a lot more crotchety when it comes to deployments than GlassFish is. First of all you have to create a "galleria-ear\src\main\application\META-INF" folder. Put a blank weblogic-application.xml there and put the following code in it:

<?xml version='1.0' encoding='UTF-8'?>
<weblogic-application xmlns="http://xmlns.oracle.com/weblogic/weblogic-application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-application http://xmlns.oracle.com/weblogic/weblogic-application/1.4/weblogic-application.xsd">
    <prefer-application-packages>
        <package-name>antlr.*</package-name>
    </prefer-application-packages>
</weblogic-application>

That tells WebLogic to prefer the application packaged libraries over those already present in the server. Let's go ahead. We need to add the Hibernate dependencies to the ear. With GlassFish we skipped that step, because we installed the Hibernate package with the server. Here we go. Open the galleria-ear pom.xml and add the following to the dependencies section:
<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>
        
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.1.0.CR2</version>
        </dependency> 
You also need to look at the maven-ear-plugin and add the following to the <configuration>:
<defaultLibBundleDir>lib</defaultLibBundleDir>
And if you are there already, remove the commons-codec jarModule. It doesn't hurt, but it get's packaged into the ear/lib folder, so you can skip it.
Next navigate to the galleria-jsf project and open the web.xml. The <login-config> is incomplete and should look like this:
<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/Login.xhtml</form-login-page>
        <form-error-page>/Login.xhtml</form-error-page>
    </form-login-config>
</login-config>
<security-role>
  <description>All registered Users belong to this Group</description>
  <role-name>RegisteredUsers</role-name>
 </security-role> 

You need to define the possible roles, too otherwise the WebLogic security stuff will start to complain.
Add a blank weblogic.xml to the galleria-jsf\src\main\webapp\WEB-INF folder and add the following lines to it:
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
    <security-role-assignment>
        <role-name>RegisteredUsers</role-name>
        <principal-name>RegisteredUsers</principal-name>
    </security-role-assignment>
    <session-descriptor>
        <timeout-secs>3600</timeout-secs>
        <invalidation-interval-secs>60</invalidation-interval-secs>
        <cookie-name>GalleriaCookie</cookie-name>
        <cookie-max-age-secs>-1</cookie-max-age-secs>
        <url-rewriting-enabled>false</url-rewriting-enabled>
    </session-descriptor>
</weblogic-web-app>

We are mapping the web.xml role to a WebLogic role here. You could have skipped this, but I like it this way so you don't get confused. The session-descriptor element is taking care of the JSESSION cookie name. If you wouldn't change it, you would get into trouble with signed in users to the admin console.
Move on the the galleria-ejb project. Create a blank weblogic-ejb-jar.xml in the "galleria-ejb\src\main\resources\META-INF" folder. Put the following code in it:

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-ejb-jar xmlns="http://xmlns.oracle.com/weblogic/weblogic-ejb-jar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-ejb-jar http://xmlns.oracle.com/weblogic/weblogic-ejb-jar/1.0/weblogic-ejb-jar.xsd">
    <security-role-assignment>
        <role-name>RegisteredUsers</role-name>
        <principal-name>RegisteredUsers</principal-name>
    </security-role-assignment>
</weblogic-ejb-jar>

Comparable to the web.xml/weblogic.xml this also tells WebLogic how to map the ejb-jar.xml security roles to WebLogic roles. Fine, open the persistence.xml and add the following lines:
 <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
    <property name="hibernate.transaction.jta.platform"
                value="org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform" />
The first one explicitly selects the Derby dialect for Hibernate. The second one tells Hibernate where and how to look for the transactions. All done. Now you should be able to build the project again and deploy it. Use the admin console or NetBeans to deploy it. Thanks for taking the time to follow this lengthy post. I hope it was helpful!

Published at DZone with permission of Markus Eisele, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Liam Knox replied on Fri, 2012/03/16 - 4:10pm

and now I can write code?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.