KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > DefaultTransactionService


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28 //----------------------------------------------------------------------------
29
//
30
// Module: DefaultTransactionService.java
31
//
32
// Description: The JTS TransactionService class.
33
//
34
// Product: com.sun.jts.CosTransactions
35
//
36
// Author: Simon Holdsworth
37
//
38
// Date: June, 1997
39
//
40
// Copyright (c): 1995-1997 IBM Corp.
41
//
42
// The source code for this program is not published or otherwise divested
43
// of its trade secrets, irrespective of what has been deposited with the
44
// U.S. Copyright Office.
45
//
46
// This software contains confidential and proprietary information of
47
// IBM Corp.
48
//----------------------------------------------------------------------------
49

50 package com.sun.jts.CosTransactions;
51
52 // Import required classes.
53

54 import java.util.*;
55 import java.net.InetAddress JavaDoc;
56 import java.net.UnknownHostException JavaDoc;
57
58 import org.omg.CORBA.*;
59 import org.omg.CORBA.ORBPackage.InvalidName JavaDoc;
60 import org.omg.CosTransactions.*;
61 import org.omg.CORBA.Policy JavaDoc.*;
62 import org.omg.CosNaming.*;
63 import org.omg.CosNaming.NamingContextPackage.*;
64 import org.omg.PortableServer.*;
65 import org.omg.CORBA.LocalObject JavaDoc;
66
67 import com.sun.corba.ee.spi.costransactions.TransactionService;
68 import com.sun.corba.ee.spi.presentation.rmi.StubAdapter;
69
70 import com.sun.jts.otsidl.*;
71
72 import java.util.logging.Logger JavaDoc;
73 import java.util.logging.Level JavaDoc;
74 import com.sun.logging.LogDomains;
75 import com.sun.jts.utils.LogFormatter;
76
77 /**The DefaultTransactionService is our implemention of the
78  * com.sun.CosTransactions.TransactionService class.
79  * <p>
80  * The TransactionService abstract class describes the packaging of a
81  * Java Transaction Service implementation. Each implementation should be
82  * packaged as a subclass of the TransactionService class.
83  *
84  * @version 0.01
85  *
86  * @author Simon Holdsworth, IBM Corporation
87  *
88  * @see
89 */

90 //----------------------------------------------------------------------------// CHANGE HISTORY
91
//
92
// Version By Change Description
93
// 0.01 SAJH Initial implementation.
94
//-----------------------------------------------------------------------------
95

