|
11 | 11 | *******************************************************************************/
|
12 | 12 | package org.eclipse.tm.terminal.view.core;
|
13 | 13 |
|
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; |
16 | 21 | import org.eclipse.core.runtime.Status;
|
17 | 22 | import org.eclipse.tm.terminal.view.core.activator.CoreBundleActivator;
|
18 | 23 | 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; |
21 | 25 |
|
22 | 26 | /**
|
23 | 27 | * Terminal service factory implementation.
|
24 | 28 | * <p>
|
25 | 29 | * Provides access to the terminal service instance.
|
26 | 30 | */
|
27 | 31 | 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); |
42 | 102 | }
|
43 | 103 | }
|
44 | 104 | }
|
| 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 | + } |
45 | 117 | }
|
46 | 118 |
|
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 | + |
50 | 123 | 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; |
52 | 150 | }
|
53 | 151 | }
|
0 commit comments