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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*******************************************************************************
* Copyright (c) 2002, 2008 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
*
* Initial Contributors:
* The following IBM employees contributed to the Remote System Explorer
* component that contains this file: David McKnight, Kushal Munir,
* Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson,
* Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
*
* Contributors:
* Martin Oberhuber (Wind River) - [186773] split ISystemRegistryUI from ISystemRegistry
* David McKnight (IBM) - [216252] SystemMessages using RSEStatus
* David McKnight (IBM) - [216252] [api][nls] Resource Strings specific to subsystems should be moved from rse.ui into files.ui / shells.ui / processes.ui where possible
* David McKnight (IBM) - [220547] [api][breaking] SimpleSystemMessage needs to specify a message id and some messages should be shared
*******************************************************************************/
package org.eclipse.rse.connectorservice.dstore.util;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.dstore.core.model.DataElement;
import org.eclipse.dstore.extra.DomainEvent;
import org.eclipse.dstore.extra.IDomainListener;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.osgi.util.NLS;
import org.eclipse.rse.core.RSECorePlugin;
import org.eclipse.rse.core.model.ISystemRegistry;
import org.eclipse.rse.core.subsystems.IConnectorService;
import org.eclipse.rse.core.subsystems.SubSystemConfiguration;
import org.eclipse.rse.internal.connectorservice.dstore.Activator;
import org.eclipse.rse.services.clientserver.messages.CommonMessages;
import org.eclipse.rse.services.clientserver.messages.ICommonMessageIds;
import org.eclipse.rse.services.clientserver.messages.SimpleSystemMessage;
import org.eclipse.rse.services.clientserver.messages.SystemMessage;
import org.eclipse.rse.ui.RSEUIPlugin;
import org.eclipse.rse.ui.SystemBasePlugin;
import org.eclipse.rse.ui.messages.SystemMessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
public class ConnectionStatusListener implements IDomainListener, IRunnableWithProgress
{
protected DataElement _dataStoreStatus;
protected IConnectorService _connection;
protected boolean _connectionDown = false;
/**
* @param status The status element for the DataStore handling this connection.
*/
public ConnectionStatusListener(DataElement status, IConnectorService connection)
{
_dataStoreStatus = status;
_connection = connection;
}
protected Shell internalGetShell()
{
Shell activeShell = SystemBasePlugin.getActiveWorkbenchShell();
if (activeShell != null)
{
return activeShell;
}
IWorkbenchWindow window = null;
try
{
window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
catch (Exception e)
{
return null;
}
if (window == null)
{
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
if (windows != null && windows.length > 0)
{
return windows[0].getShell();
}
}
else
{
return window.getShell();
}
return null;
}
/**
* The handleConnectionDown method is invoked if the network connection between the
* client and server goes down while connected. Currently this method displays
* an error to the user and the subsytem is disconnected.
*/
protected void handleConnectionDown()
{
Display.getDefault().asyncExec(new ConnectionDown(this));
}
class ConnectionDown implements Runnable
{
private ConnectionStatusListener _listener;
public ConnectionDown(ConnectionStatusListener listener)
{
_listener = listener;
}
public void run()
{
Shell shell = getShell();
_connectionDown = true;
String fmsgStr = NLS.bind(CommonMessages.MSG_CONNECT_UNKNOWNHOST, _connection.getPrimarySubSystem().getHost().getAliasName());
SystemMessage msg = new SimpleSystemMessage(Activator.PLUGIN_ID, ICommonMessageIds.MSG_CONNECT_UNKNOWNHOST, IStatus.ERROR, fmsgStr);
SystemMessageDialog dialog = new SystemMessageDialog(internalGetShell(), msg);
dialog.open();
try
{
IRunnableContext runnableContext = getRunnableContext(getShell());
runnableContext.run(false,true,_listener); // inthread, cancellable, IRunnableWithProgress
_connection.reset();
ISystemRegistry sr = RSECorePlugin.getTheSystemRegistry();
sr.connectedStatusChange(_connection.getPrimarySubSystem(), false, true, true);
}
catch (InterruptedException exc) // user cancelled
{
if (shell != null)
showDisconnectCancelledMessage(shell, _connection.getHostName(), _connection.getPort());
}
catch (java.lang.reflect.InvocationTargetException invokeExc) // unexpected error
{
Exception exc = (Exception)invokeExc.getTargetException();
if (shell != null)
showDisconnectErrorMessage(shell, _connection.getHostName(), _connection.getPort(), exc);
}
catch (Exception e)
{
SystemBasePlugin.logError("ConnectionStatusListener: Error disconnecting", e); //$NON-NLS-1$
}
}
}
/**
* @see IDomainListener#listeningTo(DomainEvent)
*/
public boolean listeningTo(DomainEvent event)
{
if (_dataStoreStatus == event.getParent())
{
return true;
}
return false;
}
/**
* @see IDomainListener#domainChanged(DomainEvent)
*/
public void domainChanged(DomainEvent event)
{
if (!_dataStoreStatus.getName().equals("okay")) //$NON-NLS-1$
{
handleConnectionDown();
}
}
public Shell getShell()
{
return internalGetShell();
}
/**
* Callback method for the IConnectorService to determine if the connection is down. This is
* called by the disconnect method to determine if we can do saves or not.
*/
public boolean isConnectionDown()
{
return _connectionDown;
}
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
String message = null;
message = SubSystemConfiguration.getDisconnectingMessage(_connection.getHostName(), _connection.getPort());
monitor.beginTask(message, IProgressMonitor.UNKNOWN);
try
{
_connection.disconnect(monitor);
}
catch(Exception exc)
{
if (exc instanceof java.lang.reflect.InvocationTargetException)
throw (java.lang.reflect.InvocationTargetException)exc;
if (exc instanceof java.lang.InterruptedException)
throw (java.lang.InterruptedException)exc;
throw new java.lang.reflect.InvocationTargetException(exc);
}
finally
{
monitor.done();
}
}
/**
* Get the progress monitor dialog for this operation. We try to
* use one for all phases of a single operation, such as connecting
* and resolving.
*/
protected IRunnableContext getRunnableContext(Shell rshell)
{
Shell shell = getShell();
// for other cases, use statusbar
IWorkbenchWindow win = SystemBasePlugin.getActiveWorkbenchWindow();
if (win != null)
{
Shell winShell = RSEUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell();
if (winShell != null && !winShell.isDisposed() && winShell.isVisible())
{
SystemBasePlugin.logInfo("Using active workbench window as runnable context"); //$NON-NLS-1$
shell = winShell;
return win;
}
else
{
win = null;
}
}
if (shell == null || shell.isDisposed() || !shell.isVisible())
{
SystemBasePlugin.logInfo("Using progress monitor dialog with given shell as parent"); //$NON-NLS-1$
shell = rshell;
}
IRunnableContext dlg = new ProgressMonitorDialog(rshell);
return dlg;
}
/**
* Show an error message when the disconnection fails.
* Shows a common message by default.
* Overridable.
*/
protected void showDisconnectErrorMessage(Shell shell, String hostName, int port, Exception exc)
{
String dfailedMsg = NLS.bind(CommonMessages.MSG_DISCONNECT_FAILED, hostName);
try{
SystemMessage msg = new SimpleSystemMessage(Activator.PLUGIN_ID, ICommonMessageIds.MSG_DISCONNECT_FAILED, IStatus.ERROR, dfailedMsg, exc);
SystemMessageDialog msgDlg = new SystemMessageDialog(shell, msg);
msgDlg.setException(exc);
msgDlg.open();
}
catch (Exception e){
}
}
/**
* Show an error message when the user cancels the disconnection.
* Shows a common message by default.
* Overridable.
*/
protected void showDisconnectCancelledMessage(Shell shell, String hostName, int port)
{
String msg = NLS.bind(CommonMessages.MSG_DISCONNECT_CANCELLED, hostName);
SystemMessageDialog msgDlg = new SystemMessageDialog(shell, new SimpleSystemMessage(Activator.PLUGIN_ID, ICommonMessageIds.MSG_DISCONNECT_CANCELLED, IStatus.CANCEL, msg));
msgDlg.open();
}
}
|