001: /*
002: * @(#) Main.java
003: *
004: * JOTM: Java Open Transaction Manager
005: *
006: *
007: * This module was originally developed by
008: *
009: * - Bull S.A. as part of the JOnAS application server code released in
010: * July 1999 (www.bull.com)
011: *
012: * --------------------------------------------------------------------------
013: * The original code and portions created by Bull SA are
014: * Copyright (c) 1999 BULL SA
015: * All rights reserved.
016: *
017: * Redistribution and use in source and binary forms, with or without
018: * modification, are permitted provided that the following conditions are met:
019: *
020: * -Redistributions of source code must retain the above copyright notice, this
021: * list of conditions and the following disclaimer.
022: *
023: * -Redistributions in binary form must reproduce the above copyright notice,
024: * this list of conditions and the following disclaimer in the documentation
025: * and/or other materials provided with the distribution.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: * --------------------------------------------------------------------------
040: * Contributor(s):
041: * Guillaume Riviere
042: *
043: * 23/09/04 Andy Glick
044: * Updated parameter handling for commons-cli 1.0
045: *
046: * -------------------------------------------------------------------------
047: * $Id: Main.java,v 1.21 2004/10/29 05:42:31 tonyortiz Exp $
048: * --------------------------------------------------------------------------
049: */
050: package org.objectweb.jotm;
051:
052: import java.io.PrintWriter;
053: import java.util.Enumeration;
054: import java.util.Hashtable;
055:
056: import javax.naming.Context;
057: import javax.naming.InitialContext;
058: import javax.naming.NamingException;
059:
060: import org.apache.commons.cli.CommandLine;
061: import org.apache.commons.cli.CommandLineParser;
062: import org.apache.commons.cli.HelpFormatter;
063: import org.apache.commons.cli.Options;
064: import org.apache.commons.cli.ParseException;
065: import org.apache.commons.cli.PosixParser;
066:
067: import org.objectweb.transaction.jta.TMService;
068:
069: /**
070: * This class is used to start JOTM as a standalone transaction manager.
071: *
072: * @author Christophe Ney - cney@batisseurs.com
073: * Created on Feb 26, 2002
074: * */
075: public class Main extends Thread {
076:
077: private static final String progname = "JOTM";
078:
079: // command line options
080: private static Options cmdLineOptions = null;
081:
082: // verbose mode
083: static boolean verbose = false;
084: // debug mode
085: static boolean debug = false;
086: // default transaction timeout
087: static int timeout = 0;
088: // is transaction factory remote
089: static boolean remote = false;
090: // user transaction URL
091: static String userTransactionName = null;
092: // transaction manager URL
093: static String transactionManagerName = null;
094: // naming context
095: static Context ictx = null;
096: // logWriter
097: static PrintWriter logWriter = new PrintWriter(System.out, true);
098:
099: // instance of JOTM
100: private static TMService jotm;
101:
102: /**
103: * used as shutdown hook
104: */
105: public void run() {
106: System.out.print("shutting down...");
107: try {
108: ictx.unbind(userTransactionName);
109: } catch (Exception e) {
110: // ignore
111: }
112: try {
113: ictx.unbind(transactionManagerName);
114: } catch (Exception e) {
115: // ignore
116: }
117: // stop jotm
118: try {
119: jotm.stop();
120: } catch (Exception e) {
121: // ignore
122: }
123: logWriter.close();
124: }
125:
126: public static void printHelp(Options cmdLineOptions) {
127: HelpFormatter hf = new HelpFormatter();
128: hf.printHelp("JOTM [options...]", cmdLineOptions);
129: }
130:
131: private static void verbose(String msg) {
132: if (verbose) {
133: System.out.println(msg);
134: }
135: }
136:
137: private static void checkRegistryMessage() {
138: System.err.println("Current JNDI settings are:");
139: boolean found = false;
140: try {
141: Hashtable env = ictx.getEnvironment();
142: for (Enumeration e = env.keys(); e.hasMoreElements();) {
143: String key = (String) e.nextElement();
144: System.err.println("- " + key + "=" + env.get(key));
145: found = true;
146: }
147: } catch (NamingException e) {
148: // ignore
149: }
150: if (!found) {
151: System.err.println("JNDI properties are not set!");
152: } else {
153: System.err
154: .println("Check that registry is running on a port matching JNDI properties");
155: }
156: }
157:
158: /**
159: * Used to start JOTM from a command line interface.
160: *
161: * @param args JOTM arguments
162: */
163: public static void main(String[] args) {
164:
165: cmdLineOptions = new Options();
166: // option parameters are: short description (String), long description (String), has arguments (boolean),
167: // description (String)
168: cmdLineOptions.addOption("d", "debug", false, "debug mode");
169: cmdLineOptions.addOption("v", "verbose", false, "verbose mode");
170: cmdLineOptions.addOption("h", "help", false,
171: "print this message and exit");
172: cmdLineOptions.addOption("m", "transaction_manager", true,
173: "JNDI URL of the TransactionManager");
174: cmdLineOptions.addOption("r", "remote", false,
175: "lookup remote transaction factory");
176: cmdLineOptions.addOption("t", "timeout", true,
177: "default transaction timeout (in seconds)");
178: cmdLineOptions.addOption("u", "user_transaction", true,
179: "JNDI URL of the UserTransaction");
180:
181: CommandLine cmd = null;
182:
183: CommandLineParser parser = new PosixParser();
184:
185: try {
186: cmd = parser.parse(cmdLineOptions, args, true);
187: } catch (ParseException e) {
188: System.err.println("\n" + e.getMessage());
189: printHelp(cmdLineOptions);
190: System.err.println();
191: System.exit(1);
192: }
193:
194: debug = cmd.hasOption('d');
195: remote = cmd.hasOption('r');
196: verbose = cmd.hasOption('v');
197: if (cmd.hasOption('h')) {
198: printHelp(cmdLineOptions);
199: System.exit(1);
200: }
201: if (cmd.hasOption('m')) {
202: transactionManagerName = cmd.getOptionValue('m');
203: }
204: if (cmd.hasOption('t')) {
205: try {
206: timeout = Integer.parseInt(cmd.getOptionValue('t'));
207: } catch (NumberFormatException e) {
208: System.err.println("\ntimeout is not a number");
209: printHelp(cmdLineOptions);
210: System.err.println();
211: System.exit(1);
212: }
213: }
214: if (cmd.hasOption('u')) {
215: userTransactionName = cmd.getOptionValue('u');
216: }
217:
218: verbose("UserTransaction Name =" + userTransactionName);
219: verbose("TransactionManager Name =" + transactionManagerName);
220: verbose("Transaction factory =" + (remote ? "remote" : "local"));
221: verbose("Default transaction timeout =" + timeout);
222:
223: TraceTimer.setLogWriter(logWriter);
224: TraceTimer.setVerbose(verbose);
225: TraceTimer.setDebug(debug);
226:
227: try {
228: // create an instance of JOTM
229: jotm = new Jotm(!remote, true);
230: } catch (NamingException e) {
231: System.out.println("unable to start JOTM!: "
232: + e.getMessage());
233: System.exit(1);
234: }
235:
236: // trap program termination
237: Runtime.getRuntime().addShutdownHook(new Main());
238:
239: try {
240: ictx = new InitialContext();
241: } catch (NamingException e) {
242: System.err.println("No initial context: "
243: + e.getExplanation());
244: e.printStackTrace();
245: System.exit(1);
246: }
247:
248: try {
249: if (userTransactionName != null) {
250: ictx.rebind(userTransactionName, jotm
251: .getUserTransaction());
252: System.out
253: .println("UserTransaction object bound in JNDI with name "
254: + userTransactionName);
255: }
256: if (transactionManagerName != null) {
257: ictx.rebind(transactionManagerName, jotm
258: .getTransactionManager());
259: System.out
260: .println("TransactionManager object bound in JNDI with name "
261: + transactionManagerName);
262: }
263: } catch (NamingException e) {
264: System.err.println("UserTransaction rebind failed :"
265: + e.getExplanation());
266: e.printStackTrace();
267: checkRegistryMessage();
268: System.exit(1);
269: }
270:
271: System.out.print(progname + " is running...");
272: }
273: }
|