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

(-)core/framework/org/eclipse/osgi/framework/internal/core/BundleLoader.java (-5 / +33 lines)
Lines 351-356 Link Here
351
		if (Debug.DEBUG && Debug.DEBUG_LOADER)
351
		if (Debug.DEBUG && Debug.DEBUG_LOADER)
352
			Debug.println("BundleLoader[" + this + "].loadBundleClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
352
			Debug.println("BundleLoader[" + this + "].loadBundleClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
353
		String pkgName = getPackageName(name);
353
		String pkgName = getPackageName(name);
354
		boolean bootDelegation = false;
354
		// follow the OSGi delegation model
355
		// follow the OSGi delegation model
355
		if (checkParent && parent != null) {
356
		if (checkParent && parent != null) {
356
			if (name.startsWith(JAVA_PACKAGE))
357
			if (name.startsWith(JAVA_PACKAGE))
Lines 363-368 Link Here
363
					return parent.loadClass(name);
364
					return parent.loadClass(name);
364
				} catch (ClassNotFoundException cnfe) {
365
				} catch (ClassNotFoundException cnfe) {
365
					// we want to continue
366
					// we want to continue
367
					bootDelegation = true;
366
				}
368
				}
367
		}
369
		}
368
370
Lines 389-402 Link Here
389
		// 6) attempt to find a dynamic import source; only do this if a required source was not found
391
		// 6) attempt to find a dynamic import source; only do this if a required source was not found
390
		if (source == null) {
392
		if (source == null) {
391
			source = findDynamicSource(pkgName);
393
			source = findDynamicSource(pkgName);
392
			if (source != null)
394
			if (source != null) {
393
				result = source.loadClass(name);
395
				result = source.loadClass(name);
396
				if (result != null)
397
					return result;
398
				// must throw CNFE if dynamic import source does not have the class
399
				throw new ClassNotFoundException(name);
400
			}
394
		}
401
		}
402
403
		// hack to support backwards compatibiility for bootdelegation
404
		if (checkParent && !bootDelegation && bundle.framework.compatibiltyBootDelegation) {
405
			bootDelegation = true;
406
			try {
407
				return parent.loadClass(name);
408
			} catch (ClassNotFoundException cnfe) {
409
				// we want to continue
410
			}
411
		}
412
395
		// do buddy policy loading
413
		// do buddy policy loading
396
		if (result == null && policy != null)
414
		if (result == null && policy != null)
397
			result = policy.doBuddyClassLoading(name);
415
			result = policy.doBuddyClassLoading(name);
398
		// last resort; do class context trick to work around VM bugs
416
		// last resort; do class context trick to work around VM bugs
399
		if (result == null && findParentResource(name))
417
		if (result == null && !bootDelegation && findParentResource(name))
400
			result = parent.loadClass(name);
418
			result = parent.loadClass(name);
401
		if (result == null)
419
		if (result == null)
402
			throw new ClassNotFoundException(name);
420
			throw new ClassNotFoundException(name);
Lines 451-456 Link Here
451
			name = name.substring(1); /* remove leading slash before search */
469
			name = name.substring(1); /* remove leading slash before search */
452
		String pkgName = getResourcePackageName(name);
470
		String pkgName = getResourcePackageName(name);
453
		// follow the OSGi delegation model
471
		// follow the OSGi delegation model
472
		boolean bootDelegation = false;
454
		// First check the parent classloader for system resources, if it is a java resource.
473
		// First check the parent classloader for system resources, if it is a java resource.
455
		if (checkParent && parent != null) {
474
		if (checkParent && parent != null) {
456
			if (pkgName.startsWith(JAVA_PACKAGE))
475
			if (pkgName.startsWith(JAVA_PACKAGE))
Lines 462-467 Link Here
462
				URL result = parent.getResource(name);
481
				URL result = parent.getResource(name);
463
				if (result != null)
482
				if (result != null)
464
					return result;
483
					return result;
484
				bootDelegation = true;
465
			}
485
			}
466
		}
486
		}
467
487
Lines 485-497 Link Here
485
		if (source == null) {
505
		if (source == null) {
486
			source = findDynamicSource(pkgName);
506
			source = findDynamicSource(pkgName);
487
			if (source != null)
507
			if (source != null)
488
				result = source.getResource(name);
508
				// must return the result of the dynamic import and do not continue
509
				return source.getResource(name);
510
		}
511
512
		// hack to support backwards compatibiility for bootdelegation
513
		if (checkParent && !bootDelegation && bundle.framework.compatibiltyBootDelegation) {
514
			bootDelegation = true;
515
			result = parent.getResource(name);
489
		}
516
		}
517
490
		// do buddy policy loading
518
		// do buddy policy loading
491
		if (result == null && policy != null)
519
		if (result == null && policy != null)
492
			return policy.doBuddyResourceLoading(name);
520
			return policy.doBuddyResourceLoading(name);
493
		// last resort; do class context trick to work around VM bugs
521
		// last resort; do class context trick to work around VM bugs
494
		if (result == null && findParentResource(name))
522
		if (result == null && !bootDelegation && findParentResource(name))
495
			result = parent.getResource(name);
523
			result = parent.getResource(name);
496
		return result;
524
		return result;
497
	}
525
	}
Lines 949-955 Link Here
949
		});
977
		});
950
	}
978
	}
