Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 162415 | Differences between
and this patch

Collapse All | Expand All

(-)eclipseAdaptor/src/org/eclipse/core/runtime/adaptor/EclipseStarter.java (-3 / +6 lines)
Lines 11-17 Link Here
11
package org.eclipse.core.runtime.adaptor;
11
package org.eclipse.core.runtime.adaptor;
12
12
13
import java.io.*;
13
import java.io.*;
14
import java.lang.reflect.*;
14
import java.lang.reflect.Constructor;
15
import java.lang.reflect.Method;
15
import java.net.*;
16
import java.net.*;
16
import java.security.CodeSource;
17
import java.security.CodeSource;
17
import java.security.ProtectionDomain;
18
import java.security.ProtectionDomain;
Lines 20-27 Link Here
20
import org.eclipse.core.runtime.internal.stats.StatsManager;
21
import org.eclipse.core.runtime.internal.stats.StatsManager;
21
import org.eclipse.osgi.framework.adaptor.FilePath;
22
import org.eclipse.osgi.framework.adaptor.FilePath;
22
import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
23
import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
23
import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
24
import org.eclipse.osgi.framework.console.Console;
24
import org.eclipse.osgi.framework.internal.core.OSGi;
25
import org.eclipse.osgi.framework.internal.core.*;
25
import org.eclipse.osgi.framework.log.FrameworkLog;
26
import org.eclipse.osgi.framework.log.FrameworkLog;
26
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
27
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
27
import org.eclipse.osgi.internal.profile.Profile;
28
import org.eclipse.osgi.internal.profile.Profile;
Lines 31-36 Link Here
31
import org.eclipse.osgi.util.ManifestElement;
32
import org.eclipse.osgi.util.ManifestElement;
32
import org.eclipse.osgi.util.NLS;
33
import org.eclipse.osgi.util.NLS;
33
import org.osgi.framework.*;
34
import org.osgi.framework.*;
35
import org.osgi.framework.Constants;
34
import org.osgi.service.packageadmin.PackageAdmin;
36
import org.osgi.service.packageadmin.PackageAdmin;
35
import org.osgi.service.startlevel.StartLevel;
37
import org.osgi.service.startlevel.StartLevel;
36
import org.osgi.util.tracker.ServiceTracker;
38
import org.osgi.util.tracker.ServiceTracker;
Lines 302-307 Link Here
302
			if (stateStamp != adaptor.getState().getTimeStamp())
304
			if (stateStamp != adaptor.getState().getTimeStamp())
303
				logUnresolvedBundles(context.getBundles());
305
				logUnresolvedBundles(context.getBundles());
304
		running = true;
306
		running = true;
307
		context.registerService(Console.class.getName(), new ConsoleFactory(osgi), null);
305
		if (Profile.PROFILE && Profile.STARTUP)
308
		if (Profile.PROFILE && Profile.STARTUP)
306
			Profile.logExit("EclipseStarter.startup()"); //$NON-NLS-1$
309
			Profile.logExit("EclipseStarter.startup()"); //$NON-NLS-1$
307
		return context;
310
		return context;
(-)console/src/org/eclipse/osgi/framework/internal/core/FrameworkConsole.java (-3 / +1 lines)
Lines 36-43 Link Here
36
	protected OSGi osgi;
36
	protected OSGi osgi;
37
	/** The command line arguments passed at launch time*/
37
	/** The command line arguments passed at launch time*/
38
	protected String[] args;
38
	protected String[] args;
39
	/** The OSGi Command Provider */
40
	protected CommandProvider osgicp;
41
	/** A tracker containing the service object of all registered command providers */
39
	/** A tracker containing the service object of all registered command providers */
42
	protected ServiceTracker cptracker;
40
	protected ServiceTracker cptracker;
43
41
Lines 190-196 Link Here
190
		cptracker.open();
188
		cptracker.open();
191
189
192
		// register the OSGi command provider
190
		// register the OSGi command provider
193
		osgicp = new FrameworkCommandProvider(osgi);
191
		FrameworkCommandProvider.getInstance(osgi.framework);
194
192
195
	}
193
	}
196
194
(-)console/src/org/eclipse/osgi/framework/internal/core/ConsoleMessages.properties (+6 lines)
Lines 13-18 Link Here
13
CONSOLE_PROMPT=osgi> 
13
CONSOLE_PROMPT=osgi> 
14
CONSOLE_ID=id
14
CONSOLE_ID=id
15
CONSOLE_MORE=-- More...Press Enter to Continue...
15
CONSOLE_MORE=-- More...Press Enter to Continue...
16
CONSOLE_RUN=Exception starting the console thread.
16
CONSOLE_HELP_CONTROLLING_CONSOLE_HEADING=---Controlling the Console---
17
CONSOLE_HELP_CONTROLLING_CONSOLE_HEADING=---Controlling the Console---
17
CONSOLE_HELP_MORE=More prompt for console output
18
CONSOLE_HELP_MORE=More prompt for console output
18
CONSOLE_HELP_DISCONNECT=Disconnects from telnet session
19
CONSOLE_HELP_DISCONNECT=Disconnects from telnet session
Lines 152-154 Link Here
152
CONSOLE_REQUIRES_MESSAGE=[requires]
153
CONSOLE_REQUIRES_MESSAGE=[requires]
153
CONSOLE_HELP_PROFILE_HEADING=---Controlling the Profiling---
154
CONSOLE_HELP_PROFILE_HEADING=---Controlling the Profiling---
154
CONSOLE_HELP_PROFILELOG_DESCRIPTION=Display & flush the profile log messages
155
CONSOLE_HELP_PROFILELOG_DESCRIPTION=Display & flush the profile log messages
156
SOCKET_RUN=Exception listening for connections.
157
SOCKET_STREAM_UNKOWNHOST=UnknownHostException attempting to open a socket.
158
SOCKET_STREAM_EXCEPTION=Exception attempting to open a socket.
159
LOG_ENTRY=\!ENTRY 
160
LOG_MESSAGE=\!MESSAGE 
(-)console/src/org/eclipse/osgi/framework/internal/core/FrameworkCommandInterpreter.java (-256 / +19 lines)
Lines 11-24 Link Here
11
11
12
package org.eclipse.osgi.framework.internal.core;
12
package org.eclipse.osgi.framework.internal.core;
13
13
14
import java.io.*;
15
import java.lang.reflect.*;
16
import java.net.URL;
17
import java.util.*;
18
import org.eclipse.osgi.framework.console.CommandInterpreter;
14
import org.eclipse.osgi.framework.console.CommandInterpreter;
19
import org.eclipse.osgi.framework.console.CommandProvider;
15
20
import org.eclipse.osgi.util.NLS;
21
import org.osgi.framework.Bundle;
22
16
23
/**
17
/**
24
 * This class knows how to parse and execute the command line arguments to the FrameworkConsole.
18
 * This class knows how to parse and execute the command line arguments to the FrameworkConsole.
Lines 30-50 Link Here
30
 *
24
 *
31
 * FrameworkCommandInterpreter provides several print methods which handle the "More" command.
25
 * FrameworkCommandInterpreter provides several print methods which handle the "More" command.
32
 */
