KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > J2EEWebApplicationHandler


1 // ========================================================================
2
// $Id: J2EEWebApplicationHandler.java,v 1.6 2004/10/03 01:19:51 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 // A Jetty HttpServer with the interface expected by JBoss'
17
// J2EEDeployer...
18

19 /* JFox, the OpenSource J2EE Application Server
20  *
21  * Distributable under GNU LGPL license by gun.org
22  * more details please visit http://www.huihoo.org/jfox
23  */

24 package org.mortbay.j2ee;
25
26 import java.io.IOException JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.transaction.Status JavaDoc;
29 import javax.transaction.Transaction JavaDoc;
30 import javax.transaction.TransactionManager JavaDoc;
31
32 import org.jfox.ioc.logger.Logger;
33 import org.mortbay.http.HttpRequest;
34 import org.mortbay.http.HttpResponse;
35 import org.mortbay.jetty.servlet.WebApplicationHandler;
36
37 public abstract class J2EEWebApplicationHandler extends WebApplicationHandler
38 {
39   protected static final Logger _log=Logger.getLogger(J2EEWebApplicationHandler.class);
40
41   protected javax.naming.Context JavaDoc _ctx;
42   protected TransactionManager JavaDoc _tm;
43
44   protected void doStart()
45     throws Exception JavaDoc
46     {
47     super.doStart();
48     _ctx=new InitialContext JavaDoc();
49     _tm=(TransactionManager JavaDoc)_ctx.lookup("java:/TransactionManager");
50     }
51
52   protected void doStop()
53       throws Exception JavaDoc
54     {
55     super.doStop();
56     _ctx=null;
57     _tm=null;
58     }
59
60   public void handle(String JavaDoc pathInContext,
61                      String JavaDoc pathParams,
62                      HttpRequest httpRequest,
63                      HttpResponse httpResponse)
64     throws IOException JavaDoc
65     {
66       // _log.info("HANDLE()...");
67
try
68       {
69     super.handle(pathInContext, pathParams, httpRequest, httpResponse);
70       }
71       finally
72       {
73     disassociateTransaction();
74     disassociateSecurity();
75       }
76       // _log.info("...HANDLE()");
77
}
78
79   protected void disassociateTransaction()
80     {
81       int status=Status.STATUS_NO_TRANSACTION;
82       try
83       {
84     status=_tm.getStatus();
85       }
86       catch (Exception JavaDoc e)
87       {
88     _log.error("could not get status of current Transaction", e);
89       }
90
91       // these are the possible statuses:
92
// STATUS_ACTIVE
93
// STATUS_COMMITTED
94
// STATUS_COMMITTING
95
// STATUS_MARKED_ROLLBACK
96
// STATUS_NO_TRANSACTION
97
// STATUS_PREPARED
98
// STATUS_PREPARING
99
// STATUS_ROLLEDBACK
100
// STATUS_ROLLING_BACK
101
// STATUS_UNKNOWN
102

103       if (!(status==Status.STATUS_COMMITTED ||
104             status==Status.STATUS_NO_TRANSACTION ||
105             status==Status.STATUS_ROLLEDBACK))
106       {
107     // we should rollback this transactions and disassociate it
108
// from the current thread...
109
try
110     {
111       _log.warn("UserTransactions MUST be completed by end of service() method");
112       _log.warn("Rolling back incomplete transaction on current thread");
113       _tm.rollback();
114     }
115     catch (Exception JavaDoc e)
116     {
117       _log.error("could not rollback incomplete transaction", e);
118     }
119       }
120
121       Transaction JavaDoc garbage=null;
122       try
123       {
124     garbage=_tm.suspend();
125       }
126       catch (Exception JavaDoc e)
127       {
128     _log.error("could not disassociate transaction context from current thread", e);
129       }
130
131       garbage=null;
132     }
133
134   protected abstract void disassociateSecurity();
135 }
136
Popular Tags