#5207 Add validation of credentials on target server before starting application...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / RemotingLoginDialog.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.ui.dialog;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.core.runtime.preferences.ConfigurationScope;
26 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.custom.StyledText;
29 import org.eclipse.swt.events.MouseAdapter;
30 import org.eclipse.swt.events.MouseEvent;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.swt.layout.FillLayout;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Combo;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Dialog;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.forms.events.ExpansionEvent;
46 import org.eclipse.ui.forms.events.IExpansionListener;
47 import org.eclipse.ui.forms.widgets.ExpandableComposite;
48 import org.eclipse.ui.progress.IProgressConstants;
49 import org.eclipse.wb.swt.SWTResourceManager;
50 import org.osgi.service.prefs.BackingStoreException;
51 import org.osgi.service.prefs.Preferences;
52 import org.springframework.security.authentication.BadCredentialsException;
53 import org.springframework.security.authentication.LockedException;
54 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
55
56 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
57 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
58 import eu.etaxonomy.cdm.api.service.IUserService;
59 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
60 import eu.etaxonomy.taxeditor.model.MessagingUtils;
61 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
62 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
63 import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
64 import eu.etaxonomy.taxeditor.remoting.server.CDMServerUtils;
65 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
66 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
67 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
68 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
69 import eu.etaxonomy.taxeditor.store.CdmStore;
70 import eu.etaxonomy.taxeditor.store.LoginManager;
71 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
72 import eu.etaxonomy.taxeditor.webapp.CDMEmbeddedServerException;
73 import eu.etaxonomy.taxeditor.webapp.CDMServer;
74 import eu.etaxonomy.taxeditor.webapp.ICDMServerError;
75
76
77 /**
78 * @author cmathew
79 * @date 20 Jan 2015
80 *
81 */
82 public class RemotingLoginDialog extends Dialog implements ICDMServerError {
83
84 protected Object result;
85 protected Shell shlConnect;
86 private Text txtCdmServerStatus;
87 private Text txtCdmInstanceStatus;
88 private Combo comboCdmServer;
89 private Combo comboCdmInstance;
90 private Button btnConnect;
91
92 private final Map<String, CdmServerInfo> csiiMap = new HashMap<String, CdmServerInfo>();
93
94 private final static String STATUS_AVAILABLE = "Available";
95 private final static String STATUS_NOT_AVAILABLE = "Not Available";
96 private final static String STATUS_STARTED = "Started";
97 private final static String STATUS_NOT_STARTED = "Not Started";
98 private final static String STATUS_RETRIEVING = "Retrieving ...";
99 private final static String STATUS_CHECKING_AVAILABILITY = "Checking ...";
100 private final static String STATUS_NO_INSTANCES = "No Instances Found";
101 private final static String STATUS_ERROR = "Error";
102 private final static String STATUS_REMOTING_NOT_ACTIVATED = "Remoting not activated";
103 private final static String STATUS_NOT_COMPATIBLE = "Not Compatible";
104
105 private final static String MESG_COMPATIBLE_EDITOR_OLD = "Please update the Taxonomic Editor (Help->Check for Updates) or choose a compatible cdm-server";
106 private final static String MESG_COMPATIBLE_SERVER_OLD = "Please choose a compatible cdm-server or update the chosen cdm-server";
107
108 private final static String STORE_PREFERENCES_NODE = "eu.etaxonomy.taxeditor.store";
109
110 private final static String LOGIN_NODE = "login";
111 private final static String USERNAME_SUFFIX = "_username";
112 private final static String PASSWORD_SUFFIX = "_password";
113
114 private final static String LAST_SERVER_INSTANCE_NODE = "lastServerInstance";
115 private final static String LAST_SERVER_KEY = "lastServerKey";
116 private final static String LAST_INSTANCE_KEY = "lastInstanceKey";
117
118 private final static String REFRESH_LABEL = "Refresh";
119
120 private Composite remotingComposite;
121 private CdmServerInfo selectedCsii;
122 private CdmInstanceInfo selectedCdmInstance;
123 private Button btnCdmServerRefresh;
124 private Button btnCdmInstanceRefresh;
125 private Button btnStopServer;
126 private Composite loginComposite;
127 private Label lblLogin;
128 private Text txtLogin;
129 private Label lblPassword;
130 private Text txtPassword;
131 private Button btnRememberMe;
132 private Composite compAdvanced;
133 private Label lblPort;
134 private Text txtPort;
135 private Label lblServerVersion;
136 private Text txtServerVersion;
137 private ExpandableComposite xpndblcmpstAdvanced;
138 private StyledText styledTxtMessage;
139
140
141 private final int MIN_WIDTH = 530;
142 private final int MIN_HEIGHT = 220;
143 private final int MIN_EXP_HEIGHT = 380;
144 private final int MESSAGE_HEIGHT = 25;
145 private Label lblEditorVersion;
146 private Text txtEditorVersion;
147 private Label lblServerCDMVersion;
148 private Text txtServerCDMVersion;
149 private Label lblEditorCDMVersion;
150 private Text txtEditorCDMVersion;
151
152 private String serverName, instanceName;
153 private boolean autoConnect = false;
154 private boolean loadLoginPrefs = true;
155 private boolean isDevRemoteSource = false;
156 private Job serverJob;
157 /**
158 * Create the dialog.
159 * @param parent
160 * @param style
161 */
162 public RemotingLoginDialog(Shell parent, int style) {
163 super(parent, style);
164 setText("Login");
165 }
166
167 public Object open(CdmRemoteSource source, boolean loadLoginPrefs, boolean autoConnect) {
168 this.loadLoginPrefs = loadLoginPrefs;
169 this.serverName = source.getName();
170 String contextPath = source.getContextPath();
171 this.instanceName = contextPath.substring(contextPath.lastIndexOf("/") + 1);
172 return open(serverName, instanceName, loadLoginPrefs, autoConnect);
173 }
174
175
176 public Object open(String serverName, String instanceName, boolean loadLoginPrefs, boolean autoConnect) {
177 this.serverName = serverName;
178 this.instanceName = instanceName;
179 this.loadLoginPrefs = loadLoginPrefs;
180 this.autoConnect = autoConnect;
181 return open();
182 }
183
184 /**
185 * Open the dialog.
186 * @return the result
187 */
188 public Object open() {
189
190 createContents();
191 if(serverName == null && instanceName == null) {
192 readPrefLastServerInstance();
193 }
194
195 setEditorInfo();
196 populateCdmServerCombo();
197 shlConnect.open();
198 shlConnect.layout();
199
200 xpndblcmpstAdvanced.setExpanded(false);
201
202 Display display = getParent().getDisplay();
203
204 while (!shlConnect.isDisposed()) {
205 if (!display.readAndDispatch()) {
206 display.sleep();
207 }
208 }
209
210 return result;
211 }
212
213 /**
214 * Create contents of the dialog.
215 */
216 private void createContents() {
217 shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM);
218 shlConnect.setMinimumSize(new Point(MIN_WIDTH, MIN_HEIGHT));
219 shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
220 shlConnect.setText("Connect");
221 shlConnect.setLayout(new FillLayout(SWT.HORIZONTAL));
222
223 remotingComposite = new Composite(shlConnect, SWT.NONE);
224 remotingComposite.setLayout(new GridLayout(1, false));
225
226 Composite cdmServerComposite = new Composite(remotingComposite, SWT.NONE);
227 GridData gd_cdmServerComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
228 gd_cdmServerComposite.heightHint = 68;
229 cdmServerComposite.setLayoutData(gd_cdmServerComposite);
230 cdmServerComposite.setLayout(new GridLayout(4, false));
231
232 Label lblCdmServer = new Label(cdmServerComposite, SWT.NONE);
233 lblCdmServer.setText("CDM Server : ");
234 lblCdmServer.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
235 lblCdmServer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
236
237 comboCdmServer = new Combo(cdmServerComposite, SWT.READ_ONLY);
238 comboCdmServer.addSelectionListener(new SelectionAdapter() {
239 @Override
240 public void widgetSelected(SelectionEvent e) {
241 refreshCdmServer();
242
243 }
244 });
245 GridData gd_comboCdmServer = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
246 gd_comboCdmServer.widthHint = 150;
247 comboCdmServer.setLayoutData(gd_comboCdmServer);
248 comboCdmServer.select(0);
249
250 txtCdmServerStatus = new Text(cdmServerComposite, SWT.BORDER);
251 txtCdmServerStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
252 txtCdmServerStatus.setEditable(false);
253 GridData gd_txtCdmServerStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
254 gd_txtCdmServerStatus.widthHint = 100;
255 txtCdmServerStatus.setLayoutData(gd_txtCdmServerStatus);
256
257 btnCdmServerRefresh = new Button(cdmServerComposite, SWT.NONE);
258 btnCdmServerRefresh.addSelectionListener(new SelectionAdapter() {
259 @Override
260 public void widgetSelected(SelectionEvent e) {
261 refreshCdmServer();
262 }
263 });
264 btnCdmServerRefresh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
265 btnCdmServerRefresh.setText("Refresh");
266
267 Label lblCdmInstance = new Label(cdmServerComposite, SWT.NONE);
268 GridData gd_lblCdmInstance = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
269 gd_lblCdmInstance.heightHint = 30;
270 lblCdmInstance.setLayoutData(gd_lblCdmInstance);
271 lblCdmInstance.setText("CDM Instance : ");
272 lblCdmInstance.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
273
274 comboCdmInstance = new Combo(cdmServerComposite, SWT.READ_ONLY);
275 comboCdmInstance.addSelectionListener(new SelectionAdapter() {
276 @Override
277 public void widgetSelected(SelectionEvent e) {
278 refreshCdmInstance();
279 }
280 });
281 GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
282 gd_comboCdmInstance.widthHint = 150;
283 comboCdmInstance.setLayoutData(gd_comboCdmInstance);
284 comboCdmInstance.select(0);
285
286 txtCdmInstanceStatus = new Text(cdmServerComposite, SWT.BORDER);
287 txtCdmInstanceStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
288 txtCdmInstanceStatus.setEditable(false);
289 GridData gd_txtCdmInstanceStatus = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
290 gd_txtCdmInstanceStatus.widthHint = 100;
291 txtCdmInstanceStatus.setLayoutData(gd_txtCdmInstanceStatus);
292
293 btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
294 btnCdmInstanceRefresh.addSelectionListener(new SelectionAdapter() {
295 @Override
296 public void widgetSelected(SelectionEvent e) {
297 refreshCdmInstance();
298 }
299 });
300 GridData gd_btnCdmInstanceRefresh = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
301 gd_btnCdmInstanceRefresh.widthHint = 110;
302 gd_btnCdmInstanceRefresh.heightHint = 30;
303 btnCdmInstanceRefresh.setLayoutData(gd_btnCdmInstanceRefresh);
304 btnCdmInstanceRefresh.setText("Refresh");
305
306 loginComposite = new Composite(remotingComposite, SWT.NONE);
307 GridData gd_loginComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
308 gd_loginComposite.widthHint = 487;
309 gd_loginComposite.heightHint = 70;
310 loginComposite.setLayoutData(gd_loginComposite);
311 GridLayout gl_loginComposite = new GridLayout(6, false);
312 gl_loginComposite.marginTop = 5;
313 loginComposite.setLayout(gl_loginComposite);
314
315 lblLogin = new Label(loginComposite, SWT.CENTER);
316 GridData gd_lblLogin = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
317 gd_lblLogin.widthHint = 50;
318 lblLogin.setLayoutData(gd_lblLogin);
319 lblLogin.setText("Login : ");
320 lblLogin.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
321
322 txtLogin = new Text(loginComposite, SWT.BORDER);
323 GridData gd_txtLogin = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
324 gd_txtLogin.minimumWidth = 80;
325 gd_txtLogin.widthHint = 80;
326 gd_txtLogin.heightHint = 15;
327 txtLogin.setLayoutData(gd_txtLogin);
328
329 lblPassword = new Label(loginComposite, SWT.CENTER);
330 lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
331 lblPassword.setText("Password : ");
332 lblPassword.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
333
334 txtPassword = new Text(loginComposite, SWT.BORDER | SWT.PASSWORD);
335 GridData gd_txtPassword = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
336 gd_txtPassword.minimumWidth = 80;
337 gd_txtPassword.widthHint = 80;
338 gd_txtPassword.heightHint = 15;
339 txtPassword.setLayoutData(gd_txtPassword);
340 new Label(loginComposite, SWT.NONE);
341
342 btnConnect = new Button(loginComposite, SWT.FLAT);
343 btnConnect.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
344 btnConnect.addMouseListener(new MouseAdapter() {
345 @Override
346 public void mouseUp(MouseEvent e) {
347 if(selectedCsii.isLocalhostMgd() && !isSelectedCdmInstanceRunningInManagedServer()) {
348 startManagedServer();
349 } else {
350 connect();
351 }
352 }
353 });
354 btnConnect.setText("Connect");
355
356 btnRememberMe = new Button(loginComposite, SWT.CHECK);
357 btnRememberMe.setSelection(true);
358 GridData gd_btnRememberMe = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
359 gd_btnRememberMe.widthHint = 107;
360 btnRememberMe.setLayoutData(gd_btnRememberMe);
361 btnRememberMe.setText("Remember Me");
362 new Label(loginComposite, SWT.NONE);
363 new Label(loginComposite, SWT.NONE);
364 new Label(loginComposite, SWT.NONE);
365 new Label(loginComposite, SWT.NONE);
366
367 styledTxtMessage = new StyledText(remotingComposite, SWT.NONE);
368 styledTxtMessage.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
369 styledTxtMessage.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
370 styledTxtMessage.setFont(SWTResourceManager.getFont("Ubuntu", 12, SWT.BOLD));
371 styledTxtMessage.setSelectionBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
372 styledTxtMessage.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
373 styledTxtMessage.setDoubleClickEnabled(false);
374 styledTxtMessage.setEditable(false);
375 GridData gd_styledTxtMessage = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
376 gd_styledTxtMessage.exclude = true;
377 gd_styledTxtMessage.minimumHeight = MESSAGE_HEIGHT;
378 gd_styledTxtMessage.heightHint = MESSAGE_HEIGHT;
379 styledTxtMessage.setLayoutData(gd_styledTxtMessage);
380
381 xpndblcmpstAdvanced = new ExpandableComposite(remotingComposite, SWT.NONE, ExpandableComposite.TWISTIE);
382 GridData gd_xpndblcmpstAdvanced = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
383 gd_xpndblcmpstAdvanced.heightHint = 19;
384 xpndblcmpstAdvanced.setLayoutData(gd_xpndblcmpstAdvanced);
385 xpndblcmpstAdvanced.addExpansionListener(new IExpansionListener() {
386 @Override
387 public void expansionStateChanged(ExpansionEvent e) {
388 GridData gridData = (GridData) xpndblcmpstAdvanced.getLayoutData();
389 if(e.getState()) {
390 shlConnect.setSize(MIN_WIDTH, MIN_EXP_HEIGHT);
391 } else {
392 shlConnect.setSize(MIN_WIDTH, MIN_HEIGHT);
393 }
394
395 }
396 @Override
397 public void expansionStateChanging(ExpansionEvent e) {
398 }
399 });
400 xpndblcmpstAdvanced.setText("advanced");
401 xpndblcmpstAdvanced.setExpanded(true);
402
403 compAdvanced = new Composite(xpndblcmpstAdvanced, SWT.NONE);
404 xpndblcmpstAdvanced.setClient(compAdvanced);
405 compAdvanced.setLayout(new GridLayout(4, false));
406
407 lblPort = new Label(compAdvanced, SWT.CENTER);
408 lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
409 lblPort.setSize(0, 0);
410 lblPort.setText("Port : ");
411 lblPort.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
412
413 txtPort = new Text(compAdvanced, SWT.BORDER);
414 GridData gd_txtPort = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
415 gd_txtPort.minimumWidth = 50;
416 gd_txtPort.widthHint = 50;
417 txtPort.setLayoutData(gd_txtPort);
418
419 lblServerVersion = new Label(compAdvanced, SWT.CENTER);
420 lblServerVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
421 lblServerVersion.setText("Server Cdmlib Version :");
422 lblServerVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
423
424 txtServerVersion = new Text(compAdvanced, SWT.BORDER);
425 txtServerVersion.setEditable(false);
426 txtServerVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
427 btnStopServer = new Button(compAdvanced, SWT.FLAT);
428 btnStopServer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
429 btnStopServer.addMouseListener(new MouseAdapter() {
430 @Override
431 public void mouseUp(MouseEvent e) {
432 stopManagedServer();
433 }
434 });
435 btnStopServer.setText("Stop Managed Server");
436
437 lblEditorVersion = new Label(compAdvanced, SWT.CENTER);
438 lblEditorVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
439 lblEditorVersion.setText("Editor Cdmlib Version :");
440 lblEditorVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
441
442 txtEditorVersion = new Text(compAdvanced, SWT.BORDER);
443 txtEditorVersion.setEditable(false);
444 txtEditorVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
445 new Label(compAdvanced, SWT.NONE);
446 new Label(compAdvanced, SWT.NONE);
447
448 lblServerCDMVersion = new Label(compAdvanced, SWT.CENTER);
449 lblServerCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
450 lblServerCDMVersion.setText("Server CDM Version :");
451 lblServerCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
452
453 txtServerCDMVersion = new Text(compAdvanced, SWT.BORDER);
454 txtServerCDMVersion.setEditable(false);
455 txtServerCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
456 new Label(compAdvanced, SWT.NONE);
457 new Label(compAdvanced, SWT.NONE);
458
459 lblEditorCDMVersion = new Label(compAdvanced, SWT.CENTER);
460 lblEditorCDMVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
461 lblEditorCDMVersion.setText("Editor CDM Version :");
462 lblEditorCDMVersion.setFont(SWTResourceManager.getFont("Ubuntu", 9, SWT.NORMAL));
463
464 txtEditorCDMVersion = new Text(compAdvanced, SWT.BORDER);
465 txtEditorCDMVersion.setEditable(false);
466 txtEditorCDMVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
467
468 }
469
470
471
472 private void populateCdmServerCombo() {
473 Job job = new Job("Retrieve Server Instances") {
474 @Override
475 protected IStatus run(IProgressMonitor monitor) {
476 Display.getDefault().syncExec(new Runnable() {
477 @Override
478 public void run() {
479 for(CdmServerInfo csii : CdmServerInfo.getCdmServers()) {
480 csiiMap.put(csii.getName(), csii);
481 comboCdmServer.add(csii.getName());
482 }
483 int serverIndex = -1;
484 if(serverName != null) {
485 serverIndex = comboCdmServer.indexOf(serverName);
486 }
487 if(serverIndex == -1) {
488 comboCdmServer.select(0);
489 autoConnect = false;
490 } else {
491 comboCdmServer.select(serverIndex);
492 }
493 CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
494 if(devRemoteSource != null) {
495 isDevRemoteSource = true;
496 String username = System.getProperty("cdm.server.dev.username");
497 String password = System.getProperty("cdm.server.dev.password");
498 if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
499 txtLogin.setText(username);
500 txtPassword.setText(password);
501 CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
502 }
503 } else {
504 refreshCdmServer();
505 }
506 }
507 });
508 return Status.OK_STATUS;
509 }
510 };
511 job.schedule();
512 }
513
514
515 private void refreshCdmServer() {
516 txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
517 clearOnServerChange();
518 emptyCredentials();
519 updateSelectedCdmServer();
520 Display.getDefault().asyncExec(new Runnable() {
521 @Override
522 public void run() {
523 checkSelectedCdmServer();
524 }
525 });
526
527 }
528
529 private void updateSelectedCdmServer() {
530 int selIndex = comboCdmServer.getSelectionIndex();
531 if(selIndex != -1) {
532 selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
533 }
534 }
535
536 private void updatePort() {
537 txtPort.setText("");
538 if(selectedCsii != null) {
539 int port = selectedCsii.getPort();
540 if(port == CdmServerInfo.NULL_PORT) {
541 txtPort.setText(CdmServerInfo.NULL_PORT_STRING);
542 } else {
543 txtPort.setText(String.valueOf(port));
544 }
545 }
546 }
547
548 private int getPort() {
549 int port = CdmServerInfo.NULL_PORT;
550 try {
551 port = Integer.valueOf(txtPort.getText());
552 } catch (NumberFormatException nfe) {
553 if(!CdmServerInfo.NULL_PORT_STRING.equals(txtPort.getText())) {
554 setMessage("Port should be an integer");
555 }
556 }
557 return port;
558 }
559
560 private void checkSelectedCdmServer() {
561 if(selectedCsii != null) {
562 if(selectedCsii.isLocalhost()) {
563 txtPort.setEditable(true);
564 txtPort.setEnabled(true);
565 }
566 if(selectedCsii.pingServer()) {
567 txtCdmServerStatus.setText(STATUS_AVAILABLE);
568 populateCdmInstanceCombo(true);
569 String serverVersionTimestamp = generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified());
570 txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion() + ":" + serverVersionTimestamp);
571 } else {
572 txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
573 comboCdmInstance.removeAll();
574 disableCdmInstanceControls("", "");
575 }
576 }
577 updatePort();
578 }
579
580
581 private void populateCdmInstanceCombo(final boolean forceRefresh) {
582 comboCdmInstance.removeAll();
583 comboCdmInstance.setEnabled(false);
584 btnConnect.setEnabled(false);
585 txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
586 txtCdmInstanceStatus.setToolTipText("");
587
588 serverJob = new Job("Retrieve Server Instances") {
589 @Override
590 protected IStatus run(IProgressMonitor monitor) {
591 try {
592 if(selectedCsii != null) {
593 if(forceRefresh) {
594 selectedCsii.refreshInstances();
595 }
596 final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
597 Display.getDefault().asyncExec(new Runnable() {
598 @Override
599 public void run() {
600 if(!instances.isEmpty()) {
601 for(CdmInstanceInfo cdmInstance : instances) {
602 comboCdmInstance.add(cdmInstance.getName());
603 }
604 int instanceIndex = -1;
605 if(instanceName != null) {
606 instanceIndex = comboCdmInstance.indexOf(instanceName);
607 }
608 if(instanceIndex == -1) {
609 comboCdmInstance.select(0);
610 autoConnect = false;
611 } else {
612 comboCdmInstance.select(instanceIndex);
613 }
614 refreshCdmInstance();
615 comboCdmInstance.setEnabled(true);
616 if(autoConnect) {
617 connect();
618 }
619
620 } else {
621 txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
622 btnConnect.setEnabled(false);
623 }
624 }
625 });
626 }
627 } catch (final CDMServerException e) {
628 MessagingUtils.warn(getClass(), e);
629 Display.getDefault().asyncExec(new Runnable() {
630 @Override
631 public void run() {
632 disableCdmInstanceControls(STATUS_NOT_AVAILABLE, e.getMessage());
633 }
634 });
635 }
636 return Status.OK_STATUS;
637 }
638 };
639
640 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
641 // Start the Job
642 serverJob.schedule();
643 }
644 }
645
646 private void disableCdmInstanceControls(String cdmInstanceStatus, String tooltip) {
647 txtCdmInstanceStatus.setText(cdmInstanceStatus);
648 txtCdmInstanceStatus.setToolTipText(tooltip);
649 comboCdmInstance.setEnabled(false);
650 btnConnect.setEnabled(false);
651
652 }
653 private void refreshCdmInstance() {
654 txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
655 clearOnInstanceChange();
656 updateSelectedCdmInstance();
657 checkSelectedCdmInstance();
658 updateManagedServerControls();
659 }
660
661 private void updateSelectedCdmInstance() {
662 int selIndex = comboCdmInstance.getSelectionIndex();
663 if(selIndex != -1) {
664 selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
665
666 if(loadLoginPrefs && !isDevRemoteSource) {
667 readPrefCredentials();
668 }
669 }
670 }
671
672 private void updateManagedServerControls() {
673 if(selectedCsii.isLocalhostMgd()) {
674 if(isSelectedCdmInstanceRunningInManagedServer()) {
675 txtCdmInstanceStatus.setText(STATUS_STARTED);
676 } else {
677 txtCdmInstanceStatus.setText(STATUS_NOT_STARTED);
678 }
679 btnConnect.setEnabled(true);
680 selectedCsii.setPort(getManagedServerPort());
681 updatePort();
682 }
683
684
685 if(isManagedServerRunning()) {
686 btnStopServer.setEnabled(true);
687 } else {
688 btnStopServer.setEnabled(false);
689 }
690 }
691
692 private boolean isManagedServerRunning() {
693 return CdmStore.getManagedServer() != null && CdmStore.getManagedServer().isAlive();
694 }
695
696 private boolean isSelectedCdmInstanceRunningInManagedServer() {
697 return CdmStore.getManagedServer() != null &&
698 CdmStore.getManagedServer().isAlive() &&
699 selectedCsii.isLocalhostMgd() &&
700 CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName());
701 }
702
703 private void startManagedServer() {
704 if(isManagedServerRunning()) {
705 if(CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName())) {
706 return;
707 } else {
708 Display.getDefault().syncExec(new Runnable() {
709 @Override
710 public void run() {
711 stopManagedServer();
712 }
713 });
714 }
715 }
716
717
718 Job job = new Job("Managed CDM Server Launch") {
719
720 @Override
721 public IStatus run(IProgressMonitor monitor) {
722 String mgdServerConfigFileName = "mgd.datasources.xml";
723 String config = CDMServerUtils.convertEditorToServerConfig();
724 File managedServerConfigFile;
725 int maxUnits = 50;
726 monitor.beginTask("Launching Managed CDM Server", maxUnits);
727 try {
728 monitor.subTask("Generating datasources config file for " + selectedCdmInstance.getName());
729 managedServerConfigFile = CDMServerUtils.writeManagedServerConfig(config, mgdServerConfigFileName);
730 monitor.worked(1);
731 CdmStore.setManagedServer(new CDMServer(selectedCdmInstance.getName(), managedServerConfigFile));
732 monitor.subTask("Starting Managed CDM Server. This may take a while.");
733 CdmStore.getManagedServer().start(false, RemotingLoginDialog.this);
734 int serverUnits = 0;
735
736 // the following loop is a 'fake' progress monitoring where the progress
737 // bar is advanced by one unit every second until maxUnits -2
738 while(!CdmStore.getManagedServer().isStarted()) {
739 if(serverUnits < maxUnits - 2) {
740 try {
741 Thread.sleep(1000);
742 } catch (InterruptedException e) {
743 }
744 monitor.worked(1);
745 serverUnits++;
746 }
747 }
748 Display.getDefault().asyncExec(new Runnable() {
749 @Override
750 public void run() {
751 hide(false);
752 updateManagedServerControls();
753 connect();
754 }
755 });
756 } catch (IOException ioe) {
757 MessagingUtils.errorDialog("Error generating server config file",
758 this,
759 ioe.getMessage(),
760 TaxeditorStorePlugin.PLUGIN_ID,
761 ioe,
762 true);
763 } catch (CDMEmbeddedServerException cse) {
764 MessagingUtils.errorDialog("Error starting managed server",
765 this,
766 cse.getMessage(),
767 TaxeditorStorePlugin.PLUGIN_ID,
768 cse,
769 true);
770 } finally {
771 monitor.done();
772 }
773 return Status.OK_STATUS;
774 }
775 };
776
777 // configure the job
778 job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
779 job.setUser(true);
780 // schedule job
781 hide(true);
782 job.schedule();
783 }
784
785 private void stopManagedServer() {
786 try {
787 CdmStore.getManagedServer().stop();
788 } catch (Exception e) {
789 MessagingUtils.errorDialog("Error stopping managed server",
790 this,
791 "Could not stop managed server running at port " + CdmStore.getManagedServer().getPort() + ". Please stop it manually",
792 TaxeditorStorePlugin.PLUGIN_ID,
793 e,
794 true);
795 }
796 CdmStore.setManagedServer(null);
797 updateManagedServerControls();
798 }
799
800 private int getManagedServerPort() {
801 return CdmStore.getManagedServer() == null ? CdmServerInfo.NULL_PORT : CdmStore.getManagedServer().getPort();
802 }
803
804 private void checkSelectedCdmInstance() {
805 boolean available = false;
806 String status = STATUS_NOT_AVAILABLE;
807 String message = null;
808
809 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
810 try {
811 if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
812 status = STATUS_AVAILABLE;
813 available = true;
814 } else {
815 status = STATUS_NOT_AVAILABLE;
816 available = false;
817 }
818
819 if(available) {
820 txtServerCDMVersion.setText(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()).getDbSchemaVersion());
821 int compareDbSchemaVersion = selectedCsii.compareDbSchemaVersion(selectedCdmInstance, getPort());
822 int compareCdmlibServicesVersion = 0;
823 boolean disableServicesApiTimestampCheck =
824 PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
825 if(!disableServicesApiTimestampCheck) {
826 compareCdmlibServicesVersion = selectedCsii.compareCdmlibServicesVersion();
827 }
828 if(compareDbSchemaVersion > 0 || compareCdmlibServicesVersion > 0) {
829 status = STATUS_NOT_COMPATIBLE;
830 available = false;
831 message = MESG_COMPATIBLE_EDITOR_OLD;
832 } else if(compareDbSchemaVersion < 0 || compareCdmlibServicesVersion < 0) {
833 status = STATUS_NOT_COMPATIBLE;
834 available = false;
835 message = MESG_COMPATIBLE_SERVER_OLD;
836 } else {
837 status = STATUS_AVAILABLE;
838 available = true;
839 message = "";
840 }
841 }
842 } catch (Exception e) {
843 txtCdmInstanceStatus.setToolTipText(e.getMessage());
844 } finally {
845 btnConnect.setEnabled(available);
846 txtCdmInstanceStatus.setText(status);
847 if(!StringUtils.isBlank(message)) {
848 setMessage(message);
849 }
850 }
851 }
852 }
853
854 private void connect() {
855 checkSelectedCdmInstance();
856
857 if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
858 return;
859 }
860
861 ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
862
863 if(!validateLogin(source)) {
864 return;
865 }
866
867 try {
868 CdmStore.connect(source, this);
869 } catch (Exception e) {
870 // Do not expect anything to go wrong at this point, so we throw a runtime exception
871 // if any problems
872 throw new RuntimeException(e);
873 }
874
875 }
876
877
878 public boolean isRememberMe() {
879 return btnRememberMe.getSelection();
880 }
881
882 private void persistPrefLastServerInstance() {
883 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
884 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
885
886 lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
887 lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
888
889 flushPreferences(lastServerInstancePrefs);
890 }
891
892 private void persistPrefCredentials() {
893 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
894 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
895 credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
896 credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
897 flushPreferences(credentialsPrefs);
898 }
899
900 private void flushPreferences(Preferences prefs) {
901 try {
902 prefs.flush();
903 } catch (BackingStoreException bse) {
904 setMessage(bse.getMessage());
905 }
906 }
907
908 private void readPrefCredentials() {
909 String username, password;
910 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
911 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
912 username = credentialsPrefs.get(getUsernamePrefKey(), "");
913 txtLogin.setText(username);
914 password = credentialsPrefs.get(getPasswordPrefKey(),"");
915 txtPassword.setText(password);
916 if(username.isEmpty() || password.isEmpty()) {
917 autoConnect = false;
918 }
919 }
920
921 private void readPrefLastServerInstance() {
922 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
923 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
924
925 serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
926 instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
927 }
928
929 private void emptyCredentials() {
930 txtLogin.setText("");
931 txtPassword.setText("");
932 }
933
934 private String getUsernamePrefKey() {
935 return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
936 }
937
938 private String getPasswordPrefKey() {
939 return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
940 }
941
942 private boolean validateLogin(ICdmRemoteSource remoteSource) {
943 if(getUsername() == null || getUsername().isEmpty()) {
944 setMessage("User login cannot be empty");
945 return false;
946 }
947 if(getPassword() == null || getPassword().isEmpty()) {
948 setMessage("Password cannot be empty");
949 return false;
950 }
951
952
953 try {
954 IUserService userService = CdmApplicationRemoteConfiguration.getUserService(remoteSource);
955 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(getUsername(), getPassword());
956 CdmApplicationRemoteConfiguration.getAuthenticationManager(userService).authenticate(token);
957 } catch(BadCredentialsException e){
958 setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
959 return false;
960 } catch(LockedException e){
961 setMessage(LoginManager.ACCOUNT_LOCKED_MESSAGE);
962 return false;
963 } catch(IllegalArgumentException e){
964 setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
965 return false;
966 }
967 return true;
968 }
969
970 public String getUsername() {
971 return txtLogin.getText();
972 }
973
974 public String getPassword() {
975 return txtPassword.getText();
976 }
977
978 public void setMessage(String message) {
979 if(message != null && !message.isEmpty()) {
980 if(message.length() > 60) {
981 styledTxtMessage.setToolTipText(message);
982 message = message.substring(0, 60) + "...";
983 }
984 styledTxtMessage.setText(message);
985 styledTxtMessage.setVisible(true);
986 ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
987 shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
988 shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
989 } else {
990 styledTxtMessage.setText("");
991 styledTxtMessage.setVisible(false);
992 ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
993 shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
994 shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
995 }
996 remotingComposite.layout();
997 }
998
999 private int getHeightWithoutMessage() {
1000 if(xpndblcmpstAdvanced.isExpanded()) {
1001 return MIN_EXP_HEIGHT;
1002 } else {
1003 return MIN_HEIGHT;
1004 }
1005 }
1006
1007
1008 public void hide(boolean isHidden) {
1009 if(shlConnect != null && shlConnect.getDisplay() != null) {
1010 shlConnect.setVisible(!isHidden);
1011 }
1012 }
1013 public void dispose() {
1014 if(shlConnect != null && shlConnect.getDisplay() != null) {
1015 shlConnect.dispose();
1016 }
1017 }
1018
1019 public void onComplete() {
1020 Display.getDefault().asyncExec(new Runnable() {
1021 @Override
1022 public void run() {
1023 if(!isDevRemoteSource) {
1024 if(isRememberMe()) {
1025 persistPrefCredentials();
1026 }
1027 persistPrefLastServerInstance();
1028 }
1029 dispose();
1030 }
1031 });
1032 }
1033
1034 private String generateLastModifiedTooltip(String cdmlibLastModified) {
1035 if(StringUtils.isBlank(cdmlibLastModified)) {
1036 return "";
1037 }
1038 SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.z");
1039 Date cdmlibLastModifiedDate;
1040 String cdmlibLastModifiedTimestamp = "";
1041
1042 cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
1043 cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
1044
1045 return cdmlibLastModifiedTimestamp;
1046 }
1047
1048 private void setEditorInfo() {
1049 txtEditorCDMVersion.setText(CdmMetaData.getDbSchemaVersion());
1050 String editorVersionTimestamp = generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified());
1051 txtEditorVersion.setText(CdmApplicationState.getCdmlibVersion() + ":" + editorVersionTimestamp);
1052 }
1053
1054 private void clearOnServerChange() {
1055 setMessage("");
1056 txtServerCDMVersion.setText("");
1057 txtServerVersion.setText("");
1058 txtServerVersion.setToolTipText("");
1059 txtServerCDMVersion.setText("");
1060 comboCdmInstance.removeAll();
1061 txtCdmInstanceStatus.setText("");
1062 txtPort.setEditable(false);
1063 txtPort.setEnabled(false);
1064 }
1065
1066 private void clearOnInstanceChange() {
1067 setMessage("");
1068 txtServerCDMVersion.setText("");
1069 }
1070
1071 /**
1072 * {@inheritDoc}
1073 */
1074 @Override
1075 public void handleError(final Throwable t) {
1076
1077 Display.getDefault().syncExec(new Runnable() {
1078 @Override
1079 public void run() {
1080 serverJob.cancel();
1081
1082 String title = "CDM Server launch error";
1083 String message = t.getMessage();
1084
1085
1086 MessagingUtils.errorDialog(title,
1087 this,
1088 message,
1089 TaxeditorStorePlugin.PLUGIN_ID,
1090 t,
1091 true);
1092 }
1093 });
1094 }
1095
1096
1097 }