1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 $RCSfile: ScriptShell.java,v $
19 Created on $Date: 2003/09/06 08:46:35 $
20 */
21 package net.sf.bloof.script;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Vector;
30
31 import net.sf.bloof.script.events.ScriptAction;
32 import net.sf.bloof.script.events.ScriptEvent;
33
34 /***
35 *
36 * @author Lukasz Pekacki <pekacki@users.sourceforge.net>
37 * @version $Id: ScriptShell.java,v 1.1 2003/09/06 08:46:35 pekacki Exp $
38 */
39 public class ScriptShell extends AbstractShell implements Runnable {
40
41 /***
42 * @param aParams
43 */
44 public ScriptShell(HashMap aParams) {
45 super(aParams);
46 mInputStream = (InputStream) aParams.get(PARAM_INPUT_STREAM);
47 }
48
49 /***
50 * @see net.sf.bloof.shell.AbstractShell#shellEventOccured(net.sf.bloof.shell.events.ShellEvent)
51 */
52 public synchronized void scriptEventOccured(ScriptEvent bE) {
53 HashMap params = bE.getParams();
54 switch (bE.getEventType()) {
55 case ScriptEvent.TYPE_NEW_METRIC_RUN :
56 {
57 break;
58 }
59 case ScriptEvent.TYPE_PROJECT_CHANGED :
60 {
61 break;
62 }
63 case ScriptEvent.TYPE_ERROR :
64 {
65 String reason = (String) params.get(ScriptEvent.PARAM_OBJECT);
66 System.err.println("Error occured. Reason:" + reason);
67 break;
68 }
69 case ScriptEvent.TYPE_EXIT :
70 {
71 mRunning = false;
72 break;
73 }
74 case ScriptEvent.TYPE_MESSAGE :
75 {
76 String message = (String) params.get(ScriptEvent.PARAM_OBJECT);
77 System.out.println(message);
78 break;
79 }
80 }
81 mCurrentAcionProcessed = true;
82 synchronized (this) {
83 notifyAll();
84 }
85
86
87 }
88
89 /* (non-Javadoc)
90 * @see java.lang.Runnable#run()
91 */
92 public void run() {
93 Vector actions = getActions();
94 for (Iterator iter = actions.iterator(); iter.hasNext();) {
95 if (!mRunning) {
96 return;
97 }
98 ScriptAction action = (ScriptAction) iter.next();
99 informScriptActionListeners(action);
100
101 while (!mCurrentAcionProcessed) {
102 synchronized (this) {
103 try {
104 wait();
105 } catch (InterruptedException e) {
106 e.printStackTrace();
107 }
108 }
109
110 }
111 mCurrentAcionProcessed = false;
112 }
113 System.out.println("Finished script processing.");
114 }
115
116 private Vector getActions() {
117 Vector v = new Vector();
118 BufferedReader lineReader = new BufferedReader(new InputStreamReader(mInputStream));
119 try {
120 int avail = mInputStream.available();
121 if (avail == 0) {
122 return v;
123 }
124 } catch (IOException e2) {
125 Main.fail("Could not analyse InputStream.");
126 }
127 String line = null;
128 while (mRunning) {
129 try {
130 line = lineReader.readLine();
131 if (line == null) {
132 return v;
133 }
134 } catch (IOException e) {
135 informScriptActionListeners(
136 new ScriptAction(
137 ScriptAction.ERROR,
138 ScriptAction.PARAM_OBJECT,
139 "Error reading console input. " + e.toString()));
140 }
141 try {
142 ScriptAction commandAction = CommandParser.parse(line);
143 v.add(commandAction);
144 } catch (CommandException e1) {
145 informScriptActionListeners(
146 new ScriptAction(
147 ScriptAction.ERROR,
148 ScriptAction.PARAM_OBJECT,
149 "Error parsing command. " + line + " Reason:" + e1.toString()));
150 }
151 }
152 return v;
153 }
154
155 public static final String PARAM_INPUT_STREAM = "input stream";
156 private InputStream mInputStream;
157 private boolean mRunning = true, mCurrentAcionProcessed = false;
158 }
This page was automatically generated by Maven