Archive for the ‘Tech’ Category

Drools Plugin for Grails Application

Monday, August 4th, 2008

I have just released a Grails Plugin for Drools. The plugin details can also be found at http://www.grails.org/Drools+Plugin.

Drools is a business rule management system (BRMS) and an enhanced Rules Engine implementation, ReteOO, based on Charles Forgy’s Rete algorithm tailored for the Java language.

Features

  • Easy implementation of Drools
  • Rules can be stored in file or in database
  • Rules can be dynamically changed without rebuilding or restarting application

Installation

grails install-plugin drools

Usage

  • Install the plugin
  • Verify example rules:
    http://localhost:8080/your_app/drools/example
  • Create your own rules: http://localhost:8080/your_app/drools and use droolsService in your code

Components

  • DroolsController: Provides CRUD functionality for Drools Rule
  • DroolsService: Fires all rules

DroolsService
droolsService.fireRules(resource, ruleKey, objList)

  • resource: specifies where the rules are stored. ‘file’ or ‘db’
  • ruleKey: identifier for the rule. For ‘file’ resource, this would be the name of the file (eg:ticket_example.drl). For ‘db’ resource, this is the Drools Rule key
  • objList: list of objects that need to be put in session

Example

A simple Ticket processing system is available in the plugin. This example shows how tickets are processed based on their subscription plan.

Run the example: http://localhost:8080/your_app/drools/example

Your console output should be:

Start processing Ticket # 3
Start processing Ticket # 2
Start processing Ticket # 1
Firing rule Bronze Priority for Ticket # 3
Firing rule Silver Priority for Ticket # 2
Firing rule Gold Priority for Ticket # 1
Firing rule Special Discount for Ticket # 1

Display on your browser should be:

Tickets due for processing:
Ticket #1: Customer[Name:Jack, Subscription:Gold, Discount:0%] Status[New]
Ticket #2: Customer[Name:Tom, Subscription:Silver, Discount:0%] Status[New]
Ticket #3: Customer[Name:Bill, Subscription:Bronze, Discount:0%] Status[New]
Firing rules now ...
Tickets after processing:
Ticket #1: Customer[Name:Jack, Subscription:Gold, Discount:5%] Status[Escalate]
Ticket #2: Customer[Name:Tom, Subscription:Silver, Discount:0%] Status[Escalate]
Ticket #3: Customer[Name:Bill, Subscription:Bronze, Discount:0%] Status[Pending]

Grails 1.0.3 upgrade issues

Tuesday, July 29th, 2008

Recently, we upgraded our application running in Grails 1.0.1 to 1.0.3. Just to give you an idea, the application is pretty complicated involving multiple data sources, complex hibernate mappings with JPA annotations, legacy system, linking tons of external services etc. We had to overcome few significant hurdles to get our application to run in Grails 1.0.3.

1. Update hibernate-entitymanager.jar (3.3.2 GA) in <project>/lib

2. Add hibernate-validator.jar (3.0.0 GA) in <project>/lib. Grails 1.0.1 did not require this jar

3. Changes to g:render taglib.
<g:render template=”templates/my_template.gsp” /> used to work in Grails 1.0.1 template file in _templates/my_template.gsp under grails-app/views/mycontroller/. Now, for Grails 1.0.3 you need to have the template in template/_my_template.gsp.

4. Issues with domain properties.
my_domain.properties = another_domain.properties. This doesn’t work anymore in Grails 1.0.3, at least in some cases. Had to manually copy properties.

5. There are definitely issues with complex hibernate mapping involving legacy system. A particular hibernate relationship was throwing javax.persistence.PersistenceException with Grails 1.0.3. We couldn’t figure out the actual reason, had to make the relationship transient and manually manage it.

6. Changes to taglib g:actionSubmitImage.
The following works fine in Grails 1.0.1.
<g:form name="myForm" action="myAction">
<g:actionSubmitImage value="Show" src=".."
/>
</g:form>

