The Source for Java Technology Collaboration
User: Password:



Vivek Pandey's Blog

March 2008 Archives


My first Jython web app on GlassFish v3

Posted by vivekp on March 26, 2008 at 04:17 PM | Permalink | Comments (6)

I have been thinking of playing with Jython and GlassFish for quite some time. I thought of taking the first shot at it. So after looking around a bit I found out that I can simply use PyServlet to delegate the HTTP requests to my Jython servlet which can do whatever with the Python script and write it back to the Servlet output stream. Let us see how I did it:

Setup

Create a Python Servlet

  • Create the servlet layout:

    cd c:/dev
    mkdir calendar
    cd calendar
    mkdir WEB-INF
    mkdir WEB-INF/lib
    

    Now, create Show.py, which is a Python Servlet and save it at c:/dev/calendar:

    from javax.servlet.http import HttpServlet 
    class Show (HttpServlet): 
        def doGet(self,request,response): 
            self.doPost (request,response) 
        def doPost(self,request,response): 
            toClient = response.getWriter() 
            response.setContentType ("text/html") 
            toClient.println ("<body><h1>Calendar</h1><pre>No 
    Calendar</pre></body></html>")
    
  • Create web.xml to serve all the python files by PyServlet:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <servlet>
            <servlet-name>PyServlet</servlet-name>
            <servlet-class>org.python.util.PyServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>PyServlet</servlet-name>
            <url-pattern>*.py</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
    </web-app>    
    
  • Copy jython.jar and python libraries to WEB-INF/lib. The Python libraries are located under Lib directory of Jython installation directory. You will also find jython.jar under the Jython installation directory.
    cd c:/tools/jython
    cp jython.jar c:/dev/calendar/WEB-INF/lib
    cp -rf Lib c:/dev/calendar/WEB-INF/lib
    

Deploy the Python web application

  • Start GlassFish v3

    gfstart-jython.png
[/c/dev]
$./glassfish/bin/asadmin deploy calendar
properties=(name=calendar)

Now access http://localhost:8080/calendar/Show.py, it should show this page:

cal1.png

This was simply a jython servlet but there was no Python specific libraries involved.

Invoke Python libraries

We will show Year 2008 calendar using python class calendar.

Let us modify Show.py.

import sys,calendar,time 
from javax.servlet.http import HttpServlet 
class Show (HttpServlet): 
    def doGet(self,request,response): 
        self.doPost (request,response) 
    def doPost(self,request,response): 
        toClient = response.getWriter() 
        response.setContentType ("text/html") 
        toClient.println 
("<body><h1>Calendar</h1><pre>%s</pre></body></html>" % 
calendar.calendar(time.localtime()[0]))

Notice that the main change made to Show.py is that we invoke calendar.calendar() and pass it the localtime. Also, there is an import of calendar and time.

Now, go back to your browser and refresh the page and you would see Year 2008 calendar:
cal2.png

This is it! However it involves Jython Servlet and some wiring and could be lot simpler. Frank plans to work on simplified and better integrated support for Jython on GlassFish. Stay tuned there is more to come...



Using JPA with Rails on GlassFish and MySQL

Posted by vivekp on March 21, 2008 at 12:56 AM | Permalink | Comments (2)

In my last post I mentioned that "...with Rails there are some known limitations, such as the ORM in Rails is not pluggable" by which I basically meant that you can't plug in another ORM and the Rails Model creation and db migration will not generate the correct code for another kind of ORM. The good news is that with little bit of coding you can reuse your entity classes. caroljmcdonald commented on my blog that Brian in his blog succssfully tried out JPA and Rails on WEBRick using JavaDB. So, in this blog I would go over what you would need to do to make JPA and Rails work on GlassFish v2.

Let's first try to understand how it is going to work: Rails by default uses ActiveRecords for Object Relational Mapping purposes or ORM. JPA is another ORM technique in JavaEE. Model class and database migration in Rails is what Entity class and persistence provider does for you in Java. Essentially In Java you just provide the Entity class and on the Rails side, you would provide the Model corresponding to the Entity class to do CRUD and that is pretty much it. The JPA provider does the rest.

This just means that to use Rails with JPA we simply do not use ActiveRecords - this means we don't create Model using Rails and we dont migrate the database. Just define your Entity class and code up the Rails Model without extending from ActiveRecord::Base.

Here is how I did it, which is basically few changes in Brian's sample and setting up GlassFish v2 with JRuby on Rails UC module.

Setup GlassFish v2 and NetBeans

  1. Install NetBeans 6.1 Beata with Web and Java EE profile.
  2. Follow these directions to install and setup JRuby on GlassFish v2.
  3. Setup JRuby jars as shared libraries on Glassfish

    cd $GLASSFISH_HOME/jruby
    $ant -f install.xml setup-shared-env
    
  4. Copy MySQL driver from your NetBeans installation:

    cp "C:\Program Files\NetBeans 6.1 Beta\ide9\modules\ext\mysql-connector-java-5.1.5-bin.jar" c:/tools/glassfish/lib
    
    or from here to $GLASSFISH_HOME/lib.
  5. Start MySQL server and create test database if not already there

    mysqladmin -u root create test;
    

Create Java Entity Beans with MySQL

First we create an entity class and a persistence unit.

  1. Create a new Java Application named Post. There's no need for a Main Class.
  2. Create a new Persistence Unit. Select jdbc:mysql://localhost:3306/test [root on Default Schema] as the database connection and the persistence unit name as PostPUclick Finish. This will generate src/META-INF/persistence.xml
  3. .
  4. Create a new Entity Class named Posts. Set the package name to entity.
  5. Add fields and accessors for title and body. Your completed class should look as follows:

    package entity;
    
    import java.io.Serializable;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    /**
     *
     * @author vivek
     */
    @Entity
    public class Posts implements Serializable {
        private static final long serialVersionUID = 1L;
        private Long id;
        private String title;
        private String body;
    
        public void setId(Long id) {
            this.id = id;
        }
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        public Long getId() {
            return id;
        }
    
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
    
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Posts)) {
                return false;
            }
            Posts other = (Posts) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
    
        @Override
        public String toString() {
            return "entity.Post[id=" + id + "]";
        }
        
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getBody() {
            return body;
        }
    
        public void setBody(String body) {
            this.body = body;
        }
    
    }
        
  6. Create Post.jar by clicking Build->Clean and Build Main Project or Shift+F11. This will create dist/Post.jar inside your project.
  7. Copy Post.jar to $GLASSFISH_HOME/lib directory. $GLASSFISH_HOME is where you installed GlassFish.
        cp dist/Post.jar c:/tools/glassfish/lib
        

