Monday August 31, 2009
TOTD #97 showed how to install GlassFish Tools Bundle for Eclipse 1.1. Basically there are two options - either install Eclipse 3.4.2 with WTP and pre-bundled/configured with GlassFish v2/v3, MySQL JDBC driver and other features. Or if you are using Eclipse 3.5, then you can install the plug-in separately and get most of the functionality.
TOTD #98 showed how to create a simple Metro/JAX-WS compliant Web service using that bundle and deploy on GlassFish.
This Tip Of The Day (TOTD) shows how to create a simple Java EE 6 application that reads data from a MySQL database using JPA 2.0 and Servlet 3.0 and display the results. A more formal support of Java EE 6/Servlet 3.0 is coming but in the meanwhile the approach mentioned below will work.
Lets get started!














@WebServlet(urlPatterns="/ServletClient")
public class ServletClient extends HttpServlet {
@PersistenceUnit
EntityManagerFactory factory;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletOutputStream out = resp.getOutputStream();
List list = factory.createEntityManager().createQuery("select f from Film f where f.title like 'GL%';").getResultList();
out.println("<html><table>");
for (Object film : list) {
out.print("<tr><td>" + ((Film)film).getTitle() + "</tr></td>");
}
out.println("</table></html>");
}
}
import java.io.IOException; import java.util.List; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.Film;Basically, this is a Servlet 3.0 specification compliant Servlet that uses @WebServlet annotation. It uses @PersistenceUnit to inject the generated JPA Persistence Unit which is then used to query the database. The database query return all the movies whose title start with "GL" and the response is displayed in an HTML formatted table.


Simple, easy and clean!
How are you using Eclipse and GlassFish - the consolidated bundle or standalone Eclipse + GlassFish plugin ?
Download GlassFish Tools Bundle for Eclipse now.
Please send your questions and comments to users@glassfishplugins.dev.java.net.
Please leave suggestions on other TOTD that you’d like to see. A complete archive of all the tips is available here.
Technorati: glassfish eclipse mysql jpa database
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Friday August 28, 2009
svn.dev.java.net is down ... what's new!
Here is how it looks like ...

Do "+1" every time and any time in the comments you see java.net is not behaving for you and let's see how quickly the comments fill up!
UPDATE: Expanding this post to include failures in any java.net domain.Technorati: java.net
Posted by Arun Gupta in General | Comments[3]
|
|
|
|
|
Tuesday August 25, 2009
FREE GlassFish Webinar: "Java EE 6 Overview" - Aug 26, 2009, 10am PT
Java EE 6 is developed as JSR 316 under the Java Community Process. It breaks the "one size fits all" approach with Profiles and improves on the Java EE 5 developer productivity features. Several existing specifications are getting an extreme makeover such as Java Server Faces 2.0 and Servlet 3.0. GlassFish v3 is the Reference Implementation of Java EE 6.
So you'd like to get an overview of Java EE 6 and start developing with GlassFish v3. Please register for a free webinar with coordinates:
Date: Aug 26, 2009
Time: 10am PT
Register here.
Yep, it's starting in a few hours so make sure to sign up and be ready with your questions. Several specification leads will be available to field your questions.
Technorati: glassfish javaee webinar
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
| TOTD #98: Create a Metro JAX-WS Web service using GlassFish Tools Bundle for Eclipse
Now that you've installed GlassFish Tools Bundle for Eclipse 1.1, lets use this bundle to create a simple Metro/JAX-WS compliant Web service and deploy on GlassFish. These steps will work with either Eclipse 3.4.2 or 3.5 with WTP Java EE support.


public String sayHello(String name) {
return "Hello " + name + "!!";
}










Posted by Arun Gupta in webservices | Comments[2]
|
|
|
|
|
Friday August 21, 2009
TOTD #97: GlassFish Plugin with Eclipse 3.5
A new version of GlassFish Tools Bundle for Eclipse (ver 1.1) was recently
released. The build contains








