-- added fancybox 1.3.1 to project, initially for showing screenshots. See documentat...
[Bookkeeping.git] / src / com / interrupt / bookkeeping / cc / bkell / Bkell.java
blob70d076348e737346fabad03fa5beaf6d6e6e2e2b
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() throws IOException {
127 ((PipedInputStream)inputStream).close();
128 this.getExpVisitor().setActive(false);
132 public void run() throws BkellException {
135 while(expVisitor.isActive()) {
137 logger.debug("bkell > ");
138 try {
140 //** parse the syntax and provide the MODEL
141 Parser parser = new Parser(new Lexer(pbreader) {
143 Logger logger = Logger.getLogger(Lexer.class);
144 protected void filter() {
145 logger.debug(token.getClass() + ", state : " + state.id() + ", text : [" + token.getText() + "]");
147 });
149 logger.debug("bkell > 1");
151 // parse the input
152 Start tree = parser.parse();
154 logger.debug("bkell > 2");
156 // apply the Visitor
157 tree.apply(expVisitor);
159 logger.debug("bkell > 3");
161 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
164 logger.debug("bkell > 4 > expVisitor.isActive ["+ expVisitor.isActive() +"]");
167 catch(java.io.IOException e) {
169 logger.error(e.getMessage(), e);
170 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
171 logger.error(bke.getLogMessages().toXML(false));
173 this.setBkellException(bke);
174 this.fillInPreviousCommandResult(bke.getLogMessages());
176 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
179 catch(LexerException e) {
181 logger.error(e.getMessage(), e);
182 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
183 logger.error(bke.getLogMessages().toXML(false));
185 this.setBkellException(bke);
186 this.fillInPreviousCommandResult(bke.getLogMessages());
188 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
191 catch(ParserException e) {
193 logger.error(e.getMessage(), e);
194 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
195 logger.error(bke.getLogMessages().toXML(false));
197 this.setBkellException(bke);
198 this.fillInPreviousCommandResult(bke.getLogMessages());
200 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
203 catch(AuthorisationException e) {
205 logger.error(e.getMessage(), e);
206 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
207 logger.error(bke.getLogMessages().toXML(false));
209 this.setBkellException(bke);
210 this.fillInPreviousCommandResult(bke.getLogMessages());
212 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
215 catch(BkellException e) {
217 System.out.println("Here... ["+ e.getLogMessages().toXML(false) +"]");
219 this.setBkellException(e);
220 this.fillInPreviousCommandResult(e.getLogMessages());
222 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
225 catch(Exception e) {
227 logger.error(e.getMessage(), e);
228 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
229 logger.error(bke.getLogMessages().toXML(false));
231 this.setBkellException(bke);
232 this.fillInPreviousCommandResult(bke.getLogMessages());
234 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
237 catch(Throwable e) {
239 logger.error(e.getMessage(), e);
240 BkellException bke = com.interrupt.bookkeeping.util.Util.generateBkellException(e);
241 logger.error(bke.getLogMessages().toXML(false));
243 this.setBkellException(bke);
244 this.fillInPreviousCommandResult(bke.getLogMessages());
246 synchronized(this) { this.notify(); //** if any thread is waiting on this thread's monitor, wake them up
252 logger.debug("bkell no longer active. EXITing");
253 this.notify();
258 private void fillInPreviousCommandResult(IBob bob) {
260 AXmlCommandInputRef replacorReference = new AXmlCommandInputRef();
261 replacorReference.setReference(bob);
262 this.expVisitor.setPreviousCommandResult(replacorReference);
265 public static void main(String args[]) {
268 Logger logger = Logger.getLogger(Bkell.class);
269 logger.debug("bkell >");
271 System.getProperties().setProperty(com.interrupt.bob.util.Util.DEF, BookkeepingSystemProperties.instance().getProperty("bob.def"));
272 System.getProperties().setProperty(com.interrupt.bob.util.Util.BASE, ".");
273 System.getProperties().setProperty(com.interrupt.bob.util.Util.END, ".xml");
275 ISystem system =
276 (ISystem)Bob.loadS(
277 Bkell.class.getResourceAsStream("/bookkeeping.system.xml"),
278 BookkeepingSystemProperties.instance().getProperty("bob.def") );
280 //logger.debug("Loaded Bookkkeeping system > "+ system.toXML(false));
282 Bkell bkell = null;
283 try {
284 bkell = (Bkell)((com.interrupt.bookkeeping.System)system).findBkellById("bkell.main");
286 catch(Exception e) {
287 e.printStackTrace();
289 bkell.initialise();
292 //**
293 BkellUncaughtExceptionHandler ehandler = new BkellUncaughtExceptionHandler();
294 Thread bkellThread = new Thread(bkell);
295 bkellThread.setDefaultUncaughtExceptionHandler(ehandler);
296 try {
298 bkellThread.start();
300 // 'join()' is a block method for the thread
301 bkellThread.join();
303 catch(BkellException e) {
305 ILogs logs = e.getLogMessages();
306 logger.error(logs.toXML(false));
308 e.printStackTrace();
310 catch(InterruptedException e) {
311 e.printStackTrace();
313 System.exit(0);