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