In Grails 1.0.3, you will need to change to <g:actionSubmitImage value="Show" src=".." action=”myAction” />

7. Issues with blank spaces in directories in Windows:
This is the nasty one and can drive you crazy. You may have problems running the app or executing tests. Errors such as java.util.zip.ZipException: The system cannot find the path specified or error trying to scan <jar-file> etc could be noticed. These errors are due to blank spaces in JAVA_HOME(c:\Program Files\..) and user.home (c:\Documents and Settings\..). To fix this:

  • Re-install Java in directory without blank spaces (c:\Java\..)
  • By default, the project is built in ${user.home}/.grails. We can change this location by specifying the user.home property in command-line such as grails -Duser.home=c:\ run-app or grails -Duser.home=c:\ test-app

Grails 1.0.3 seems much faster than Grails 1.0.1 and is probably worth the trouble.

Hosting Services for Grails Application

Thursday, June 12th, 2008

I was looking for a hosting service for running my Grails and other Spring enabled Java applications. I was looking at both shared and virtual private servers. My Budget was $20.

GoDaddy: I was already using GoDaddy hosting services with Java support for about $7/month. While they claim to support Java applications, in practice it can run only simple ‘Hello World’ applications. They do not allow Spring/Hibernate enabled applications. So, this is not an option.

EATJ: They have a free service that allows you to upload war, restart your Tomcat. (But the server is automatically shutdown every 6 hours). This run Grails application. The Basic service with no server shutdown is $10/month. They offer only 64MB. What can you run with only 64MB? The next level of service offers 128MB and costs $20/month. You can upload your WAR, restart server. Nothing more.

RimuHosting: Offers VPS and runs Grails applications. Costs $20/month for 96MB. Lot of features, good support. I have also read good feedback on their service.

EAPPS: Offers VPS and supports Grails applications. Costs $20/month for 288MB. Tons of features - Tomcat, MySQL, Ruby on Rails, Subversion, Wordpress, ssh/sftp, Apache, mod_jk, mod_proxy, root access etc. I dumped GoDaddy and have been using eapps for last few months. Great customer service.

My recommendation is eapps.

License management plugin for Grails application

Wednesday, March 26th, 2008

I have been recently working on a plugin that provides ability to securely create, install and verify license for closed source grails applications. This plugin utilizes TrueLicense Library Collection (TLC). You can download the plugin here.

Installation

grails install-plugin <path-to-grails-license-0.1.zip>

Usage

  • Generate license file: grails generate-license
  • Start app: grails run-app
  • Instal/verify license at http://localhost:8080/your_app/license. If you try to access your app without installing license, you will be redirected to this page.

Components

  • /plugins/license-0.1/etc/privateKey.store - private key file. This never gets included in your WAR. (You should generate your own private/public keys with keytool command. see tips section below)
  • /plugins/license-0.1/etc/LicensePrivateConfig.properties - holds all necessary configuration to create a license such as issuer, holder, validity etc. Again not included in WAR
  • /plugins/license-0.1/conf/publicCerts.store - public key file. This is included in WAR
  • /plugins/license-0.1/conf/LicensePublicConfig.groovy - holds all necessary information required for the client. Included in WAR
  • /plugins/license-0.1/controller/LicenseController - Install, verify license
  • /plugins/license-0.1/services/LicenseService - Install, verify license
  • /plugins/license-0.1/conf/LicenseFilter - defines which controllers, actions need to be protected.

Tips

  • To generate private key:
    keytool -genkey -alias privatekey -keystore privateKeys.store
  • To generate public key
    keytool -export -alias privatekey -file certfile.cer -keystore privateKeys.store
    keytool -import -alias publiccert -file certfile.cer -keystore publicCerts.store
  • The system preferences are stored in the registry in windows and file system (/etc/.java/) in Linux. You may need appropriate permissions to be able to do this.