96 public class DefaultTransactionService implements TransactionService,
97     ProxyChecker {
98     private static CurrentImpl currentInstance = null;
99     private static TransactionFactoryImpl factoryInstance = null;
100     //private static AdministrationImpl adminInstance = null;
101
private static NamingContext namingContext = null;
102     private static ORB orb = null;
103     private static boolean recoverable = false;
104     private static boolean poasCreated = false;
105     private static boolean active = false;
106     /*
107         Logger to log transaction messages
108     */

109     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
110     public static final String JavaDoc JTS_SERVER_ID = "com.sun.jts.persistentServerId"; /* FROZEN */
111
112    
113     /**Default constructor.
114      *
115      * @param
116      *
117      * @return
118      *
119      * @see
120      */

121     public DefaultTransactionService() {
122         // We do not set up the Current instance until we know the ORB.
123
// This method is not traced as trace is not configured until the init method
124
// is called.
125

126     }
127
128     /**
129      * @return true, if transaction manager is available.
130      */

131     public static boolean isActive() {
132         return active;
133     }
134     
135     /**Obtain the implementation of the Current interface provided
136      * by the transaction service implementation.
137      *
138      * @param
139      *
140      * @return An instance of the Current class
141      *
142      * @see
143      */

144     public org.omg.CosTransactions.Current get_current() {
145         org.omg.CosTransactions.Current result = null;
146
147         result = (org.omg.CosTransactions.Current)currentInstance;
148
149         return result;
150     }
151
152     /**Request the transaction service to identify itself with a communication
153      * manager.
154      * <p>
155      * Multiple communication managers may request a transaction
156      * service to identify itself.
157      *
158      * @param orb The ORB to be used for communication.
159      * @param ident The TSIdentification object with which the Sender and
160      * Receiver must be registered.
161      * @param properties The Properties with which the ORB was initialised.
162      *
163      * @return
164      *
165      * @see
166      */

167     public void identify_ORB(ORB orb,
168                              TSIdentification ident,
169                              Properties properties) {
170         if( this.orb == null ) {
171             this.orb = orb;
172             Configuration.setORB(orb);
173             Configuration.setProperties(properties);
174             Configuration.setProxyChecker(this);
175         }
176
177         // We have to wait until this point to trace entry into this method as trace
178
// is only started by setting the properties in the Configuration class.
179

180         // Get the persistent server id of the server. If it does not represent a
181
// transient server, then the server is recoverable.
182

183         if( !poasCreated ) {
184             //String serverId = properties.getProperty("com.sun.corba.ee.internal.POA.ORBServerId"/*#Frozen*/);
185
String JavaDoc serverId = properties.getProperty(JTS_SERVER_ID);
186             if (serverId == null) {
187                 serverId =
188                     properties.getProperty("com.sun.CORBA.POA.ORBServerId"/*#Frozen*/);
189             }
190             if (serverId != null) {
191                     _logger.log(Level.INFO,"jts.startup_msg",serverId);
192             }
193             String JavaDoc serverName = "UnknownHost"/*#Frozen*/;
194             try {
195                 serverName = InetAddress.getLocalHost().getHostName();
196             } catch (UnknownHostException JavaDoc ex) {
197             }
198             if( serverId != null ) {
199                 Configuration.setServerName(serverName + ",P" + serverId/*#Frozen*/, true);
200                 recoverable = true;
201             } else {
202                 long timestamp = System.currentTimeMillis();
203                 Configuration.setServerName(serverName + ",T" +
204                                             String.valueOf(timestamp)/*#Frozen*/, false);
205             }
206
207             // Set up the POA objects for transient and persistent references.
208

209             try {
210                 createPOAs();
211             } catch( Exception JavaDoc exc ) {
212                 _logger.log(Level.WARNING,"jts.unexpected_error_when_creating_poa",exc);
213                 throw new INTERNAL(MinorCode.TSCreateFailed,CompletionStatus.COMPLETED_NO);
214             }
215         }
216
217         // Set up the instance of the Current object now that we know the ORB.
218

219         if( currentInstance == null )
220            try {
221                 currentInstance = new CurrentImpl();
222             } catch( Exception JavaDoc exc ) {
223                 _logger.log(Level.WARNING,"jts.unexpected_error_when_creating_current",exc);
224                 throw new INTERNAL(MinorCode.TSCreateFailed,CompletionStatus.COMPLETED_NO);
225             }
226
227         // Identify Sender and Receiver objects to the Comm Manager.
228

229         SenderReceiver.identify(ident);
230
231         // If the server is recoverable, create a NamingContext with which to
232
// register the factory and admin objects.
233

234         if( recoverable && namingContext == null )
235             try {
236                 namingContext = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"/*#Frozen*/));
237             } catch( InvalidName JavaDoc inexc ) {
238                 // _logger.log(Level.WARNING,"jts.orb_not_running");
239
if (_logger.isLoggable(Level.FINE)) {
240                     _logger.log(Level.FINE,"jts.orb_not_running");
241                 }
242             } catch( Exception JavaDoc exc ) {
243                 // _logger.log(Level.WARNING,"jts.orb_not_running");
244
if (_logger.isLoggable(Level.FINE)) {
245                     _logger.log(Level.FINE,"jts.orb_not_running");
246                 }
247             }
248
249         // Create a TransactionFactory object and register it with the naming service
250
// if recoverable.
251

252         if( factoryInstance == null )
253             try {
254                 boolean localFactory = true;
255                 TransactionFactory factory = null;
256                 factoryInstance = new TransactionFactoryImpl();
257                 if (localFactory) {
258                     factory = (TransactionFactory) factoryInstance;
259                 } else {
260                     factory = factoryInstance.object();
261                 }
262
263                 // Since we are instantiating the TransactionFactory object
264
// locally, we have a local transaction factory.
265

266                 Configuration.setFactory(factory, /*local factory*/ localFactory);
267
268                 if (Configuration.isLocalFactory() == false && namingContext != null) {
269                     NameComponent nc = new NameComponent(TransactionFactoryHelper.id(),"");
270                     NameComponent path[] = {nc};
271                     namingContext.rebind(path,factory);
272                 }
273
274                 // Commented out by TN
275
//if( !recoverable )
276
//_logger.log(Level.WARNING,"jts.non_persistent_server");
277
} catch( Exception JavaDoc exc ) {
278                 _logger.log(Level.WARNING,"jts.cannot_register_with_orb","TransactionFactory");
279             }
280
281             active = true; // transaction manager is alive and available
282
}
283
284     /**Request the transaction service to stop any further transactional activity.
285      *
286      * @param immediate Indicates whether to ignore running transactions.
287      *
288      * @return
289      *
290      * @see
291      */

292     public static void shutdown( boolean immediate ) {
293         // Remove the admin and factory objects from the naming service.
294

295         if( namingContext != null )
296             try {
297                 NameComponent nc = new NameComponent(TransactionFactoryHelper.id(),"");
298                 NameComponent path[] = {nc};
299                 namingContext.unbind(path);
300
301                 namingContext = null;
302             } catch( Exception JavaDoc exc ) {}
303
304         // Inform the local TransactionFactory and CurrentImpl classes that no more
305
// transactional activity may occur.
306

307         TransactionFactoryImpl.deactivate();
308         CurrentImpl.deactivate();
309
310         // Shut down the basic transaction services.
311

312         currentInstance.shutdown(immediate);
313
314         // Discard the factory and current instances.
315

316         currentInstance = null;
317         factoryInstance = null;
318         //adminInstance = null;
319
active = false;
320     }
321
322     /**Determines whether the given object is a proxy.
323      *
324      * @param obj The potential proxy.
325      *
326      * @return Indicates whether the object is a proxy.
327      *
328      * @see
329      */

330     public final boolean isProxy( org.omg.CORBA.Object JavaDoc obj ) {
331
332         // TN POA changes
333
return !( StubAdapter.isStub(obj) && StubAdapter.isLocal(obj) );
334     }
335
336     /**Creates the POA objects which are used for objects within the JTS.
337      *
338      * @param
339      *
340      * @return
341      *
342      * @exception Exception The operation failed.
343      *
344      * @see
345      */

346     final static void createPOAs()
347         throws Exception JavaDoc {
348
349         POA rootPOA = (POA)orb.resolve_initial_references("RootPOA"/*#Frozen*/);
350
351         // Create the POA used for CoordinatorResource objects.
352

353         POA CRpoa = null;
354         if( recoverable ) {
355
356             // Create the POA with PERSISTENT and USE_SERVANT_MANAGER policies.
357

358             Policy JavaDoc[] tpolicy = new Policy JavaDoc[2];
359             tpolicy[0] = rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
360             tpolicy[1] = rootPOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
361             CRpoa = rootPOA.create_POA("com.sun.jts.CosTransactions.CoordinatorResourcePOA"/*#Frozen*/, null, tpolicy);
362
363             // Register the ServantActivator with the POA, then activate POA.
364

365             CoordinatorResourceServantActivator crsa = new CoordinatorResourceServantActivator(orb);
366             CRpoa.set_servant_manager(crsa);
367         }
368
369         // If the process is not recoverable, then we do not create a persistent POA.
370

371         else
372             CRpoa = rootPOA;
373
374         Configuration.setPOA("CoordinatorResource"/*#Frozen*/,CRpoa);
375
376         // Create the POA used for RecoveryCoordinator objects.
377

378         POA RCpoa = null;
379         if( recoverable ) {
380
381             // Create the POA with PERSISTENT and USE_SERVANT_MANAGER policies.
382

383             Policy JavaDoc[] tpolicy = new Policy JavaDoc[2];
384             tpolicy[0] = rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
385             tpolicy[1] = rootPOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
386             RCpoa = rootPOA.create_POA("com.sun.jts.CosTransactions.RecoveryCoordinatorPOA"/*#Frozen*/, null, tpolicy);
387
388             // Register the ServantActivator with the POA, then activate POA.
389

390             RecoveryCoordinatorServantActivator rcsa = new RecoveryCoordinatorServantActivator(orb);
391             RCpoa.set_servant_manager(rcsa);
392         }
393
394         // If the process is not recoverable, then we do not create a persistent POA.
395

396         else
397             RCpoa = rootPOA;
398
399         Configuration.setPOA("RecoveryCoordinator"/*#Frozen*/,RCpoa);
400
401         // Create the POA used for Coordinator objects.
402

403         POA Cpoa = rootPOA.create_POA("CoordinatorPOA"/*#Frozen*/, null, null);
404         // POA Cpoa = rootPOA;
405
Configuration.setPOA("Coordinator"/*#Frozen*/,Cpoa);
406
407         // Create the POA used for transient objects.
408

409         Configuration.setPOA("transient"/*#Frozen*/,rootPOA);
410
411         // rootPOA.the_activator(new JTSAdapterActivator(orb));
412
CRpoa.the_POAManager().activate();
413         RCpoa.the_POAManager().activate();
414         Cpoa.the_POAManager().activate();
415         rootPOA.the_POAManager().activate();
416
417         poasCreated = true;
418
419     }
420 }
421
422 /**The RecoveryCoordinatorServantActivator class provides the means to locate
423  * instances of the RecoveryCoordinator class using the transaction identifier
424  * and the RecoveryManager.
425  *
426  * @version 0.01
427  *
428  * @author Simon Holdsworth, IBM Corporation
429  *
430  * @see
431 */

