Friday, June 25, 2010

Scala and Code Kata 5

After completing the first couple of Code Kata exercises in Ruby I decided I would do the rest in my other favorite language Scala. I don't know Scala as well as I know Ruby, but I am really starting to like it. The syntax is funky and can be a bit intimidating at first glance, but once you get comfortable with it you can write some really slick code.

Kata #5 in Scala is here.

Wednesday, June 9, 2010

Code Kata 2

I've been tuning up my ruby skills since I have been working on Java projects lately. I decided to go through some exercises like Dave Thomas's Code Kata and do all the solutions in Ruby. Here is my solution for Code Kata 2: Karate Chop. Will post more as I get to them.

Grizzly Spring and JTA Transactions

If you want to have declarative transactions using the @Transactional annotation in Spring and you are running an embedded Grizzly instance (possibly with Jersey) you are going to have to jump through some hoops. Since I've already had to jump through those hoops allow me to save you the trouble.

Most of this info was gleaned/compiled from Ian Pojman's blog posts: here and here.

First you need to get Grizzly and Spring working happily:


ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("application-context.xml");
ResourceConfig rc = new DefaultResourceConfig();

IoCComponentProviderFactory factory = new SpringComponentProviderFactory(rc,
context);

final Adapter adapter =
ContainerFactory.createContainer(Adapter.class, rc, factory);

log.info("Starting grizzly...");
SelectorThread threadSelector = GrizzlyServerFactory.create(BASE_URI,
new TransactionalAdapterDecorator(adapter));
//server is now running


And the code for the TransactionalAdapterDecorator:


private static class TransactionalAdapterDecorator implements Adapter {

public static final String JTA_TX = "JTA.TX";

private final Adapter adapter;

private PlatformTransactionManager txManager;

public TransactionalAdapterDecorator(Adapter adapter) {
this.adapter = adapter;
txManager = (PlatformTransactionManager) context.getBean(
"transactionManager");
}

/** allow for pre-request processing */
protected void preRequest(Request req) {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
}

public void service(Request req, Response res)
throws Exception {
preRequest(req);
try {
adapter.service(req, res);
} catch (Exception ex) {
log.error("Exception: ", ex);
TransactionStatus status = getCurrentTx(req);
txManager.rollback(status);
}
}

public void afterService(Request req, Response res)
throws Exception {
adapter.afterService(req, res);
TransactionStatus status = getCurrentTx(req);
txManager.commit(status);
}

private TransactionStatus getCurrentTx(Request req) {
TransactionStatus status = (TransactionStatus) req.getAttribute(JTA_TX);
return status;
}

private void setCurrentTx(TransactionStatus status, Request req) {
req.setAttribute(JTA_TX, status);
}
}
}


From here you're on your own.