adding algorithm debug statments. fixing / improving build
[Bookkeeping.git] / src / com / interrupt / bookkeeping / http / BookkeepingSystemFacade.java
blob2c51e4144cb24017235a235d482961aeb36c437a
1 package com.interrupt.bookkeeping.http;
4 import java.io.ByteArrayInputStream;
5 import java.io.File;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.PipedInputStream;
9 import java.io.PipedOutputStream;
10 import java.io.PrintWriter;
11 import java.io.StringBufferInputStream;
12 import java.io.StringReader;
14 import javax.servlet.ServletException;
15 import javax.servlet.ServletRequest;
16 import javax.servlet.http.Cookie;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletRequestWrapper;
19 import javax.servlet.http.HttpServletResponse;
20 import javax.servlet.http.HttpSession;
22 import org.apache.log4j.Logger;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Queue;
29 import java.util.concurrent.BlockingQueue;
30 import java.util.concurrent.LinkedBlockingQueue;
32 import com.interrupt.bob.base.Bob;
33 import com.interrupt.bob.base.IBob;
34 import com.interrupt.bookkeeping.ISystem;
36 import com.interrupt.bookkeeping.account.IAccount;
37 import com.interrupt.bookkeeping.account.IAccounts;
39 import com.interrupt.bookkeeping.journal.IJournal;
40 import com.interrupt.bookkeeping.journal.IJournals;
41 import com.interrupt.bookkeeping.journal.IEntry;
42 import com.interrupt.bookkeeping.journal.IEntries;
44 import com.interrupt.bookkeeping.cc.bkell.Bkell;
45 import com.interrupt.bookkeeping.cc.bkell.command.AddCommand;
46 import com.interrupt.bookkeeping.cc.bkell.command.GOption;
47 import com.interrupt.bookkeeping.cc.bkell.command.GOptions;
48 import com.interrupt.bookkeeping.cc.bkell.command.GResult;
49 import com.interrupt.bookkeeping.cc.bkell.command.GToken;
50 import com.interrupt.bookkeeping.cc.bkell.command.ICommand;
51 import com.interrupt.bookkeeping.cc.bkell.command.IResult;
52 import com.interrupt.bookkeeping.cc.bkell.command.ITokens;
53 import com.interrupt.bookkeeping.cc.bkell.command.ListCommand;
54 import com.interrupt.bookkeeping.http.ServletInputParams;
55 import com.interrupt.bookkeeping.system.BookkeepingSystem;
56 import com.interrupt.bookkeeping.system.BookkeepingSystemProperties;
57 import com.interrupt.util.IdGenerator;
59 public class BookkeepingSystemFacade {
62 public static final String FACADE_NAME = "system.facade";
63 private Logger logger = Logger.getLogger(BookkeepingSystemFacade.class);
64 private BookkeepingSystem bsystem = null;
65 private static BookkeepingSystemFacade instance = null;
67 private Thread bkellThread = null;
68 private Bkell bkell = null;
70 private InputStream inputStream = null;
71 private PipedOutputStream poutputStream = null;
74 public BookkeepingSystemFacade() {
76 //** initialise the Bookkeeping system including Spittoon
77 bsystem = BookkeepingSystem.instance();
79 poutputStream = new PipedOutputStream();
80 inputStream = new PipedInputStream();
84 public Bkell getBkell() {
85 return bkell;
87 public void setBkell(Bkell bkell) {
88 this.bkell = bkell;
92 /*public static BookkeepingSystemFacade instance() {
94 if(instance == null) {
96 instance = new BookkeepingSystemFacade();
97 instance.initialise();
99 return instance;
104 public void initialise() {
106 System.getProperties().setProperty(com.interrupt.bob.util.Util.DEF, BookkeepingSystemProperties.instance().getProperty("bob.def"));
107 System.getProperties().setProperty(com.interrupt.bob.util.Util.BASE, ".");
108 System.getProperties().setProperty(com.interrupt.bob.util.Util.END, ".xml");
110 ISystem bkellSystem =
111 (ISystem)Bob.loadS(
112 Bkell.class.getResourceAsStream("/bookkeeping.system.xml"),
113 BookkeepingSystemProperties.instance().getProperty("bob.def") );
116 try {
118 //** loading bkell state
119 bkell = (Bkell)bkellSystem.findBkellById("bkell.main");
120 bkell.initialise();
121 bkell.setInputStream(inputStream);
122 bkell.connectPipe(poutputStream);
124 bkellThread = new Thread(bkell);
125 bkellThread.start();
127 logger.debug(">>> Printing ISystem");
128 logger.debug(bkellSystem.toXML());
130 logger.debug(">>> Printing IBkell");
131 logger.debug(bkell.toXML());
134 catch(IOException e) {
135 e.printStackTrace();
140 public void shutdown() throws IOException, InterruptedException {
142 /*poutputStream.write("exit;".getBytes());
143 synchronized(bkell) {
145 bkell.wait(); //** wait for a notification from this thread.
151 public synchronized void perform(HttpServletRequest req, HttpServletResponse resp)
152 throws ServletException, IOException, InterruptedException {
154 String cmdValue = req.getParameter("cmd");
155 String tokenValue = req.getParameter("token");
156 String dataValue = req.getParameter("data");
157 String idValue = req.getParameter("id");
159 // ** A session id should correspond to a user/group. This is better than
160 // explicitly specifying the user / group in the http request because
161 // a session id indicates that this person has been authenticated.
163 // ** If the session id is null and the bkexpr is to 'register' or 'login',
164 // then that is allowed
165 //String sessionIdValue = req.getParameter("sessionid");
166 String bkexpr = req.getParameter("bkexpr");
167 //bkexpr += "\\n";
169 //** BKell TODO list
170 // Actions (or expressions) now go through interface; which also specifies a session id (corresponding to a user/group
171 // Authenticating / Authenticated sessions
172 // creating user / groups
173 // EOF
174 logger.debug("");
175 logger.debug(">> cmd > " + cmdValue);
176 logger.debug(">> token > " + tokenValue);
177 logger.debug(">> data > " + dataValue);
178 logger.debug(">> id > " + idValue);
179 logger.debug("");
180 //logger.debug(">> session > " + req.getSession());
181 //logger.debug(">> timout > " + req.getSession().getMaxInactiveInterval());
183 logger.debug(" >> Session Validity > sessionFromCookie: "+ req.isRequestedSessionIdFromCookie() + " / " +
184 "sessionIdValue: "+ req.getRequestedSessionId() + " / " +
185 "sessionFromURL: "+ req.isRequestedSessionIdFromURL() + " / " +
186 "sessionIdValid: "+ req.isRequestedSessionIdValid());
188 logger.debug("");
189 logger.debug(">> bkexpr > " + bkexpr);
191 //ByteArrayInputStream sreader = new ByteArrayInputStream(bkexpr.getBytes());
192 //StringReader sreader = new StringReader(bkexpr);
193 //bkell.updatePushbackReader(sreader);
194 //bkell.run();
196 poutputStream.write(bkexpr.getBytes());
197 synchronized(bkell) {
198 bkell.wait(); //** wait for a notification from this thread.
201 //** return
202 String result = bkell.getPreviousCommandResult().toXML(false);
203 resp.setContentType("text/xml");
205 logger.debug("DONE BookkeepingSystemFacade");
206 logger.debug(result);
208 PrintWriter pwriter = new PrintWriter(resp.getOutputStream());
209 pwriter.println(result);
210 pwriter.close();