KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > iiop > CorbaTransactionService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.tm.iiop;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27
28 import org.jboss.iiop.CorbaNamingService;
29 import org.jboss.iiop.CorbaORBService;
30 import org.jboss.system.ServiceMBeanSupport;
31 import org.jboss.tm.TMUtil;
32 import org.jboss.tm.TxManager;
33 import org.omg.CORBA.ORB JavaDoc;
34 import org.omg.CosNaming.NameComponent JavaDoc;
35 import org.omg.CosNaming.NamingContextExt JavaDoc;
36 import org.omg.CosNaming.NamingContextExtHelper JavaDoc;
37 import org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc;
38 import org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc;
39 import org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc;
40 import org.omg.PortableServer.POA JavaDoc;
41 import org.omg.PortableServer.POAManagerPackage.AdapterInactive JavaDoc;
42
43 /**
44  * Service MBean that provides the CORBA transaction service for JBoss.
45  *
46  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
47  * @version $Revision: 37459 $
48  */

49 public class CorbaTransactionService
50       extends ServiceMBeanSupport
51       implements CorbaTransactionServiceMBean
52 {
53    // Constants -----------------------------------------------------
54
public static String JavaDoc COSNAMING_NAME = "TransactionService";
55    public static String JavaDoc COSNAMING_USERTX_NAME = "UserTransaction";
56    public static String JavaDoc STR_TO_REMOTE_CONVERTER = "OtsStrToRemoteRefConverter";
57     
58    // Attributes ----------------------------------------------------
59

60    /** The POA used by the CORBA transaction service. */
61    private POA JavaDoc poa;
62    
63    /** The POA used by OTS <code>Resource>/code> instances. */
64    private POA JavaDoc resourcePoa;
65
66    // ServiceMBeanSupport overrides ---------------------------------
67

68    protected void startService()
69       throws Exception JavaDoc
70    {
71       Context JavaDoc jndiContext;
72       ORB JavaDoc orb;
73
74       try
75       {
76          jndiContext = new InitialContext JavaDoc();
77       }
78       catch (NamingException JavaDoc e)
79       {
80          throw new RuntimeException JavaDoc("Cannot get intial JNDI context: " + e);
81       }
82       try
83       {
84          orb = (ORB JavaDoc)jndiContext.lookup("java:/" + CorbaORBService.ORB_NAME);
85       }
86       catch (NamingException JavaDoc e)
87       {
88          throw new RuntimeException JavaDoc("Cannot lookup java:/"
89                                     + CorbaORBService.ORB_NAME + ": " + e);
90       }
91       
92       // Get a POA for OTS objects that are not Resource instances
93
poa = CorbaORBService.getOtsPoa();
94       
95       // Get another POA for OTS Resources, so that they can have their own
96
// default servant. (The name clash between Terminator::rollback and
97
// Resource::rollback disallows us to use a single default servant for
98
// all OTS objects.)
99
resourcePoa = CorbaORBService.getOtsResourcePoa();
100
101       // Create the transaction service
102
OTSServant theDefaultServant = new OTSServant(orb, poa, resourcePoa);
103       poa.set_servant(theDefaultServant);
104       resourcePoa.set_servant(theDefaultServant.getResourceServant());
105       
106       // Both OTS POAs should still be in the holding state at this point.
107
// Activate them.
108
poa.the_POAManager().activate();
109       org.omg.CORBA.Object JavaDoc theTransactionFactory =
110          poa.create_reference_with_id(OTSServant.theFactoryId(),
111                                       TransactionFactoryExtHelper.id());
112
113       // Register the transaction factory in the CORBA naming service
114
NamingContextExt JavaDoc rootContext = null;
115       try
116       {
117          // Obtain local (in-VM) CORBA naming context
118
rootContext = NamingContextExtHelper.narrow((org.omg.CORBA.Object JavaDoc)
119                jndiContext.lookup("java:/" + CorbaNamingService.NAMING_NAME));
120       }
121       catch (NamingException JavaDoc e)
122       {
123          throw new Exception JavaDoc("Cannot lookup java:/" +
124                              CorbaNamingService.NAMING_NAME + ":\n" + e);
125       }
126       try
127       {
128          // Register transaction factory in local CORBA naming context
129
rootContext.rebind(rootContext.to_name(COSNAMING_NAME),
130                                                 theTransactionFactory);
131           getLog().info("CORBA TransactionFactory started");
132           getLog().debug("TransactionFactory: ["
133                         + orb.object_to_string(theTransactionFactory)
134                         + "]");
135
136          // Register it also as "UserTransaction" to please user tx clients
137
rootContext.rebind(rootContext.to_name(COSNAMING_USERTX_NAME),
138                                                 theTransactionFactory);
139       }
140       catch (Exception JavaDoc e)
141       {
142          getLog().error("Cannot bind transaction factory in CORBA naming service:", e);
143          throw new Exception JavaDoc("Cannot bind transaction factory in CORBA naming service:\n"
144                              + e);
145       }
146
147       // Set the TxManager's OTSResourceFactory, OTSContextFactory
148
// and OTSStringRemoteRefConverter
149
TxManager tm = (TxManager)TMUtil.getTransactionManager();
150       tm.setOTSResourceFactory(theDefaultServant);
151       tm.setOTSContextFactory(theDefaultServant);
152       tm.setOTSStringRemoteRefConverter(theDefaultServant);
153 }
154     
155    protected void stopService()
156    {
157       try
158       {
159          // Put both OTS POAs in the holding state
160
// (TODO: make sure JacORB's hold_requests bug gets fixed.)
161
poa.the_POAManager().hold_requests(false);
162          
163          // Unset the TxManager's OTS ResourceFactory.
164
TxManager tm = (TxManager)TMUtil.getTransactionManager();
165          tm.setOTSResourceFactory(null);
166          
167          // Get local (in-VM) CORBA naming context
168
Context JavaDoc jndiContext = new InitialContext JavaDoc();
169          NamingContextExt JavaDoc rootContext =
170                NamingContextExtHelper.narrow((org.omg.CORBA.Object JavaDoc)
171                      jndiContext.lookup("java:/"
172                                         + CorbaNamingService.NAMING_NAME));
173
174          // Unregister the transaction factory from the CORBA naming service
175
try
176          {
177             NameComponent JavaDoc[] name = rootContext.to_name(COSNAMING_NAME);
178             rootContext.unbind(name);
179          }
180          catch (InvalidName JavaDoc invalidName)
181          {
182             getLog().error("Cannot unregister transaction factory from CORBA naming service",
183                            invalidName);
184          }
185          catch (NotFound JavaDoc notFound)
186          {
187             getLog().error("Cannot unregister transaction factory from CORBA naming service",
188                            notFound);
189          }
190          catch (CannotProceed JavaDoc cannotProceed)
191          {
192             getLog().error("Cannot unregister transaction factory from CORBA naming service",
193                            cannotProceed);
194          }
195       }
196       catch (NamingException JavaDoc namingException)
197       {
198          getLog().error("Unexpected error in JNDI lookup", namingException);
199       }
200       catch (AdapterInactive JavaDoc adapterInactive)
201       {
202          getLog().error("Unexpected error when stopping OTS POAs", adapterInactive);
203       }
204    }
205
206 }
207
Popular Tags