Thursday, September 9, 2010

Evaluating expressions with JEXL

JEXL is a useful library for implementing scripting features in Java programs - specifically it is useful if you require scripts to be evaluated like boolean expressions.

The first thing is to set up a JEXL engine,

import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;

.....

JexlEngine jexl = new JexlEngine();

Then you initialize an jexl context

JexlContext context = new MapContext();

After that you add variables to your jexl context which can be used within your expressions.

e.g.

Integer x = new Integer(11);
context.set("x", x);

Now your jexl expressions can refer to x - e.g.
String expr = "x > 12"
Expression e = jexl.createExpression( expr );

Finally you can evaluate your expression, casting the result to the approproriate type:

Boolean result = (Boolean)e.evaluate(context);

You can see how this would be useful for any sort of user inputted expressions, etc.

Wednesday, July 14, 2010

Setting up Glassfish on Ubuntu Linux

from this site:

java -version // Check java version. It must be either Java 1.5 or Java 1.6. If not install Java by typing sudo apt-get install sun-java6-jdk. It asks your password, after you enter it, it will install Java 6.
wget http://java.net/download/javaee5/v2ur2/promoted/Linux/glassfish-installer-v2ur2-b04-linux.jar // get the link of latest glassfish server jar for linux from https://glassfish.dev.java.net/public/downloadsindex.html
java -Xmx256m -jar glassfish-installer-v2ur2-b04-linux.jar // Click Accept in the license window that opens
cd glassfish
sudo chmod -R +x lib/ant/bin // Enter password
lib/ant/bin/ant -f setup.xml //Run ant script
cd bin //Go to the directory (usually bin) where asadmin command is available. You can find this by simple ls command or through GUI file xplorer
./asadmin //Run asadmin
asadmin> start-database
asadmin> start-domain domain1
Type http://localhost:4848/ in browser and login as username: admin and password: adminadmin
asadmin> exit // To shutdown the server, type this in the terminal window