Create the Rails Application

Simply download the Rails application then do the following:


cd $GLASSFISH_HOME/jruby/samples
unzip jpa_blog.zip
cd jpa_blog

Brian's sample Rails application was created using Rails 1.2.3 and GlassFish v2 JRuby module includes Rails version 1.2.6. So edit config/environment.rb and replace RAILS_GEM_VERSION = '1.2.3' with RAILS_GEM_VERSION = '1.2.6'.

The last thing you want to do is prepare this rails application for deployment over GlassFish v2:

[/c/tools/glassfish/jruby/samples/jpa_blog]
$ant -f ../../install.xml create-shared
Buildfile: ..\..\install.xml

create-shared:
    [mkdir] Created dir: c:\tools\glassfish\jruby\samples\jpa_blog\WEB-INF
     [copy] Copying 1 file to c:\tools\glassfish\jruby\samples\jpa_blog\WEB-INF

BUILD SUCCESSFUL
Total time: 0 seconds

Simply deploy your rails application on to GlassFish (if you have not started Glassfish during GlassFish setup, you should do it now):

[/c/tools/glassfish/jruby/samples/jpa_blog]
$cd ..
[/c/tools/glassfish/jruby/samples]
$../../bin/asadmin.bat deploy jpa_blog
Command deploy executed successfully.

In the GlassFish server.log you should see:

