Skip to content

Commit 949ad11

Browse files
committed
Terminal Connector creation: throw instead of returning null
Returning `null` from the `create` method is very confusing. If terminal connector cannot be created the `CoreException` should be thrown
1 parent dc45f3a commit 949ad11

File tree

11 files changed

+98
-74
lines changed

11 files changed

+98
-74
lines changed

terminal/bundles/org.eclipse.terminal.connector.local/src/org/eclipse/terminal/connector/local/launcher/LocalLauncherDelegate.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2012, 2018 Wind River Systems, Inc. and others. All rights reserved.
2+
* Copyright (c) 2012, 2025 Wind River Systems, Inc. and others. All rights reserved.
33
* This program and the accompanying materials are made available under the terms
44
* of the Eclipse Public License 2.0 which accompanies this distribution, and is
55
* available at https://www.eclipse.org/legal/epl-2.0/
@@ -9,6 +9,7 @@
99
* Contributors:
1010
* Wind River Systems - initial API and implementation
1111
* Dirk Fauth <[email protected]> - Bug 460496
12+
* Alexander Fedorov (ArSysOp) - further evolution
1213
*******************************************************************************/
1314
package org.eclipse.terminal.connector.local.launcher;
1415

@@ -292,7 +293,7 @@ private final File defaultShell() {
292293
}
293294

294295
@Override
295-
public ITerminalConnector createTerminalConnector(Map<String, Object> properties) {
296+
public ITerminalConnector createTerminalConnector(Map<String, Object> properties) throws CoreException {
296297
Assert.isNotNull(properties);
297298

298299
// Check for the terminal connector id
@@ -419,13 +420,10 @@ public ITerminalConnector createTerminalConnector(Map<String, Object> properties
419420

420421
// Construct the terminal connector instance
421422
ITerminalConnector connector = TerminalConnectorExtension.makeTerminalConnector(connectorId);
422-
if (connector != null) {
423-
// Apply default settings
424-
connector.setDefaultSettings();
425-
// And load the real settings
426-
connector.load(store);
427-
}
428-
423+
// Apply default settings
424+
connector.setDefaultSettings();
425+
// And load the real settings
426+
connector.load(store);
429427
return connector;
430428
}
431429
}

terminal/bundles/org.eclipse.terminal.connector.process/src/org/eclipse/terminal/connector/process/internal/ProcessLauncherDelegate.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2015, 2018 Wind River Systems, Inc. and others. All rights reserved.
2+
* Copyright (c) 2015, 2025 Wind River Systems, Inc. and others. All rights reserved.
33
* This program and the accompanying materials are made available under the terms
44
* of the Eclipse Public License 2.0 which accompanies this distribution, and is
55
* available at https://www.eclipse.org/legal/epl-2.0/
@@ -8,13 +8,15 @@
88
*
99
* Contributors:
1010
* Wind River Systems - initial API and implementation
11+
* Alexander Fedorov (ArSysOp) - further evolution
1112
*******************************************************************************/
1213
package org.eclipse.terminal.connector.process.internal;
1314

1415
import java.util.Map;
1516

1617
import org.eclipse.cdt.utils.pty.PTY;
1718
import org.eclipse.core.runtime.Assert;
19+
import org.eclipse.core.runtime.CoreException;
1820
import org.eclipse.terminal.connector.ISettingsStore;
1921
import org.eclipse.terminal.connector.ITerminalConnector;
2022
import org.eclipse.terminal.connector.InMemorySettingsStore;
@@ -55,7 +57,7 @@ public void execute(Map<String, Object> properties, ITerminalService.Done done)
5557
}
5658

