Skip to content

Commit 3f5a716

Browse files
committed
Implement a first delegation model for the old terminal view
Currently the old Terminal View is referenced in some other codebase and we can not update them all at once. To ease the transition this now do the following: - deprecate the bundles to make consumer aware of the new terminal view - implement a delegation where the old code calls redirect / transform to the new ones
1 parent 878cc37 commit 3f5a716

File tree

11 files changed

+206
-513
lines changed

11 files changed

+206
-513
lines changed

terminal/features/org.eclipse.tm.terminal.view.feature/feature.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
label="%featureName"
1414
version="12.2.0.qualifier"
1515
provider-name="%providerName"
16+
plugin="org.eclipse.tm.terminal.view.core"
1617
license-feature="org.eclipse.license"
1718
license-feature-version="0.0.0">
1819

@@ -29,8 +30,16 @@
2930
</license>
3031

3132
<requires>
32-
<import plugin="org.eclipse.terminal.view.core" />
33-
<import plugin="org.eclipse.terminal.view.ui" />
33+
<import plugin="org.eclipse.terminal.view.core"/>
34+
<import plugin="org.eclipse.terminal.view.ui"/>
3435
</requires>
3536

37+
<plugin
38+
id="org.eclipse.tm.terminal.view.core"
39+
version="0.0.0"/>
40+
41+
<plugin
42+
id="org.eclipse.tm.terminal.view.ui"
43+
version="0.0.0"/>
44+
3645
</feature>

terminal/plugins/org.eclipse.tm.terminal.view.core/META-INF/MANIFEST.MF

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
4-
Bundle-SymbolicName: org.eclipse.tm.terminal.view.core;singleton:=true
5-
Bundle-Version: 4.10.500.qualifier
4+
Bundle-SymbolicName: org.eclipse.tm.terminal.view.core;singleton:=true;deprecated:="Deprecated in favour of org.eclipse.terminal.view.core."
5+
Bundle-Version: 4.11.0.qualifier
66
Bundle-Activator: org.eclipse.tm.terminal.view.core.activator.CoreBundleActivator
77
Bundle-Vendor: %providerName
88
Require-Bundle: org.eclipse.core.expressions;bundle-version="[3.9.400,4)",
@@ -19,4 +19,5 @@ Export-Package: org.eclipse.tm.terminal.view.core,
1919
org.eclipse.tm.terminal.view.core.preferences,
2020
org.eclipse.tm.terminal.view.core.tracing,
2121
org.eclipse.tm.terminal.view.core.utils
22+
Import-Package: org.eclipse.terminal.view.core;version="1.0.0"
2223
Automatic-Module-Name: org.eclipse.tm.terminal.view.core

terminal/plugins/org.eclipse.tm.terminal.view.core/about.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# NOTE TO TRANSLATOR: Please do not translate the featureVersion variable.
2020

2121

22-
blurb=TM Terminal\n\
22+
blurb=Terminal (Console) View\n\
2323
\n\
2424
Version: {featureVersion}\n\
2525
Build id: {0}\n\

terminal/plugins/org.eclipse.tm.terminal.view.core/src/org/eclipse/tm/terminal/view/core/TerminalServiceFactory.java

Lines changed: 120 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,141 @@
1111
*******************************************************************************/
1212
package org.eclipse.tm.terminal.view.core;
1313

14-
import org.eclipse.core.runtime.IStatus;
15-
import org.eclipse.core.runtime.Platform;
14+
import java.util.LinkedHashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Map.Entry;
18+
import java.util.concurrent.CopyOnWriteArrayList;
19+
20+
import org.eclipse.core.runtime.ILog;
1621
import org.eclipse.core.runtime.Status;
1722
import org.eclipse.tm.terminal.view.core.activator.CoreBundleActivator;
1823
import org.eclipse.tm.terminal.view.core.interfaces.ITerminalService;
19-
import org.eclipse.tm.terminal.view.core.nls.Messages;
20-
import org.osgi.framework.Bundle;
24+
import org.eclipse.tm.terminal.view.core.interfaces.ITerminalTabListener;
2125