432 //----------------------------------------------------------------------------
433
// CHANGE HISTORY
434
//
435
// Version By Change Description
436
// 0.01 SAJH Initial implementation.
437
//------------------------------------------------------------------------------
438

439 class RecoveryCoordinatorServantActivator extends LocalObject JavaDoc implements ServantActivator {
440     private ORB orb = null;
441
442     /*
443      * COMMENT(Ram J) The _ids() method needs to be removed/modified
444      * (along with LocalObjectImpl) once OMG standardizes the local
445      * object interfaces.
446      *
447      * TN - removed ids after switching to LocalObject
448      */

449
450      // Type-specific CORBA::Object operations
451
/*
452     private static String[] __ids = {
453         "IDL:omg.org/PortableServer/ServantActivator:1.0",
454         "IDL:omg.org/PortableServer/ServantManager:1.0"
455     };
456
457     public String[] _ids () {
458         return __ids;
459     }
460     */

461
462     /**Creates the servant activator for the RecoveryCoordinator class.
463      *
464      * @param orb The ORB.
465      *
466      * @return
467      *
468      * @see
469      */

470     RecoveryCoordinatorServantActivator(ORB orb) {
471         this.orb = orb;
472     }
473
474     /**Returns the servant object which corresponds to the given object identity
475      * for the given POA.
476      *
477      * @param oid The object identifier.
478      * @param adapter The POA.
479      *
480      * @return The servant.
481      *
482      * @see
483      */

484     public Servant incarnate( byte[] oid, POA adapter )
485         throws org.omg.PortableServer.ForwardRequest JavaDoc {
486
487         Servant servant = new RecoveryCoordinatorImpl(oid);
488
489         return servant;
490     }
491
492     /**Does nothing.
493      *
494      * @param
495      *
496      * @return
497      *
498      * @see
499      */

500     public void etherealize( byte[] oid,
501                              POA adapter,
502                              Servant servant,
503                              boolean cleanup_in_progress,
504                              boolean remaining_activations ) {
505     }
506 }
507
508 /**The CoordinatorResourceServantActivator class provides the means to locate
509  * instances of the CoordinatorResource class after a failure, using the
510  * transaction identifier and the RecoveryManager.
511  *
512  * @version 0.01
513  *
514  * @author Simon Holdsworth, IBM Corporation
515  *
516  * @see
517 */