Posted by Arun Gupta in General | Comments[2]
|
|
|
|
|
Wednesday August 19, 2009
GlassFish
Monitoring allows you to monitor the state of various runtime
components of the application server. This information is used to
identify performance bottlenecks and tuning the system for optimal
performance, to aid capacity planning, to predict failures, to do root
cause analysis in case of failures and sometimes to just ensure that
everything is functioning as expected.
GlassFish Management allows you to manage the running Application
Server instance such as query/create/delete resources (JDBC, JMS, etc),
stop/restart the instance, rotate the log and other similar functions.
GlassFish v3 exposes Monitoring and Management data using a REST
Interface. This Tip
Of The Day (TOTD) shows how
to play with this new functionality. Rajeshwar's blog
has lot of useful information on this topic.
Most of the functionality available in
web-based Admin Console and CLI (asadmin) is now available using the
REST interface. Both of these are pre-built
tools that ships with the GlassFish bundle. 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
such as Java, JavaScript, Ruby or Groovy.
The default URL for the REST interface of monitoring is
"http://localhost:4848/monitoring/domain" and for the management is
"http://localhost:4848/management/domain". Each URL provides an XML,
JSON and HTML representation of the
resources. If a web browser is used then a HTML representation is
returned and displayed nicely in the browser. Rajeshwar's
blog described a Java
client written using Jersey
Client APIs that can be used to make all the
GET/PUT/POST/DELETE requests. This blog will use something more basic,
and extremely popular,
to make all the RESTful invocations - cURL.
At this time the monitoring resources are read-only (GET) and
management can
be done using GET/POST/DELETE methods. POST is used for creating and
updating resources/objects and the updates can be partial.
Lets get started.
| ~/tools/glassfish/v3/2023/glassfishv3 >./bin/asadmin start-domain
--verbose Aug 19, 2009 9:52:45 AM com.sun.enterprise.admin.launcher.GFLauncherLogger info INFO: JVM invocation command line: /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java -cp . . . INFO: felix.fileinstall.dir /Users/arungupta/tools/glassfish/v3/2023/glassfishv3/glassfish/domains/domain1/autodeploy-bundles Aug 19, 2009 9:53:05 AM INFO: felix.fileinstall.debug 1 Aug 19, 2009 9:53:05 AM INFO: felix.fileinstall.bundles.new.start true |
| ~/tools/glassfish/v3/2023/glassfishv3 >curl -H "Accept:
application/json" http://localhost:4848/monitoring/domain -v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > GET /monitoring/domain HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: application/json > < HTTP/1.1 200 OK < Content-Type: application/json < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 17:40:29 GMT < {Domain:{},"child-resources":["http://localhost:4848/monitoring/domain/server"]} * Connection #0 to host localhost left intact * Closing connection #0 |
| {Domain:{},"child-resources":["http://localhost:4848/monitoring/domain/server"]} |
| ~/tools/glassfish/v3/2023/glassfishv3 >curl -H "Accept: application/xml"
http://localhost:4848/monitoring/domain -v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > GET /monitoring/domain HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: application/xml > < HTTP/1.1 200 OK < Content-Type: application/xml < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 17:43:51 GMT < <Domain> <child-resource>http://localhost:4848/monitoring/domain/server</child-resource> </Domain> * Connection #0 to host localhost left intact * Closing connection #0 |
| <Domain> <child-resource>http://localhost:4848/monitoring/domain/server</child-resource> </Domain> |

| </tools/glassfish/v3/2023/glassfishv3
>curl -H "Accept:
application/json" http://localhost:4848/monitoring/domain/server -v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > GET /monitoring/domain/server HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: application/json > < HTTP/1.1 200 OK < Content-Type: application/json < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 17:56:41 GMT < {Server:{},"child-resources":["http://localhost:4848/monitoring/domain/server/webintegration", "http://localhost:4848/monitoring/domain/server/transaction-service", "http://localhost:4848/monitoring/domain/server/network", "http://localhost:4848/monitoring/domain/server/jvm", "http://localhost:4848/monitoring/domain/server/web", "http://localhost:4848/monitoring/domain/server/realm", "http://localhost:4848/monitoring/domain/server/http-service"]} * Connection #0 to host localhost left intact * Closing connection #0 |