2226
/**
2327
* Terminal service factory implementation.
2428
* <p>
2529
* Provides access to the terminal service instance.
2630
*/
2731
public final class TerminalServiceFactory {
28-
private static ITerminalService instance = null;
29-
30-
static {
31-
// Tries to instantiate the terminal service implementation
32-
// from the o.e.tm.terminal.view.ui bundle
33-
Bundle bundle = Platform.getBundle("org.eclipse.tm.terminal.view.ui"); //$NON-NLS-1$
34-
if (bundle != null && bundle.getState() != Bundle.UNINSTALLED && bundle.getState() != Bundle.STOPPING) {
35-
try {
36-
Class<?> clazz = bundle.loadClass("org.eclipse.tm.terminal.view.ui.services.TerminalService"); //$NON-NLS-1$
37-
instance = (ITerminalService) clazz.getDeclaredConstructor().newInstance();
38-
} catch (Exception e) {
39-
if (Platform.inDebugMode()) {
40-
Platform.getLog(bundle).log(new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
41-
Messages.TerminalServiceFactory_error_serviceImplLoadFailed, e));
32+
33+
private static final String TM_TERMINAL = ".tm.terminal."; //$NON-NLS-1$
34+
35+
private static final class ITerminalServiceImplementation
36+
implements ITerminalService, org.eclipse.terminal.view.core.ITerminalTabListener {
37+
38+
private List<ITerminalTabListener> listeners = new CopyOnWriteArrayList<>();
39+
40+
@Override
41+
public void terminateConsole(Map<String, Object> properties, Done done) {
42+
org.eclipse.terminal.view.core.ITerminalService delegate = CoreBundleActivator.getTerminalService();
43+
if (delegate == null) {
44+
done.done(Status.error("Not running!")); //$NON-NLS-1$
45+
return;
46+
}
47+
delegate.terminateConsole(convert(properties)).handle((o, e) -> {
48+
if (e != null) {
49+
done.done(Status.error("Operation failed", e)); //$NON-NLS-1$
50+
} else {
51+
done.done(Status.OK_STATUS);
52+
}
53+
return null;
54+
});
55+
}
56+
57+
@Override
58+
public void openConsole(Map<String, Object> properties, Done done) {
59+
org.eclipse.terminal.view.core.ITerminalService delegate = CoreBundleActivator.getTerminalService();
60+
if (delegate == null) {
61+
done.done(Status.error("Not running!")); //$NON-NLS-1$
62+
return;
63+
}
64+
delegate.openConsole(convert(properties)).handle((o, e) -> {
65+
if (e != null) {
66+
done.done(Status.error("Operation failed", e)); //$NON-NLS-1$
67+
} else {
68+
done.done(Status.OK_STATUS);
69+
}
70+
return null;
71+
});
72+
73+
}
74+
75+
@Override
76+
public void closeConsole(Map<String, Object> properties, Done done) {
77+
org.eclipse.terminal.view.core.ITerminalService delegate = CoreBundleActivator.getTerminalService();
78+
if (delegate == null) {
79+
done.done(Status.error("Not running!")); //$NON-NLS-1$
80+
return;
81+
}
82+
delegate.closeConsole(convert(properties)).handle((o, e) -> {
83+
if (e != null) {
84+
done.done(Status.error("Operation failed", e)); //$NON-NLS-1$
85+
} else {
86+
done.done(Status.OK_STATUS);
87+
}
88+
return null;
89+
});
90+
91+
}
92+
93+
@Override
94+
public void addTerminalTabListener(ITerminalTabListener listener) {
95+
if (!listeners.contains(listener)) {
96+
if (listeners.add(listener)) {
97+
org.eclipse.terminal.view.core.ITerminalService delegate = CoreBundleActivator.getTerminalService();
98+
if (delegate == null) {
99+
return;
100+
}
101+
delegate.addTerminalTabListener(this);
42102
}
43103
}
44104
}
105+
106+
@Override
107+
public void removeTerminalTabListener(ITerminalTabListener listener) {
108+
listeners.remove(listener);
109+
}
110+
111+
@Override
112+
public void terminalTabDisposed(Object source, Object data) {
113+
for (ITerminalTabListener listener : listeners) {
114+
listener.terminalTabDisposed(source, data);
115+
}
116+
}
45117
}
46118

47-
/**
48-
* Returns the terminal service instance.
49-
*/
119+
public static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
120+
121+
private static final ITerminalService DELEGATE = new ITerminalServiceImplementation();
122+
50123
public static ITerminalService getService() {
51-
return instance;
124+
ILog.of(STACK_WALKER.getCallerClass()).warn(
125+
"This bundle is using the deprecated terminal API consider migration of your bundle to the new 'org.eclipse.terminal.view.core'"); //$NON-NLS-1$
126+
return DELEGATE;
127+
}
128+
129+
private static Map<String, Object> convert(Map<String, Object> properties) {
130+
if (properties == null) {
131+
return null;
132+
}
133+
LinkedHashMap<String, Object> enhanced = new LinkedHashMap<>(properties);
134+
for (Entry<String, Object> entry : properties.entrySet()) {
135+
String key = (String) transform(entry.getKey());
136+
Object value = transform(entry.getValue());
137+
enhanced.put(key, value);
138+
}
139+
return enhanced;
140+
}
141+
142+
private static Object transform(Object object) {
143+
if (object instanceof String s) {
144+
if (s.contains(TM_TERMINAL)) {
145+
//like org.eclipse.tm.terminal.view.core...
146+
return s.replace(TM_TERMINAL, ".terminal."); //$NON-NLS-1$
147+
}
148+
}
149+
return object;
52150
}
53151
}

terminal/plugins/org.eclipse.tm.terminal.view.core/src/org/eclipse/tm/terminal/view/core/activator/CoreBundleActivator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111
*******************************************************************************/
1212
package org.eclipse.tm.terminal.view.core.activator;
1313

14+
import org.eclipse.terminal.view.core.ITerminalService;
1415
import org.osgi.framework.BundleActivator;
1516
import org.osgi.framework.BundleContext;
17+
import org.osgi.util.tracker.ServiceTracker;
1618

1719
/**
1820
* The activator class controls the plug-in life cycle
1921
*/
2022
public class CoreBundleActivator implements BundleActivator {
2123
// The bundle context
2224
private static BundleContext context;
25+
private ServiceTracker<ITerminalService, ITerminalService> serviceTracker;
26+
private static CoreBundleActivator instance;
2327

2428
/**
2529
* Returns the bundle context
@@ -42,7 +46,18 @@ public static String getUniqueIdentifier() {
4246

4347
@Override
4448
public void start(BundleContext bundleContext) throws Exception {
49+
instance = this;
4550
CoreBundleActivator.context = bundleContext;
51+
serviceTracker = new ServiceTracker<>(bundleContext, ITerminalService.class, null);
52+
serviceTracker.open();
53+
}
54+
55+
public static ITerminalService getTerminalService() {
56+
CoreBundleActivator activator = instance;
57+
if (activator == null) {
58+
return null;
59+
}
60+
return activator.serviceTracker.getService();
4661
}
4762

4863
@Override

0 commit comments

Comments
 (0)