Thursday, October 29, 2009

Writing a Java applet in Grails


  • Install the plugin:
  • As per the reference, self-sign a certificate for yourself with these 2 command-line steps:
    • keytool -genkeypair -alias <alias> -storepass <password>
    • keytool -selfcert -alias <alias> -storepass <password>
      • where <alias> & <password> are values that you choose
      • this resulted in a .keystore file appearing in my home directory
  • Add this chunk to your conf/Config.groovy file:

    •   plugins {  
          applet {  
            //groovyJar = 'C:/groovy/groovy-1.6.0/embeddable/groovy-all-1.6.0.jar'  
            jars {  
              '/testApplet.jar' { // leading '/' is optional **didn't work for me until I added it though**  
              groovy = false; // set to false to skip including groovy-all-* in the jar  
              classes= ['ca.upei.cs.avc.applet.*.class', 'lib/classes111.jar', 'lib/LabDataViewer.jar', 'lib/ResultsLib.jar']  
              sign {  
                alias = 'alias' //your alias in keystore  
                storepass = 'password'//alternatively, use a system property with -Dstorepass=password  
              }  
              pack200 = true  
            }  
          }  
        }  
      

    • Above, the classes value contains the Java class name of my applet (to be written) and some JARs it needs at compile or runtime. Any number of applets can be listed in the jars stanza. Notice, you can reference the standard Grails lib directory so you don't need to repeat yourself i.e. copy them somewhere special for the applet(s).
  • My applet (i.e. src/java/whatever/applet/LabDataViewerApplet.java ) looks like this:

    •  package whatever.applet;  
         
          import javax.swing.JApplet;  
          import java.sql.*;  
         
          import oracle.jdbc.driver.OracleDriver;  
          import upei.avc.ds.LabDataViewer;  
         
          public class LabDataViewerApplet extends JApplet {  
         
           int year = 1899;  
           int labNumber = -1;  
           private Connection con;  
         
           public LabDataViewerApplet() {}  
         
           public void init() {  
            if (null != this.getParameter("labNumber")) {  
             labNumber = Integer.parseInt(this.getParameter("labNumber"));  
            }  
            if (null != this.getParameter("year")) {  
             year = Integer.parseInt(this.getParameter("year"));  
            }  
            try {  
             jbInit();  
            } catch(Exception e) {  
             e.printStackTrace();  
            }  
           }  
         
           private void jbInit() throws Exception {  
            this.getContentPane().setLayout(null);  
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
            con = DriverManager.getConnection(  
                                "jdbc:oracle:thin:@host:port:database",  
                                "id","password");  
            LabDataViewer viewer = new LabDataViewer(labNumber, year, con);  
            this.getContentPane().add(viewer, null);  
           }  
         
          }  
         
      

    • This applet will get a JDBC Connection to an Oracle instance and instantiate & attach Robert Page's LabDataViewer (Java Swing) app to it's contentPane; the (applet) containing page will pass in parameters to the viewer to customize the data/view.

    • You compile & package the applet by running this command:
      • grails package-applet
      • This should result in the file testApplet.jar being placed in your web directory e.g.

        • C:\projx\grails_apps\testApplet>grails package-applet
          Welcome to Grails 1.1.1 - http://grails.org/
          Licensed under Apache Standard License 2.0
          Grails home is set to: C:\tools\grails\grails-1.1.1
          
          Base Directory: C:\projx\grails_apps\testApplet
          Running script C:\Documents and Settings\default\.grails\1.1.1\projects\testApplet\plugins\applet-0.1\scripts\
          PackageApplet.groovy
          Environment set to development
           [groovyc] Compiling 1 source file to C:\Documents and Settings\default\.grails\1.1.1\projects\testApplet\classes
              [echo] Building web-app//testApplet.jar with the following Autojar arguments: [-o, web-app//testApplet.jar, -c, C:\Documents and Settings\default\.grails\1.1.1\projects\testApplet\classes, ca.upei.cs.avc.applet.*.class, lib/classes111.jar, lib/LabDataViewer.jar, lib/ResultsLib.jar]
          2009-10-20 16:00:33,299 [main] WARN  root  - Missing files:
             com/keyoti/rapidSpell/desktop/RapidSpellAsYouType.class
             org/jdesktop/layout/GroupLayout$Group.class
             org/jdesktop/layout/GroupLayout$ParallelGroup.class
             org/jdesktop/layout/GroupLayout$SequentialGroup.class
             org/jdesktop/layout/GroupLayout.class
             upei/avc/oracle/OraOCIConn.class
              [echo] pack200 is enabled, packing jar...
              [echo] Signing web-app//testApplet.jar as alias: alias
           [signjar] Signing JAR: C:\projx\grails_apps\testApplet\web-app\testApplet.jar to C:\projx\grails_apps\testAp
          plet\web-app\testApplet.jar as alias
           [signjar] Warning:
           [signjar] The signer certificate will expire within six months.
           [signjar] Enter Passphrase for keystore:
          C:\projx\grails_apps\testApplet>

        • Don't be concerned with the reported missing files above; in this case they were never used by the Swing application
        • AutoJar (used by the plugin) uses reflection to find the class & jar files required by the applet
  • The applet-containing webpage looks like this; any number of parameters can be passed into the applet (they're passed as Strings):

    • <html>
      <head>
       <script src="http://java.com/js/deployJava.js"></script>
      </head>
      <body>
      <script>
       var attributes = {
         name:'myApplet',
         codebase:'.',
         code:'whatever.applet.LabDataViewerApplet.class',
         archive:'testApplet.jar',
         width:500,
         height:500
       } ;
       var parameters = {fontSize:16, labNumber: 6, year: 2006};
       var version = '1.6' ;
       deployJava.runApplet(attributes, parameters, version);
      </script>
      </body>
      </html>

    • The resulting page appears blank (except for a 500x500 grey area I reserved for the applet) and the LabDataViewer launches in it's own window, closing the webpage kills the applet/application.
    • Brought to you thanks to AutoJar, deployJava.js and the Grails' Applet plugin!

No comments: