1 50 package org.objectweb.jotm; 51 52 import java.io.PrintWriter ; 53 import java.util.Enumeration ; 54 import java.util.Hashtable ; 55 56 import javax.naming.Context ; 57 import javax.naming.InitialContext ; 58 import javax.naming.NamingException ; 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 75 public class Main extends Thread { 76 77 private static final String progname = "JOTM"; 78 79 private static Options cmdLineOptions = null; 81 82 static boolean verbose = false; 84 static boolean debug = false; 86 static int timeout = 0; 88 static boolean remote = false; 90 static String userTransactionName = null; 92 static String transactionManagerName = null; 94 static Context ictx = null; 96 static PrintWriter logWriter = new PrintWriter (System.out, true); 98 99 private static TMService jotm; 101 102 105 public void run() { 106 System.out.print("shutting down..."); 107 try { 108 ictx.unbind(userTransactionName); 109 } catch (Exception e) { 110 } 112 try { 113 ictx.unbind(transactionManagerName); 114 } catch (Exception e) { 115 } 117 try { 119 jotm.stop(); 120 } catch (Exception e) { 121 } 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 } 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 163 public static void main(String [] args) { 164 165 cmdLineOptions = new Options(); 166 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 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 jotm = new Jotm(!remote, true); 225 } catch (NamingException e) { 226 System.out.println("unable to start JOTM!: "+ e.getMessage()); 227 System.exit(1); 228 } 229 230 Runtime.getRuntime().addShutdownHook(new Main()); 232 233 try { 234 ictx = new InitialContext (); 235 } catch (NamingException 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 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 |