Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 97cd3aae24b54712e4a58bede3a68423965809cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 * Copyright (c) 2009, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.framework.console;

import java.io.InputStream;
import java.io.OutputStream;
import org.osgi.framework.*;

/**
 * A console session service provides the input and output to a single console session.
 * The input will be used by the console to read in console commands.  The output will
 * be used to print the results of console commands. 
 * <p>
 * The console session must be registered as an OSGi service in order to be associated 
 * with a console instance. The console implementation will discover any console session 
 * services and will create a new console instance using the console session for input and 
 * output.  When a session is closed then the console session service will be unregistered 
 * and the console instance will terminate and be disposed of.  The console instance will 
 * also terminate if the console session service is unregistered for any reason.
 * </p>
 * @since 3.6
 */
public abstract class ConsoleSession implements ServiceFactory<Object> {
	private volatile ServiceRegistration<Object> sessionRegistration;

	/**
	 * Called by the console implementation to free resources associated
	 * with this console session.  This method will result in the console
	 * session service being unregistered from the service registry.
	 * @noreference This method is not intended to be referenced by clients.
	 */
	public final void close() {
		doClose();
		ServiceRegistration<Object> current = sessionRegistration;
		if (current != null) {
			sessionRegistration = null;
			try {
				current.unregister();
			} catch (IllegalStateException e) {
				// This can happen if the service is in the process of being 
				// unregistered or if another thread unregistered the service.
				// Ignoring the exception.
			}
		}
	}

	/**
	 * Called by the {@link #close()} method to free resources associated 
	 * with this console session.  For example, closing the streams 
	 * associated with the input and output for this session.
	 * @noreference This method is not intended to be referenced by clients.
	 */
	protected abstract void doClose();

	/**
	 * Returns the input for this console session.  This input will be used
	 * to read console commands from the user of the session.
	 * @return the input for this console session
	 * @noreference This method is not intended to be referenced by clients.
	 */
	public abstract InputStream getInput();

	/**
	 * Returns the output for this console session.  This output will be 
	 * used to write the results of console commands.
	 * @return the output for this console session.
	 * @noreference This method is not intended to be referenced by clients.
	 */
	public abstract OutputStream getOutput();

	/**
	 * @noreference This method is not intended to be referenced by clients.
	 */
	public final Object getService(Bundle bundle, ServiceRegistration<Object> registration) {
		if (sessionRegistration == null)
			sessionRegistration = registration;
		return this;
	}

	/**
	 * @noreference This method is not intended to be referenced by clients.
	 */
	public final void ungetService(Bundle bundle, ServiceRegistration<Object> registration, Object service) {
		// do nothing
	}

}

Back to the top