KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > xmlimporter > TransactionHandler


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

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 /**
21  * This class parses the TML code and calls the appropriate methods
22  * in TransactionManager TemporarayNodeManager org.mmabse.module.core
23  * Furthermore is does some nameserving.
24  *
25  * @author John Balder: 3MPS
26  * @author Rob Vermeulen: VPRO
27  * @author Rob van Maris: Finnalist IT Group
28  * @author Erik Visser: Finnalist IT Group
29  * @since MMBase-1.5
30  * @version $Id: TransactionHandler.java,v 1.10 2006/05/15 10:14:34 andre Exp $
31  */

32
33 public class TransactionHandler extends Module {
34    /** Logger instance. */
35    private static Logger log = Logging.getLoggerInstance(TransactionHandler.class.getName());
36
37    /** Current version number. */
38    private static String JavaDoc version="1.12.2001";
39
40    /** Hashtable to store a UserTransactionInfo object for each user. */
41    private static Hashtable transactionsPerUser = new Hashtable();
42
43    /** Hashtable to store a UserTransactionInfo object for each user. */
44    private static Consultant consultant;
45
46    /** XML file header, consisting of XML and DOCTYPE declaration. */
47    private final String JavaDoc 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    /**
54     * Create new TransactionHandler.
55     */

56    public TransactionHandler() {}
57
58    /**
59     * This method is called on loading of the transactionhandler module
60     * and writes a log entry that the TransactionHandler is loaded.
61     */

62    public void onload(){
63       log.info("Module TransactionHandler ("+version+") loaded.");
64    }
65
66    /**
67     * Initialize the transactionhandler module.
68     */

69    public void init(){
70       log.service("Module TransactionHandler ("+version+") started.");
71    }
72
73    private String JavaDoc getTime() {
74       return new Date().toString();
75    }
76
77    /**
78     * Get transaction info for this user.
79     * A UserTransactionInfo object is stored for each user,
80     * holding information on the transactions held by that user.
81     * Use this method to access it.
82     * @param user the user.
83     */

84    private UserTransactionInfo userInfo(String JavaDoc user) {
85       if (!transactionsPerUser.containsKey(user)) {
86          if (log.isDebugEnabled()) {
87             log.debug("Create UserTransactionInfo for user "+user);
88          }
89          // make acess to all variables indexed by user;
90
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    /**
100     * starts handleTransaction(java.io.Reader input) in a seperate thread
101     * @param input input
102     * @param consultant The intermediate import object. Used to set and get status from and set and get objects to and from.
103     */

104    public void handleTransactionAsynchronously(final java.io.Reader JavaDoc input,
105    final org.mmbase.applications.xmlimporter.Consultant consultant) {
106       //anonymous inner class
107
Thread JavaDoc t = new Thread JavaDoc() {
108          public void run() {
109             handleTransaction(input, consultant);
110          }
111       };
112       t.start();
113    }
114
115    /**
116     * parses transactions xml file delivered by the reader and executes the TCP commands.
117     * @param input the connection to the input source
118     * @param consultant The intermediate import object. Used to set and get status from and set and get objects to and from.
119     */

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 JavaDoc sb = new StringBuffer JavaDoc();
128             String JavaDoc s;
129             BufferedReader in = new BufferedReader(input);
130             while((s=in.readLine()) != null) {
131                sb.append(s);
132             }
133             String JavaDoc 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       // Get user transactions info object.
143
uti = new UserTransactionInfo();
144       uti.user = new User("automaticUser");
145
146       log.debug("Transactions started..");
147
148       // Parse template.
149
try {
150          // Parse.
151
consultant.setImportStatus(Consultant.IMPORT_RUNNING);
152          parser = new TransactionsParser(uti, consultant);
153          parser.parse(input);
154          consultant.setImportStatus(Consultant.IMPORT_FINISHED);
155       } catch (Exception JavaDoc 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    /**
166     * Performs JB key test. Compares key with TransactionHandler keycode,
167     * logs message and throws exception when key rejected, depending
168     * on TransactionHandler security mode.
169     * @param key The key provided with the transactions.
170     * @throws TransactionHandlerException When the key is rejected,
171     * while in secure mode.
172     */

173    void checkKey(String JavaDoc key) throws TransactionHandlerException {
174        // JB key test, only if there was a key defined in transactionhandler.xml
175
String JavaDoc keycode = getInitParameter("keycode");
176        String JavaDoc mode = getInitParameter("security");
177        if (keycode != null) {
178
179            if (key == null || !key.equals(keycode)) {
180                // Keycode rejected.
181
String JavaDoc message = "Transaction (TCP) key is incorrect."
182                + " TCP key='" + key + "' Server TCP key='"
183                + getInitParameter("keycode") + "'";
184
185                // No mode specified: do nothing.
186
if (mode == null) {
187                    return;
188
189                // Signal mode: just note in log.
190
} else if (mode.equals("signal")) {
191                    log.info(message);
192
193                // Secure mode: throw exception
194
} 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