951
979
952
	private static final class ClassContext extends SecurityManager {
980
	static final class ClassContext extends SecurityManager {
953
		// need to make this method public
981
		// need to make this method public
954
		public Class[] getClassContext() {
982
		public Class[] getClassContext() {
955
			return super.getClassContext();
983
			return super.getClassContext();
(-)core/framework/org/eclipse/osgi/framework/internal/core/Framework.java (+1 lines)
Lines 99-104 Link Here
99
	String[] bootDelegationStems;
99
	String[] bootDelegationStems;
100
	boolean bootDelegateAll = false;
100
	boolean bootDelegateAll = false;
101
	boolean contextBootDelegation = "true".equals(FrameworkProperties.getProperty("osgi.context.bootdelegation", "true")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
101
	boolean contextBootDelegation = "true".equals(FrameworkProperties.getProperty("osgi.context.bootdelegation", "true")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
102
	boolean compatibiltyBootDelegation = "true".equals(FrameworkProperties.getProperty("osgi.compatibility.bootdelegation", "true"));  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
102
103
103
	/**
104
	/**
104
	 * The AliasMapper used to alias OS Names.
105
	 * The AliasMapper used to alias OS Names.
(-)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/FrameworkCommandInterpreter.java (-258 / +21 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 452-458 Link Here
452
		return input.toLowerCase().charAt(0) == ConsoleMsg.CONSOLE_Y.charAt(0);
207
		return input.toLowerCase().charAt(0) == ConsoleMsg.CONSOLE_Y.charAt(0);
453
	}
208
	}
454
209
455
	/**
210
	/*
456
	 * Prompts the user for input from the input medium providing a default value.
211
	 * Prompts the user for input from the input medium providing a default value.
457
	 *
212
	 *
458
	 * @param	string			the message to present to the user
213
	 * @param	string			the message to present to the user
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 484-490 Link Here
484
		return defaultAnswer;
239
		return defaultAnswer;
485
	}
240
	}
486
241
487
	/**
242
	/*
488
	 * Prompts the user for input of a positive integer.
243
	 * Prompts the user for input of a positive integer.
489
	 *
244
	 *
490
	 * @param	string			the message to present to the user
245
	 * @param	string			the message to present to the user
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/BaseCommandInterpreter.java (+323 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2003, 2005 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 FrameworkConsole.
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
 * FrameworkCommandInterpreter provides support for the "More" command which allows the operator to configure
29
 * the number of lines to display before being prompted to continue.
30
 *
31
 * FrameworkCommandInterpreter provides several print methods which handle the "More" command.
32
 */
33
public class BaseCommandInterpreter implements CommandInterpreter {
34
	/** Strings used to format other strings */
35
	protected static final String newline = "\r\n"; //$NON-NLS-1$
36
	/** The command line in StringTokenizer form */
37
	protected StringTokenizer tok;
38
	/** The active CommandProviders */
39
	protected Object[] commandProviders;
40
	/** The stream to send output to */
41
	protected PrintWriter out;
42
43
	protected boolean firstCommand = true;
44
45
	/**
46
	 *  The constructor.  It turns the cmdline string into a StringTokenizer and remembers
47
	 *  the input parms.
48
	 */
49
	public BaseCommandInterpreter(String cmdline, Object[] commandProviders, PrintWriter out) {
50
		tok = new StringTokenizer(cmdline);
51
		this.commandProviders = commandProviders;
52
		this.out = out;
53
	}
54
55
	/**
56
	 Get the next argument in the input.
57
	 
58
	 E.g. if the commandline is hello world, the _hello method
59
	 will get "world" as the first argument.
60
	 
61
	 @return A string containing the next argument on the command line
62
	 */
63
	public String nextArgument() {
64
		if (tok == null || !tok.hasMoreElements()) {
65
			return null;
66
		}
67
		String token = tok.nextToken();
68
		//check for quotes
69
		int index = token.indexOf('"');
70
		if (index != -1) {
71
			//if we only have one quote, find the second quote
72
			if (index == token.lastIndexOf('"')) {
73
				token += tok.nextToken("\""); //$NON-NLS-1$
74
			}
75
			StringBuffer buf = new StringBuffer(token);
76
77
			//strip quotes
78
			while (index != -1) {
79
				buf.deleteCharAt(index);
80
				token = buf.toString();
81
				index = token.indexOf('"');
82
			}
83
			return buf.toString();
84
		}
85
		return (token);
86
	}
87
88
	/**
89
	 Execute a command line as if it came from the end user.
90
	 
91
	 Searches the list of command providers using introspection until
92
	 it finds one that contains a matching method.  It searches for a method
93
	 with the name "_cmd" where cmd is the command to execute.  For example,
94
	 for a command of "launch" execute searches for a method called "_launch".
95
	 
96
	 @param cmd The name of the command to execute.
97
	 @return The object returned by the method executed.
98
	 */
99
	public Object execute(String cmd) {
100
		if (!firstCommand)
101
			return innerExecute(cmd);
102
		firstCommand = false;
103
104
		Class[] parameterTypes = new Class[] {CommandInterpreter.class};
105
		Object[] parameters = new Object[] {this};
106
		boolean executed = false;
107
		int size = commandProviders.length;
108
		Object retval = null;
109
		for (int i = 0; !executed && (i < size); i++) {
110
			try {
111
				Object target = commandProviders[i];
112
				Method method = target.getClass().getMethod("_" + cmd, parameterTypes); //$NON-NLS-1$
113
				retval = method.invoke(target, parameters);
114
				executed = true; // stop after the command has been found
115
			} catch (NoSuchMethodException ite) {
116
				// keep going - maybe another command provider will be able to execute this command
117
			} catch (InvocationTargetException ite) {
118
				executed = true; // don't want to keep trying - we found the method but got an error
119
				printStackTrace(ite.getTargetException());
120
			} catch (Exception ee) {
121
				executed = true; // don't want to keep trying - we got an error we don't understand
122
				printStackTrace(ee);
123
			}
124
		}
125
		// if no command was found to execute, display help for all registered command providers
126
		if (!executed) {
127
			for (int i = 0; i < size; i++) {
128
				try {
129
					CommandProvider commandProvider = (CommandProvider) commandProviders[i];
130
					out.print(commandProvider.getHelp());
131
					out.flush();
132
				} catch (Exception ee) {
133
					printStackTrace(ee);
134
				}
135
			}
136
			// call help for the more command provided by this class
137
			out.print(getHelp());
138
			out.flush();
139
		}
140
		return retval;
141
	}
142
143
	private Object innerExecute(String cmd) {
144
		if (cmd != null && cmd.length() > 0) {
145
			CommandInterpreter intcp = getInnerCommandInterpreter(cmd);
146
			String command = intcp.nextArgument();
147
			if (command != null)
148
				return intcp.execute(command);
149
		}
150
		return null;
151
	}
152
153
	protected CommandInterpreter getInnerCommandInterpreter(String cmd) {
154
		return new BaseCommandInterpreter(cmd, commandProviders, out);
155
	}
156
157
	/**
158
	 * Prints a string to the output medium (appended with newline character).
159
	 * <p>
160
	 * This method does not increment the line counter for the 'more' prompt.
161
	 *
162
	 * @param o the string to be printed
163
	 */
164
	private void printline(Object o) {
165
		print(o + newline);
166
	}
167
168
	/**
169
	 * Prints an object to the outputstream
170
	 *
171
	 * @param o	the object to be printed
172
	 */
173
	public void print(Object o) {
174
		synchronized (out) {
175
			check4More();
176
			out.print(o);
177
			out.flush();
178
		}
179
	}
180
181
	/**
182
	 * Prints a empty line to the outputstream
183
	 */
184
	public void println() {
185
		println(""); //$NON-NLS-1$
186
	}
187
188
	/**
189
	 * Print a stack trace including nested exceptions.
190
	 * @param t The offending exception
191
	 */
192
	public void printStackTrace(Throwable t) {
193
		t.printStackTrace(out);
194
195
		Method[] methods = t.getClass().getMethods();
196
197
		int size = methods.length;
198
		Class throwable = Throwable.class;
199
200
		for (int i = 0; i < size; i++) {
201
			Method method = methods[i];
202
203
			if (Modifier.isPublic(method.getModifiers()) && method.getName().startsWith("get") && throwable.isAssignableFrom(method.getReturnType()) && (method.getParameterTypes().length == 0)) { //$NON-NLS-1$
204
				try {
205
					Throwable nested = (Throwable) method.invoke(t, null);
206
207
					if ((nested != null) && (nested != t)) {
208
						out.println(ConsoleMsg.CONSOLE_NESTED_EXCEPTION);
209
						printStackTrace(nested);
210
					}
211
				} catch (IllegalAccessException e) {
212
				} catch (InvocationTargetException e) {
213
				}
214
			}
215
		}
216
	}
217
218
	/**
219
	 * Prints an object to the output medium (appended with newline character).
220
	 * <p>
221
	 * If running on the target environment, the user is prompted with '--more'
222
	 * if more than the configured number of lines have been printed without user prompt.
223
	 * This enables the user of the program to have control over scrolling.
224
	 * <p>
225
	 * For this to work properly you should not embed "\n" etc. into the string.
226
	 *
227
	 * @param	o	the object to be printed
228
	 */
229
	public void println(Object o) {
230
		if (o == null) {
231
			return;
232
		}
233
		synchronized (out) {
234
			check4More();
235
			printline(o);
236
			incrementLineCount(1 + (o.toString().length() / 80));
237
		}
238
	}
239
240
	/**
241
	 * Prints the given dictionary sorted by keys.
242
	 *
243
	 * @param dic	the dictionary to print
244
	 * @param title	the header to print above the key/value pairs
245
	 */
246
	public void printDictionary(Dictionary dic, String title) {
247
		if (dic == null)
248
			return;
249
250
		int count = dic.size();
251
		String[] keys = new String[count];
252
		Enumeration keysEnum = dic.keys();
253
		int i = 0;
254
		while (keysEnum.hasMoreElements()) {
255
			keys[i++] = (String) keysEnum.nextElement();
256
		}
257
		Util.sort(keys);
258
259
		if (title != null) {
260
			println(title);
261
		}
262
		for (i = 0; i < count; i++) {
263
			println(" " + keys[i] + " = " + dic.get(keys[i])); //$NON-NLS-1$//$NON-NLS-2$
264
		}
265
		println();
266
	}
267
268
	/**
269
	 * Prints the given bundle resource if it exists
270
	 *
271
	 * @param bundle	the bundle containing the resource
272
	 * @param resource	the resource to print
273
	 */
274
	public void printBundleResource(Bundle bundle, String resource) {
275
		URL entry = null;
276
		entry = bundle.getEntry(resource);
277
		if (entry != null) {
278
			try {
279
				println(resource);
280
				InputStream in = entry.openStream();
281
				byte[] buffer = new byte[1024];
282
				int read = 0;
283
				try {
284
					while ((read = in.read(buffer)) != -1)
285
						print(new String(buffer, 0, read));
286
				} finally {
287
					if (in != null) {
288
						try {
289
							in.close();
290
						} catch (IOException e) {
291
						}
292
					}
293
				}
294
			} catch (Exception e) {
295
				System.err.println(NLS.bind(ConsoleMsg.CONSOLE_ERROR_READING_RESOURCE, resource));
296
			}
297
		} else {
298
			println(NLS.bind(ConsoleMsg.CONSOLE_RESOURCE_NOT_IN_BUNDLE, resource, bundle.toString())); 
299
		}
300
	}
301
302
	/**
303
	 *  Displays the more... prompt if the max line count has been reached 
304
	 *  and waits for the operator to hit enter.
305
	 *
306
	 */
307
	protected void check4More() {
308
		// do nothing by default
309
	}
310
311
	protected void incrementLineCount(int i) {
312
		// do nothing by default
313
	}
314
315
	/**
316
	 Answer a string (may be as many lines as you like) with help
317
	 texts that explain the command.
318
	 */
319
	public String getHelp() {
320
		return newline;
321
	}
322
323
}
(-)console/src/org/eclipse/osgi/framework/internal/core/ConsoleImpl.java (+55 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
public class ConsoleImpl implements Console {
21
	private Framework framework;
22
	/** A tracker containing the service object of all registered command providers */
23
	private ServiceTracker providers;
24
25
	public ConsoleImpl(Framework framework) {
26
		this.framework = framework;
27
		// register the OSGi command provider
28
		FrameworkCommandProvider.getInstance(framework);
29
	}
30
31
	public String runCommand(String commandLine) {
32
		if (commandLine == null || commandLine.length() == 0)
33
			return null;
34
		ByteArrayOutputStream out = new ByteArrayOutputStream();
35
		PrintWriter writer = new PrintWriter(out);
36
		BaseCommandInterpreter interpreter = new BaseCommandInterpreter(commandLine, getProviders(), writer);
37
		String command = interpreter.nextArgument();
38
		if (command != null)
39
			interpreter.execute(command);
40
		writer.flush();
41
		return out.toString();
42
	}
43
44
	public synchronized CommandProvider[] getProviders() {
45
		if (providers == null)
46
			providers = new ServiceTracker(framework.systemBundle.getBundleContext(), CommandProvider.class.getName(), null);
47
		ServiceReference[] serviceRefs = providers.getServiceReferences();
48
		Util.dsort(serviceRefs, 0, serviceRefs.length);
49
50
		CommandProvider[] serviceObjects = new CommandProvider[serviceRefs.length];
51
		for (int i = 0; i < serviceRefs.length; i++)
52
			serviceObjects[i] = (CommandProvider) framework.systemBundle.getBundleContext().getService(serviceRefs[i]);
53
		return serviceObjects;
54
	}
55
}
(-)core/framework/org/eclipse/osgi/framework/console/Console.java (+16 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
public interface Console {
15
	public String runCommand(String command);
16
}

Return to bug 162415