Triple boot - Mac OS X, Windows XP & Ubuntu Linux on Intel PC

Monday, November 5th, 2007

Okay! You decided to have three OS on your machine. After experimenting with various procedures, this worked well for me. Here are the steps:

Note: This procedure involves re-partioning your system, hence you would lose your data. Please BACKUP your data before you try any of these.
I. Instal Mac OS X 10.4

  • Boot your machine with Mac CD. From the Disk Utilities menu, make 3 partitions. I created partitions with sizes 16GB for Mac, 8GB for Windows and 13GB for Ubuntu.
  • Format Mac partition with Mac OS X journaled, Windows partition with NTFS and Linux partition with Linux types.
  • Instal Mac OS X on Mac partition.

II. Instal Windows

  • Insert windows CD, make sure you select the windows partition.
  • Just follow the instructions and complete the installation.

III. Instal Ubuntu Linux

  • Restart the machine with Ubuntu Feisty CD.
  • Select the linux partition. You may also need to create a 1GB partition for swap
  • Follow instructions and complete installation.

Now restart the system. GRUB starts and you will have options to choose Ubuntu and Windows. But where is Mac option? Don’t panic! Log into Ubuntu. Add the following entry to /boot/grub/menu.lst

title Mac OS X (Tiger)
root (hd0,0)
makeactive
chainloader +1

Restart now. Hopefully you now have options to choose all three OS.

Setting Wireless Network in Ubuntu Feisty 7.04

Thursday, September 6th, 2007

Recently I installed Ubuntu Feisty 7.04 on my Dell Latitude D600 laptop, but still had to do some work to get wireless working.
For those interested, here are the steps involved:

  • Determine your Wireless card.
  • lspci | grep -i network
    02:03.0 Network controller: Broadcom Corporation BCM4306 802.11b/g Wireless LAN Controller (rev 03)

  • Disable BCM43XX Driver
  • sudo -s
    echo blacklist bcm43xx >> /etc/modprobe.d/blacklist
    exit

  • Instal ndiswrapper. This enables wireless network cards on Linux by implementing the Windows kernel and NDIS APIs and dynamically linking the vendor’s Windows drivers
  • sudo apt-get install ndiswrapper-utils

  • Get your windows driver
  • wget http://ftp.us.dell.com/network/R151517.EXE

  • Extract driver to MYDRIVER directory
  • unzip -a R151517.EXE

  • Install the driver
  • cd MYDRIVER
    sudo ndiswrapper -i bcmwl5.inf

  • Verify driver, hardware
  • sudo ndiswrapper -l

  • configure ndsiwrapper
  • sudo ndiswrapper -m
    sudo modprobe ndiswrapper
    sudo -s
    echo ndiswrapper >> /etc/modules
    exit

  • Reboot now!
  • Test wireless
    sudo iwlist scanning

If this is your lucky day, you should have your wireless up and ready.

Licence Management for Java Web Applications Using Aspects

Wednesday, December 13th, 2006

This post provides step by setp instruction on implementing license management for Java web applications. I have used open source product TrueLicense for license management and aspects to weave the license verification into the code.

TrueLicense Library Collection(TLC) is a collection of Java packages to securely create, install and verify license for closed source products. TLC is rich in features - License can be perpetual or temparary, bound to users, systems or other entity, free trial period implementation, privacy of license content using password based encryption provided by Java Cryptography Extension (JCE), authenticity of license using digital signature mechanism provided by Java Security API etc.

How to implement License management