| ~/tools/glassfish/v3/2023/glassfishv3 >curl -X OPTIONS
http://localhost:4848/management/domain -v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > OPTIONS /management/domain HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: application/json < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 18:07:14 GMT < { "Method":"GET" "Method":"PUT" } * Connection #0 to host localhost left intact * Closing connection #0 |
| ~/tools/glassfish/v3/2023/glassfishv3 >curl -H "Accept:
application/json" http://localhost:4848/management/domain -v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > GET /management/domain HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: application/json > < HTTP/1.1 200 OK < Content-Type: application/json < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 18:14:46 GMT < {Domain:{"log-root" : "${com.sun.aas.instanceRoot}/logs","application-root" : "${com.sun.aas.instanceRoot}/applications","locale" : "","version" : "re-continuous"},"child-resources":["http://localhost:4848/management/domain/configs", "http://localhost:4848/management/domain/resources","http://localhost:4848/management/domain/servers", "http://localhost:4848/management/domain/property","http://localhost:4848/management/domain/applications", "http://localhost:4848/management/domain/system-applications","http://localhost:4848/management/domain/stop", "http://localhost:4848/management/domain/restart","http://localhost:4848/management/domain/uptime", "http://localhost:4848/management/domain/version","http://localhost:4848/management/domain/rotate-log", "http://localhost:4848/management/domain/host-port"]} * Connection #0 to host localhost left intact * Closing connection #0 |
| curl -H "Accept: application/json" http://localhost:4848/management/domain/host-port -v |
| {"GetHostAndPort":{"value" : "dhcp-usca14-132-79.SFBay.Sun.COM:8080"}} |
| curl -H "Accept: application/json" http://localhost:4848/management/domain/system-applications/application/__admingui -v |
| {__admingui:{"libraries" : "","availability-enabled" : "false","enabled" : "true","context-root" : "","location" : "${com.sun.aas.installRootURI}/lib/install/applications/__admingui","description" : "","name" : "__admingui","directory-deployed" : "true","object-type" : "system-admin"},"child-resources":["http://localhost:4848/management/domain/system-applications/application/__admingui/module"]} |
| curl -H "Accept: application/json" http://localhost:4848/management/domain/configs/config/server-config/monitoring-service/module-monitoring-levels -v |
| {ModuleMonitoringLevels:{"transaction-service" : "OFF","ejb-container" : "OFF","jdbc-connection-pool" : "OFF","orb" : "OFF","http-service" : "OFF","connector-connection-pool" : "OFF","jms-service" : "OFF","connector-service" : "OFF","jvm" : "OFF","thread-pool" : "OFF","web-container" : "OFF"},"child-resources":[]} |
| ~/tools/glassfish/v3/2023/glassfishv3 >curl -X POST -d
"web-container=ON" -H "Accept: application/json"
http://localhost:4848/management/domain/configs/config/server-config/monitoring-service/module-monitoring-levels
-v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > POST /management/domain/configs/config/server-config/monitoring-service/module-monitoring-levels HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: application/json > Content-Length: 16 > Content-Type: application/x-www-form-urlencoded > < HTTP/1.1 200 OK < Content-Type: application/json < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 22:01:31 GMT < * Connection #0 to host localhost left intact * Closing connection #0 "http://localhost:4848/management/domain/configs/config/server-config/monitoring-service/module-monitoring-levels" updated successfully |
| ~/tools/glassfish/v3/2023/glassfishv3 >curl -H "Accept:
application/json"
http://localhost:4848/management/domain/configs/config/server-config/monitoring-service/module-monitoring-levels
-v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > GET /management/domain/configs/config/server-config/monitoring-service/module-monitoring-levels HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: application/json > < HTTP/1.1 200 OK < Content-Type: application/json < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 22:36:47 GMT < * Connection #0 to host localhost left intact * Closing connection #0 {ModuleMonitoringLevels:{"transaction-service" : "OFF","ejb-container" : "OFF","jdbc-connection-pool" : "OFF","orb" : "OFF","http-service" : "OFF","connector-connection-pool" : "OFF","jms-service" : "OFF","connector-service" : "OFF","jvm" : "OFF","thread-pool" : "OFF","web-container" : "ON"},"child-resources":[]} |
| curl -H "Accept: application/json" http://localhost:4848/management/domain/stop -v |
| curl -H "Accept: application/json" http://localhost:4848/management/domain/restart -v |
| curl -H "Accept: application/json" http://localhost:4848/management/domain/resources -v |
| {Resources:{},"child-resources":["http://localhost:4848/management/domain/resources/jdbc-connection-pool", "http://localhost:4848/management/domain/resources/jdbc-resource"]} |
| curl -H "Accept: application/json" http://localhost:4848/management/domain/resources/jdbc/connection-pool -v |
| {JdbcConnectionPool:{},"child-resources":["http://localhost:4848/management/domain/resources/jdbc-connection-pool/__TimerPool", "http://localhost:4848/management/domain/resources/jdbc-connection-pool/DerbyPool"]} |
| curl "Accept: application/json" http://localhost:4848/management/domain/resources/jdbc-resource -v |
| {JdbcResource:{},"child-resources":["http://localhost:4848/management/domain/resources/jdbc-resource/jdbc/__TimerPool", "http://localhost:4848/management/domain/resources/jdbc-resource/jdbc/__default"]} |
| curl -X OPTIONS -H "Accept: application/json" http://localhost:4848/management/domain/resources/jdbc-resource -v |
| { "Method":"POST", "Message Parameters":{ "id":{"Acceptable Values":"","Default Value":"","Type":"class java.lang.String","Optional":"false"}, "enabled":{"Acceptable Values":"","Default Value":"true","Type":"class java.lang.Boolean","Optional":"true"}, "description":{"Acceptable Values":"","Default Value":"","Type":"class java.lang.String","Optional":"true"}, "target":{"Acceptable Values":"","Default Value":"","Type":"class java.lang.String","Optional":"true"}, "property":{"Acceptable Values":"","Default Value":"","Type":"class java.util.Properties","Optional":"true"}, "connectionpoolid":{"Acceptable Values":"","Default Value":"","Type":"class java.lang.String","Optional":"false"} } "Method":"GET" |
| ~/tools/glassfish/v3/2023/glassfishv3 >curl -d
"id=jdbc/sample&connectionpoolid=DerbyPool"
http://localhost:4848/management/domain/resources/jdbc-resource -v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > POST /management/domain/resources/jdbc-resource HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: */* > Content-Length: 42 > Content-Type: application/x-www-form-urlencoded > < HTTP/1.1 201 Created < Content-Type: text/html < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 20:45:51 GMT < * Connection #0 to host localhost left intact * Closing connection #0 "http://localhost:4848/management/domain/resources/jdbc-resource/jdbc/sample" created successfully. |
| curl -H "Accept: application/json" http://localhost:4848/management/domain/resources/jdbc-resource -v |
| {JdbcResource:{},"child-resources":["http://localhost:4848/management/domain/resources/jdbc-resource/jdbc/__TimerPool", "http://localhost:4848/management/domain/resources/jdbc-resource/jdbc/__default", "http://localhost:4848/management/domain/resources/jdbc-resource/jdbc/sample"]} |
| ~/tools/glassfish/v3/2023/glassfishv3 >curl -H "Accept: application/xml"
http://localhost:4848/management/domain -v * About to connect() to localhost port 4848 (#0) * Trying ::1... connected * Connected to localhost (::1) port 4848 (#0) > GET /management/domain HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: localhost:4848 > Accept: application/xml > < HTTP/1.1 200 OK < Content-Type: application/xml < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 18:17:07 GMT < <Domain log-root="${com.sun.aas.instanceRoot}/logs" application-root="${com.sun.aas.instanceRoot}/applications" locale="" version="re-continuous"> <child-resource>http://localhost:4848/management/domain/configs</child-resource> <child-resource>http://localhost:4848/management/domain/resources</child-resource> <child-resource>http://localhost:4848/management/domain/servers</child-resource> <child-resource>http://localhost:4848/management/domain/property</child-resource> <child-resource>http://localhost:4848/management/domain/applications</child-resource> <child-resource>http://localhost:4848/management/domain/system-applications</child-resource> <child-resource>http://localhost:4848/management/domain/stop</child-resource> <child-resource>http://localhost:4848/management/domain/restart</child-resource> <child-resource>http://localhost:4848/management/domain/uptime</child-resource> <child-resource>http://localhost:4848/management/domain/version</child-resource> <child-resource>http://localhost:4848/management/domain/rotate-log</child-resource> <child-resource>http://localhost:4848/management/domain/host-port</child-resource> * Connection #0 to host localhost left intact * Closing connection #0 |

Posted by Arun Gupta in General | Comments[8]
|
|
|
|
|
Tuesday August 18, 2009
LOTD #22: How to inject JPA resources ? - PersistenceUnit vs PersistenceContext
Java Persistence API defines a standard object/relational mapping using
POJOs. In JPA, a persistence
unit is described using "persistence.xml", bundled with
the web application, injected into your web application and then POJOs
are used to access all the information from the underlying persistence
mechanism such as a database.
JPA can injected into your application couple of different ways as
shown below:
| @PersistenceUnit EntityManagerFactory emf; |
| @PersistenceContext EntityManager manager; |
Posted by Arun Gupta in General | Comments[1]
|
|
|
|
|
Monday August 17, 2009
TOTD
#93
showed how to get started with Java EE 6
using NetBeans
6.8 M1 and
GlassFish v3 by
building a simple Servlet 3.0 + JPA 2.0 web
application. TOTD
#94 built upon it by using Java Server Faces 2 instead of
Servlet 3.0 for displaying the results. However we are still using a
POJO
for all the database interactions. This works fine if we are only
reading values from the database but that's not how a typical web
application behaves. The web application would typically perform all
CRUD operations. More typically they like to perform one or more CRUD
operations within the context of a transaction. And how do you do
transactions in the context of a web application ? Java EE 6 comes to
your rescue.
The EJB 3.1
specification (another new specification in Java EE 6) allow
POJO classes to be annotated with @EJB and bundled within
WEB-INF/classes of a WAR file. And so you get all transactional
capabilities in your web application very easily.
This Tip
Of The Day (TOTD) shows how
to enhance the application created in TOTD #94 and use EJB 3.1 instead
of the JSF managed bean
for
performing the business logic. There are two ways to achieve this
pattern as described below.
Lets call this TOTD #95.1
| @javax.ejb.Stateless @ManagedBean public class StateList { @PersistenceUnit EntityManagerFactory emf; public List<States> getStates() { return emf.createEntityManager().createNamedQuery("States.findAll").getResultList(); } } |


| @Stateless public class StateBeanBean { @PersistenceUnit EntityManagerFactory emf; public List<States> getStates() { return emf.createEntityManager().createNamedQuery("States.findAll").getResultList(); } } |
| @ManagedBean public class StateList { @EJB StateBeanBean bean; public List<States> getStates() { return bean.getStates(); } } |
| @Stateless public class StateBeanBean { @PersistenceContext EntityManager em; public List<States> getStates() { return em.createNamedQuery("States.findAll").getResultList(); } } |

Posted by Arun Gupta in General | Comments[1]
|
|
|
|
|
Friday August 14, 2009
TOTD
#93
showed how to get started with Java EE 6
using NetBeans
6.8 M1 and
GlassFish v3 by
building a simple Servlet 3.0 + JPA 2.0 web
application. JPA 2.0 + Eclipselink was used for the database
connectivity
and Servlet 3.0 was used for displaying the results to the user. The
sample demonstrated how the two technologies can be mixed to create a
simple web application. But Servlets are meant for server-side
processing rather than displaying the results to end user. JavaServer
Faces 2 (another new specification in Java EE 6) is designed
to fulfill
that purpose.
This Tip
Of The Day (TOTD) shows how
to enhance the application created in TOTD #93 and use JSF 2 for
displaying the results.


| package server; import java.util.List; import javax.faces.bean.ManagedBean; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import states.States; /** * @author arungupta */ @ManagedBean public class StateList { @PersistenceUnit EntityManagerFactory emf; public List<States> getStates() { return emf.createEntityManager().createNamedQuery("States.findAll").getResultList(); } } |
| Show States |
|
<h:dataTable var="state" value="#{stateList.states}"
border="1"> <h:column><h:outputText value="#{state.abbrev}"/></h:column> <h:column><h:outputText value="#{state.name}"/></h:column> </h:dataTable> |


Posted by Arun Gupta in General | Comments[3]
|
|
|
|
|
Thursday August 13, 2009
NetBeans
6.8 M1 introduces support for creating Java EE 6 applications
... cool!
This Tip Of The Day (TOTD) shows how
to create a simple web application using JPA 2.0 and Servlet 3.0 and
deploy on GlassFish v3 latest
promoted build (58
as of this writing). If you can work with the one week older build then
NetBeans 6.8 M1 comes pre-bundled with 57. The example below should
work fine on that as well.
| ~/tools/glassfish/v3/58/glassfishv3/bin >sudo mysql --user root Password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1592 Server version: 5.1.30 MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> create database states; Query OK, 1 row affected (0.02 sec) mysql> CREATE USER duke IDENTIFIED by 'glassfish'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL on states.* TO duke; Query OK, 0 rows affected (0.24 sec) mysql> use states; Database changed mysql> CREATE TABLE STATES ( -> id INT, -> abbrev VARCHAR(2), -> name VARCHAR(50), -> PRIMARY KEY (id) -> ); Query OK, 0 rows affected (0.16 sec) mysql> INSERT INTO STATES VALUES (1, "AL", "Alabama"); INSERT INTO STATES VALUES (2, "AK", "Alaska"); . . . mysql> INSERT INTO STATES VALUES (49, "WI", "Wisconsin"); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO STATES VALUES (50, "WY", "Wyoming"); Query OK, 1 row affected (0.00 sec) |
| ~/tools/glassfish/v3/58/glassfishv3/bin >asadmin start-domain |
| ~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin
create-jdbc-connection-pool --datasourceclassname
com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype
javax.sql.DataSource --property
"User=duke:Password=glassfish:URL=jdbc\:mysql\://localhost/states"
jdbc/states Command create-jdbc-connection-pool executed successfully. ~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin ping-connection-pool jdbc/states Command ping-connection-pool executed successfully. ~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin create-jdbc-resource --connectionpoolid jdbc/states jdbc/jndi_states Command create-jdbc-resource executed successfully. |





| @PersistenceUnit EntityManagerFactory emf; |
|
List<States> list =
emf.createEntityManager().createNamedQuery("States.findAll").getResultList(); out.println("<table border=\"1\">"); for (States state : list) { out.println("<tr><td>" + state.getAbbrev() + "</td><td>" + state.getName() + "</td></tr>"); } out.println("</table>"); |



Posted by Arun Gupta in General | Comments[5]
|
|
|
|
|
Wednesday August 12, 2009
TOTD #92: Session Failover for Rails applications running on GlassFish
The GlassFish
High Availability
allows to setup a cluster of GlassFish instances and achieve highly
scalable architecture using in-memory session state replication. This
cluster can be very
easily created and tested using the "clusterjsp" sample
bundled with GlassFish. Here are some clustering related entries
published on this blog so far:
| ~/samples/jruby/session >~/tools/jruby/bin/jruby
script/generate controller home index JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL exists app/controllers/ exists app/helpers/ create app/views/home exists test/functional/ create test/unit/helpers/ create app/controllers/home_controller.rb create test/functional/home_controller_test.rb create app/helpers/home_helper.rb create test/unit/helpers/home_helper_test.rb create app/views/home/index.html.erb |
| class HomeController < ApplicationController include Java def index @server_served = servlet_request.get_server_name @port = servlet_request.get_server_port @instance = java.lang.System.get_property "com.sun.aas.instanceName" @server_executed = java.net.InetAddress.get_local_host().get_host_name() @ip = java.net.InetAddress.get_local_host().get_host_address @session_id = servlet_request.session.get_id @session_created = servlet_request.session.get_creation_time @session_last_accessed = servlet_request.session.get_last_accessed_time @session_inactive = servlet_request.session.get_max_inactive_interval if (params[:name] != nil) servlet_request.session[params[:name]] = params[:value] end @session_values = "" value_names = servlet_request.session.get_attribute_names unless (value_names.has_more_elements) @session_values = "<br>No parameter entered for this request" else @session_values << "<UL>" while (value_names.has_more_elements) param = value_names.next_element unless (param.starts_with?("__")) value = servlet_request.session.get_attribute(param) @session_values << "<LI>" + param + " = " + value + "</LI>" end end @session_values << "</UL>" end end def adddata servlet_request.session.set_attribute(params[:name], params[:value]) render :action => "index" end def cleardata servlet_request.session.invalidate render :action => "index" end end |
| <h1>Home#index</h1> <p>Find me in app/views/home/index.html.erb</p> <B>HttpSession Information:</B> <UL> <LI>Served From Server: <b><%= @server_served %></b></LI> <LI>Server Port Number: <b><%= @port %></b></LI> <LI>Executed From Server: <b><%= @server_executed %></b></LI> <LI>Served From Server instance: <b><%= @instance %></b></LI> <LI>Executed Server IP Address: <b><%= @ip %></b></LI> <LI>Session ID: <b><%= @session_id %></b></LI> <LI>Session Created: <%= @session_created %></LI> <LI>Last Accessed: <%= @session_last_accessed %></LI> <LI>Session will go inactive in <b><%= @session_inactive %> seconds</b></LI> </UL> <BR> <% form_tag "/session/home/index" do %> <label for="name">Name of Session Attribute:</label> <%= text_field_tag :name, params[:name] %><br> <label for="value">Value of Session Attribute:</label> <%= text_field_tag :value, params[:value] %><br> <%= submit_tag "Add Session Data" %> <% end %> <% form_tag "/session/home/cleardata" do %> <%= submit_tag "Clear Session Data" %> <% end %> <% form_tag "/session/home/index" do %> <%= submit_tag "Reload Page" %> <% end %> <BR> <B>Data retrieved from the HttpSession: </B> <%= @session_values %> |
| ~/samples/jruby/session >~/tools/jruby/bin/jruby -S warble
war:webxml mkdir -p tmp/war/WEB-INF ~/samples/jruby/session >cp tmp/war/WEB-INF/web.xml config/ |
| <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> |
| <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> |
| <distributable/> |
| ~/samples/jruby/session >~/tools/jruby/bin/jruby -S warble mkdir -p tmp/war/WEB-INF/gems/specifications cp /Users/arungupta/tools/jruby-1.3.0/lib/ruby/gems/1.8/specifications/rails-2.3.2.gemspec tmp/war/WEB-INF/gems/specifications/rails-2.3.2.gemspec . . . mkdir -p tmp/war/WEB-INF cp config/web.xml tmp/war/WEB-INF jar cf session.war -C tmp/war . |
| ~/samples/jruby/session >asadmin deploy --target wines --port 5048 --availabilityenabled=true session.war |








Posted by Arun Gupta in General | Comments[2]
|
|
|
|
|
Tuesday August 11, 2009
TOTD #91: Applying Java EE 6 "web-fragment.xml" to Apache Wicket - Deploy on GlassFish v3
"Extensibility" is a major theme of Java EE 6.
This theme enables seamless pluggability of other popular Web
frameworks with Java EE 6.
Before
Java EE 6, these frameworks have to rely upon registering servlet
listeners/filters in "web.xml" or some other similar mechanism to
register the framework with the Web container. Thus your application
and framework deployment descriptors are mixed together. As an
application developer you need to figure out the magical descriptors of
the framework that will make this registration.
What if you are
using multiple frameworks ? Then "web.xml" need to have multiple of
those listeners/servlets. So your deployment descriptor becomes
daunting and maintenance nightmare even before any application
deployment artifacts are added.
Instead you should focus on your
application descriptors and let the framework developer provide the
descriptors along with their jar file so that the registration is
indeed magical.
For that, the Servlet
3.0 specification introduces "web module deployment
descriptor fragment" (aka "web-fragment.xml"). The spec defines it as:
A
web fragment is a logical partitioning of the web app in such a way
that the frameworks being used within the web app can define all the
artifacts without asking devlopers to edit or add information in the
web.xml.
Basically, the framework configuration
deployment descriptor can now be defined in "META-INF/web-fragment.xml"
in the JAR file of the framework. The Web container picks up and use
the configuration for registering the framework. The spec clearly
defines the rules around ordering, duplicates and other complexities.
TOTD
#86 explained how to get started with Apache Wicket on
GlassFish. This Tip Of The Day (TOTD) explains
how to
leverage "web-fragment.xml" to deploy a Wicket application on
GlassFish v3. The basic concepts are also discussed here.
For the "Hello World" app discussed in TOTD
#86, the generated "web.xml" looks like:
| <?xml version="1.0"
encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>helloworld</display-name> <!-- There are three means to configure Wickets configuration mode and they are tested in the order given. 1) A system property: -Dwicket.configuration 2) servlet specific <init-param> 3) context specific <context-param> The value might be either "development" (reloading when templates change) or "deployment". If no configuration is found, "development" is the default. --> <filter> <filter-name>wicket.helloworld</filter-name> <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> <init-param> <param-name>applicationClassName</param-name> <param-value>org.glassfish.samples.WicketApplication</param-value> </init-param> </filter> <filter-mapping> <filter-name>wicket.helloworld</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
|
<dependencies> . . . <!-- web-fragment --> <dependency> <groupId>org.glassfish.extras</groupId> <artifactId>wicket-quickstart-web-fragment</artifactId> <version>1.0</version> <scope>runtime</scope> </dependency> </dependencies> . . . <repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> </repository> </repositories> |
| <web-fragment> <filter> <filter-name>wicket.helloworld</filter-name> <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> <init-param> <param-name>applicationClassName</param-name> <param-value>org.glassfish.samples.WicketApplication</param-value> </init-param> </filter> <filter-mapping> <filter-name>wicket.helloworld</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-fragment> |
|
<plugins> . . . <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> |
| helloworld-1.0-SNAPSHOT helloworld-1.0-SNAPSHOT/META-INF helloworld-1.0-SNAPSHOT/WEB-INF helloworld-1.0-SNAPSHOT/WEB-INF/classes helloworld-1.0-SNAPSHOT/WEB-INF/classes/log4j.properties helloworld-1.0-SNAPSHOT/WEB-INF/classes/org helloworld-1.0-SNAPSHOT/WEB-INF/classes/org/glassfish helloworld-1.0-SNAPSHOT/WEB-INF/classes/org/glassfish/samples helloworld-1.0-SNAPSHOT/WEB-INF/classes/org/glassfish/samples/HomePage.class helloworld-1.0-SNAPSHOT/WEB-INF/classes/org/glassfish/samples/HomePage.html helloworld-1.0-SNAPSHOT/WEB-INF/classes/org/glassfish/samples/WicketApplication.class helloworld-1.0-SNAPSHOT/WEB-INF/lib helloworld-1.0-SNAPSHOT/WEB-INF/lib/log4j-1.2.14.jar helloworld-1.0-SNAPSHOT/WEB-INF/lib/slf4j-api-1.4.2.jar helloworld-1.0-SNAPSHOT/WEB-INF/lib/slf4j-log4j12-1.4.2.jar helloworld-1.0-SNAPSHOT/WEB-INF/lib/wicket-1.4.0.jar helloworld-1.0-SNAPSHOT/WEB-INF/lib/wicket-quickstart-web-fragment-1.0.jar |

