Monday February 08, 2010
TOTD #121: JDBC resource for MySQL and Oracle sample database in GlassFish v3
This blog clearly explains how to configure the MySQL sample database (sakila) with GlassFish. Even though the instructions use a specific database but should work for other databases (such as Oracle, JavaDB, PostgreSQL, and others) as well. The second half of the blog provide specific syntax for the Oracle sample database.
CREATE USER glassfish IDENTIFIED BY 'glassfish'; GRANT ALL PRIVILEGES ON *.* TO 'glassfish'@'localhost' IDENTIFIED BY 'glassfish'; FLUSH PRIVILEGES;
source sakila-schema.sql; source sakila-data.sql;
asadmin start-domain
asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype javax.sql.DataSource --property "User=glassfish:Password=glassfish:URL=jdbc\:mysql\://localhost/sakila" jdbc/sakilaPool
asadmin ping-connection-pool jdbc/sakilaPool
asadmin create-jdbc-resource --connectionpoolid jdbc/sakilaPool jdbc/sakila
That's it!
Creating a JDBC resource for any other database requires the following updates to the steps mentioned above. Lets consider modifying these steps for the Oracle sample database.
sqlplus "/ as sysdba"
CREATE USER glassfish IDENTIFIED BY glassfish DEFAULT tablespace users TEMPORARY tablespace temp; GRANT CONNECT TO glassfish IDENTIFIED BY glassfish; GRANT UNLIMITED TABLESPACE TO glassfish; GRANT CREATE TABLE TO glassfish; GRANT CREATE SEQUENCE TO glassfish;
asadmin create-jdbc-connection-pool --datasourceclassname oracle.jdbc.pool.OracleDataSource --restype javax.sql.DataSource --property "User=hr:Password=hr:URL=jdbc\:oracle\:thin\:@localhost\:1521\:orcl" jdbc/hr asadmin ping-connection-pool jdbc/hr asadmin create-jdbc-resource --connectionpoolid jdbc/hr jdbc/hr
Here are a few other related entries:
RESTful representation of sakila using NetBeans and GlassFish
JSF + JPA + EJB Application using Oracle, NetBeans, and GlassFish
Technorati: totd javaee glassfish v3 jpa mysql sakila oracle
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Friday February 05, 2010
TOTD #120: Deployment Descriptor-free Java EE 6 application using JSF 2.0 + EJB 3.1 + Servlets 3.0
Here is trivial Java EE 6 application that is keeping you away from any deployment descriptors. It uses Java Server Faces 2.0, Enterprise Java Beans 3.1, and Servlet 3.0. This application shows the following Java EE 6 features:
The WAR file structure is:
./index.jsp ./index.xhtml ./META-INF ./show.xhtml ./WEB-INF ./WEB-INF/classes ./WEB-INF/classes/org ./WEB-INF/classes/org/glassfish ./WEB-INF/classes/org/glassfish/samples ./WEB-INF/classes/org/glassfish/samples/SimpleBean.class ./WEB-INF/classes/org/glassfish/samples/SimpleEJB.class ./WEB-INF/classes/org/glassfish/samples/SimpleServlet.class
Look ma, no deployment descriptors!
So how do you create this application:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.glassfish.samples -DartifactId=simplewebapp
This application is purposely not generated as a web application (missing "-DarchetypeArtifactId=maven-archetype-webapp"). If you specify this property then it will generate "WEB-INF/web.xml" which we don't intend to use.
Change "pom.xml" to:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.glassfish.samples</groupId> <artifactId>simplewebapp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>simplewebapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>glassfish-repository</id> <name>Java.net Repository for Glassfish</name> <url>http://download.java.net/maven/glassfish</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1-beta-1</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
Create the directory structure as:
./src/main ./src/main/java ./src/main/java/org ./src/main/java/org/glassfish ./src/main/java/org/glassfish/samples ./src/main/java/org/glassfish/samples/SimpleBean.java ./src/main/java/org/glassfish/samples/SimpleEJB.java ./src/main/java/org/glassfish/samples/SimpleServlet.java ./src/main/webapp ./src/main/webapp/index.jsp ./src/main/webapp/index.xhtml ./src/main/webapp/show.xhtml
Once again, there are no deployment descriptors, just plain Java files and XHTML/JSP pages.
Here are the different source files with explanation after each one of them:
SimpleBean.java
package org.glassfish.samples;
import javax.faces.bean.ManagedBean;
@ManagedBean(name="simplebean")
public class SimpleBean {
private String name;
private int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
SimpleEJB.java
package org.glassfish.samples;
import javax.ejb.Stateless;
@Stateless
public class SimpleEJB {
public String sayHello(String name) {
return "Hello " + name + "!!!";
}
}
SimpleServlet.java
package org.glassfish.samples;
import javax.ejb.EJB;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.PrintWriter;
import java.io.IOException;
/**
* Hello world!
*/
@WebServlet(urlPatterns={"/SimpleServlet"})
public class SimpleServlet extends HttpServlet {
@EJB SimpleEJB bean;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h2>Serving at: " + request.getContextPath() + "</h2>");
out.println("<h2>Invoking EJB: " + bean.sayHello("Duke") + "</h2>");
out.println("</body></html>");
}
}
index.jsp
<html> <body> <h2>Hello World!</h2> Invoke the Servlet by clicking <a href="SimpleServlet">here</a>. </body> </html>
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm
l1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Enter Name & Age</title>
</h:head>
<h:body>
<h1>Enter Name & Age</h1>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Name:"/>
<h:inputText value="#{simplebean.name}" title="name" id="name" required="true"/>
<h:outputText value="Age:"/>
<h:inputText value="#{simplebean.age}" title="age" id="age" required="true"/>
</h:panelGrid>
<h:commandButton action="show" value="submit"/>
</h:form>
</h:body>
</html>
show.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm
l1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Show Name & Age</title>
</h:head>
<h:body>
<h1>Show Name & Age</h1>
<h:form action="show">
<h:panelGrid columns="2">
<h:outputText value="Name:"/>
<h:outputText value="#{simplebean.name}" />
<h:outputText value="Age:"/>
<h:outputText value="#{simplebean.age}" />
</h:panelGrid>
</h:form>
</h:body>
</html>
How do you build this entire application ?
mvn clean package
Lets deploy the application on a Java EE 6 compliant application server, GlassFish v3 (download here):
./bin/asadmin deploy --force=true ~/samples/javaee6/simplewebapp/target/simplewebapp-1.0-SNAPSHOT.war