26
 */
33
public class FrameworkCommandInterpreter implements CommandInterpreter {
27
public class FrameworkCommandInterpreter extends BaseCommandInterpreter {
28
	/** Strings used to format other strings */
29
	private static final String tab = "\t"; //$NON-NLS-1$
34
30
35
	/** The command line in StringTokenizer form */
36
	private StringTokenizer tok;
37
	/** The active CommandProviders */
38
	private Object[] commandProviders;
39
	/** The FrameworkConsole */
40
	private FrameworkConsole con;
31
	private FrameworkConsole con;
41
	/** The stream to send output to */
42
	private PrintWriter out;
43
44
	/** Strings used to format other strings */
45
	private String tab = "\t"; //$NON-NLS-1$
46
	private String newline = "\r\n"; //$NON-NLS-1$
47
	private boolean firstCommand = true;
48
32
49
	/**
33
	/**
50
	 * The maximum number of lines to print without user prompt.
34
	 * The maximum number of lines to print without user prompt.
Lines 60-103 Link Here
60
	 *  the input parms.
44
	 *  the input parms.
61
	 */
45
	 */
62
	public FrameworkCommandInterpreter(String cmdline, Object[] commandProviders, FrameworkConsole con) {
46
	public FrameworkCommandInterpreter(String cmdline, Object[] commandProviders, FrameworkConsole con) {
63
		tok = new StringTokenizer(cmdline);
47
		super(cmdline, commandProviders, con.out);
64
		this.commandProviders = commandProviders;
65
		this.con = con;
48
		this.con = con;
66
		this.out = con.getWriter();
67
	}
49
	}
68
50
69
	/**
70
	 Get the next argument in the input.
71
	 
72
	 E.g. if the commandline is hello world, the _hello method
73
	 will get "world" as the first argument.
74
	 
75
	 @return A string containing the next argument on the command line
76
	 */
77
	public String nextArgument() {
78
		if (tok == null || !tok.hasMoreElements()) {
79
			return null;
80
		}
81
		String token = tok.nextToken();
82
		//check for quotes
83
		int index = token.indexOf('"');
84
		if (index != -1) {
85
			//if we only have one quote, find the second quote
86
			if (index == token.lastIndexOf('"')) {
87
				token += tok.nextToken("\""); //$NON-NLS-1$
88
			}
89
			StringBuffer buf = new StringBuffer(token);
90
91
			//strip quotes
92
			while (index != -1) {
93
				buf.deleteCharAt(index);
94
				token = buf.toString();
95
				index = token.indexOf('"');
96
			}
97
			return buf.toString();
98
		}
99
		return (token);
100
	}
101
51
102
	/**
52
	/**
103
	 Execute a command line as if it came from the end user.
53
	 Execute a command line as if it came from the end user.
Lines 111-119 Link Here
111
	 @return The object returned by the method executed.
61
	 @return The object returned by the method executed.
112
	 */
62
	 */
113
	public Object execute(String cmd) {
63
	public Object execute(String cmd) {
114
		if (!firstCommand)
115
			return innerExecute(cmd);
116
		firstCommand = false;
117
		resetLineCount();
64
		resetLineCount();
118
		Object retval = null;
65
		Object retval = null;
119
		// handle "more" command here
66
		// handle "more" command here
Lines 134-185 Link Here
134
			}
81
			}
135
			return retval;
82
			return retval;
136
		}
83
		}
137
		Class[] parameterTypes = new Class[] {CommandInterpreter.class};
84
		return super.execute(cmd);
138
		Object[] parameters = new Object[] {this};
139
		boolean executed = false;
140
		int size = commandProviders.length;
141
		for (int i = 0; !executed && (i < size); i++) {
142
			try {
143
				Object target = commandProviders[i];
144
				Method method = target.getClass().getMethod("_" + cmd, parameterTypes); //$NON-NLS-1$
145
				retval = method.invoke(target, parameters);
146
				executed = true; // stop after the command has been found
147
			} catch (NoSuchMethodException ite) {
148
				// keep going - maybe another command provider will be able to execute this command
149
			} catch (InvocationTargetException ite) {
150
				executed = true; // don't want to keep trying - we found the method but got an error
151
				printStackTrace(ite.getTargetException());
152
			} catch (Exception ee) {
153
				executed = true; // don't want to keep trying - we got an error we don't understand
154
				printStackTrace(ee);
155
			}
156
		}
157
		// if no command was found to execute, display help for all registered command providers
158
		if (!executed) {
159
			for (int i = 0; i < size; i++) {
160
				try {
161
					CommandProvider commandProvider = (CommandProvider) commandProviders[i];
162
					out.print(commandProvider.getHelp());
163
					out.flush();
164
				} catch (Exception ee) {
165
					printStackTrace(ee);
166
				}
167
			}
168
			// call help for the more command provided by this class
169
			out.print(getHelp());
170
			out.flush();
171
		}
172
		return retval;
173
	}
174
175
	private Object innerExecute(String cmd) {
176
		if (cmd != null && cmd.length() > 0) {
177
			CommandInterpreter intcp = new FrameworkCommandInterpreter(cmd, commandProviders, con);
178
			String command = intcp.nextArgument();
179
			if (command != null)
180
				return intcp.execute(command);
181
		}
182
		return null;
183
	}
85
	}
184
86
185
	/**
87
	/**
Lines 208-214 Link Here
208
		if (lines < 0) {
110
		if (lines < 0) {
209
			throw new IllegalArgumentException(ConsoleMsg.CONSOLE_LINES_TO_SCROLL_NEGATIVE_ERROR); 
111
			throw new IllegalArgumentException(ConsoleMsg.CONSOLE_LINES_TO_SCROLL_NEGATIVE_ERROR); 
210
		}
112
		}
211
212
		maxLineCount = lines;
113
		maxLineCount = lines;
213
	}
114
	}
214
115
Lines 220-376 Link Here
220
	}
121
	}
221
122
222
	/**
123
	/**
223
	 * Prints a string to the output medium (appended with newline character).
224
	 * <p>
225
	 * This method does not increment the line counter for the 'more' prompt.
226
	 *
227
	 * @param o the string to be printed
228
	 */
