KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > transaction > TransactionService


1 package org.jacorb.transaction;
2
3
4 /*
5  * JacORB transaction service - a free TS for JacORB
6  *
7  * Copyright (C) 1999-2004 LogicLand group Alex Sinishin.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the Free
21  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */

23
24 import org.apache.avalon.framework.logger.Logger;
25
26 import org.omg.CosTransactions.*;
27 import org.omg.CosNaming.*;
28 import java.io.*;
29
30 public class TransactionService
31     extends TransactionFactoryPOA
32 {
33     private static boolean initialized = false;
34     private static TransactionService factory;
35     private static TransactionFactory fact_ref;
36     private static CoordinatorImpl[] coordinators;
37     private static Timer timer;
38     private static int trans_id = 0;
39     private static org.omg.PortableServer.POA JavaDoc poa;
40
41     private static Logger logger;
42
43     static Timer get_timer(){
44         return timer;
45     }
46
47
48     public static boolean is_initialized(){
49         return initialized;
50     }
51
52     public static TransactionFactory get_reference(){
53         return fact_ref;
54     }
55
56     static void release_coordinator(int hash_code)
57     {
58         coordinators[hash_code] = null;
59     }
60
61     private int find_free(){
62         for (int i = 0;i < coordinators.length;i++){
63             if (coordinators[i] == null){
64                 return i;
65             }
66         }
67         throw new org.omg.CORBA.INTERNAL JavaDoc();
68     }
69
70     public Control create(int time_out)
71     {
72         trans_id++;
73         int ix;
74         synchronized(coordinators){
75             ix = find_free();
76             coordinators[ix] = new CoordinatorImpl(poa, trans_id, ix, time_out);
77         }
78         return coordinators[ix]._get_control();
79     }
80
81     public Control recreate(PropagationContext ctx){
82         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
83     }
84
85     public static void start(org.omg.PortableServer.POA JavaDoc _poa, int max_of_trans)
86     {
87         if (initialized)
88     {
89             throw new org.omg.CORBA.INTERNAL JavaDoc();
90         }
91         try
92     {
93             poa = _poa;
94             factory = new TransactionService();
95             fact_ref =
96                 TransactionFactoryHelper.narrow(poa.servant_to_reference(factory));
97
98             coordinators = new CoordinatorImpl [max_of_trans];
99
100             for (int i = 0;i < coordinators.length;i++)
101             {
102                 coordinators[i] = null;
103             }
104             timer = new Timer(max_of_trans);
105
106         }
107     catch(Exception JavaDoc e)
108     {
109             e.printStackTrace();
110             System.exit(1);
111         }
112         initialized = true;
113     }
114
115
116     public static void main( String JavaDoc[] args )
117     {
118         org.omg.CORBA.ORB JavaDoc orb =
119             org.omg.CORBA.ORB.init(args, null);
120         logger =
121             ((org.jacorb.orb.ORB)orb).getConfiguration().getNamedLogger("jacorb.tx_service");
122         try
123         {
124             org.omg.PortableServer.POA JavaDoc poa =
125                 org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
126         poa.the_POAManager().activate();
127             
128         TransactionService transactionService =
129                 new TransactionService();
130         transactionService.start(poa,10);
131
132             if( args.length == 1 )
133             {
134                 // write the object reference to args[0]
135

136                 PrintWriter ps =
137                     new PrintWriter(new FileOutputStream(new File( args[0])));
138                 ps.println( orb.object_to_string( transactionService.get_reference() ) );
139                 ps.close();
140             }
141             else
142             {
143                 NamingContextExt nc =
144             NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
145                 NameComponent [] name = new NameComponent[1];
146                 name[0] = new NameComponent( "TransactionService", "service");
147                 nc.bind(name, transactionService.get_reference());
148             }
149             if (logger.isInfoEnabled())
150                 logger.info("TransactionService up");
151         }
152         catch ( Exception JavaDoc e )
153         {
154             e.printStackTrace();
155         }
156         orb.run();
157     }
158
159 }
160
161
162
Popular Tags