Clicking on "here" looks like:

The JSF page is accessible at "http://localhost:8080/simplewebapp-1.0-SNAPSHOT/index.jsf" and looks like (after entering the values):

Notice that even though the page is named "index.xhtml", it's accessed as "index.jsf". This is because the JSF specification provides recommended mapping for FacesServlet to "*.faces" and "/faces/*". In addition, Mojarra (Reference Implementation of JSF2 in GlassFish) also adds a mapping to "*.jsf". Any views using these URL pattersn are routed through FacesServlet. So alternative URLs for our page are "http://localhost:8080/simplewebapp-1.0-SNAPSHOT/index.faces" and "http://localhost:8080/simplewebapp-1.0-SNAPSHOT/faces/index.xhtml".
Clicking on "Submit" shows the following page:

That's it!
Here are several other useful entries:
The next follow up blog will show "Hello World"s of Context & Dependency Injection, Bean Validation, Java API for Restful Web services, Java Persistence API, Interceptors, and other Java EE 6 specifications in this application.
Technorati: totd javaee glassfish v3 javaserverfaces servlet3 ejb maven
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Friday January 22, 2010
TOTD #119: Telnet to GlassFish v3 with NetBeans 6.8 - "Could not open connection to the host"
As explained in TOTD #118, one of the ways to manage OSGi bundles in GlassFish is by giving the command "telnet localhost 6666".
This straight forward command works fine if you installed either the Sun GlassFish Enterprise Server or the GlassFish community bits. The "domain.xml" in both of them is pre-configured for the telnet port to 6666 using the "jvm-options" as shown below:
<jvm-options>-Dosgi.shell.telnet.port=6666</jvm-options>
However if you installed GlassFish as part of NetBeans 6.8, then you might see an error message as shown below:
C:\Users\Arun>telnet localhost 6666 Connecting To localhost...Could not open connection to the host, on port 6666: Connect failed
This error is more prominent in Windows Vista / 7 because of the Windows User Account Control (UAC). Or anywhere where GlassFish is installed in a directory that require root/administrator/sudo access to read/write. This Tip Of The Day will explain how to workaround this issue.
The default NetBeans installation directory is "C:\Program Files\NetBeans 6.8" and GlassFish goes in "C:\Program Files\sges-v3". At first start of NetBeans (typically as a non-Administrator), it tries to register the pre-configured domain in GlassFish installed in "C:\Program Files". But the non-Adminstrator user do not have read/write access to "C:\Program Files" and any sub-directories. So NetBeans create a new "personal" domain and assign a random port available at that moment for telnet. It shows all the ports assigned during the domain creation as shown below:

The image shows the port number "22007" for OSGI_SHELL.
How do you find that port later ? - Go to "Services" tab, expand "Servers", right-click on "Personal GlassFish v3 Domain", select "Properties" from the popup menu to see the following window:

The "Domains folder" shows the directory location of newly created domain and "Domain Name" has the domain name. The exact assigned port can be found by looking at "Domains folder"\"Domain Name"\config\domain.xml. On my Windows7, it showed the following line:
<jvm-options>-Dosgi.shell.telnet.port=22007</jvm-options>
Some other possible solutions that will work:

A complete archive of all the TOTDs is available here.
Technorati: totd netbeans glassfish v3 felix osgi telnet
Posted by Arun Gupta in General | Comments[2]
|
|
|
|
|
Thursday January 21, 2010
GlassFish v3 and OSGi integration is now known for almost two years. Several blogs have been published on this topic and googling on "glassfish osgi" shows 817,000 results. This blog has published four entries on the topic so far.
This Tip Of The Day (TOTD) will show the different ways you can manage OSGi bundles in GlassFish v3.
The first part is to create a trivial OSGi bundle as explained in TOTD #36.
~/samples/v3/osgi >mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.glassfish.samples.osgi.helloworld -DartifactId=helloworld [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [archetype:create] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'. [INFO] Setting property: velocimacro.messages.on => 'false'. [INFO] Setting property: resource.loader => 'classpath'. [INFO] Setting property: resource.manager.logwhenfound => 'false'. [INFO] [archetype:create] [WARNING] This goal is deprecated. Please use mvn archetype:generate instead [INFO] Defaulting package to group ID: org.glassfish.samples.osgi.helloworld [INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking for updates from central [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: org.glassfish.samples.osgi.helloworld [INFO] Parameter: packageName, Value: org.glassfish.samples.osgi.helloworld [INFO] Parameter: package, Value: org.glassfish.samples.osgi.helloworld [INFO] Parameter: artifactId, Value: helloworld [INFO] Parameter: basedir, Value: /Users/arungupta/samples/v3/osgi [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] OldArchetype created in dir: /Users/arungupta/samples/v3/osgi/helloworld [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11 seconds [INFO] Finished at: Wed Jan 20 14:12:41 PST 2010 [INFO] Final Memory: 12M/80M [INFO] ------------------------------------------------------------------------
package org.glassfish.samples.osgi.helloworld;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
/**
* Hello world!
*
*/
public class App implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println("Hey!");
}
public void stop(BundleContext context) throws Exception {
System.out.println("Bye!");
}
}
This is a trivial Activator class but sitll shows the key methods. The changes are highlighted in bold.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.glassfish.samples.osgi.helloworld</groupId>
<artifactId>helloworld</artifactId>
<packaging>bundle</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloworld</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>${pom.groupId}</Export-Package>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Activator>${pom.groupId}.App</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
~/samples/v3/osgi/helloworld >mvn install [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building helloworld [INFO] task-segment: [install] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] [INFO] Compiling 1 source file to /Users/arungupta/samples/v3/osgi/helloworld/target/classes [INFO] [resources:testResources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:testCompile] [INFO] Compiling 1 source file to /Users/arungupta/samples/v3/osgi/helloworld/target/test-classes [INFO] [surefire:test] [INFO] Surefire report directory: /Users/arungupta/samples/v3/osgi/helloworld/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.glassfish.samples.osgi.helloworld.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [bundle:bundle] [INFO] [install:install] [INFO] Installing /Users/arungupta/samples/v3/osgi/helloworld/target/helloworld-1.0-SNAPSHOT.jar to /Users/arungupta/.m2/repository/org/glassfish/samples/osgi/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-SNAPSHOT.jar [INFO] [bundle:install] [INFO] Parsing file:/Users/arungupta/.m2/repository/repository.xml [INFO] Installing org/glassfish/samples/osgi/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-SNAPSHOT.jar [INFO] Writing OBR metadata [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 8 seconds [INFO] Finished at: Wed Jan 20 14:18:31 PST 2010 [INFO] Final Memory: 20M/80M [INFO] ------------------------------------------------------------------------The generated "target/helloworld-1.0-SNAPSHOT.jar" has the following contents:
META-INF/MANIFEST.MF META-INF/ META-INF/maven/ META-INF/maven/org.glassfish.samples.osgi.helloworld/ META-INF/maven/org.glassfish.samples.osgi.helloworld/helloworld/ META-INF/maven/org.glassfish.samples.osgi.helloworld/helloworld/pom.properties META-INF/maven/org.glassfish.samples.osgi.helloworld/helloworld/pom.xml org/ org/glassfish/ org/glassfish/samples/ org/glassfish/samples/osgi/ org/glassfish/samples/osgi/helloworld/ org/glassfish/samples/osgi/helloworld/App.class
Manifest-Version: 1.0 Export-Package: org.glassfish.samples.osgi.helloworld;uses:="org.osgi. framework" Built-By: arungupta Tool: Bnd-0.0.357 Bundle-Name: helloworld Created-By: Apache Maven Bundle Plugin Bundle-Version: 1.0.0.SNAPSHOT Build-Jdk: 1.6.0_17 Bnd-LastModified: 1264025910352 Bundle-ManifestVersion: 2 Bundle-Activator: org.glassfish.samples.osgi.helloworld.App Import-Package: org.glassfish.samples.osgi.helloworld,org.osgi.framewo rk;version="1.3" Bundle-SymbolicName: helloworld
Lets install this newly created OSGi bundle in GlassFish v3. First, fire up GlassFish as:
~/tools/glassfish/v3/74b/glassfishv3/glassfish >./bin/asadmin start-domain -v
Jan 20, 2010 2:30:39 PM com.sun.enterprise.admin.launcher.GFLauncherLogger info
INFO: JVM invocation command line:
/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java
-cp
/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/glassfish.jar
-XX:+UnlockDiagnosticVMOptions
-XX:MaxPermSize=192m
-XX:NewRatio=2
-XX:+LogVMOutput
. . .
Jan 20, 2010 2:30:40 PM com.sun.enterprise.admin.launcher.GFLauncherLogger info
INFO: Successfully launched in 52 msec.
Jan 20, 2010 2:30:40 PM com.sun.enterprise.glassfish.bootstrap.ASMain main
INFO: Launching GlassFish on Felix platform
Welcome to Felix
================
[#|2010-01-20T14:30:49.437-0800|INFO|glassfishv3.0|com.sun.grizzly.config.GrizzlyServiceListener|_ThreadID=11;_ThreadName=FelixStartLevel;|Perform lazy SSL initialization for the listener 'http-listener-2'|#]
[#|2010-01-20T14:30:49.527-0800|INFO|glassfishv3.0|com.sun.grizzly.config.GrizzlyServiceListener|_ThreadID=12;_ThreadName=Thread-11;|Starting Grizzly Framework 1.9.18-k - Wed Jan 20 14:30:49 PST 2010|#]
. . .
[#|2010-01-20T14:30:58.668-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart/, felix.fileinstall.debug=1};|Started bundle: file:/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart/org.apache.felix.scr.jar|#]
[#|2010-01-20T14:30:58.786-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart/, felix.fileinstall.debug=1};|Started bundle: file:/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart/osgi-web-container.jar|#]
[#|2010-01-20T14:31:00.436-0800|INFO|glassfishv3.0|null|_ThreadID=23;_ThreadName=ping;|Total number of available updates : 0|#]
There are several ways to manage the OSGi bundles in GlassFish v3:
Lets explore each option in detail now.
Option 1: Manage the OSGi bundle using the "asadmin" command
~/samples/v3/osgi/helloworld/target >~/tools/glassfish/v3/74b/glassfishv3/glassfish/bin/asadmin deploy --type osgi helloworld-1.0-SNAPSHOT.jar Application deployed successfully with name helloworld-1.0-SNAPSHOT. Command deploy executed successfully.The server log shows the following output:
[#|2010-01-20T16:15:10.553-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com. sun.enterprise.v3.services.impl|_ThreadID=36;_ThreadName=http-thread-pool-4848-(2); |Hey!|#]Notice "Hey!" message in the server log as the bundle gets started.
~/samples/v3/osgi/helloworld/target >~/tools/glassfish/v3/74b/glassfishv3/glassfish/bin/asadmin list-applications helloworld-1.0-SNAPSHOT Command list-applications executed successfully.
Or if there are multiple applications deployed then only the OSGi bundles can be queried as:
~/samples/v3/osgi/helloworld/target >~/tools/glassfish/v3/74b/glassfishv3/glassfish/bin/asadmin list-applications --type osgi helloworld-1.0-SNAPSHOT Command list-applications executed successfully.
The bundle can be undeployed as:
~/samples/v3/osgi/helloworld/target >~/tools/glassfish/v3/74b/glassfishv3/glassfish/bin/asadmin undeploy helloworld-1.0-SNAPSHOT Command undeploy executed successfully.And then the following message is shown on the console:
[#|2010-01-20T16:22:19.554-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com. sun.enterprise.v3.services.impl|_ThreadID=37;_ThreadName=http-thread-pool-4848-(1); |Bye!|#]Notice "Bye!" message in second line of the log output indicating the bundle is stopped.
Option 2: Manage the OSGi bundle using file system operations
~/tools/glassfish/v3/74b/glassfishv3/glassfish >cp ~/samples/v3/osgi/helloworld/target/helloworld-1.0-SNAPSHOT.jar modules/autostart/and that shows the log output as:
[#|2010-01-20T16:29:04.625-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com.
sun.enterprise.v3.services.impl|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish
/modules/autostart/, felix.fileinstall.debug=1};|Installed /Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart/
helloworld-1.0-SNAPSHOT.jar|#]
[#|2010-01-20T16:29:04.635-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com.
sun.enterprise.v3.services.impl|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish
/modules/autostart/, felix.fileinstall.debug=1};|Hey!|#]
[#|2010-01-20T16:29:04.636-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com.
sun.enterprise.v3.services.impl|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish
/modules/autostart/, felix.fileinstall.debug=1};|Started bundle: file:/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart
/helloworld-1.0-SNAPSHOT.jar|#]
Notice "Hey!" message in the second line of log output as the bundle gets started. ~/tools/glassfish/v3/74b/glassfishv3/glassfish >rm modules/autostart/helloworld-1.0-SNAPSHOT.jarthat shows the following output:
~/tools/glassfish/v3/74b/glassfishv3/glassfish >[#|2010-01-20T16:32:04.677-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com.
sun.enterprise.v3.services.impl|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish
/modules/autostart/, felix.fileinstall.debug=1};|Uninstalling bundle 224 (helloworld)|#]
[#|2010-01-20T16:32:04.679-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com.
sun.enterprise.v3.services.impl|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish
/modules/autostart/, felix.fileinstall.debug=1};|Bye!|#]
[#|2010-01-20T16:32:04.682-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com.
sun.enterprise.v3.services.impl|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish
/modules/autostart/, felix.fileinstall.debug=1};|Uninstalled /Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart
/helloworld-1.0-SNAPSHOT.jar|#]
Notice "Bye!" message in second line of the log output indicating the bundle is stopped. Option 3: Manage the OSGi bundle using a remote Telnet Shell
~/tools/glassfish/v3/74b/glassfishv3/glassfish >telnet localhost 6666 Trying ::1... telnet: connect to address ::1: Connection refused Trying fe80::1... telnet: connect to address fe80::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Felix Remote Shell Console: ============================ ->
-> install file:///Users/arungupta/samples/v3/osgi/helloworld/target/helloworld-1.0-SNAPSHOT.jar Bundle ID: 225The command output shows "225" as the bundle id. This id is used to start / stop / uninstall the bundle.
-> find hello START LEVEL 1 ID State Level Name [ 225] [Installed ] [ 1] helloworld (1.0.0.SNAPSHOT)and then start, stop, and uninstall the bundle as:
-> start 225 -> stop 225 -> uninstall 225 -> find hello No matching bundles foundwhich shows following output in the logs:
[#|2010-01-20T16:43:45.399-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com. sun.enterprise.v3.services.impl|_ThreadID=38;_ThreadName=telnetconsole.shell remote=/127.0.0.1:4894;|Hey!|#] [#|2010-01-20T16:43:58.516-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com. sun.enterprise.v3.services.impl|_ThreadID=38;_ThreadName=telnetconsole.shell remote=/127.0.0.1:4894;|Bye!|#]
Option 4 - Manage the OSGi bundle using a Web browser
Lets see how the OSGi bundles in GlassFish can be managed using Apache Felix Web Console. This is originally explained in Sahoo's blog.
Copy GlassFish OSGi HTTP Service bundle from here (latest) and save it in the "modules/autostart" directory.
Copy Apache Felix Web Console bundle from here (latest) and save it in the "modules/autostart" directory.
Ignore the "NoClassDefFoundError" in the server log. The key is to look for the following message in server log:
Started bundle: file:/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart/org.apache.felix.webconsole-2.0.4.jar|#]
Open the URL "http://localhost:8080/osgi/system/console/bundles" in a browser and use "admin" as the username and "admin" as the password as shown below:
I had to enter the credentials couple of times for the login to work but finally the following window showed up:
It shows a complete summary of all the OSGi bundles available/installed/active etc in GlassFish v3. A new OSGi bundle can be installed by clicking on "Choose File" button. Several administration commands such as Start/Stop, Update, Uninstall, Refresh Import Packages can be issued for each bundle by clicking on associated buttons.
Install the OSGi bundle by clicking on "Choose File" and selecting "helloworld-1.0.-SNAPSHOT.jar" and then click on "Install or Update" button. The following message is shown in the server log:
[#|2010-01-20T17:04:46.654-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com. sun.enterprise.v3.services.impl|_ThreadID=39;_ThreadName=Background Install /var/folders/+E/+E6YtSvGGEKNwOA77I-9Fk+++TI/-Tmp-/install1657418488877506078.tmp; |Hey!|#]
The bundle gets installed and started as identified by "Hey!" message.
The recently installed "HelloWorld" bundle looks like:
Clicking on "helloworld" shows the complete status about the bundle as shown below:
The bundle can be stopped by clicking on the Stopped, Refreshed Package Imports, Updated, and Uninstalled by clicking on the respective buttons in the "Actions" column. Clicking on the Stop button shows the following message:
[#|2010-01-20T17:10:56.359-0800|INFO|glassfishv3.0|javax.enterprise.system.std.com .sun.enterprise.v3.services.impl|_ThreadID=25;_ThreadName=http-thread-pool-8080-(2); |Bye!|#]Notice "Bye!" message indicating the bundle has stopped.
Option 5: Manage the OSGi bundle using a REST console
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 16198 0 16198 0 0 1173k 0 --:--:-- --:--:-- --:--:-- 1173kbundles
bundle
id
0
symbolic-name
org.apache.felix.framework
. . .
description
Generated using Pax-Construct
vendor
version
1.0.0.SNAPSHOT
location
file:/Users/arungupta/tools/glassfish/v3/74b/glassfishv3/glassfish/modules/autostart/
com.knokode.osgi.restconsole.main-1.0-PREVIEW01.jar
state
ACTIVE
curl -X PUT file:///Users/arungupta/samples/v3/osgi/helloworld/target/helloworld-1.0-SNAPSHOT.jar http://localhost:8080/osgi/restconsole/bundlesbut it's giving a "Segmentation fault". Am following with @fdiotalevi.
So how do you manage OSGi bundles in GlassFish v3 - asadmin, file system operations, telnet console, web browser, or REST ?
A complete archive of all the TOTDs is available here.
Technorati: totd glassfish v3 osgi apache felix bundles maven
Posted by Arun Gupta in General | Comments[2]
|
|
|
|
|
Tuesday January 12, 2010
TOTD #117: Invoke a JAX-WS Web service from a Rails app deployed in GlassFish
A user on GlassFish Forum tried invoking a JAX-WS Web service from a Rails application and faced some issues. This Tip Of The Day (TTOD) will discuss the different approaches and shows their current status.
A Rails app can be deployed on GlassFish in 3 different ways:
Now lets get to the issue reported by the user using these 3 deployment models.
First, lets deploy a simple Web service endpoint and generate a JAR file of the client-side artifacts:
package server;
import javax.jws.WebService;
/**
* @author arungupta
*/
@WebService()
public class HelloService {
public String sayHello(String name) {
return "Hello " + name;
}
}
~/samples/v3/rails/webservice/tmp >wsimport -keep http://localhost:8080/HelloWebService/HelloServiceService?wsdl parsing WSDL... generating code... compiling code...
jar cvf wsclient.jar ./server
Now lets write a Rails application and invoke this Web service:
jruby -S rails webservice
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
jruby script/generate controller home index
include Java
class HomeController < ApplicationController
def index
service = Java::server.HelloServiceService.new
port = service.getHelloServicePort
@result = port.sayHello("Duke")
end
end
<h1>Home#index</h1%gt; <p>Find me in app/views/home/index.html.erb</p> <%= @result %>
Now lets deploy this Web service using the 3 different deployment models mentioned above.
GlassFish v3 allows a directory-based deployment of Rails applications. This application needs to locate the Web service client classes. The "wsclient.jar" can be copied to the "lib" directory of Rails application ("webservice/lib" in our case), "domains/domain1/lib/ext" or "JRUBY_HOME/lib". The library can also be passed during deployment using "--libraries" switch. None of this approach seem to work correctly as explained in issue# 11408. So for now, invoking a JAX-WS Web service from a Rails application deployed directly on GlassFish v3 is not possible, at least until the bug is fixed.
In order to deploy the same application using GlassFish Gem, you can copy "wsclient.jar" to the "lib" directory of your Rails application. And also add the following line to "app/controllers/home_controller.rb":
require 'lib/wsclient.jar'
Alternatively you can copy it to "JRUBY_HOME/lib" directory if this Web service client is accessed my multiple applications. In this case there is no need to add any "require" statement to your Controller. Anyway, running the application as:
jruby -S glassfish
and accessing "http://localhost:3000/home/index" shows the following output:

And finally as explained in TOTD #73, bundle up your original Rails application as WAR and then deploy on GlassFish v3 as:
asadmin deploy webservice.war
Make sure to copy "wsclient.jar" to the "lib" directory of your Rails application and then Warbler will copy it to "WEB-INF/lib" of the generated WAR file. The output is shown as below:

So if you want to invoke a Metro/JAX-WS Web service from a Rails application, then run your Rails application using GlassFish Gem or deploying as a WAR file. It'll work on GlassFish v3 server when issue# 11408 is fixed.
Here are some additional links:
A complete archive of all the TOTDs is available here.
Technorati: totd glassfish v3 jruby rails webservice jax-ws metro
Posted by Arun Gupta in webservices | Comments[0]
|
|
|
|
|
Tuesday December 08, 2009
TOTD #116: GlassFish v3 Administration using JavaFX front-end - JNLP available
As mentioned in TOTD #113, this Tip Of The Day (TOTD) provides a working version of the JavaFX front-end for GlassFish v3 administration.
Please click here to launch the JNLP or click here to a page that provides some introduction along with the link to JNLP. You may like to enable Java Console as explained in TOTD #114 for any log messages.
See a video of the tool in action:
Many thanks to Rajeshwar for providing feedback and helping me understand the RESTful interface better. TOTD #96 explains how the REST interface can be used.
Here is a TODO list in no particular order:
How are you using JavaFX with GlassFish ?
How will you use GlassFish v3 REST interface in your tools ?
Technorati: totd javafx glassfish v3 rest web jruby rubyonrails rest administration monitoring management
Posted by Arun Gupta in General | Comments[2]
|
|
|
|
|
Friday December 04, 2009
TOTD #115: GlassFish in Eclipse - Integrated Bundle, Install Stand-alone or Update Existing plugin
There are three options for Eclipse users interested in exploring GlassFish. They can either use an integrated bundle, install the GlassFish plug-in in an existing Eclipse version, or update an older GlassFish plugin in a stand-alone Eclipse to the latest version. These options are explained below.
GlassFish Tools Bundle for Eclipse 1.1 is an integrated bundle based on Eclipse 3.4.2 and includes GlassFish v2 and v3 Prelude pre-registered and configured and optionally JDK 1.6 U12. The work towards version 1.2 can be tracked by following the 1.1.x releases (1.1.7 is the latest). This new version is based on Eclipse 3.5.1, includes GlassFish v3 build 74 pre-registered and configured, several plugins (JSF Facelets, JAX-WS, Maven m2) and several Java EE 6 wizards to provide a seamless development and deployment experience with Java EE 6 & GlassFish v3. There are several other niceties in the newer release like pre-registered MySQL JDBC driver, updated Java EE 5 and Java EE 6 javadoc and code completion.

However what to do if you are already using an Eclipse version in your environment ?
The basic requirement is Eclipse 3.4+. A GlassFish plugin can be easily installed in any Eclipse 3.4+ and provides all the GlassFish-related functionality. The screencast #28 shows how to get started with Eclipse 3.4.2 and configure GlassFish as a standard server adapter.
If you are using Eclipse 3.5.0, then the standard technique described in the above screencast will not work because of the issue #280365. Fortunately, the bug report also has a workaround. Instead of using the "Download additional server adapter", install using the "Help", "Install New Software ..." and explicitly adding the GlassFish plugins update site. A screen snapshot looks like:

If you are using Eclipse 3.5.1, then the standard technique of "Download additional server adapter" works as described in the screencast #28. A screen snapshot looks like:

If you already have an existing version of GlassFish plugin installed, then it can be updated as described in TOTD #66. The process is much more simplified now and menu items have changed little bit in Eclipse 3.5.x - "Check for Updates" instead of "Software Updates ...". The screen snapshot looks like:

Now you can start developing your applications using Eclipse. Several blog entries have already been published:
Future blogs will provide more details on the new features added recently. Until then, the GlassFish Plugin Release Notes provide a summary.
On a slightly different note, you can even run GlassFish with Eclipse Equinox OSGi runtime as explained in TOTD #103.
Technorati: totd eclipse glassfish v3 javaee
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Thursday December 03, 2009
TOTD #114: How to enable Java Console in Mac OS X, Windows, ... ?
Debugging an applet running with Java plug-in or JNLP application running with Java Web Start in the browser requires the "Java Console" so that debugging messages printed using System.out and System.err can be seen. In Windows, there is a "Show Java Console" menu item in Firefox but clicking it still does not show the console. And this happens because the console window is disabled by default.
However the setting can be easily altered as explained below.
On Mac OS, open "Applications" -> "Utilities" -> "Java Preferences"
On Windows, open "Control Panel" -> "Java"
On any platform, type "javaws -viewer"
Click on the "Advanced" tab to see a window similar to the following on Mac OS X:

And the following on Windows:

Change the "Java Console" setting from "Hide console" to "Show console". Launching your JNLP from the browser next time now will also open the "Java Console" as shown below:

and debugging messages will be nicely printed in the console. Read more about the options displayed in the console here.
Technorati: osxtips windows java console jnlp debugging totd
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Wednesday November 11, 2009
TOTD #113: JavaFX front-end for GlassFish v3 Administration - Using REST interface
GlassFish v3 provides a REST interface to management and monitoring information as discussed in TOTD #96. As mentioned in that blog "the REST interface is a lower level API that enables toolkit developers and IT administrators to write their custom scripts/clients using language of their choice". This blog introduces a tool that uses the REST API to provide management and monitoring of GlassFish v3 and is written using JavaFX.
This tool is only a proof-of-concept that demonstrates that GlassFish v3 REST interface is functionally very rich and can indeed be used to write third-party administration tools. The tool uses a subset of the REST interface and exposes only a limited amount of management and monitoring capabilities otherwise exposed. After all this is a proof-of-concept :-)
A screencast of this tool in action along with a downloadable JNLP version will soon be available. For now, here is a snapshot of the main window of this tool:

The main screen allows you to enter a URL for the GlassFish administration. Then the GlassFish instance can be stopped/restarted from the main window using the buttons on top right. There is an animation at the bottom of the screen where the glassfish is swimming in the ocean and is directly related to the state of server running in the background. If the server is running, the animation works. If the server is not running then the animation stops as well.
The main screen has three main buttons:
Clicking on "List Applications" shows the list of applications deployed on this particular instance. Here is how a snapshot looks like for an instance running on my localhost at port 4848:

As shown in the screen, it shows a radio-bulleted list of all the applications. Each bullet is also accompanied by an image indicating the type of application - Web or Rails for now. Select the application and click on "Monitor" button to monitor that particular application. The REST API exposes a vast amount of monitoring data but a subset of monitoring data is displayed for Web and Rails application for now. Here is a snapshot of the monitoring data published for a Web application:

As evident by the list of engines, this web application has EJBs bundled as well. It also shows total number of Servlets/JSPs loaded, number of requests made to this web application and some other monitoring data.
Here is a snapshot of the monitoring data published for a Rails application:

It shows number of JRuby runtimes configured for the application, number of requests sent to the application, number of responses with different HTTP access codes and some other data.
The monitoring levels of different containers can be easily updated by clicking on "Show Monitoring Levels" as shown below:

And finally some server statistics are shown by clicking on "Server Stats" as shown below:

It shows when the server was started, host/port information, version and finally how long the server has been running for. The dials are an animation that shows the server up time.
Here are other related JavaFX and GlassFish related blogs published earlier:
How are you going to use the REST interface exposed by GlassFish v3 in your environment ?
Are you using JavaFX with GlassFish together in any way ?
Leave a comment on this blog if you do!
Technorati: javafx glassfish v3 rest web jruby rubyonrails rest administration monitoring management
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Thursday October 08, 2009
TOTD #112: Exposing Oracle database tables as RESTful entities using JAX-RS, GlassFish, and NetBeans
This Tip Of The Day explains how to expose an existing Oracle database table as a RESTful Web service endpoint using NetBeans tooling and deployed on GlassFish.
Lets get started!















Do you have the need to expose your Oracle database tables as RESTful entities ?
A complete archive of all the TOTDs is available here.
This and other similar applications will be demonstrated at the upcoming Oracle Open World.
Technorati: totd oracle database glassfish v3 netbeans javaee jax-rs jpa rest
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Wednesday October 07, 2009
TOTD #111: Rails Scaffold for a pre-existing table using Oracle and GlassFish
TOTD #110 explained how to create a brand new Rails application using Oracle database and run it using GlassFish v Gem. This Tip Of The Day explains how to create a scaffold for a sample schema that ships with Oracle database. Even though Rails Scaffold are good for, well, scaffolding but they do get you started easily. This blog will use the sample HR schema that comes along with Oracle database.
Lets get started!
development:
adapter: oracle_enhanced
host: localhost
database: orcl
username: hr
password: hr
~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby script/reverse_scaffold departments department
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/departments
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/departments/index.html.erb
create app/views/departments/show.html.erb
create app/views/departments/new.html.erb
create app/views/departments/edit.html.erb
create app/views/layouts/departments.html.erb
create public/stylesheets/scaffold.css
create app/controllers/departments_controller.rb
create test/functional/departments_controller_test.rb
create app/helpers/departments_helper.rb
create test/unit/helpers/departments_helper_test.rb
route map.resources :departments
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/department.rb
create test/unit/department_test.rb
create test/fixtures/departments.yml
set_primary_key "department_id"
~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S glassfish -l
Starting GlassFish server at: 129.145.133.197:3000 in development environment...
Writing log messages to: /Users/arungupta/samples/v3/rails/oracle/bookstore/log/development.log.
Press Ctrl+C to stop.
Oct 6, 2009 2:14:19 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 3000
. . .
~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby script/reverse_scaffold employees employee
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/employees
exists app/views/layouts/
exists test/functional/
exists test/unit/
exists test/unit/helpers/
exists public/stylesheets/
create app/views/employees/index.html.erb
create app/views/employees/show.html.erb
create app/views/employees/new.html.erb
create app/views/employees/edit.html.erb
create app/views/layouts/employees.html.erb
identical public/stylesheets/scaffold.css
create app/controllers/employees_controller.rb
create test/functional/employees_controller_test.rb
create app/helpers/employees_helper.rb
create test/unit/helpers/employees_helper_test.rb
route map.resources :employees
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/employee.rb
create test/unit/employee_test.rb
create test/fixtures/employees.yml
set_primary_key "employee_id"

So we created a simple Rails CRUD application accessing information from a pre-existing table in the Oracle database server.
Thanks to @mediachk for all the help!
A complete archive of all the TOTDs is available here. The complete list of Rails blog entries are available here.
This and other similar applications will be demonstrated at the upcoming Oracle Open World.
Technorati: totd oracle database glassfish v3 jruby rails oow
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Tuesday October 06, 2009
TOTD #110: JRuby on Rails application using Oracle on GlassFish
GlassFish v3 is the Reference Implementation for Java EE 6. Following the "extensibility" principle of Java EE 6, it also allows Ruby-on-Rails, Groovy and Grails and Python/Django applications to be seamlessly deployed as well, without any additional packaging. This blog has published multiple entries on deploying a Rails application on GlassFish as given below:
All the existing applications have used JavaDB, SQLite3, or MySQL as the database so far. In the process of getting ready for the upcoming Oracle Open World 2009, this Tip Of The Day will show how to use an Oracle database with a JRuby-on-Rails application deployed on GlassFish v3.
Lets get started!
>./bin/jruby -S gem install glassfish
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed rack-1.0.0
Successfully installed glassfish-0.9.5-universal-java
2 gems installed
Installing ri documentation for rack-1.0.0...
Installing ri documentation for glassfish-0.9.5-universal-java...
Installing RDoc documentation for rack-1.0.0...
Installing RDoc documentation for glassfish-0.9.5-universal-java...
Macintosh-187:~ oracle$ sqlplus "/ as sysdba"
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Oct 1 12:32:33 2009
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Release 10.2.0.4.0 - Production
SQL> CREATE USER glassfish IDENTIFIED BY glassfish DEFAULT tablespace users TEMPORARY tablespace temp;
User created.
SQL> GRANT CONNECT TO glassfish IDENTIFIED BY glassfish;
Grant succeeded.
SQL> GRANT UNLIMITED TABLESPACE TO glassfish;
Grant succeeded.
SQL> GRANT CREATE TABLE TO glassfish;
Grant succeeded.
SQL> GRANT CREATE SEQUENCE TO glassfish;
Grant succeeded.
SQL> exit
Disconnected from Oracle Database 10g Release 10.2.0.4.0 - Production
The user name and password are chosen as "glassfish" for simplicity. This is not a recommended setting for production usage though.
rails (2.3.4)
activerecord-jdbc-adapter (0.9.2)
glassfish (0.9.5)
jruby -S gem install rails activercord-jdbc-adapter glassfish
jruby -S rails bookstore -d oracle
ActionView::TemplateError (book_url failed to generate from {:controller=>"books", :action=>"show", :id=>#<Book id: #<BigDecimal:3feef1eb,'10000.0',1(8)>, title: "Ultramarathon Man", author: "Dean Karnazes", created_at: "2009-10-06 00:03:14", updated_at: "2009-10-06 00:03:14">}, expected: {:controller=>"books", :action=>"show"}, diff: {:id=>#<Book id: #<BigDecimal:459bdb65,'10000.0',1(8)>, title: "Ultramarathon Man", author: "Dean Karnazes", created_at: "2009-10-06 00:03:14", updated_at: "2009-10-06 00:03:14">}) on line #13 of app/views/books/index.html.erb:
bookstore >~/tools/jruby/bin/jruby -S gem install activerecord-oracle_enhanced-adapter
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed activerecord-oracle_enhanced-adapter-1.2.2
1 gem installed
Installing ri documentation for activerecord-oracle_enhanced-adapter-1.2.2...
Installing RDoc documentation for activerecord-oracle_enhanced-adapter-1.2.2...
development:
adapter: oracle_enhanced
host: localhost
database: orcl
username: glassfish
password: glassfish
bookstore >~/tools/jruby/bin/jruby script/generate scaffold book title:string author:string
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/books
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/books/index.html.erb
create app/views/books/show.html.erb
create app/views/books/new.html.erb
create app/views/books/edit.html.erb
create app/views/layouts/books.html.erb
create public/stylesheets/scaffold.css
create app/controllers/books_controller.rb
create test/functional/books_controller_test.rb
create app/helpers/books_helper.rb
create test/unit/helpers/books_helper_test.rb
route map.resources :books
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/book.rb
create test/unit/book_test.rb
create test/fixtures/books.yml
create db/migrate
create db/migrate/20091005233152_create_books.rb
bookstore >~/tools/jruby/bin/jruby script/generate jdbc
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
exists config/initializers
create config/initializers/jdbc.rb
exists lib/tasks
create lib/tasks/jdbc.rake
~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S rake db:migrate
(in /Users/arungupta/samples/v3/rails/oracle/bookstore)
== CreateBooks: migrating ====================================================
-- create_table(:books)
-> 0.0740s
-> 0 rows
== CreateBooks: migrated (0.0750s) ===========================================
~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S glassfish -l
Starting GlassFish server at: 129.145.133.197:3000 in development environment...
Writing log messages to: /Users/arungupta/samples/v3/rails/oracle/bookstore/log/development.log.
Press Ctrl+C to stop.
Oct 6, 2009 9:45:51 AM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 3000
. . .






So we created a brand new JRuby/Rails application and ran it using GlassFish and Oracle backend. A subsequent blog entry will show how to create a similar application using an existing database.
A complete archive of all the TOTDs is available here. The complete list of Rails blog entries are available here.
This and other similar applications will be demonstrated at the upcoming Oracle Open World.
Technorati: totd oracle database glassfish v3 jruby rails oow
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Friday October 02, 2009
TOTD #109: How to convert a JSF managed bean to JSR 299 bean (Web Beans) ?
This entry is a follow up to TOTD #95 and shows how to use the recent integrations of JSR 299 in GlassFish v3 to convert a JSF managed bean to a JSR 299 bean (aka Web Beans). The TOTD #95 describes a simple Java EE 6 web application that uses Java Server Faces 2.0 components for displaying the results of a database query conducted by EJB 3.1 and JPA 2.0 classes.
The EJB class, which also acts as the JSF managed bean, looks like:
@javax.ejb.Stateless
@ManagedBean
public class StateList {
@PersistenceUnit
EntityManagerFactory emf;
public List getStates() {
return emf.createEntityManager().createNamedQuery(”States.findAll”).getResultList();
}
}
Three changes are required to convert this class into a JSR 299 compliant bean (Web Bean) as listed below:
@PersistenceUnit
EntityManagerFactory emf;
EntityManager emf = Persistence.createEntityManagerFactory("HelloEclipseLinkPU");

That's it, re-deploy your application and now you are using the Web Beans integration in GlassFish v3 instead of JSF managed bean. The output is available at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" as shown:
This is the exact same output as shown in TOTD #95.
Now, one-by-one, JPA, EJB, Transactions and other components will start working. Read Roger's blog for another example of Web Beans in GlassFish.
A complete archive of all the tips is available here.
Technorati: totd glassfish v3 mysql javaee6 javaserverfaces webbeans jsr299 netbeans
Posted by Arun Gupta in General | Comments[2]
|
|
|
|
|
Thursday October 01, 2009
TOTD #106 explained how to install Oracle database 10g R2 on Mac OS X. TOTD #107 explained how to connect this Oracle database using NetBeans. This Tip Of The Day will explain how to use the sample HR database (that comes with Oracle database server) to write a simple Java EE 6 application.
This application will use Java Server Faces 2.0 for displaying the results, Enterprise Java Beans 3.1 + Java Persistence API 2.0 for middle tier, and Oracle database server + GlassFish v3 as the backend. The latest promoted build (65 of this writing) will not work because of the issue #9885 so this blog will use build 63 instead.
Several improvements have been made over NetBeans 6.8 M1 build and this blog is using the nightly build of 9/27. The environment used in this blog is:
Lets get started!
./bin/asadmin start-domain --verbose &
./bin/asadmin create-jdbc-connection-pool --datasourceclassname oracle.jdbc.pool.OracleDataSource --restype javax.sql.DataSource --property "User=hr:Password=hr:URL=jdbc\:oracle\:thin\:@localhost\:1521\:orcl" jdbc/hr
./bin/asadmin ping-connection-pool jdbc/hr
./bin/asadmin create-jdbc-resource --connectionpoolid jdbc/hr jdbc/hr









@javax.ejb.Stateless
@javax.faces.bean.ManagedBean
@PersistenceUnit
EntityManagerFactory emf;
public List getEmployees() {
return em.createNamedQuery("Employees.findAll").getResultList();
}
@Stateless
@ManagedBean
public class EmployeesBean {
@PersistenceContext
EntityManager em;
public List getEmployees() {
return em.createNamedQuery("Employees.findAll").getResultList();
}
}
<h1>First Java EE 6 app using Oracle database</>
<h:dataTable var="emp" value="#{employeesBean.employees}" border="1">
<h:column><h:outputText value="#{emp.lastName}"/>, <h:outputText value="#{emp.firstName}"/></h:column>
<h:column><h:outputText value="#{emp.email}"/></h:column>
<h:column><h:outputText value="#{emp.hireDate}"/></h:column>
</h:dataTable>

So we can easily create a Java EE 6 application using NetBeans, Oracle, and GlassFish.
A complete archive of all the TOTDs is available here.
This and other similar applications will be demonstrated at the upcoming Oracle Open World.
Technorati: totd oracle database glassfish v3 javaee javaserverfaces ejb jpa netbeans oow
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Wednesday September 30, 2009
TOTD #107: Connect to Oracle database using NetBeans
TOTD #106 explained how to install Oracle database 10g R2 on Mac OS X. This Tip Of The Day will explain how to connect Oracle database with NetBeans to leverage all the goodness provided by NetBeans for Java EE application development, Rails, and others.




ALTER USER HR IDENTIFIED BY hr ACCOUNT UNLOCK;
as explained in TOTD #106. 






A complete archive of all the TOTDs is available here.
Subsequent blogs will show how to write a Java EE application to access the Oracle database.
Technorati: totd oracle database netbeans
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Today's Page Hits: 1984
Total # blog entries: 1024