Posted by Arun Gupta in web2.0 | Comments[5]
|
|
|
|
|
Monday August 10, 2009
"Miles to go" is 4 years old ... yaay!
Last week (Aug 2 to be precise), this blog turned 4 years old.
All the way from Hello
Blogsphere more than 4 years ago to multiple tips,
screencasts, deep dive technical entries, fun stuff, running tips and
lots more ... I've thoroughly enjoyed publishing content on this blog.
Hope you did too :)
Here are some comparable statistics with blogs.sun.com ...
| blogs.sun.com | Miles to go ... | |
| Age | 5 yrs, 4 months | 4 years and 1 week |
| First Entry | Apr 27, 2004 | Aug 2, 2005 |
| Total Entries | 138248 | 944 (0.68%) |
| Total Comments | 156060 | 3537 (2.26%) |



![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Friday August 07, 2009
TOTD #91: Retrieve JSON libraries using Maven dependency: json-lib
So you need to include JSON
libraries in your Maven project. The only option that seems to be
currently available is using json-lib
with the following dependencies:
|
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.3</version> <classifier>jdk15</classifier> </dependency> |

Posted by Arun Gupta in web2.0 | Comments[6]
|
|
|
|
|
Thursday August 06, 2009
TOTD #90: Migrating from Wicket 1.3.x to 1.4 - "Couldn't load DiskPageStore index from file" error
Now that Apache Wicket
1.4
is available, migrating from previous versions is pretty
straight forward.
Change the version in your POM file as:
| <wicket.version>1.4.0</wicket.version> |
| <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket</artifactId> <version>1.4.0</version> </dependency> |
| <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket</artifactId> <version>${wicket.version}</version> </dependency> |
| 2009-08-05 05:58:49.387::INFO: No Transaction
manager found - if your webapp requires one, please configure one. ERROR - DiskPageStore - Couldn't load DiskPageStore index from file /Users/arungupta/workspaces/runner~subversion/wicket/runner/target/work/wicket.runner-filestore/DiskPageStoreIndex. java.lang.ClassNotFoundException: org.apache.wicket.util.concurrent.ConcurrentHashMap at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:319) |
| mvn clean jetty:run |
Posted by Arun Gupta in General | Comments[0]
|
|
|
|
|
Today's Page Hits: 784
Total # blog entries: 1002