What you (Developer) need to do:

  1. You already have your web application (say HelloWorld) and you would like to implement License Management. If this is your case, read on.
  2. You may want to add a new page for licene management. The client should be able to see the validity of the current license, and instal new license from this page.
  3. Write an ascpect to verify license. The pointcut will specify the critical areas of the application. The right approach is to have the licence verification done in every critical functionality of the application. License verification is a ‘cross-cutting’ concern, and hence we can effectively use aspects to weave license verification into our code.
  4. Generate private and public key using keytool command.
  5. Generate license file. A license file contains encrypted, digitally signed information.
  6. Use AspectJ compiler to compile your application.
  7. Use a good Code Obfuscator, such as Proguard to guard against Decompilation tools. This is a very important step in real life situtation. If this step is skipped, hackers could exchange your public key and KeyStoreParam implementation with their their own code and bypass the license verification. Since this is a demo app and I’m lazy, this step is skipped. :)
  8. Build your application along with the public key.
  9. Distribute application and license file to your client. (NEVER give your private key)

What your client needs to do:

  1. Pays $$$ and buys your HelloWorld application
  2. Fires up the app, navigates to the licenese management page, instals the license file.
  3. Uses the app. If the license is expired or not installed, he will not be able to use the application.

Demo Application
The demo application can be used with Tomcat. Download LicenseDemo. Please refer README before using the demo app.

How to use this Demo

  1. Unzip HelloWorld-Licensed.zip
  2. Run ‘ant clean generateLicense’ from /buildtools folder. This creates a file called sample.lic
  3. Run ‘ant clean dist’. This creates HelloWorld-Licensed.war in dist folder.
  4. Drop the HelloWorld-Licensed.war file in your webapps folder of Tomcat installation. You are all done!
  5. Access the application by http://localhost:8080/HelloWorld-Licensed

First, access the Greetings page without installing license. The error message should be dislayed.
Next, instal sample.lic from the License management page and then access Greetings page. Later, allow the license to expire and access the Greetings page again.

Running multiple JBoss instances on the same machine

Wednesday, August 30th, 2006

Did you ever had the requirement to run multiple JBoss instances on the same machine? Its a trivial task if you exactly know the parameters to be tweaked.

Carryout the following steps with your second instance (This is applicable for JBoss 4.0.3):

  • default/deploy/jbossweb-tomcat55.sar/server.xml
    • change 8080 to 18080
  • default/conf/jboss-service.xml
    • change 1099 to 11099
    • change 1098 to 11098
    • change 4445 to 14445
    • change 4444 to 14444
  • default/conf/jboss-minimal.xml
    • change 1099 to 11099
    • change 1098 to 11098
  • default/deploy/jms/uil2-service.xml
    • change 8093 to 18093

Voila! You’re done!!

Update (04/24/2008):

Many of you requested steps for current release of JBoss 4.2.2 GA, here it goes:

  • deploy/jboss-web.deployer/server.xml
    • change 8080 to 18080
    • change 8443 to 18443
    • change 8009 to 18009
  • deploy/http-invoker.sar/META-INF/jboss-service.xml
    • change 8080 to 18080
  • deploy/jbossws.sar/jbossws.beans/META-INF/jboss-beans.xml
    • change 8080 to 18080
    • change 8443 to 18443
  • deploy/ejb3.deployer/META-INF/jboss-service.xml
    • change 3873 to 13873
  • deploy/jms/uil2-service.xml
    • change 8093 to 18093
  • conf/jboss-service.xml
    • change 8083 to 18083
  • conf/jboss-minimal.xml
    • change 1099 to 11099
    • change 1098 to 11098
  • conf/jboss-service.xml
    • change 1099 to 11099
    • change 1098 to 11098
    • change 4444 to 14444
    • change 4445 to 14445
    • change 4446 to 14446

There can be many entries of these port numbers in these files. Make sure you change all of them.

Developing RESTful Web Services in Java

Tuesday, July 18th, 2006

When you think of Web services, SOAP immediately comes to your mind. Not any more! Thanks to REST, there is a simpler way to develop web services. While SOAP is well established with most vendors supporting it, REST is really catching up.

Representational State Transfer (REST) is an architectural style for distributed hypermedia systems. REST relies on a single application protocol (HTTP), universal resource indicators (URI) and standardized data formats, through XML. It employs established HTTP methods such as GET and POST to direct applications.

