KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jotm > Main


1 /*
2  * @(#) Main.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  *
7  * This module was originally developed by
8  *
9  * - Bull S.A. as part of the JOnAS application server code released in
10  * July 1999 (www.bull.com)
11  *
12  * --------------------------------------------------------------------------
13  * The original code and portions created by Bull SA are
14  * Copyright (c) 1999 BULL SA
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * -Redistributions of source code must retain the above copyright notice, this
21  * list of conditions and the following disclaimer.
22  *
23  * -Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * --------------------------------------------------------------------------
40  * Contributor(s):
41  * Guillaume Riviere
42  *
43  * 23/09/04 Andy Glick
44  * Updated parameter handling for commons-cli 1.0
45  *
46  * -------------------------------------------------------------------------
47  * $Id: Main.java,v 1.21 2004/10/29 05:42:31 tonyortiz Exp $
48  * --------------------------------------------------------------------------
49  */

50 package org.objectweb.jotm;
51
52 import java.io.PrintWriter JavaDoc;
53 import java.util.Enumeration JavaDoc;
54 import java.util.Hashtable JavaDoc;
55
56 import javax.naming.Context JavaDoc;
57 import javax.naming.InitialContext JavaDoc;
58 import javax.naming.NamingException JavaDoc;
59
60 import org.apache.commons.cli.CommandLine;
61 import org.apache.commons.cli.CommandLineParser;
62 import org.apache.commons.cli.HelpFormatter;
63 import org.apache.commons.cli.Options;
64 import org.apache.commons.cli.ParseException;
65 import org.apache.commons.cli.PosixParser;
66
67 import org.objectweb.transaction.jta.TMService;
68
69 /**
70  * This class is used to start JOTM as a standalone transaction manager.
71  *
72  * @author Christophe Ney - cney@batisseurs.com
73  * Created on Feb 26, 2002
74  * */

75 public class Main extends Thread JavaDoc {
76
77     private static final String JavaDoc progname = "JOTM";
78
79     // command line options
80
private static Options cmdLineOptions = null;
81
82     // verbose mode
83
static boolean verbose = false;
84     // debug mode
85
static boolean debug = false;
86     // default transaction timeout
87
static int timeout = 0;
88     // is transaction factory remote
89
static boolean remote = false;
90     // user transaction URL
91
static String JavaDoc userTransactionName = null;
92     // transaction manager URL
93
static String JavaDoc transactionManagerName = null;
94     // naming context
95
static Context JavaDoc ictx = null;
96     // logWriter
97
static PrintWriter JavaDoc logWriter = new PrintWriter JavaDoc(System.out, true);
98
99     // 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 JavaDoc e) {
110             // ignore
111
}
112         try {
113             ictx.unbind(transactionManagerName);
114         } catch (Exception JavaDoc e) {
115             // ignore
116
}
117         // stop jotm
118
try {
119             jotm.stop();
120         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc env = ictx.getEnvironment();
142             for (Enumeration JavaDoc e = env.keys(); e.hasMoreElements();) {
143                 String JavaDoc key = (String JavaDoc) e.nextElement();
144                 System.err.println("- " + key + "=" + env.get(key));
145                 found = true;
146             }
147         } catch (NamingException JavaDoc e) {
148             // ignore
149
}
150         if (!found) {
151             System.err.println("JNDI properties are not set!");
152         } else {
153             System.err.println("Check that registry is running on a port matching JNDI properties");
154         }
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 JavaDoc[] 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, "print this message and exit");
171         cmdLineOptions.addOption("m", "transaction_manager", true, "JNDI URL of the TransactionManager");
172         cmdLineOptions.addOption("r", "remote", false, "lookup remote transaction factory");
173         cmdLineOptions.addOption("t", "timeout", true, "default transaction timeout (in seconds)");
174         cmdLineOptions.addOption("u", "user_transaction", true, "JNDI URL of the UserTransaction");
175
176         CommandLine cmd = null;
177
178         CommandLineParser parser = new PosixParser();
179
180         try {
181             cmd = parser.parse(cmdLineOptions, args, true);
182         } catch (ParseException e) {
183             System.err.println("\n" + e.getMessage());
184             printHelp(cmdLineOptions);
185             System.err.println();
186             System.exit(1);
187         }
188
189         debug = cmd.hasOption('d');
190         remote = cmd.hasOption('r');
191         verbose = cmd.hasOption('v');
192         if (cmd.hasOption('h')) {
193             printHelp(cmdLineOptions);
194             System.exit(1);
195         }
196         if (cmd.hasOption('m')) {
197             transactionManagerName = cmd.getOptionValue('m');
198         }
199         if (cmd.hasOption('t')) {
200             try {
201                 timeout = Integer.parseInt(cmd.getOptionValue('t'));
202             } catch (NumberFormatException JavaDoc e) {
203                 System.err.println("\ntimeout is not a number");
204                 printHelp(cmdLineOptions);
205                 System.err.println();
206                 System.exit(1);
207             }
208         }
209         if (cmd.hasOption('u')) {
210             userTransactionName = cmd.getOptionValue('u');
211         }
212
213         verbose("UserTransaction Name =" + userTransactionName);
214         verbose("TransactionManager Name =" + transactionManagerName);
215         verbose("Transaction factory =" + (remote ? "remote" : "local"));
216         verbose("Default transaction timeout =" + timeout);
217
218         TraceTimer.setLogWriter(logWriter);
219         TraceTimer.setVerbose(verbose);
220         TraceTimer.setDebug(debug);
221
222         try {
223             // create an instance of JOTM
224
jotm = new Jotm(!remote, true);
225         } catch (NamingException JavaDoc e) {
226             System.out.println("unable to start JOTM!: "+ e.getMessage());
227             System.exit(1);
228         }
229
230         // trap program termination
231
Runtime.getRuntime().addShutdownHook(new Main());
232
233         try {
234             ictx = new InitialContext JavaDoc();
235         } catch (NamingException JavaDoc e) {
236             System.err.println("No initial context: " + e.getExplanation());
237             e.printStackTrace();
238             System.exit(1);
239         }
240
241         try {
242             if (userTransactionName != null) {
243                 ictx.rebind(userTransactionName, jotm.getUserTransaction());
244                 System.out.println("UserTransaction object bound in JNDI with name " + userTransactionName);
245             }
246             if (transactionManagerName != null) {
247                 ictx.rebind(transactionManagerName, jotm.getTransactionManager());
248                 System.out.println("TransactionManager object bound in JNDI with name " + transactionManagerName);
249             }
250         } catch (NamingException JavaDoc e) {
251             System.err.println("UserTransaction rebind failed :" + e.getExplanation());
252             e.printStackTrace();
253             checkRegistryMessage();
254             System.exit(1);
255         }
256
257         System.out.print(progname + " is running...");
258     }
259 }
Popular Tags