5759
@Override
58-
public ITerminalConnector createTerminalConnector(Map<String, Object> properties) {
60+
public ITerminalConnector createTerminalConnector(Map<String, Object> properties) throws CoreException {
5961
Assert.isNotNull(properties);
6062

6163
// Check for the terminal connector id
@@ -113,13 +115,10 @@ public ITerminalConnector createTerminalConnector(Map<String, Object> properties
113115

114116
// Construct the terminal connector instance
115117
ITerminalConnector connector = TerminalConnectorExtension.makeTerminalConnector(connectorId);
116-
if (connector != null) {
117-
// Apply default settings
118-
connector.setDefaultSettings();
119-
// And load the real settings
120-
connector.load(store);
121-
}
122-
118+
// Apply default settings
119+
connector.setDefaultSettings();
120+
// And load the real settings
121+
connector.load(store);
123122
return connector;
124123
}
125124

terminal/bundles/org.eclipse.terminal.connector.ssh/src/org/eclipse/terminal/connector/ssh/launcher/SshLauncherDelegate.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2018 Wind River Systems, Inc. and others. All rights reserved.
2+
* Copyright (c) 2011, 2025 Wind River Systems, Inc. and others. All rights reserved.
33
* This program and the accompanying materials are made available under the terms
44
* of the Eclipse Public License 2.0 which accompanies this distribution, and is
55
* available at https://www.eclipse.org/legal/epl-2.0/
@@ -9,6 +9,7 @@
99
* Contributors:
1010
* Wind River Systems - initial API and implementation
1111
* Max Weninger (Wind River) - [361352] [TERMINALS][SSH] Add SSH terminal support
12+
* Alexander Fedorov (ArSysOp) - further evolution
1213
*******************************************************************************/
1314
package org.eclipse.terminal.connector.ssh.launcher;
1415

@@ -17,6 +18,7 @@
1718
import java.util.Map;
1819

1920
import org.eclipse.core.runtime.Assert;
21+
import org.eclipse.core.runtime.CoreException;
2022
import org.eclipse.osgi.util.NLS;
2123
import org.eclipse.terminal.connector.ISettingsStore;
2224
import org.eclipse.terminal.connector.ITerminalConnector;
@@ -118,7 +120,7 @@ public <T> T getAdapter(Class<T> adapter) {
118120
}
119121

120122
@Override
121-
public ITerminalConnector createTerminalConnector(Map<String, Object> properties) {
123+
public ITerminalConnector createTerminalConnector(Map<String, Object> properties) throws CoreException {
122124
Assert.isNotNull(properties);
123125

124126
// Check for the terminal connector id
@@ -171,13 +173,10 @@ public ITerminalConnector createTerminalConnector(Map<String, Object> properties
171173

172174
// Construct the terminal connector instance
173175
ITerminalConnector connector = TerminalConnectorExtension.makeTerminalConnector(connectorId);
174-
if (connector != null) {
175-
// Apply default settings
176-
connector.setDefaultSettings();
177-
// And load the real settings
178-
connector.load(store);
179-
}
180-
176+
// Apply default settings
177+
connector.setDefaultSettings();
178+
// And load the real settings
179+
connector.load(store);
181180
return connector;
182181
}
183182
}

terminal/bundles/org.eclipse.terminal.connector.telnet/src/org/eclipse/terminal/connector/telnet/launcher/TelnetLauncherDelegate.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2018 Wind River Systems, Inc. and others. All rights reserved.
2+
* Copyright (c) 2011, 2025 Wind River Systems, Inc. and others. All rights reserved.
33
* This program and the accompanying materials are made available under the terms
44
* of the Eclipse Public License 2.0 which accompanies this distribution, and is
55
* available at https://www.eclipse.org/legal/epl-2.0/
@@ -9,6 +9,7 @@
99
* Contributors:
1010
* Wind River Systems - initial API and implementation
1111
* Max Weninger (Wind River) - [366374] [TERMINALS][TELNET] Add Telnet terminal support
12+
* Alexander Fedorov (ArSysOp) - further evolution
1213
*******************************************************************************/
1314
package org.eclipse.terminal.connector.telnet.launcher;
1415

@@ -17,6 +18,7 @@
1718
import java.util.Map;
1819

1920
import org.eclipse.core.runtime.Assert;
21+
import org.eclipse.core.runtime.CoreException;
2022
import org.eclipse.osgi.util.NLS;
2123
import org.eclipse.terminal.connector.ISettingsStore;
2224
import org.eclipse.terminal.connector.ITerminalConnector;
@@ -108,7 +110,7 @@ public <T> T getAdapter(Class<T> adapter) {
108110
}
109111

110112
@Override
111-
public ITerminalConnector createTerminalConnector(Map<String, Object> properties) {
113+
public ITerminalConnector createTerminalConnector(Map<String, Object> properties) throws CoreException {
112114
Assert.isNotNull(properties);
113115

114116
// Check for the terminal connector id
@@ -154,13 +156,10 @@ public ITerminalConnector createTerminalConnector(Map<String, Object> properties
154156

155157
// Construct the terminal connector instance
156158
ITerminalConnector connector = TerminalConnectorExtension.makeTerminalConnector(connectorId);
157-
if (connector != null) {
158-
// Apply default settings
159-
connector.setDefaultSettings();
160-
// And load the real settings
161-
connector.load(store);
162-
}
163-
159+
// Apply default settings
160+
connector.setDefaultSettings();
161+
// And load the real settings
162+
connector.load(store);
164163
return connector;
165164
}
166165
}

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/connector/TerminalConnectorExtension.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2006, 2018 Wind River Systems, Inc. and others.
2+
* Copyright (c) 2006, 2025 Wind River Systems, Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -11,16 +11,21 @@
1111
* Michael Scharf (Wind River) - initial API and implementation
1212
* Martin Oberhuber (Wind River) - fixed copyright headers and beautified
1313
* Uwe Stieber (Wind River) - [282996] [terminal][api] Add "hidden" attribute to terminal connector extension point
14+
* Alexander Fedorov (ArSysOp) - further evolution
1415
*******************************************************************************/
1516
package org.eclipse.terminal.connector;
1617

1718
import java.util.ArrayList;
1819
import java.util.List;
1920

21+
import org.eclipse.core.runtime.CoreException;
2022
import org.eclipse.core.runtime.IConfigurationElement;
2123
import org.eclipse.core.runtime.RegistryFactory;
24+
import org.eclipse.core.runtime.Status;
25+
import org.eclipse.osgi.util.NLS;
2226
import org.eclipse.terminal.connector.provider.AbstractTerminalConnector;
2327
import org.eclipse.terminal.internal.connector.TerminalConnector;
28+
import org.eclipse.terminal.internal.control.impl.TerminalMessages;
2429

2530
/**
2631
* A factory to get {@link ITerminalConnector} instances.
@@ -51,18 +56,19 @@ static private ITerminalConnector makeConnector(final IConfigurationElement conf
5156
* @param id the id of the terminal connector in the
5257
* <code>org.eclipse.terminal.control.connectors</code>
5358
* extension point
54-
* @return a new ITerminalConnector with id or <code>null</code> if there
55-
* is no extension with that id.
59+
* @return a new ITerminalConnector for the given id.
60+
* @throws CoreException if connector cannot be created for provided input
5661
*/
57-
public static ITerminalConnector makeTerminalConnector(String id) {
62+
public static ITerminalConnector makeTerminalConnector(String id) throws CoreException {
5863
IConfigurationElement[] config = RegistryFactory.getRegistry()
5964
.getConfigurationElementsFor("org.eclipse.terminal.control.connectors"); //$NON-NLS-1$
6065
for (int i = 0; i < config.length; i++) {
6166
if (id.equals(config[i].getAttribute("id"))) { //$NON-NLS-1$
6267
return makeConnector(config[i]);
6368
}
6469
}
65-
return null;
70+
throw new CoreException(
71+
Status.error(NLS.bind(TerminalMessages.TerminalConnectorExtension_e_no_connector_for_id, id)));
6672
}
6773

6874
/**

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/TerminalMessages.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2006, 2018 Wind River Systems, Inc. and others.
2+
* Copyright (c) 2006, 2025 Wind River Systems, Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
99
* Michael Scharf (Wind River) - initial API and implementation
1010
* Martin Oberhuber (Wind River) - fixed copyright headers and beautified
1111
* Martin Oberhuber (Wind River) - [378691][api] push Preferences into the Widget
12+
* Alexander Fedorov (ArSysOp) - further evolution
1213
*******************************************************************************/
1314
package org.eclipse.terminal.internal.control.impl;
1415

@@ -46,6 +47,7 @@ public class TerminalMessages extends NLS {
4647
public static String TerminalColorsFieldEditor_TextColor;
4748
public static String TerminalColorsFieldEditor_White;
4849
public static String TerminalColorsFieldEditor_Yellow;
50+
public static String TerminalConnectorExtension_e_no_connector_for_id;
4951
public static String TerminalError;
5052
public static String SocketError;
5153
public static String IOError;

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/TerminalMessages.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
###############################################################################
2-
# Copyright (c) 2003, 2020 Wind River Systems, Inc. and others.
2+
# Copyright (c) 2003, 2025 Wind River Systems, Inc. and others.
33
# All rights reserved. This program and the accompanying materials
44
# are made available under the terms of the Eclipse Public License 2.0
55
# which accompanies this distribution, and is available at
@@ -16,6 +16,7 @@
1616
# Michael Scharf (Wind River) - split into core, view and connector plugins
1717
# Martin Oberhuber (Wind River) - fixed copyright headers and beautified
1818
# Martin Oberhuber (Wind River) - [378691][api] push Preferences into the Widget
19+
# Alexander Fedorov (ArSysOp) - further evolution
1920
###############################################################################
2021

2122
# NLS_MESSAGEFORMAT_VAR
@@ -46,6 +47,7 @@ TerminalColorsFieldEditor_Selection=Selection
4647
TerminalColorsFieldEditor_TextColor=Text color
4748
TerminalColorsFieldEditor_White=White
4849
TerminalColorsFieldEditor_Yellow=Yellow
50+
TerminalConnectorExtension_e_no_connector_for_id=No terminal connector registered for id {0}
4951
TerminalError = Terminal Error
5052
SocketError = Socket Error
5153
IOError = IO Error

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/TerminalService.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.eclipse.terminal.view.core.ITerminalsConnectorConstants;
3232
import org.eclipse.terminal.view.ui.IUIConstants;
3333
import org.eclipse.terminal.view.ui.TerminalViewId;
34+
import org.eclipse.terminal.view.ui.launcher.ILauncherDelegate;
3435
import org.eclipse.terminal.view.ui.launcher.ILauncherDelegateManager;
3536
import org.eclipse.terminal.view.ui.launcher.ITerminalConsoleViewManager;
3637
import org.eclipse.ui.PlatformUI;
@@ -180,29 +181,28 @@ protected final void executeServiceOperation(final Map<String, Object> propertie
180181
title = normalizeTitle(title, data);
181182

182183
// Create the terminal connector instance
183-
final ITerminalConnector connector = createTerminalConnector(properties);
184-
if (connector == null) {
184+
ITerminalConnector connector;
185+
try {
186+
connector = createTerminalConnector(properties);
187+
} catch (CoreException e) {
185188
// Properties contain invalid connector arguments
186189
if (done != null) {
187-
done.done(Status.error(Messages.TerminalService_error_cannotCreateConnector));
190+
done.done(e.getStatus());
188191
}
189192
return;
190193
}
194+
executeServiceOperation(runnable, new TerminalViewId(id, secondaryId), title, connector, data, done);
195+
}
191196

192-
// Finalize the used variables
193-
final String finId = id;
194-
final String finSecondaryId = secondaryId;
195-
final String finTitle = title;
196-
final Object finData = data;
197-
TerminalViewId tvid = new TerminalViewId(finId, finSecondaryId);
198-
197+
private void executeServiceOperation(final TerminalServiceRunnable runnable, TerminalViewId tvid,
198+
final String title, final ITerminalConnector connector, final Object data, final Done done) {
199199
// Execute the operation
200200
if (!runnable.isExecuteAsync()) {
201-
runnable.run(tvid, finTitle, connector, finData, done);
201+
runnable.run(tvid, title, connector, data, done);
202202
} else {
203203
try {
204204
Display display = PlatformUI.getWorkbench().getDisplay();
205-
display.asyncExec(() -> runnable.run(tvid, finTitle, connector, finData, done));
205+
display.asyncExec(() -> runnable.run(tvid, title, connector, data, done));
206206
} catch (Exception e) {
207207
// if display is disposed, silently ignore.
208208
}
@@ -243,14 +243,18 @@ protected String normalizeTitle(String title, Object data) {
243243
* Creates the terminal connector configured within the given properties.
244244
*
245245
* @param properties The terminal console properties. Must not be <code>null</code>.
246-
* @return The terminal connector or <code>null</code>.
246+
* @return The created terminal connector
247+
* @throws CoreException if connector cannot be created for provided input
247248
*/
248-
protected ITerminalConnector createTerminalConnector(Map<String, Object> properties) {
249+
protected ITerminalConnector createTerminalConnector(Map<String, Object> properties) throws CoreException {
249250
Assert.isNotNull(properties);
250-
return Optional.of(properties).map(map -> map.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID))
251-
.filter(String.class::isInstance).map(String.class::cast)
252-
.flatMap(id -> launchDelegateManager.findLauncherDelegate(id, false))
253-
.map(d -> d.createTerminalConnector(properties)).orElse(null);
251+
ILauncherDelegate delegate = Optional.of(properties)
252+
.map(map -> map.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID)).filter(String.class::isInstance)
253+
.map(String.class::cast).flatMap(id -> launchDelegateManager.findLauncherDelegate(id, false))
254+
.orElseThrow(
255+
() -> new CoreException(Status.error(Messages.TerminalService_error_cannotCreateConnector)));
256+
257+
return delegate.createTerminalConnector(properties);
254258
}
255259

256260
@Override

0 commit comments

Comments
 (0)