518 //----------------------------------------------------------------------------
519
// CHANGE HISTORY
520
//
521
// Version By Change Description
522
// 0.01 SAJH Initial implementation.
523
//------------------------------------------------------------------------------
524

525 class CoordinatorResourceServantActivator extends LocalObject JavaDoc implements ServantActivator {
526     private ORB orb = null;
527
528     /*
529      * COMMENT(Ram J) The _ids() method needs to be removed/modified
530      * (along with LocalObjectImpl) once OMG standardizes the local
531      * object interfaces.
532      *
533      * TN - removed ids after switching to LocalObject
534      */

535
536     /*
537      // Type-specific CORBA::Object operations
538     private static String[] __ids = {
539         "IDL:omg.org/PortableServer/ServantActivator:1.0",
540         "IDL:omg.org/PortableServer/ServantManager:1.0"
541     };
542
543     public String[] _ids () {
544         return __ids;
545     }
546     */

547
548     /**Creates the servant activator for the CoordinatorResource class.
549      *
550      * @param orb The ORB.
551      *
552      * @return
553      *
554      * @see
555      */

556     CoordinatorResourceServantActivator(ORB orb) {
557         this.orb = orb;
558     }
559
560     /**Returns the servant object which corresponds to the given object identity
561      * for the given POA.
562      *
563      * @param oid The object identifier.
564      * @param adapter The POA.
565      *
566      * @return The servant.
567      *
568      * @see
569      */

570     public Servant incarnate( byte[] oid, POA adapter )
571         throws org.omg.PortableServer.ForwardRequest JavaDoc {
572         Servant servant = new CoordinatorResourceImpl(oid);
573         return servant;
574     }
575
576     /**Does nothing.
577      *
578      * @param
579      *
580      * @return
581      *
582      * @see
583      */

584     public void etherealize( byte[] oid,
585                              POA adapter,
586                              Servant servant,
587                              boolean cleanup_in_progress,
588                              boolean remaining_activations ) {
589     }
590 }
591
592 class JTSAdapterActivator extends LocalObject JavaDoc implements AdapterActivator {
593     private ORB orb;
594
595     /*
596      * COMMENT(Ram J) The _ids() method needs to be removed/modified
597      * (along with LocalObjectImpl) once OMG standardizes the local
598      * object interfaces.
599      *
600      * TN - removed ids after switching to LocalObject
601      */

602
603     /*
604     // Type-specific CORBA::Object operations
605     private static String[] __ids = {
606         "IDL:omg.org/PortableServer/AdapterActivator:1.0"
607     };
608
609     public String[] _ids() {
610         return __ids;
611     }
612     */

613
614     JTSAdapterActivator(ORB orb) {
615         this.orb = orb;
616     }
617
618     public boolean unknown_adapter(POA parent, String JavaDoc name) {
619
620         try {
621             DefaultTransactionService.createPOAs();
622         } catch( Exception JavaDoc exc ) {
623         }
624
625         return true;
626     }
627 }
628
Popular Tags