001: package org.jacorb.transaction;
002:
003: /*
004: * JacORB transaction service - a free TS for JacORB
005: *
006: * Copyright (C) 1999-2004 LogicLand group Alex Sinishin.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import org.apache.avalon.framework.logger.Logger;
024:
025: import org.omg.CosTransactions.*;
026: import org.omg.CosNaming.*;
027: import java.io.*;
028:
029: public class TransactionService extends TransactionFactoryPOA {
030: private static boolean initialized = false;
031: private static TransactionService factory;
032: private static TransactionFactory fact_ref;
033: private static CoordinatorImpl[] coordinators;
034: private static Timer timer;
035: private static int trans_id = 0;
036: private static org.omg.PortableServer.POA poa;
037:
038: private static Logger logger;
039:
040: static Timer get_timer() {
041: return timer;
042: }
043:
044: public static boolean is_initialized() {
045: return initialized;
046: }
047:
048: public static TransactionFactory get_reference() {
049: return fact_ref;
050: }
051:
052: static void release_coordinator(int hash_code) {
053: coordinators[hash_code] = null;
054: }
055:
056: private int find_free() {
057: for (int i = 0; i < coordinators.length; i++) {
058: if (coordinators[i] == null) {
059: return i;
060: }
061: }
062: throw new org.omg.CORBA.INTERNAL();
063: }
064:
065: public Control create(int time_out) {
066: trans_id++;
067: int ix;
068: synchronized (coordinators) {
069: ix = find_free();
070: coordinators[ix] = new CoordinatorImpl(poa, trans_id, ix,
071: time_out);
072: }
073: return coordinators[ix]._get_control();
074: }
075:
076: public Control recreate(PropagationContext ctx) {
077: throw new org.omg.CORBA.NO_IMPLEMENT();
078: }
079:
080: public static void start(org.omg.PortableServer.POA _poa,
081: int max_of_trans) {
082: if (initialized) {
083: throw new org.omg.CORBA.INTERNAL();
084: }
085: try {
086: poa = _poa;
087: factory = new TransactionService();
088: fact_ref = TransactionFactoryHelper.narrow(poa
089: .servant_to_reference(factory));
090:
091: coordinators = new CoordinatorImpl[max_of_trans];
092:
093: for (int i = 0; i < coordinators.length; i++) {
094: coordinators[i] = null;
095: }
096: timer = new Timer(max_of_trans);
097:
098: } catch (Exception e) {
099: e.printStackTrace();
100: System.exit(1);
101: }
102: initialized = true;
103: }
104:
105: public static void main(String[] args) {
106: org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
107: logger = ((org.jacorb.orb.ORB) orb).getConfiguration()
108: .getNamedLogger("jacorb.tx_service");
109: try {
110: org.omg.PortableServer.POA poa = org.omg.PortableServer.POAHelper
111: .narrow(orb.resolve_initial_references("RootPOA"));
112: poa.the_POAManager().activate();
113:
114: TransactionService transactionService = new TransactionService();
115: transactionService.start(poa, 10);
116:
117: if (args.length == 1) {
118: // write the object reference to args[0]
119:
120: PrintWriter ps = new PrintWriter(new FileOutputStream(
121: new File(args[0])));
122: ps.println(orb.object_to_string(transactionService
123: .get_reference()));
124: ps.close();
125: } else {
126: NamingContextExt nc = NamingContextExtHelper.narrow(orb
127: .resolve_initial_references("NameService"));
128: NameComponent[] name = new NameComponent[1];
129: name[0] = new NameComponent("TransactionService",
130: "service");
131: nc.bind(name, transactionService.get_reference());
132: }
133: if (logger.isInfoEnabled())
134: logger.info("TransactionService up");
135: } catch (Exception e) {
136: e.printStackTrace();
137: }
138: orb.run();
139: }
140:
141: }
|