KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > invocation > TransactionInterceptor


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.invocation;
10
11 import org.jboss.portal.server.invocation.Interceptor;
12 import org.jboss.portal.server.invocation.Invocation;
13 import org.jboss.portal.server.Window;
14 import org.jboss.portal.server.Instance;
15 import org.jboss.portal.server.Component;
16 import org.jboss.portal.server.WindowContext;
17 import org.jboss.portal.server.output.ErrorResult;
18 import org.jboss.portal.portlet.invocation.PortletKey;
19 import org.jboss.portal.common.transaction.Transactions;
20 import org.jboss.portal.common.transaction.TransactionException;
21 import org.jboss.portal.core.plugins.transaction.TransactionPlugin;
22
23 import javax.transaction.TransactionManager JavaDoc;
24 import javax.transaction.Transaction JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
31  * @version $Revision: 1.1 $
32  */

33 public class TransactionInterceptor implements Interceptor
34 {
35
36    /** Our transaction manager. */
37    private TransactionManager JavaDoc tm;
38
39    public Object JavaDoc invoke(final Invocation invocation)
40    {
41       WindowContext windowContext = (WindowContext)invocation.getAttachment(PortletKey.WINDOW_CONTEXT);
42
43       try
44       {
45          //
46
TransactionManager JavaDoc tm = getTransactionManager();
47          Transactions.Type txType = Transactions.TYPE_NOT_SUPPORTED;
48
49          //
50
Window window = (Window)invocation.getAttachment(PortletKey.WINDOW);
51          Instance instance = window.getInstance();
52          Component component = instance.getComponent();
53          TransactionPlugin txPlugin = (TransactionPlugin)component.getPlugin("Transaction");
54          if (txPlugin != null)
55          {
56             txType = txPlugin.getTxType();
57          }
58
59          //
60
Transaction JavaDoc oldTx = null;
61          try
62          {
63             oldTx = Transactions.applyBefore(txType, tm);
64             return invocation.invokeNext();
65          }
66          catch (TransactionException e)
67          {
68             return new ErrorResult(windowContext, e);
69          }
70          finally
71          {
72             try
73             {
74                Transactions.applyAfter(txType, tm, oldTx);
75             }
76             catch (TransactionException e)
77             {
78                e.printStackTrace();
79             }
80          }
81       }
82       catch (NamingException JavaDoc e)
83       {
84          return new ErrorResult(windowContext, e);
85       }
86    }
87
88    private TransactionManager JavaDoc getTransactionManager() throws NamingException JavaDoc
89    {
90       if (tm == null)
91       {
92          Context ctx = new InitialContext JavaDoc();
93          tm = (TransactionManager JavaDoc)ctx.lookup("java:/TransactionManager");
94       }
95       return tm;
96    }
97 }
98
Popular Tags