229
	private void printline(Object o) {
230
		print(o + newline);
231
	}
232
233
	/**
234
	 * Prints an object to the outputstream
235
	 *
236
	 * @param o	the object to be printed
237
	 */
238
	public void print(Object o) {
239
		synchronized (out) {
240
			check4More();
241
			out.print(o);
242
			out.flush();
243
		}
244
	}
245
246
	/**
247
	 * Prints a empty line to the outputstream
248
	 */
249
	public void println() {
250
		println(""); //$NON-NLS-1$
251
	}
252
253
	/**
254
	 * Print a stack trace including nested exceptions.
255
	 * @param t The offending exception
256
	 */
257
	public void printStackTrace(Throwable t) {
258
		t.printStackTrace(out);
259
260
		Method[] methods = t.getClass().getMethods();
261
262
		int size = methods.length;
263
		Class throwable = Throwable.class;
264
265
		for (int i = 0; i < size; i++) {
266
			Method method = methods[i];
267
268
			if (Modifier.isPublic(method.getModifiers()) && method.getName().startsWith("get") && throwable.isAssignableFrom(method.getReturnType()) && (method.getParameterTypes().length == 0)) { //$NON-NLS-1$
269
				try {
270
					Throwable nested = (Throwable) method.invoke(t, null);
271
272
					if ((nested != null) && (nested != t)) {
273
						out.println(ConsoleMsg.CONSOLE_NESTED_EXCEPTION);
274
						printStackTrace(nested);
275
					}
276
				} catch (IllegalAccessException e) {
277
				} catch (InvocationTargetException e) {
278
				}
279
			}
280
		}
281
	}
282
283
	/**
284
	 * Prints an object to the output medium (appended with newline character).
285
	 * <p>
286
	 * If running on the target environment, the user is prompted with '--more'
287
	 * if more than the configured number of lines have been printed without user prompt.
288
	 * This enables the user of the program to have control over scrolling.
289
	 * <p>
290
	 * For this to work properly you should not embed "\n" etc. into the string.
291
	 *
292
	 * @param	o	the object to be printed
293
	 */
294
	public void println(Object o) {
295
		if (o == null) {
296
			return;
297
		}
298
		synchronized (out) {
299
			check4More();
300
			printline(o);
301
			currentLineCount++;
302
			currentLineCount += o.toString().length() / 80;
303
		}
304
	}
305
306
	/**
307
	 * Prints the given dictionary sorted by keys.
308
	 *
309
	 * @param dic	the dictionary to print
310
	 * @param title	the header to print above the key/value pairs
311
	 */
312
	public void printDictionary(Dictionary dic, String title) {
313
		if (dic == null)
314
			return;
315
316
		int count = dic.size();
317
		String[] keys = new String[count];
318
		Enumeration keysEnum = dic.keys();
319
		int i = 0;
320
		while (keysEnum.hasMoreElements()) {
321
			keys[i++] = (String) keysEnum.nextElement();
322
		}
323
		Util.sort(keys);
324
325
		if (title != null) {
326
			println(title);
327
		}
328
		for (i = 0; i < count; i++) {
329
			println(" " + keys[i] + " = " + dic.get(keys[i])); //$NON-NLS-1$//$NON-NLS-2$
330
		}
331
		println();
332
	}
333
334
	/**
335
	 * Prints the given bundle resource if it exists
336
	 *
337
	 * @param bundle	the bundle containing the resource
338
	 * @param resource	the resource to print
339
	 */
340
	public void printBundleResource(Bundle bundle, String resource) {
341
		URL entry = null;
342
		entry = bundle.getEntry(resource);
343
		if (entry != null) {
344
			try {
345
				println(resource);
346
				InputStream in = entry.openStream();
347
				byte[] buffer = new byte[1024];
348
				int read = 0;
349
				try {
350
					while ((read = in.read(buffer)) != -1)
351
						print(new String(buffer, 0, read));
352
				} finally {
353
					if (in != null) {
354
						try {
355
							in.close();
356
						} catch (IOException e) {
357
						}
358
					}
359
				}
360
			} catch (Exception e) {
361
				System.err.println(NLS.bind(ConsoleMsg.CONSOLE_ERROR_READING_RESOURCE, resource));
362
			}
363
		} else {
364
			println(NLS.bind(ConsoleMsg.CONSOLE_RESOURCE_NOT_IN_BUNDLE, resource, bundle.toString())); 
365
		}
366
	}
367
368
	/**
369
	 *  Displays the more... prompt if the max line count has been reached 
124
	 *  Displays the more... prompt if the max line count has been reached 
370
	 *  and waits for the operator to hit enter.
125
	 *  and waits for the operator to hit enter.
371
	 *
126
	 *
372
	 */
127
	 */
