Wednesday, November 7, 2007

Log4j and MySQL

When configuring log4j with an appender to log to a MySQL database there are some nuances you might need to be aware of.

First, here is what the relevant settings in the file should look like:

# Set root logger level to DEBUG and its only appender to DB.
log4j.rootLogger=DEBUG, DB

# The database logger
log4j.appender.DB.sql=INSERT INTO mytable (mycolmn) VALUES ("%m")

Two things to note:

  1. I had to specify the driver property because log4j couldn't automatically determine that or was defaulting to an incorrect driver class name. I was using log4j 1.2.12 and MySQL Connector/J version 5.1.5
  2. The values being inserted in the database need to be enclosed in double quotes (") as opposed to the normal single quote (') you would use in SQL syntax

Tuesday, October 30, 2007

Hello World! REST web service using Jersey and Glassfish

Recently I had the need to expose some EJB application functionality to a client PHP application. I wanted to avoid the heavy weight SOAP protocol and REST seemed like a good choice. Wanting to stay within the JAVA language for a more seamless developer experience I came across the JSR 311 spec and the implementation Jersey.

If you are a JAVA programmer and are wanting to use REST in your architecture you might want to investigate Jersey. Most of the Jersey documentation on the web is using Netbeans. I wanted to use Eclipse since that is my primary IDE. So a short post on the Jersey Users mailing list elicited some quick responses with clear instructions on how to use Jersey outside of an IDE.

Here's step by step instruction on how to implement a Hello World REST Web Service. This post assumes that you are using Eclipse with the Maven 2 plugin and using Glassfish V2 and Jersey 0.4-ea release. Jersey is still an evolving spec and so these steps might very well change in the future.

First download Jersey and unzip the file. The 0.4-ea version of the Jersey libraries are not available on the maven site though the older versions are. So you have to install the Jersey libraries (jersey.jar, jsr311-api.jar, jettison-1.0-RC1.jar) into a local maven repository.

Create a new Maven2 project called restapp in Eclipse with the following folder structure


Edit the maven pom.xml file so it looks something like below:

<project xmlns=\"\" xsi=\"\" schemalocation=\"\">






















































































Depending on how you installed the downloaded Jersey libraries into your local repository you might have to fix the pom.xml correctly. Also you might not need some of the other libraries I've listed in the pom.xml. I had them anyway because I needed those to make some EJB lookups.

Create a new file web.xml in the src/main/webapp/WEB-INF folder with the following contents



<servlet-name>Jersey Web Application</servlet-name>












<servlet-name>Jersey Web Application</servlet-name>




Create a Java class called in src/main/java. This class should match the webresourceclass parameter value in the web.xml.

The class should like below:

package rest.resources;


public class RootResources extends DefaultResourceConfig {

public RootResources() {


Now create the as below:

package rest.resources;


// The Java class will be hosted at the URI path "/helloworld"
public class HelloResource {

public String sayHello() {
return "Hello World!";

Now execute the mvn clean package command to package the app as a war. You can deploy the war into Glassfish via the admin console or the autodeploy folder. Make sure there are no deployment error messages logged in the Glassfish server.log file.

Fire up your browser and visit http://localhost:8080/restapp-1.0.0/helloworld to REST easy :)

Friday, October 26, 2007

Programmatic Login to Authenticate Against a EJB in Glassfish


This is the method used to authenticate a standalone java client (including Eclipse RCP plugins) to the Glassfish EJB container.




See here on how to configure Glassfish's server.policy file:

Make sure the following jars from Glassfish are in the classpath:

  1. javaee.jar
  2. appserv-admin.jar
  3. appserv-deployment-client.jar
  4. appserv-ext.jar
  5. appserv-rt.jar
  6. and your client classes with the EJB lookup code

The following parameter needs to be passed the VM

where PATH is the fully qualified path to the appclientlogin.conf. You can get this file from your glassfish installation. This file should be shipped with your client code.

Edit the appclientlogin.conf to add the following

file { required debug=true;

where "file" is the realm name you have configured in Glassfish. By default glassfish comes with the file realm pre-configured. See [here|] for instructions on how to configure an LDAP realm.

Now, add the following code to your client to authenticate a bean:

Properties props = new Properties();

props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
// NOTE: IIOP is set on port 3701 but this works on port 3700
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
// props.setProperty("", "");

ProgrammaticLogin programmaticLogin = new ProgrammaticLogin();
try {

Boolean login = programmaticLogin.login("myuser", "password",
"file", false);
// login always returns true
// always uses default realm for some reason
System.out.println("State:" + login);
} catch (Exception e) {

InitialContext ctx;
try {
ctx = new InitialContext(props);
// this is where the actual login happens!
serviceBean = (PermitServiceRemote) ctx
} catch (NamingException e1) {
// TODO Auto-generated catch block

You should be set. For some of discussions surrounding this see here:

Sunday, July 8, 2007

Set the Application Server > Logging > Log Levels > Deployment > Debugging to FINE to quickly troubleshoot deployment issues in Glassfish

Sunday, March 4, 2007

Standalone EJB 3.0 client for remote EJB server

In order for a client to connect to your remote GlassFish server you need to add a second IIOP listener with the hostname listening at a different port (ex: 3701). You can do this via the GlassFish admin console.

Create a Java project in Eclipse that will be a standalone EJB 3.0 client application.

When deploying the previously created EJB jar on glassfish using the admin console check the option that creates the client .jar file. Then locate the *Client.jar file on the server and make your project depend on this jar.

Your client app should have code similar to below:

public class Client {

public void runTest() throws Exception {
Properties props = new Properties();

props.setProperty("org.omg.CORBA.ORBInitialHost", "");
// NOTE: IIOP is set on port 3701 but this works on port 3700
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

InitialContext ctx = new InitialContext(props);

OrderBean bean = (OrderBean) ctx.lookup("ejb/OrderBean");

Order result = bean.findByOrderNum(9);

public static void main(String[] args) {
Client cli = new Client();
try {
} catch (Exception e) {

EJB 3.0 + Glassfish + JPA + Eclipse

Follow instructions here to create the EJB project in eclipse.

Create the jdbc connection pool and the corresponding resource via the GlassFish admin console.

Now create a persistence.xml fiel in the src/META-INF folder in your project to point to the JDBC resource.

<persistence-unit name=\"myPU\">
<property name=\"toplink.logging.level\" value=\"FINEST\" />

In the bean implementation class use

@PersistenceContext(unitName = "myPU")
protected EntityManager em;

to inject the EntityManager. Change the unitName value to your persistence definition in the persistence.xml.

The exported jar file can be deployed into either the local or a remote glassfish instance.

Stay tuned for the next post on writing a standalone client to access this ejb.