sync
[Bookkeeping.git] / src / com / interrupt / bookkeeping / cc / bkell / Bkell.java
blob5511a580a73ba4d7508edf108eb06a1fbe2fc9f3
1 package com.interrupt.bookkeeping.cc.bkell;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.PipedInputStream;
8 import java.io.PipedOutputStream;
9 import java.io.PushbackReader;
10 import java.io.Reader;
11 import java.io.Serializable;
12 import java.lang.Thread.UncaughtExceptionHandler;
13 import java.lang.reflect.InvocationTargetException;
15 import org.apache.log4j.Logger;
16 import com.interrupt.bob.base.IBob;
17 import com.interrupt.bob.base.IVisitor;
19 import com.interrupt.bob.base.Bob;
20 import com.interrupt.bookkeeping.ISystem;
21 import com.interrupt.bookkeeping.cc.ExpressionVisitor;
22 import com.interrupt.bookkeeping.cc.bkell.aauth.Aauthentication;
23 import com.interrupt.bookkeeping.cc.lexer.Lexer;
24 import com.interrupt.bookkeeping.cc.lexer.LexerException;
25 import com.interrupt.bookkeeping.cc.node.AXmlCommandInputRef;
26 import com.interrupt.bookkeeping.cc.node.Start;
27 import com.interrupt.bookkeeping.cc.parser.Parser;
28 import com.interrupt.bookkeeping.cc.parser.ParserException;
29 import com.interrupt.bookkeeping.exception.AuthorisationException;
30 import com.interrupt.bookkeeping.exception.BkellException;
31 import com.interrupt.bookkeeping.exception.BkellUncaughtExceptionHandler;
32 import com.interrupt.bookkeeping.system.BookkeepingSystem;
33 import com.interrupt.bookkeeping.system.BookkeepingSystemProperties;
34 import com.interrupt.bookkeeping.users.IUserSession;
35 import com.interrupt.logs.GLog;
36 import com.interrupt.logs.GLogMessage;
37 import com.interrupt.logs.GLogMessages;
38 import com.interrupt.logs.GLogs;
39 import com.interrupt.logs.ILog;
40 import com.interrupt.logs.ILogMessage;
41 import com.interrupt.logs.ILogMessages;
42 import com.interrupt.logs.ILogs;
43 import com.interrupt.spittoon.Spittoon;
45 public class Bkell extends GBkell implements Runnable, Serializable {
48 public static final String BKELL_NAME = "bookkeeping.shell";
49 private Logger logger = Logger.getLogger(Bkell.class);
50 private transient InputStream inputStream = null;
51 private BkellException bkellException = null;
53 private ExpressionVisitor expVisitor = null;
54 private PushbackReader pbreader = null;
56 public Bkell() {
58 inputStream = System.in;
59 pbreader = new PushbackReader( new InputStreamReader(inputStream), 1024 );
63 public BkellException getBkellException() { return bkellException; }
64 public void setBkellException(BkellException bkellException) { this.bkellException = bkellException; }
67 // inputStream
68 public InputStream getInputStream() {
69 return inputStream;
71 public void setInputStream(InputStream inputStream) {
72 this.inputStream = inputStream;
73 pbreader = new PushbackReader( new InputStreamReader(this.inputStream), 1024 );
76 // pushbackReader
77 public void updatePushbackReader(Reader reader) {
78 pbreader = new PushbackReader( reader, 1024 );
81 public synchronized void connectPipe(PipedOutputStream postream) throws IOException {
82 ((PipedInputStream)inputStream).connect(postream);
85 // expression visitor
86 public ExpressionVisitor getExpVisitor() {
87 return expVisitor;
89 public void setExpVisitor(ExpressionVisitor expVisitor) {
90 this.expVisitor = expVisitor;
91 this.expVisitor.setBkellEnv(this);
95 public IUserSession getUserSession() {
96 return this.getExpVisitor().getUserSession();
98 public void setUserSession(IUserSession userSession) {
99 this.getExpVisitor().setUserSession(userSession);
103 // previousCommandResult
104 public IBob getPreviousCommandResult() {
105 return this.expVisitor.getPreviousCommandResult().getReference();
109 public void initialise() {
111 logger.debug("Bkell.initialise() CALLED");
112 com.interrupt.spittoon.Spittoon spittoon = new com.interrupt.spittoon.Spittoon();
113 spittoon.initialise();
115 com.interrupt.bookkeeping.System system = (com.interrupt.bookkeeping.System)spittoon.
116 retrieve(spittoon.getAauthDbUrl(),
117 "/system[ @id='main.system' ]", true);
119 Aauthentication aauthentication = system.getAauthentication();
120 expVisitor = new ExpressionVisitor(spittoon);
121 expVisitor.setAauthentication(aauthentication);
122 expVisitor.setBkellEnv(this);
125 public void shutdown() {}
126 public void run() throws BkellException {
129 while(expVisitor.isActive()) {
131 logger.debug("bkell > ");
132 try {
134 //** parse the syntax and provide the MODEL
135 Parser parser = new Parser(new Lexer(pbreader) {
137 Logger logger = Logger.getLogger(Lexer.class);
138 protected void filter() {
139 logger.debug(token.getClass() + ", state : " + state.id() + ", text : [" + token.getText() + "]");
141 });
143 logger.debug("bkell > 1");
145 // parse the input
146 Start tree = parser.parse();
148 logger.debug("bkell > 2");
150 // apply the Visitor
151 tree.apply(expVisitor);
153 logger.debug("bkell > 3");
155 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
158 logger.debug("bkell > 4");
161 catch(java.io.IOException e) {
163 logger.error(e.getMessage(), e);
164 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
165 logger.error(bke.getLogMessages().toXML(false));
167 this.setBkellException(bke);
168 this.fillInPreviousCommandResult(bke.getLogMessages());
170 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
173 catch(LexerException e) {
175 logger.error(e.getMessage(), e);
176 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
177 logger.error(bke.getLogMessages().toXML(false));
179 this.setBkellException(bke);
180 this.fillInPreviousCommandResult(bke.getLogMessages());
182 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
185 catch(ParserException e) {
187 logger.error(e.getMessage(), e);
188 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
189 logger.error(bke.getLogMessages().toXML(false));
191 this.setBkellException(bke);
192 this.fillInPreviousCommandResult(bke.getLogMessages());
194 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
197 catch(AuthorisationException e) {
199 logger.error(e.getMessage(), e);
200 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
201 logger.error(bke.getLogMessages().toXML(false));
203 this.setBkellException(bke);
204 this.fillInPreviousCommandResult(bke.getLogMessages());
206 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
209 catch(BkellException e) {
211 System.out.println("Here... ["+ e.getLogMessages().toXML(false) +"]");
213 this.setBkellException(e);
214 this.fillInPreviousCommandResult(e.getLogMessages());
216 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
219 catch(Exception e) {
221 logger.error(e.getMessage(), e);
222 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
223 logger.error(bke.getLogMessages().toXML(false));
225 this.setBkellException(bke);
226 this.fillInPreviousCommandResult(bke.getLogMessages());
228 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
231 catch(Throwable e) {
233 logger.error(e.getMessage(), e);
234 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
235 logger.error(bke.getLogMessages().toXML(false));
237 this.setBkellException(bke);
238 this.fillInPreviousCommandResult(bke.getLogMessages());
240 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
249 private void fillInPreviousCommandResult(IBob bob) {
251 AXmlCommandInputRef replacorReference = new AXmlCommandInputRef();
252 replacorReference.setReference(bob);
253 this.expVisitor.setPreviousCommandResult(replacorReference);
256 public static void main(String args[]) {
259 Logger logger = Logger.getLogger(Bkell.class);
260 logger.debug("bkell >");
262 System.getProperties().setProperty(com.interrupt.bob.util.Util.DEF, BookkeepingSystemProperties.instance().getProperty("bob.def"));
263 System.getProperties().setProperty(com.interrupt.bob.util.Util.BASE, ".");
264 System.getProperties().setProperty(com.interrupt.bob.util.Util.END, ".xml");
266 ISystem system =
267 (ISystem)Bob.loadS(
268 Bkell.class.getResourceAsStream("/bookkeeping.system.xml"),
269 BookkeepingSystemProperties.instance().getProperty("bob.def") );
271 //logger.debug("Loaded Bookkkeeping system > "+ system.toXML(false));
273 Bkell bkell = null;
274 try {
275 bkell = (Bkell)((com.interrupt.bookkeeping.System)system).findBkellById("bkell.main");
277 catch(Exception e) {
278 e.printStackTrace();
280 bkell.initialise();
283 //**
284 BkellUncaughtExceptionHandler ehandler = new BkellUncaughtExceptionHandler();
285 Thread bkellThread = new Thread(bkell);
286 bkellThread.setDefaultUncaughtExceptionHandler(ehandler);
287 try {
289 bkellThread.start();
291 // 'join()' is a block method for the thread
292 bkellThread.join();
294 catch(BkellException e) {
296 ILogs logs = e.getLogMessages();
297 logger.error(logs.toXML(false));
299 e.printStackTrace();
301 catch(InterruptedException e) {
302 e.printStackTrace();
304 System.exit(0);