[#|2008-03-20T17:46:32.250-0700|INFO|sun-appserver9.1|javax.enterprise.system.to
ols.deployment|_ThreadID=20;_ThreadName=Thread-28;|deployed with moduleid = jpa_
blog|#]

[#|2008-03-20T17:46:33.531-0700|INFO|sun-appserver9.1|javax.enterprise.system.co
ntainer.web|_ThreadID=21;_ThreadName=ObjectPoolManager;|PWC1412: WebModule[/jpa_
blog] ServletContext.log():JRuby init time: 94ms|#]

[#|2008-03-20T17:46:39.296-0700|INFO|sun-appserver9.1|javax.enterprise.system.co
ntainer.web|_ThreadID=21;_ThreadName=ObjectPoolManager;|PWC1412: WebModule[/jpa_
blog] ServletContext.log():Rails init time: 5750ms|#]

[#|2008-03-20T17:46:39.296-0700|INFO|sun-appserver9.1|javax.enterprise.system.co
ntainer.web|_ThreadID=21;_ThreadName=ObjectPoolManager;|PWC1412: WebModule[/jpa_
blog] ServletContext.log():Runtime 0 loaded|#]

[#|2008-03-20T17:46:39.296-0700|INFO|sun-appserver9.1|javax.enterprise.system.co
ntainer.web|_ThreadID=21;_ThreadName=ObjectPoolManager;|PWC1412: WebModule[/jpa_
blog] ServletContext.log():Requests can now be processed|#]

Now, accessing http://localhost:8080/jpa_blog/blog/ should show you:

index.PNG

Create a new Blog entry:

new.png

See the list of all the blogs:

list.PNG

I wanted to try it out on Glassfish gem but JPA is under development in GlassFish v3 and I get exception coming form inside GlassFish v3 where it can't find the PersistenceProvider:

javax.persistence.PersistenceException: No resource files named META-INF/services/javax.persistence.spi.PersistenceProvider were found. Please make sure that the persistence provider jar file is in your classpath.

Mitesh is working on JPA support in Glassfish v3, I would do a follow-up blog for GlassFish gem as soon as I see it working.

You can get the Rails and Entity code using the following link:

Continue using JRuby on GlassFish and keep sending us feedback on GlassFish forums, dev@glassfish or gem mailing list. For the latest information about the gem and JRuby Update Center module can be found at GlassFish JRuby wiki.

Report on JRuby On Glassfish@Silicon Valley JUG

Posted by vivekp on March 19, 2008 at 11:22 AM | Permalink | Comments (2)

I was at Silicon Valley JUG (Java User's Group) to talk about Ruby on Rails in GlassFish. See Arun's announcement on this talk. The slides used in this presentation can be found here.

The main theme of my presentation was on highlighting Rails development and deployment on GlassFish v2 and using GlassFish gem with live demos.

These are some of the questions asked during presentation and my answer to those

Q:This is cool that Rails applications can be deployed and run over GlassFish a JavaEE application server, but can the JavaEE features be invoked from inside a Rails application?

A:Yes we think it can be done, with JRuby you can call anything in Java. We know from one of the JRuby users that he is able to call in to EJB service form Rails. Having said that with Rails there are some known limitations, such as the ORM in Rails is not pluggable, so we can't plugin GlassFish JPA implementation in to Rails to take benefit of it. Merb allows such pluggable ORM. As a followup we would provide samples/blogs on Rails and JavaEE features.

Q:What is the performance comparison between JRuby vs Ruby?

Q:No formal performance study has been done. There are some blog posts that talk about it, such as this one by Nick, that shows with warm up JRuby/GlassFish as good as C-Ruby/Mogreal and there is another JRuby on GlassFish comparison to Ruby/Mogrel. We expect with GlassFish gem these numbers would look even better. In next few months the focus is going to be on performance and scalability, so stay tuned.

Q:Can you call in to Ruby from Java?

A:Yes, you can do it using JSR-223 APIs. JSR-223 scripting APIs are part of JDK6, with JDK 5 or earlier versions, you can get it from here.

Q:Does standalone JRuby programs can also get affect from PermGen issue?

A:It really depends on the application. For example, in a Rails application, because Rails being single threaded and so it results in to creating Rails for each of the concurrent requests and hence each Rails instance ends up consuming more of Permanent heap space.

There were more questions that I can't recall now. The talk was video recorded, I am not sure where and when it will be posted but when I get that link, I will add it to this blog.

Please use JRuby on GlassFish and send us feedback on GlassFish forums, dev@glassfish or gem mailing list. For the latest information about the gem and JRuby Update Center module can be found at GlassFish JRuby wiki.

Grails application now working on GlassFish v3

Posted by vivekp on March 07, 2008 at 01:58 PM | Permalink | Comments (1)

Recently Guillaume reported to me about his Grail app not deploying on GlassFish v3 Preview 2. The problem reported was that the Grail app was taking lots of time to deploy on GlassFish v3 Preview 2. Although such failures are not acceptable but considering GlassFish v3 is a complete new architecture, is still under development and feature incomplete and above all the preview releases do not go thru the normal test cycle so such bugs can appear.

This issue was discussed at the GlassFish mailing list, see the discussion here and the corresponding bug.

The good news is that Jerome quickly found out what the problem was and after the code went thru reviews, it was checked in and the fix went into yesterday's nightly build.

Here is how I created and deployed a Grails application on GlassFish v3:

Create a simple Grails app

Setup Grails This is the standard way you would setup the Grails environments:

    export PATH=$GRAILS_HOME/bin:$PATH
    export GRAILS_HOME=/tools/grails
    

Create a simple Grails app
    vivekmz@boson(555)> grails create-app MyFirstGrailsApp
    Welcome to Grails 1.0.1 - http://grails.org/
    Licensed under Apache Standard License 2.0
    Grails home is set to: /tools/grails
    Base Directory: /ws/sb
    Environment set to development
    Note: No plugin scripts found
    Running script /tools/grails/scripts/CreateApp.groovy
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/src
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/src/java
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/src/groovy
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/controllers
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/services
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/domain
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/taglib
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/utils
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/views
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/views/layouts
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/i18n
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/conf
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/test
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/test/unit
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/test/integration
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/scripts
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/web-app
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/web-app/js
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/web-app/css
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/web-app/images
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/web-app/META-INF
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/lib
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/conf/spring
    [mkdir] Created dir: /ws/sb/MyFirstGrailsApp/grails-app/conf/hibernate
    [propertyfile] Creating new property file:
    /ws/sb/MyFirstGrailsApp/application.properties
    [copy] Copying 2 files to /ws/sb/MyFirstGrailsApp
    [copy] Copying 2 files to /ws/sb/MyFirstGrailsApp/web-app/WEB-INF
    [copy] Copying 5 files to /ws/sb/MyFirstGrailsApp/web-app/WEB-INF/tld
    [copy] Copying 87 files to /ws/sb/MyFirstGrailsApp/web-app
    [copy] Copying 17 files to /ws/sb/MyFirstGrailsApp/grails-app
    [copy] Copying 1 file to /ws/sb/MyFirstGrailsApp
    [copy] Copying 1 file to /ws/sb/MyFirstGrailsApp
    [copy] Copying 1 file to /ws/sb/MyFirstGrailsApp
    [propertyfile] Updating property file:
    /ws/sb/MyFirstGrailsApp/application.properties
    Created Grails Application at /ws/sb/MyFirstGrailsApp
    

Start GlassFish v3

    vivekmz@boson(735)> glassfish/bin/asadmin start-domain
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: HK2 initialized in 281 ms
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.naming.impl.ServicesHookup@51b48197 Init done in 307 ms
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.services.impl.CmdLineParamProcessor@9c0ec97 Init done in 310 ms
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.server.SystemTasks@1fd0fafc Init done in 382 ms
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.services.impl.LogManagerService@388ee016 Init done in 411 ms
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.services.impl.HouseKeeper@a210b5b Init done in 413 ms
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 8080
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 8181
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 4848
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.services.impl.GrizzlyService@506f9b8e startup done in 630 ms
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.security.SecurityServicesUtil@585976c2 startup done in 732 ms
    Mar 7, 2008 7:43:04 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: Glassfish v3 started in 733 ms


You can see above it took 733ms to boot up!

Deploy the Grails App

Now that I have built MyFirstGrailsApp, it is time to deploy. So first I will create a war file:

    vivekmz@boson(558)> cd MyFirstGrailsApp/
    vivekmz@boson(559)> grails war
    

Now Let's deploy to GlassFish v3:
    vivekmz@boson(749)> ../glassfish/bin/asadmin deploy MyFirstGrailsApp-0.1.war
    SUCCESS : MyFirstGrailsApp-0.1 deployed successfully
    properties=(name=MyFirstGrailsApp-0.1)

The server log tells, it took about 9.7 seconds to deploy it:

[#|2008-03-07T20:19:03.580+0000|INFO|GlassFish10.0|javax.enterprise.system.tools.deployment|_ThreadID=12;_ThreadName=Thread-4;|Deployment of MyFirstGrailsApp-0.1 done is 9765 ms|#]

Now when I accessing http://localhost:8080/MyFirstGrailsApp-0.1/ my Grails app appears in the Firefox:

GlassFish v3 has been going thru continuous improvements and the development team is busy making it rock solid while adding new features to it. Continue sending your feedbacks to users@glassfish.dev.java.net.

Python@Sun

Posted by vivekp on March 03, 2008 at 11:54 AM | Permalink | Comments (0)

This is further progress in solidifying Sun's Python story for dynamic language support in the JVM. Frank has been involved with Jython for quite some time and will be working on making Python language support in JVM. Ted will be represening Python in the open source world. Welcome Frank and Ted! See more on this story here.

This is just so exciting  - JRuby, Jython, Da Vinci Machine, all of these are going to make it easier to not only have better support of dynamic languages in JVM but also in GlassFish!

I am looking forward to working with Frank and Ted for Python/Jyton support in GlassFish! 

 



Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds