Package: LocalInputOutputManager

LocalInputOutputManager

nameinstructionbranchcomplexitylinemethod
LocalInputOutputManager(BundleContext)
M: 29 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getDelegatingPrintStreamFromServiceRegistry(BundleContext, String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getErr()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getIn()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getOut()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPrintStreamFromServiceRegistry(BundleContext, String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getService(Class, BundleContext, String)
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
grabSystemIO()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
releaseSystemIO()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
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: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.shell.internal;
13:
14: import java.io.FileDescriptor;
15: import java.io.FileInputStream;
16: import java.io.InputStream;
17: import java.io.PrintStream;
18:
19: import org.osgi.framework.BundleContext;
20: import org.osgi.framework.InvalidSyntaxException;
21: import org.osgi.framework.ServiceReference;
22:
23: import org.eclipse.virgo.medic.log.DelegatingPrintStream;
24:
25:
26: /**
27: * <p>
28: * LocalInputOutputManager can grab the system input and output streams from the service registry and block and release them
29: * </p>
30: *
31: * <strong>Concurrent Semantics</strong><br />
32: *
33: * LocalInputOutputManager is thread safe
34: *
35: */
36: public final class LocalInputOutputManager {
37:
38: private static final String SERVICE_FILTER_SYSERR_DELEGATE = "(org.eclipse.virgo.medic.log.printStream=delegating.System.err)";
39:
40: private static final String SERVICE_FILTER_SYSOUT_DELEGATE = "(org.eclipse.virgo.medic.log.printStream=delegating.System.out)";
41:
42: private final InputStream in;
43:
44: private final PrintStream out;
45:
46: private final PrintStream err;
47:
48: private final DelegatingPrintStream delegatingSysOut;
49:
50: private final DelegatingPrintStream delegatingSysErr;
51:
52: public LocalInputOutputManager(BundleContext bundleContext) {
53: this.in = new FileInputStream(FileDescriptor.in);
54:
55: this.out = getPrintStreamFromServiceRegistry(bundleContext, "(org.eclipse.virgo.medic.log.printStream=System.out)");
56: this.err = getPrintStreamFromServiceRegistry(bundleContext, "(org.eclipse.virgo.medic.log.printStream=System.err)");
57:
58: this.delegatingSysOut = getDelegatingPrintStreamFromServiceRegistry(bundleContext,SERVICE_FILTER_SYSOUT_DELEGATE);
59: this.delegatingSysErr = getDelegatingPrintStreamFromServiceRegistry(bundleContext,SERVICE_FILTER_SYSERR_DELEGATE);
60: }
61:
62: /**
63: *
64: */
65: public void grabSystemIO() {
66: this.delegatingSysOut.setDelegate(null);
67: this.delegatingSysErr.setDelegate(null);
68: }
69:
70: /**
71: *
72: */
73: public void releaseSystemIO() {
74: this.delegatingSysOut.setDelegate(this.out);
75: this.delegatingSysErr.setDelegate(this.err);
76: }
77:
78: /**
79: * Get the original {@link PrintStream} for the local command line
80: * @return PrintStream
81: */
82: public PrintStream getErr() {
83: return err;
84: }
85:
86: /**
87: * Get the original {@link PrintStream} for the local command line
88: * @return PrintStream
89: */
90: public PrintStream getOut() {
91: return out;
92: }
93:
94: /**
95: * Get the original {@link InputStream} for the local command line
96: * @return InputStream
97: */
98: public InputStream getIn() {
99: return in;
100: }
101:
102: private static PrintStream getPrintStreamFromServiceRegistry(BundleContext bundleContext, String filter) {
103: return (PrintStream) getService(PrintStream.class, bundleContext, filter);
104: }
105:
106: private static DelegatingPrintStream getDelegatingPrintStreamFromServiceRegistry(BundleContext bundleContext, String filter) {
107: return (DelegatingPrintStream) getService(DelegatingPrintStream.class, bundleContext, filter);
108: }
109:
110: private static Object getService(Class<?> clazz, BundleContext bundleContext, String filter) {
111: ServiceReference<?>[] serviceReferences;
112: try {
113: serviceReferences = bundleContext.getServiceReferences(clazz.getName(), filter);
114: } catch (InvalidSyntaxException e) {
115: throw new RuntimeException("Unexpected InvalidSyntaxException", e);
116: }
117:• if (serviceReferences != null && serviceReferences.length > 0) {
118: return bundleContext.getService(serviceReferences[0]);
119: } else {
120: return null;
121: }
122: }
123: }