373
	private void check4More() {
128
	protected void check4More() {
374
		int max = getMaximumLinesToScroll();
129
		int max = getMaximumLinesToScroll();
375
		if (max > 0) {
130
		if (max > 0) {
376
			if (currentLineCount >= max) {
131
			if (currentLineCount >= max) {
Lines 407-413 Link Here
407
	 * Toggles the use of the more prompt for displayed output.
162
	 * Toggles the use of the more prompt for displayed output.
408
	 *
163
	 *
409
	 */
164
	 */
410
	public void _more() throws Exception {
165
	private void _more() throws Exception {
411
		if (confirm(ConsoleMsg.CONSOLE_CONFIRM_MORE, true)) {
166
		if (confirm(ConsoleMsg.CONSOLE_CONFIRM_MORE, true)) {
412
			int lines = prompt(newline + ConsoleMsg.CONSOLE_MORE_ENTER_LINES, 24);
167
			int lines = prompt(newline + ConsoleMsg.CONSOLE_MORE_ENTER_LINES, 24);
413
			setMaximumLinesToScroll(lines);
168
			setMaximumLinesToScroll(lines);
Lines 430-436 Link Here
430
	 *
185
	 *
431
	 * @return	<code>true</code> if the user confirms; <code>false</code> otherwise.
186
	 * @return	<code>true</code> if the user confirms; <code>false</code> otherwise.
432
	 */
187
	 */
433
	protected boolean confirm(String string, boolean defaultAnswer) {
188
	private boolean confirm(String string, boolean defaultAnswer) {
434
		synchronized (out) {
189
		synchronized (out) {
435
			if (string.length() > 0) {
190
			if (string.length() > 0) {
436
				print(string);
191
				print(string);
Lines 461-467 Link Here
461
	 * @return	The user provided string or the defaultAnswer,
216
	 * @return	The user provided string or the defaultAnswer,
462
	 *			if user provided string was empty.
217
	 *			if user provided string was empty.
463
	 */
218
	 */
464
	protected String prompt(String string, String defaultAnswer) {
219
	private String prompt(String string, String defaultAnswer) {
465
		if (string.length() > 0) {
220
		if (string.length() > 0) {
466
			if (defaultAnswer.length() > 0) {
221
			if (defaultAnswer.length() > 0) {
467
				StringBuffer buf = new StringBuffer(256);
222
				StringBuffer buf = new StringBuffer(256);
Lines 493-499 Link Here
493
	 * @return	The user provided integer or the defaultAnswer,
248
	 * @return	The user provided integer or the defaultAnswer,
494
	 *			if user provided an empty input.
249
	 *			if user provided an empty input.
495
	 */
250
	 */
496
	protected int prompt(String string, int defaultAnswer) {
251
	private int prompt(String string, int defaultAnswer) {
497
		Integer i = new Integer(defaultAnswer);
252
		Integer i = new Integer(defaultAnswer);
498
		int answer;
253
		int answer;
499
		for (int j = 0; j < 3; j++) {
254
		for (int j = 0; j < 3; j++) {
Lines 510-513 Link Here
510
		println(ConsoleMsg.CONSOLE_TOO_MUCH_INVALID_INPUT);
265
		println(ConsoleMsg.CONSOLE_TOO_MUCH_INVALID_INPUT);
511
		return defaultAnswer;
266
		return defaultAnswer;
512
	}
267
	}
268
269
	protected CommandInterpreter getInnerCommandInterpreter(String cmd) {
270
		return new FrameworkCommandInterpreter(cmd, commandProviders, con);
271
	}
272
273
	protected void incrementLineCount(int i) {
274
		currentLineCount += i;
275
	}
513
}
276
}
(-)console/src/org/eclipse/osgi/framework/internal/core/FrameworkCommandProvider.java (-31 / +35 lines)
Lines 77-112 Link Here
77
 *  invoked by a CommandInterpreter's execute method.
77
 *  invoked by a CommandInterpreter's execute method.
78
 */
78
 */
79
public class FrameworkCommandProvider implements CommandProvider {
79
public class FrameworkCommandProvider implements CommandProvider {
80
80
	private static FrameworkCommandProvider instance;
81
	/** An instance of the OSGi framework */
81
	/** An instance of the framework */
82
	private OSGi osgi;
82
	private Framework framework;
83
	/** The system bundle context */
83
	/** The system bundle context */
84
	private org.osgi.framework.BundleContext context;
84
	private org.osgi.framework.BundleContext context;
85
	/** The start level implementation */
86
	private StartLevelManager slImpl;
87
	private ConditionalPermissionAdmin condPermAdmin;
88
	private PermissionAdmin permAdmin;
89
85
90
	/** Strings used to format other strings */
86
	/** Strings used to format other strings */
91
	private String tab = "\t"; //$NON-NLS-1$
87
	private String tab = "\t"; //$NON-NLS-1$
92
	private String newline = "\r\n"; //$NON-NLS-1$
88
	private String newline = "\r\n"; //$NON-NLS-1$
93
89
94
	/**
90
	/**
95
	 *  Constructor.
91
	 *  Get the instance of the FrameworkCommandProvider
96
	 *
92
	 *
97
	 *  It registers itself as a CommandProvider with the highest ranking possible.
93
	 *  It registers itself as a CommandProvider with the highest ranking possible.
98
	 *
94
	 *
99
	 *  @param osgi The current instance of OSGi
95
	 *  @param framework The current instance of the Framework
100
	 */
96
	 */
101
	public FrameworkCommandProvider(OSGi osgi) {
97
	public static synchronized FrameworkCommandProvider getInstance(Framework framework) {
102
		this.osgi = osgi;
98
		if (instance != null)
103
		context = osgi.getBundleContext();
99
			return instance;
104
		slImpl = osgi.framework.startLevelManager;
100
		instance = new FrameworkCommandProvider(framework);
105
		condPermAdmin = osgi.framework.condPermAdmin;
101
		BundleContext bc = framework.systemBundle.getBundleContext();
106
		permAdmin = osgi.framework.permissionAdmin;
107
		Dictionary props = new Hashtable();
102
		Dictionary props = new Hashtable();
108
		props.put(Constants.SERVICE_RANKING, new Integer(Integer.MAX_VALUE));
103
		props.put(Constants.SERVICE_RANKING, new Integer(Integer.MAX_VALUE));
109
		context.registerService(CommandProvider.class.getName(), this, props);
104
		bc.registerService(CommandProvider.class.getName(), instance, props);
105
		return instance;
106
	}
107
108
109
	private FrameworkCommandProvider(Framework framework) {
110
		this.framework = framework;
111
		context = framework.systemBundle.getBundleContext();
110
	}
112
	}
111
113
112
	/**
114
	/**
Lines 202-208 Link Here
202
	 *  @param intp A CommandInterpreter object containing the command and it's arguments.
204
	 *  @param intp A CommandInterpreter object containing the command and it's arguments.
203
	 */
205
	 */
204
	public void _launch(CommandInterpreter intp) throws Exception {
206
	public void _launch(CommandInterpreter intp) throws Exception {
205
		osgi.launch();
207
		framework.launch();
206
	}
208
	}
207
209
208
	/**
210
	/**
Lines 211-217 Link Here
211
	 *  @param intp A CommandInterpreter object containing the command and it's arguments.
213
	 *  @param intp A CommandInterpreter object containing the command and it's arguments.
212
	 */
214
	 */
213
	public void _shutdown(CommandInterpreter intp) throws Exception {
215
	public void _shutdown(CommandInterpreter intp) throws Exception {
214
		osgi.shutdown();
216
		framework.shutdown();
215
	}
217
	}
216
218
217
	/**
219
	/**
Lines 408-414 Link Here
408
	 *  @param intp A CommandInterpreter object containing the command and it's arguments.
410
	 *  @param intp A CommandInterpreter object containing the command and it's arguments.
409
	 */
411
	 */
410
	public void _status(CommandInterpreter intp) throws Exception {
412
	public void _status(CommandInterpreter intp) throws Exception {
411
		if (osgi.isActive()) {
413
		if (framework.isActive()) {
412
			intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_LAUNCHED_MESSAGE);
414
			intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_LAUNCHED_MESSAGE);
413
		} else {
415
		} else {
414
			intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_SHUTDOWN_MESSAGE);
416
			intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_SHUTDOWN_MESSAGE);
Lines 605-611 Link Here
605
			intp.print(", "); //$NON-NLS-1$
607
			intp.print(", "); //$NON-NLS-1$
606
			intp.print(NLS.bind(ConsoleMsg.CONSOLE_STATUS_MESSAGE, getStateName(bundle.getState())));
608
			intp.print(NLS.bind(ConsoleMsg.CONSOLE_STATUS_MESSAGE, getStateName(bundle.getState())));
607
			if (id != 0) {
609
			if (id != 0) {
608
				File dataRoot = osgi.framework.getDataFile(bundle, ""); //$NON-NLS-1$
610
				File dataRoot = framework.getDataFile(bundle, ""); //$NON-NLS-1$
609
611
610
				String root = (dataRoot == null) ? null : dataRoot.getAbsolutePath();
612
				String root = (dataRoot == null) ? null : dataRoot.getAbsolutePath();
611
613
Lines 671-677 Link Here
671
				intp.print(", "); //$NON-NLS-1$
673
				intp.print(", "); //$NON-NLS-1$
672
				intp.print(NLS.bind(ConsoleMsg.CONSOLE_STATUS_MESSAGE, getStateName(bundle.getState())));
674
				intp.print(NLS.bind(ConsoleMsg.CONSOLE_STATUS_MESSAGE, getStateName(bundle.getState())));
673
				if (id != 0) {
675
				if (id != 0) {
674
					File dataRoot = osgi.framework.getDataFile(bundle, ""); //$NON-NLS-1$
676
					File dataRoot = framework.getDataFile(bundle, ""); //$NON-NLS-1$
675
677
676
					String root = (dataRoot == null) ? null : dataRoot.getAbsolutePath();
678
					String root = (dataRoot == null) ? null : dataRoot.getAbsolutePath();
677
679
Lines 1044-1050 Link Here
1044
	 *  @param intp A CommandInterpreter object containing the command and it's arguments.
1046
	 *  @param intp A CommandInterpreter object containing the command and it's arguments.
1045
	 */
1047
	 */
1046
	public void _init(CommandInterpreter intp) throws Exception {
1048
	public void _init(CommandInterpreter intp) throws Exception {
1047
		if (osgi.isActive()) {
1049
		if (framework.isActive()) {
1048
			intp.print(newline);
1050
			intp.print(newline);
1049
			intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_LAUNCHED_PLEASE_SHUTDOWN_MESSAGE);
1051
			intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_LAUNCHED_PLEASE_SHUTDOWN_MESSAGE);
1050
			return;
1052
			return;
Lines 1069-1074 Link Here
1069
		} else {
1071
		} else {
1070
			intp.println(ConsoleMsg.CONSOLE_NO_INSTALLED_BUNDLES_ERROR);
1072
			intp.println(ConsoleMsg.CONSOLE_NO_INSTALLED_BUNDLES_ERROR);
1071
		}
1073
		}
1074
		PermissionAdmin permAdmin = framework.permissionAdmin;
1072
		if (permAdmin != null) {
1075
		if (permAdmin != null) {
1073
			// clear the permissions from permission admin
1076
			// clear the permissions from permission admin
1074
			permAdmin.setDefaultPermissions(null);
1077
			permAdmin.setDefaultPermissions(null);
Lines 1078-1083 Link Here
1078
					permAdmin.setPermissions(permLocations[i], null);
1081
					permAdmin.setPermissions(permLocations[i], null);
1079
		}
1082
		}
1080
		// clear the permissions from conditional permission admin
1083
		// clear the permissions from conditional permission admin
1084
		ConditionalPermissionAdmin condPermAdmin = framework.condPermAdmin;
1081
		if (condPermAdmin != null)
1085
		if (condPermAdmin != null)
1082
			for (Enumeration infos = condPermAdmin.getConditionalPermissionInfos(); infos.hasMoreElements();)
1086
			for (Enumeration infos = condPermAdmin.getConditionalPermissionInfos(); infos.hasMoreElements();)
1083
				((ConditionalPermissionInfo) infos.nextElement()).delete();
1087
				((ConditionalPermissionInfo) infos.nextElement()).delete();
Lines 1090-1096 Link Here
1090
	 */
1094
	 */
1091
	public void _close(CommandInterpreter intp) throws Exception {
1095
	public void _close(CommandInterpreter intp) throws Exception {
1092
		intp.println();
1096
		intp.println();
1093
		osgi.close();
1097
		framework.close();
1094
		System.exit(0);
1098
		System.exit(0);
1095
	}
1099
	}
1096
1100
Lines 1286-1292 Link Here
1286
	 * @param intp A CommandInterpreter object containing the command and it's arguments.
1290
	 * @param intp A CommandInterpreter object containing the command and it's arguments.
1287
	 */
1291
	 */
1288
	public void _ss(CommandInterpreter intp) throws Exception {
1292
	public void _ss(CommandInterpreter intp) throws Exception {
1289
		if (osgi.isActive()) {
1293
		if (framework.isActive()) {
1290
			intp.println();
1294
			intp.println();
1291
			intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_LAUNCHED_MESSAGE);
1295
			intp.println(ConsoleMsg.CONSOLE_FRAMEWORK_IS_LAUNCHED_MESSAGE);
1292
		} else {
1296
		} else {
Lines 1400-1409 Link Here
1400
				}
1404
				}
1401
			}
1405
			}
1402
			if (bundle == null) { // must want framework startlevel
1406
			if (bundle == null) { // must want framework startlevel
1403
				value = slImpl.getStartLevel();
1407
				value = framework.startLevelManager.getStartLevel();
1404
				intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value)));
1408
				intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value)));
1405
			} else { // must want bundle startlevel
1409
			} else { // must want bundle startlevel
1406
				value = slImpl.getBundleStartLevel(bundle);
1410
				value =  framework.startLevelManager.getBundleStartLevel(bundle);
1407
				intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, new Long(bundle.getBundleId()), new Integer(value)));
1411
				intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, new Long(bundle.getBundleId()), new Integer(value)));
1408
			}
1412
			}
1409
		}
1413
		}
Lines 1420-1432 Link Here
1420
			String token = intp.nextArgument();
1424
			String token = intp.nextArgument();
1421
			if (token == null) {
1425
			if (token == null) {
1422
				intp.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_GIVEN);
1426
				intp.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_GIVEN);
1423
				value = slImpl.getStartLevel();
1427
				value =  framework.startLevelManager.getStartLevel();
1424
				intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value)));
1428
				intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value)));
1425
			} else {
1429
			} else {
1426
				value = this.getStartLevelFromToken(intp, token);
1430
				value = this.getStartLevelFromToken(intp, token);
1427
				if (value > 0) {
1431
				if (value > 0) {
1428
					try {
1432
					try {
1429
						slImpl.setStartLevel(value);
1433
						 framework.startLevelManager.setStartLevel(value);
1430
						intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value)));
1434
						intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value)));
1431
					} catch (IllegalArgumentException e) {
1435
					} catch (IllegalArgumentException e) {
1432
						intp.println(e.getMessage());
1436
						intp.println(e.getMessage());
Lines 1462-1468 Link Here
1462
				bundle = getBundleFromToken(intp, token, true);
1466
				bundle = getBundleFromToken(intp, token, true);
1463
				if (bundle != null) {
1467
				if (bundle != null) {
1464
					try {
1468
					try {
1465
						slImpl.setBundleStartLevel(bundle, newSL);
1469
						 framework.startLevelManager.setBundleStartLevel(bundle, newSL);
1466
						intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, new Long(bundle.getBundleId()), new Integer(newSL)));
1470
						intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, new Long(bundle.getBundleId()), new Integer(newSL)));
1467
					} catch (IllegalArgumentException e) {
1471
					} catch (IllegalArgumentException e) {
1468
						intp.println(e.getMessage());
1472
						intp.println(e.getMessage());
Lines 1484-1496 Link Here
1484
			String token = intp.nextArgument();
1488
			String token = intp.nextArgument();
1485
			if (token == null) {
1489
			if (token == null) {
1486
				intp.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_GIVEN);
1490
				intp.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_GIVEN);
1487
				value = slImpl.getInitialBundleStartLevel();
1491
				value =  framework.startLevelManager.getInitialBundleStartLevel();
1488
				intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_INITIAL_BUNDLE_STARTLEVEL, String.valueOf(value)));
1492
				intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_INITIAL_BUNDLE_STARTLEVEL, String.valueOf(value)));
1489
			} else {
1493
			} else {
1490
				value = this.getStartLevelFromToken(intp, token);
1494
				value = this.getStartLevelFromToken(intp, token);
1491
				if (value > 0) {
1495
				if (value > 0) {
1492
					try {
1496
					try {
1493
						slImpl.setInitialBundleStartLevel(value);
1497
						 framework.startLevelManager.setInitialBundleStartLevel(value);
1494
						intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_INITIAL_BUNDLE_STARTLEVEL, String.valueOf(value)));
1498
						intp.println(NLS.bind(ConsoleMsg.STARTLEVEL_INITIAL_BUNDLE_STARTLEVEL, String.valueOf(value)));
1495
					} catch (IllegalArgumentException e) {
1499
					} catch (IllegalArgumentException e) {
1496
						intp.println(e.getMessage());
1500
						intp.println(e.getMessage());
(-)console/src/org/eclipse/osgi/framework/internal/core/ConsoleMsg.java (+8 lines)
Lines 27-32 Link Here
27
	public static String CONSOLE_CONFIRM_DISCONNECT;
27
	public static String CONSOLE_CONFIRM_DISCONNECT;
28
	public static String CONSOLE_CONFIRM;
28
	public static String CONSOLE_CONFIRM;
29
	public static String CONSOLE_CONFIRM_VALUES;
29
	public static String CONSOLE_CONFIRM_VALUES;
30
31
	public static String CONSOLE_RUN;
30
	public static String CONSOLE_Y;
32
	public static String CONSOLE_Y;
31
	public static String CONSOLE_N;
33
	public static String CONSOLE_N;
32
	public static String CONSOLE_PROMPT_DEFAULT;
34
	public static String CONSOLE_PROMPT_DEFAULT;
Lines 80-85 Link Here
80
	public static String CONSOLE_HELP_COMMAND_ARGUMENT_DESCRIPTION;
82
	public static String CONSOLE_HELP_COMMAND_ARGUMENT_DESCRIPTION;
81
	public static String CONSOLE_HELP_EXEC_COMMAND_DESCRIPTION;
83
	public static String CONSOLE_HELP_EXEC_COMMAND_DESCRIPTION;
82
	public static String CONSOLE_HELP_FORK_COMMAND_DESCRIPTION;
84
	public static String CONSOLE_HELP_FORK_COMMAND_DESCRIPTION;
85
86
	public static String LOG_ENTRY;
87
	public static String LOG_MESSAGE;
88
	public static String SOCKET_RUN;
89
	public static String SOCKET_STREAM_EXCEPTION;
90
	public static String SOCKET_STREAM_UNKOWNHOST;
83
	public static String STARTLEVEL_HELP_HEADING;
91
	public static String STARTLEVEL_HELP_HEADING;
84
	public static String CONSOLE_HELP_OPTIONAL_IDLOCATION_ARGUMENT_DESCRIPTION;
92
	public static String CONSOLE_HELP_OPTIONAL_IDLOCATION_ARGUMENT_DESCRIPTION;
85
	public static String STARTLEVEL_HELP_SL;
93
	public static String STARTLEVEL_HELP_SL;
(-)console/src/org/eclipse/osgi/framework/internal/core/ConsoleImpl.java (+75 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.osgi.framework.internal.core;
13
14
import java.io.*;
15
import org.eclipse.osgi.framework.console.CommandProvider;
16
import org.eclipse.osgi.framework.console.Console;
17
import org.osgi.framework.ServiceReference;
18
import org.osgi.util.tracker.ServiceTracker;
19
20
/**
21
 * This class is a service based implementation of Console, and sends commands to
22
 * the OSGi console. 
23
 */
24
public class ConsoleImpl implements Console {
25
	private Framework framework;
26
	/** A tracker containing the service object of all registered command providers */
27
	private ServiceTracker providers;
28
29
	public ConsoleImpl(Framework framework) {
30
		this.framework = framework;
31
		// register the OSGi command provider
32
		FrameworkCommandProvider.getInstance(framework);
33
	}
34
35
	/**
36
	 * Submit a command to the OSGi console, and return the output.
37
	 */
38
	public String runCommand(String commandLine) {
39
		if (commandLine == null || commandLine.length() == 0)
40
			return null;
41
		ByteArrayOutputStream out = new ByteArrayOutputStream();
42
		PrintWriter writer = new PrintWriter(out);
43
		BaseCommandInterpreter interpreter = new BaseCommandInterpreter(commandLine, getProviders(), writer);
44
		String command = interpreter.nextArgument();
45
		if (command != null)
46
			interpreter.execute(command);
47
		writer.flush();
48
		return out.toString();
49
	}
50
51
	/**
52
	 * Get everyone registered to provide console commands.
53
	 * @return List of objects registered to provide commands to the Console.
54
	 */
55
	public synchronized CommandProvider[] getProviders() {
56
		if (providers == null) {
57
			providers = new ServiceTracker(framework.systemBundle.getBundleContext(), CommandProvider.class.getName(), null);
58
			providers.open();
59
		}
60
		ServiceReference[] serviceRefs = providers.getServiceReferences();
61
		Util.dsort(serviceRefs, 0, serviceRefs.length);
62
63
		CommandProvider[] serviceObjects = new CommandProvider[serviceRefs.length];
64
		for (int i = 0; i < serviceRefs.length; i++)
65
			serviceObjects[i] = (CommandProvider) framework.systemBundle.getBundleContext().getService(serviceRefs[i]);
66
		return serviceObjects;
67
	}
68
69
	public void shutDown() {
70
		if (providers != null) {
71
			providers.close();
72
			providers = null;
73
		}
74
	}
75
}
(-)core/framework/org/eclipse/osgi/framework/console/Console.java (+31 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.osgi.framework.console;
13
14
/**
15
 * This interface allows clients to run commands against the OSGi console. Commands are defined by classes registered
16
 * as CommandProviders.
17
 * <p>
18
 * This interface is not intended to be implemented by clients.
19
 * </p>
20
 * @since 3.3
21
 */
22
public interface Console {
23
	
24
25
	/**
26
	 * Submits a command to the OSGi console, and returns the result. 
27
	 * @param command - The command to be run by the OSGi console.
28
	 * @return The result of the command.
29
	 */
30
	public String runCommand(String command);
31
}
(-)console/src/org/eclipse/osgi/framework/internal/core/ConsoleFactory.java (+41 lines)
Added Link Here
1
package org.eclipse.osgi.framework.internal.core;
2
3
import org.osgi.framework.*;
4
5
public class ConsoleFactory implements ServiceFactory {
6
7
	private OSGi osgi;
8
	private ConsoleImpl console;
9
	private int references;
10
11
	/**
12
	 * The constructor caches a reference to osgi for constructing FrameworkConsoles
13
	 */
14
	public ConsoleFactory(OSGi osgi) {
15
		super();
16
		references = 0;
17
		this.osgi = osgi;
18
	}
19
20
	/* (non-Javadoc)
21
	 * @see org.osgi.framework.ServiceFactory#getService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration)
22
	 */
23
	public Object getService(Bundle bundle, ServiceRegistration registration) {
24
		if (console == null)
25
			console = new ConsoleImpl(osgi.framework);
26
		references++;
27
		return console;
28
	}
29
30
	/* (non-Javadoc)
31
	 * @see org.osgi.framework.ServiceFactory#ungetService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration, java.lang.Object)
32
	 */
33
	public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
34
		references--;
35
		if (references == 0) {
36
			console.shutDown();
37
			console = null;
38
		}
39
	}
40
41
}
(-)console/src/org/eclipse/osgi/framework/internal/core/BaseCommandInterpreter.java (+320 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.osgi.framework.internal.core;
13
14
import java.io.*;
15
import java.lang.reflect.*;
16
import java.net.URL;
17
import java.util.*;
18
import org.eclipse.osgi.framework.console.CommandInterpreter;
19
import org.eclipse.osgi.framework.console.CommandProvider;
20
import org.eclipse.osgi.util.NLS;
21
import org.osgi.framework.Bundle;
22
23
/**
24
 * This class knows how to parse and execute the command line arguments to the OSGi Console.
25
 * It attempts to pass the command to each registered CommandProvider until it finds one
26
 * that knows what to do with it.
27
 */
28
public class BaseCommandInterpreter implements CommandInterpreter {
29
	/** Strings used to format other strings */
30
	protected static final String newline = "\r\n"; //$NON-NLS-1$
31
	/** The command line in StringTokenizer form */
32
	protected StringTokenizer tok;
33
	/** The active CommandProviders */
34
	protected Object[] commandProviders;
35
	/** The stream to send output to */
36
	protected PrintWriter out;
37
38
	protected boolean firstCommand = true;
39
40
	/**
41
	 *  The constructor.  It turns the cmdline string into a StringTokenizer and remembers
42
	 *  the input parms.
43
	 */
44
	public BaseCommandInterpreter(String cmdline, Object[] commandProviders, PrintWriter out) {
45
		tok = new StringTokenizer(cmdline);
46
		this.commandProviders = commandProviders;
47
		this.out = out;
48
	}
49
50
	/**
51
	 * Get the next argument in the input.
52
	 * 
53
	 * E.g. if the commandline is hello world, the _hello method
54
	 * will get "world" as the first argument.
55
	 * 
56
	 * @return A string containing the next argument on the command line
57
	 */
58
	public String nextArgument() {
59
		if (tok == null || !tok.hasMoreElements()) {
60
			return null;
61
		}
62
		String token = tok.nextToken();
63
		//check for quotes
64
		int index = token.indexOf('"');
65
		if (index != -1) {
66
			//if we only have one quote, find the second quote
67
			if (index == token.lastIndexOf('"')) {
68
				token += tok.nextToken("\""); //$NON-NLS-1$
69
			}
70
			StringBuffer buf = new StringBuffer(token);
71
72
			//strip quotes
73
			while (index != -1) {
74
				buf.deleteCharAt(index);
75
				token = buf.toString();
76
				index = token.indexOf('"');
77
			}
78
			return buf.toString();
79
		}
80
		return (token);
81
	}
82
83
	/**
84
	 * Execute a command line as if it came from the end user.
85
	 * 
86
	 * Searches the list of command providers using introspection until
87
	 * it finds one that contains a matching method.  It searches for a method
88
	 * with the name "_cmd" where cmd is the command to execute.  For example,
89
	 * for a command of "launch" execute searches for a method called "_launch".
90
	 * 
91
	 * @param cmd The name of the command to execute.
92
	 * @return The object returned by the method executed.
93
	 */
94
	public Object execute(String cmd) {
95
		if (!firstCommand)
96
			return innerExecute(cmd);
97
		firstCommand = false;
98
99
		Class[] parameterTypes = new Class[] {CommandInterpreter.class};
100
		Object[] parameters = new Object[] {this};
101
		boolean executed = false;
102
		int size = commandProviders.length;
103
		Object retval = null;
104
		for (int i = 0; !executed && (i < size); i++) {
105
			try {
106
				Object target = commandProviders[i];
107
				Method method = target.getClass().getMethod("_" + cmd, parameterTypes); //$NON-NLS-1$
108
				retval = method.invoke(target, parameters);
109
				executed = true; // stop after the command has been found
110
			} catch (NoSuchMethodException ite) {
111
				// keep going - maybe another command provider will be able to execute this command
112
			} catch (InvocationTargetException ite) {
113
				executed = true; // don't want to keep trying - we found the method but got an error
114
				printStackTrace(ite.getTargetException());
115
			} catch (Exception ee) {
116
				executed = true; // don't want to keep trying - we got an error we don't understand
117
				printStackTrace(ee);
118
			}
119
		}
120
		// if no command was found to execute, display help for all registered command providers
121
		if (!executed) {
122
			for (int i = 0; i < size; i++) {
123
				try {
124
					CommandProvider commandProvider = (CommandProvider) commandProviders[i];
125
					out.print(commandProvider.getHelp());
126
					out.flush();
127
				} catch (Exception ee) {
128
					printStackTrace(ee);
129
				}
130
			}
131
			// call help for the more command provided by this class
132
			out.print(getHelp());
133
			out.flush();
134
		}
135
		return retval;
136
	}
137
138
	private Object innerExecute(String cmd) {
139
		if (cmd != null && cmd.length() > 0) {
140
			CommandInterpreter intcp = getInnerCommandInterpreter(cmd);
141
			String command = intcp.nextArgument();
142
			if (command != null)
143
				return intcp.execute(command);
144
		}
145
		return null;
146
	}
147
148
	protected CommandInterpreter getInnerCommandInterpreter(String cmd) {
149
		return new BaseCommandInterpreter(cmd, commandProviders, out);
150
	}
151
152
	/**
153
	 * Prints a string to the output medium (appended with newline character).
154
	 * <p>
155
	 * This method does not increment the line counter for the 'more' prompt.
156
	 *
157
	 * @param o the string to be printed
158
	 */
159
	private void printline(Object o) {
160
		print(o + newline);
161
	}
162
163
	/**
164
	 * Prints an object to the outputstream
165
	 *
166
	 * @param o	the object to be printed
167
	 */
168
	public void print(Object o) {
169
		synchronized (out) {
170
			check4More();
171
			out.print(o);
172
			out.flush();
173
		}
174
	}
175
176
	/**
177
	 * Prints a empty line to the outputstream
178
	 */
179
	public void println() {
180
		println(""); //$NON-NLS-1$
181
	}
182
183
	/**
184
	 * Print a stack trace including nested exceptions.
185
	 * @param t The offending exception
186
	 */
187
	public void printStackTrace(Throwable t) {
188
		t.printStackTrace(out);
189
190
		Method[] methods = t.getClass().getMethods();
191
192
		int size = methods.length;
193
		Class throwable = Throwable.class;
194
195
		for (int i = 0; i < size; i++) {
196
			Method method = methods[i];
197
198
			if (Modifier.isPublic(method.getModifiers()) && method.getName().startsWith("get") && throwable.isAssignableFrom(method.getReturnType()) && (method.getParameterTypes().length == 0)) { //$NON-NLS-1$
199
				try {
200
					Throwable nested = (Throwable) method.invoke(t, null);
201
202
					if ((nested != null) && (nested != t)) {
203
						out.println(ConsoleMsg.CONSOLE_NESTED_EXCEPTION);
204
						printStackTrace(nested);
205
					}
206
				} catch (IllegalAccessException e) {
207
					
208
				} catch (InvocationTargetException e) {
209
					
210
				}
211
			}
212
		}
213
	}
214
215
	/**
216
	 * Prints an object to the output medium (appended with newline character).
217
	 * <p>
218
	 * If running on the target environment, the user is prompted with '--more'
219
	 * if more than the configured number of lines have been printed without user prompt.
220
	 * This enables the user of the program to have control over scrolling.
221
	 * <p>
222
	 * For this to work properly you should not embed "\n" etc. into the string.
223
	 *
224
	 * @param	o	the object to be printed
225
	 */
226
	public void println(Object o) {
227
		if (o == null) {
228
			return;
229
		}
230
		synchronized (out) {
231
			check4More();
232
			printline(o);
233
			incrementLineCount(1 + (o.toString().length() / 80));
234
		}
235
	}
236
237
	/**
238
	 * Prints the given dictionary sorted by keys.
239
	 *
240
	 * @param dic	the dictionary to print
241
	 * @param title	the header to print above the key/value pairs
242
	 */
243
	public void printDictionary(Dictionary dic, String title) {
244
		if (dic == null)
245
			return;
246
247
		int count = dic.size();
248
		String[] keys = new String[count];
249
		Enumeration keysEnum = dic.keys();
250
		int i = 0;
251
		while (keysEnum.hasMoreElements()) {
252
			keys[i++] = (String) keysEnum.nextElement();
253
		}
254
		Util.sort(keys);
255
256
		if (title != null) {
257
			println(title);
258
		}
259
		for (i = 0; i < count; i++) {
260
			println(" " + keys[i] + " = " + dic.get(keys[i])); //$NON-NLS-1$//$NON-NLS-2$
261
		}
262
		println();
263
	}
264
265
	/**
266
	 * Prints the given bundle resource if it exists
267
	 *
268
	 * @param bundle	the bundle containing the resource
269
	 * @param resource	the resource to print
270
	 */
271
	public void printBundleResource(Bundle bundle, String resource) {
272
		URL entry = null;
273
		entry = bundle.getEntry(resource);
274
		if (entry != null) {
275
			try {
276
				println(resource);
277
				InputStream in = entry.openStream();
278
				byte[] buffer = new byte[1024];
279
				int read = 0;
280
				try {
281
					while ((read = in.read(buffer)) != -1)
282
						print(new String(buffer, 0, read));
283
				} finally {
284
					if (in != null) {
285
						try {
286
							in.close();
287
						} catch (IOException e) {
288
						}
289
					}
290
				}
291
			} catch (Exception e) {
292
				System.err.println(NLS.bind(ConsoleMsg.CONSOLE_ERROR_READING_RESOURCE, resource));
293
			}
294
		} else {
295
			println(NLS.bind(ConsoleMsg.CONSOLE_RESOURCE_NOT_IN_BUNDLE, resource, bundle.toString())); 
296
		}
297
	}
298
299
	/**
300
	 *  Displays the more... prompt if the max line count has been reached 
301
	 *  and waits for the operator to hit enter.
302
	 *
303
	 */
304
	protected void check4More() {
305
		// do nothing by default
306
	}
307
308
	protected void incrementLineCount(int i) {
309
		// do nothing by default
310
	}
311
312
	/**
313
	 * Answer a string (may be as many lines as you like) with help
314
	 * texts that explain the command.
315
	 */
316
	public String getHelp() {
317
		return newline;
318
	}
319
320
}

Return to bug 162415