1 10 package org.mmbase.applications.xmlimporter; 11 12 import java.util.*; 13 import java.io.*; 14 15 import org.mmbase.module.Module; 16 import org.mmbase.module.sessionInfo; 17 18 import org.mmbase.util.logging.*; 19 20 32 33 public class TransactionHandler extends Module { 34 35 private static Logger log = Logging.getLoggerInstance(TransactionHandler.class.getName()); 36 37 38 private static String version="1.12.2001"; 39 40 41 private static Hashtable transactionsPerUser = new Hashtable(); 42 43 44 private static Consultant consultant; 45 46 47 private final String xmlHeader = 48 "<?xml version='1.0'?>\n" 49 + "<!DOCTYPE transactions " 50 + "PUBLIC '-//MMBase/DTD transactions config 1.0//EN' " 51 + "'http://www.mmbase.org/dtd/transactions_1_0.dtd'>\n"; 52 53 56 public TransactionHandler() {} 57 58 62 public void onload(){ 63 log.info("Module TransactionHandler ("+version+") loaded."); 64 } 65 66 69 public void init(){ 70 log.service("Module TransactionHandler ("+version+") started."); 71 } 72 73 private String getTime() { 74 return new Date().toString(); 75 } 76 77 84 private UserTransactionInfo userInfo(String user) { 85 if (!transactionsPerUser.containsKey(user)) { 86 if (log.isDebugEnabled()) { 87 log.debug("Create UserTransactionInfo for user "+user); 88 } 89 UserTransactionInfo uti = new UserTransactionInfo(); 91 transactionsPerUser.put(user, uti); 92 uti.user = new User(user); 93 } else { 94 log.warn("UserTransactionInfo already known for user "+user); 95 } 96 return ((UserTransactionInfo) transactionsPerUser.get(user)); 97 } 98 99 104 public void handleTransactionAsynchronously(final java.io.Reader input, 105 final org.mmbase.applications.xmlimporter.Consultant consultant) { 106 Thread t = new Thread () { 108 public void run() { 109 handleTransaction(input, consultant); 110 } 111 }; 112 t.start(); 113 } 114 115 120 public void handleTransaction(Reader input, Consultant consultant) { 121 UserTransactionInfo uti; 122 TransactionsParser parser = null; 123 124 log.service("TransactionHandler processing TCP"); 125 if (log.isDebugEnabled()) { 126 try{ 127 StringBuffer sb = new StringBuffer (); 128 String s; 129 BufferedReader in = new BufferedReader(input); 130 while((s=in.readLine()) != null) { 131 sb.append(s); 132 } 133 String template = sb.toString(); 134 log.debug("Received template is:"); 135 log.debug(template); 136 input = new StringReader(template); 137 } catch (IOException e) { 138 log.error("TransactionError " + e.toString()); 139 } 140 } 141 142 uti = new UserTransactionInfo(); 144 uti.user = new User("automaticUser"); 145 146 log.debug("Transactions started.."); 147 148 try { 150 consultant.setImportStatus(Consultant.IMPORT_RUNNING); 152 parser = new TransactionsParser(uti, consultant); 153 parser.parse(input); 154 consultant.setImportStatus(Consultant.IMPORT_FINISHED); 155 } catch (Exception e) { 156 log.error("TransactionError :" + e.toString()); 157 log.error("ExceptionPage "+(parser!=null?parser.getExceptionPage():"")); 158 if (consultant.getImportStatus() != Consultant.IMPORT_TIMED_OUT) { 159 consultant.setImportStatus(Consultant.IMPORT_EXCEPTION); 160 } 161 } 162 log.warn("Transaction stopped at : " + getTime()); 163 } 164 165 173 void checkKey(String key) throws TransactionHandlerException { 174 String keycode = getInitParameter("keycode"); 176 String mode = getInitParameter("security"); 177 if (keycode != null) { 178 179 if (key == null || !key.equals(keycode)) { 180 String message = "Transaction (TCP) key is incorrect." 182 + " TCP key='" + key + "' Server TCP key='" 183 + getInitParameter("keycode") + "'"; 184 185 if (mode == null) { 187 return; 188 189 } else if (mode.equals("signal")) { 191 log.info(message); 192 193 } else if (mode.equals("secure")) { 195 log.error(message); 196 TransactionHandlerException te 197 = new TransactionHandlerException(message); 198 throw te; 199 } 200 } 201 } 202 } 203 204 } 205 | Popular Tags |