The Source for Java Technology Collaboration
User: Password:



Vivek Pandey's Blog

Web Applications Archives


Scala Lift Web Framework on GlassFish v3

Posted by vivekp on May 16, 2008 at 04:35 PM | Permalink | Comments (0)

During JavaOne I heard about Lift - a Scala web framework and wanted to try it. It claims to provide best of Rails(simple and fast development), Seaside(security), Django(access control by default) and uses Wicket for view templates. In few steps I had my first Lift web application running on GlassFish v3.

Lift applicatin development process is Maven based and uses Derby as the default database. These are the steps to build a simple Lift application and deploy it on Embedded GlassFish v3 during development or deploy it on GlassFish v3 server.

Create Application

Lift uses Maven Archetype plugin to build a template project.

    
mvn org.apache.maven.plugins:maven-archetype-plugin:1.0-alpha-7:create  \
-DarchetypeGroupId=net.liftweb                             \
-DarchetypeArtifactId=lift-archetype-basic                 \
-DarchetypeVersion=0.8                            \
-DremoteRepositories=http://scala-tools.org/repo-releases  \
-DgroupId=my.liftapp -DartifactId=liftapp
    
This created liftapp directory.
  • src/main/scala directory contains the Scala source code.
  • src/main/webapp directory contains the HTML, JavaScript, CSS, and WEB-INF code.
  • lift_example directory contains the derby database.
Now let's build it
    
cd liftapp
mvn install
    

This would create a WAR directory structure at target/liftapp-1.0-SNAPSHOT/ and also the WAR file target/liftapp-1.0-SNAPSHOT.war

Deploy on GlassFish v3

I could simply deploy the application on GlassFish v3 server by doing:

    
        asadmin deploy target/liftapp-1.0-SNAPSHOT
    

Click http://localhost:8080/liftapp-1.0-SNAPSHOT/ and you should see the Welcome screen. Also note that the access controls login, add user features are automatically generated similar to Django admin.

lift-gf.png

Code-Save-Refresh with GlassFish v3

The previous deployment on to GlassFish v3 works fine but I needed something that could help me do Code-Save-Refresh cycle without restarting the v3 server. Basically I wanted to use Embedded GlassFish v3 where I would simply invoke mvn glassfish:run and make changes and compile in another window and at the end refresh the browser to see the changes.

Since pom.xml generated by Lift Archetype does not have entry for maven-glassfish-plugin so I had to add this plugin entry:

    

        <pluginRepositories>
            <pluginRepository>
                <id>maven2.java.net</id>
                <name>Java.net Repository for Maven 2</name>
                <url>http://download.java.net/maven/2</url>
            </pluginRepository>
        </pluginRepositories>

        <plugins>
            <plugin>
                <groupId>org.glassfish</groupId>
                <artifactId>maven-glassfish-plugin</artifactId>
                <version>1.0-alpha-4</version>
                <configuration>
                    <resourcesDirectory>target/liftapp-${project.version}</resourcesDirectory>
                </configuration>
            </plugin>
        </plugins>
    

I also had to make one more change in src/main/webapp/WEB-INF/web.xml to workaround due to the fact that http://java.sun.com/j2ee/dtds/web-app_2_3.dtd has been renamed to http://java.sun.com/dtd/web-app_2_3.dtd and the aliasing is not happening properly with Embedded GlassFish v3

So the change was to edit src/main/webapp/WEB-INF/web.xml and replace http://java.sun.com/j2ee/dtds/web-app_2_3.dtd by http://java.sun.com/dtd/web-app_2_3.dtd. This step would not be required once it is fixed in embedded GlassFish v3.

That's it! Next I did was invoke mvn glassfish:run


    vivekmz@boson(620)> mvn glassfish:run
    [INFO] Scanning for projects...
    [INFO] Searching repository for plugin with prefix: 'glassfish'.
    [INFO] ------------------------------------------------------------------------
    [INFO] Building liftapp
    [INFO]    task-segment: [glassfish:run]
    [INFO] ------------------------------------------------------------------------
    [INFO] Preparing glassfish:run
    [INFO] [resources:resources]
    [INFO] Using default encoding to copy filtered resources.
    [INFO] [yuicompressor:compress {execution: default}]
    [INFO] nb warnings: 0, nb errors: 0
    Downloading: http://scala-tools.org/repo-releases/org/igniterealtime/smack/smack/3.0.4/smack-3.0.4.pom
    Downloading: http://repo1.maven.org/maven2/org/igniterealtime/smack/smack/3.0.4/smack-3.0.4.pom
    Downloading: http://scala-tools.org/repo-releases/org/igniterealtime/smack/smackx/3.0.4/smackx-3.0.4.pom
    Downloading: http://repo1.maven.org/maven2/org/igniterealtime/smack/smackx/3.0.4/smackx-3.0.4.pom
    [INFO] [compiler:compile]
    [INFO] Nothing to compile - all classes are up to date
    [INFO] [scala:compile {execution: default}]
    [INFO] Nothing to compile - all classes are up to date
    [INFO] [glassfish:run]
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: HK2 initialized in 236 ms
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.naming.impl.ServicesHookup@1e708b2 Init done in 241 ms
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.server.Globals@1fbc355 Init done in 242 ms
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.server.SystemTasks@1f2edd2 Init done in 246 ms
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.services.impl.HouseKeeper@f221f6 Init done in 247 ms
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.services.impl.CmdLineParamProcessor@189a773 Init done in 249 ms
    JMXMP connector server URL = service:jmx:jmxmp://localhost:8888
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 8080
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: com.sun.enterprise.v3.services.impl.GrizzlyService@154ea79 startup done in 398 ms
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.services.impl.ApplicationLoaderService postConstruct
    INFO: loader service postConstruct started at 1210978351811
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: Application Loader startup done in 431 ms
    May 16, 2008 2:52:31 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: Glassfish v3 started in 432 ms
    May 16, 2008 2:52:37 PM com.sun.enterprise.web.WebModuleContextConfig authenticatorConfig
    SEVERE: webModuleContextConfig.missingRealm
    Hit ENTER for redeploy

Click http://localhost:8080/liftapp/ would display

lift-gf-1.png

Then I tried to make some changes in the Scala code compile it and then refresh the browser to see if the changes appear there. So in another window, edit src/main/scala/my/liftapp/snippet/HelloWorld.scala

Replace
    
        def howdy = Welcome to liftapp at {new java.util.Date}
    
by
    
        def howdy = Welcome to liftapp deployed on GlassFish v3 at {new java.util.Date}
    

Save this file and run mvn install and hit Entner key in the windows where I ran embedded GlassFish v3.

That's

Now, I go back to the browser and refresh the window and it displays the changed text, but first see the GlassFish log where you hit Enter and see the log printed when refresh button was pressed in the browser

    
        Hit ENTER for redeploy
        May 16, 2008 2:57:30 PM com.sun.enterprise.web.WebModuleContextConfig authenticatorConfig
        SEVERE: webModuleContextConfig.missingRealm
        INFO - Service request (GET) /liftapp/ took 412 Milliseconds
        INFO - Service request (GET) /liftapp/classpath/blueprint/screen.css took 13 Milliseconds
        INFO - Service request (GET) /liftapp/classpath/blueprint/print.css took 2 Milliseconds
        INFO - Service request (GET) /liftapp/classpath/blueprint/plugins/fancy-type/fancy-type.css took 2 Milliseconds
        INFO - Service request (GET) /liftapp/classpath/jquery.js took 4 Milliseconds
        INFO - Service request (GET) /liftapp/classpath/json.js took 1 Milliseconds
    

lift-gf2.png

To summarize, a Scala Lift application can be deployed on GlassFish v3 server as well as can be run in development mode with hot deployment by running embedded GlassFish mvn glassfish:run. Go ahead and try it out and let us know at the the forum or alias



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.



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