I’m not attempting to explain the concept of REST, there are already enough stuff all over the Internet. You may have noticed that most of the Internet giants offer their services as REST web services. They may be good APIs but not RESTful.

What is not REST?

  • APIs that involve Plain Old XML over HTTP are not necessarily REST.
  • Using GET for all operations - including insert, updates etc is not REST

Creating a new Person resource:

GET myhost/api?action=create&item=person&name=mano Incorrect
POST myhost/api/person
name=mano
Correct
  • Not having distinct URI for first class objects is also a violation of the REST way.

Retrieving a Person resource

GET myhost/api?item=person&id=202 Incorrect
GET myhost/api/person/202 Correct

A RESTful service has use http methods such as POST, GET, PUT and DELETE to achieve the basic CRUD operations (create, read, update, and delete). All resources are identified using distinct URIs.

Framework in Java

I found Restlet to be a good framework. Restlet ensures that your application can easily be developed in a RESTful manner. Each concept of REST has a corresponding Java interface or class in the Restlet API. Another advantage is the ability to work with multiple protocols (HTTP, JDBC, SMTP, etc.) using the exact same API.

However, I could not find a good example for restlet + spring + tomcat integration, I took the liberty of writing one. You can download the restdemo application here. Just run ant, drop the WAR in your webapps folder - you are all set.

Download RestDemo ZIP

Better jUnit-ing

Sunday, April 2nd, 2006

jUnit has been around for a long time. But there are always better techniques to implement old ways. I have provided below an efficient junit aprroach integrated with DbUnit and Spring with support for transaction and hibernate open session. Significant features are:

  • Integration with DbUnit: DbUnit puts database into a known state between test runs. Pre-loads database with known data set before the test
  • Transaction support using spring: After each test all changes made by the test are rolled back. Hence there is no need to programatically undo channges made by the test. Much cleaner code. (Thanks, Jim)
  • After each test, clear the pre-loaded data set from database
  • Support for open session in view: Web applications using Hibernate with lazy loading need to keep the session open till the view is rendered. (Read more)

/**
* Junit test integrated with DbUnit and spring. Provides support for
* transaction and open session. This wraps each test method with a
* transaction and rollbacks after the method exits. DbUnit
* is used to load up the table with initial set of values.
*/

public class MyTest extends AbstractTransactionalDataSourceSpringContextTests {

private SessionFactory sessionFactory;
private Session session;
private static ConfigurableApplicationContext factory;
private final String[] TABLES = { “EmployeeTable”, “DepartmentTable” };
private final String DATA_FILE = “data.xml”;
private final String[] CONFIG_LOCATIONS = { “spring-context.xml” };

public static Test suite() {
return new TestSuite(MyTest.class);
}

protected String[] getConfigLocations() {
return CONFIG_LOCATIONS;
}

// This method is used to perform any setup operations, such as
// populating a database table, within the transaction.
protected void onSetUpInTransaction() throws Exception {
super.onSetUpInTransaction();
if (factory == null) {
factory = new ClassPathXmlApplicationContext(getConfigLocations());
}
// Load test data using DBUnit
DataSource ds = (DataSource) factory.getBean(”dataSource”);
Connection con = DataSourceUtils.getConnection(ds);
IDatabaseConnection dbUnitCon = new DatabaseConnection(con);
IDataSet dataSet = new FlatXmlDataSet(Thread.currentThread().
getContextClassLoader().getResourceAsStream(DATA_FILE));
try {
DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon, dataSet);
} finally {
DataSourceUtils.releaseConnection(con, ds);
}
// Support of open session
sessionFactory = (SessionFactory) factory.getBean(”sessionFactory”);
session = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}

protected void onTearDownAfterTransaction() throws Exception {
// delete table entries
deleteFromTables(TABLES);
// release session
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
session = holder.getSession();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.releaseSession(session, sessionFactory);
}

public void testSomething() throws Exception {
// …
}

}