a46299076a3daaa045f0c8ed8a7bad7e6b7c449a
[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
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 String username = System.getProperty("cdm.server.dev.username");
491 String password = System.getProperty("cdm.server.dev.password");
492 if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
493 txtLogin.setText(username);
494 txtPassword.setText(password);
495 CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
496 }
497 } else {
498 refreshCdmServer();
499 }
500 }
501 });
502 return Status.OK_STATUS;
503 }
504 };
505 job.schedule();
506 }
507
508
509 private void refreshCdmServer() {
510 Display.getDefault().asyncExec(new Runnable() {
511 @Override
512 public void run() {
513 txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
514 updateSelectedCdmServer();
515 checkSelectedCdmServer();
516 updatePort();
517 }
518 });
519 }
520
521 private void updateSelectedCdmServer() {
522 int selIndex = comboCdmServer.getSelectionIndex();
523 if(selIndex != -1) {
524 selectedCsii = csiiMap.get(comboCdmServer.getItem(selIndex));
525 }
526 }
527
528 private void updatePort() {
529 txtPort.setText("");
530 if(selectedCsii != null) {
531 int port = selectedCsii.getPort();
532 if(port == CdmServerInfo.NULL_PORT) {
533 txtPort.setText(CdmServerInfo.NULL_PORT_STRING);
534 } else {
535 txtPort.setText(String.valueOf(port));
536 }
537 }
538 }
539
540 private int getPort() {
541 int port = CdmServerInfo.NULL_PORT;
542 try {
543 port = Integer.valueOf(txtPort.getText());
544 } catch (NumberFormatException nfe) {
545 if(!CdmServerInfo.NULL_PORT_STRING.equals(txtPort.getText())) {
546 setMessage("Port should be an integer");
547 }
548 }
549 return port;
550 }
551
552 private void checkSelectedCdmServer() {
553
554 clearOnServerChange();
555 emptyCredentials();
556
557 if(selectedCsii != null) {
558 if(selectedCsii.isLocalhost()) {
559 txtPort.setEditable(true);
560 txtPort.setEnabled(true);
561 }
562 if(selectedCsii.pingServer()) {
563 txtCdmServerStatus.setText(STATUS_AVAILABLE);
564 populateCdmInstanceCombo(true);
565 String serverVersionTimestamp = generateLastModifiedTooltip(selectedCsii.getCdmlibLastModified());
566 txtServerVersion.setText(selectedCsii.getCdmlibServicesVersion() + ":" + serverVersionTimestamp);
567 } else {
568 txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
569 comboCdmInstance.removeAll();
570 disableCdmInstanceControls("", "");
571 }
572 }
573 }
574
575
576 private void populateCdmInstanceCombo(final boolean forceRefresh) {
577 comboCdmInstance.removeAll();
578 comboCdmInstance.setEnabled(false);
579 btnConnect.setEnabled(false);
580 txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
581 txtCdmInstanceStatus.setToolTipText("");
582
583 serverJob = new Job("Retrieve Server Instances") {
584 @Override
585 protected IStatus run(IProgressMonitor monitor) {
586 try {
587 if(selectedCsii != null) {
588 if(forceRefresh) {
589 selectedCsii.refreshInstances();
590 }
591 final List<CdmInstanceInfo> instances = selectedCsii.getInstances();
592 Display.getDefault().asyncExec(new Runnable() {
593 @Override
594 public void run() {
595 if(!instances.isEmpty()) {
596 for(CdmInstanceInfo cdmInstance : instances) {
597 comboCdmInstance.add(cdmInstance.getName());
598 }
599 int instanceIndex = -1;
600 if(instanceName != null) {
601 instanceIndex = comboCdmInstance.indexOf(instanceName);
602 }
603 if(instanceIndex == -1) {
604 comboCdmInstance.select(0);
605 autoConnect = false;
606 } else {
607 comboCdmInstance.select(instanceIndex);
608 }
609 refreshCdmInstance();
610 comboCdmInstance.setEnabled(true);
611 if(autoConnect) {
612 connect();
613 }
614
615 } else {
616 txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
617 btnConnect.setEnabled(false);
618 }
619 }
620 });
621 }
622 } catch (final CDMServerException e) {
623 MessagingUtils.warn(getClass(), e);
624 Display.getDefault().asyncExec(new Runnable() {
625 @Override
626 public void run() {
627 disableCdmInstanceControls(STATUS_NOT_AVAILABLE, e.getMessage());
628 }
629 });
630 }
631 return Status.OK_STATUS;
632 }
633 };
634
635 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
636 // Start the Job
637 serverJob.schedule();
638 }
639 }
640
641 private void disableCdmInstanceControls(String cdmInstanceStatus, String tooltip) {
642 txtCdmInstanceStatus.setText(cdmInstanceStatus);
643 txtCdmInstanceStatus.setToolTipText(tooltip);
644 comboCdmInstance.setEnabled(false);
645 btnConnect.setEnabled(false);
646
647 }
648 private void refreshCdmInstance() {
649 txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
650 clearOnInstanceChange();
651 updateSelectedCdmInstance();
652 checkSelectedCdmInstance();
653 updateManagedServerControls();
654 }
655
656 private void updateSelectedCdmInstance() {
657 int selIndex = comboCdmInstance.getSelectionIndex();
658 if(selIndex != -1) {
659 selectedCdmInstance = selectedCsii.getInstanceFromName(comboCdmInstance.getItem(selIndex));
660
661 if(loadLoginPrefs && !isDevRemoteSource) {
662 readPrefCredentials();
663 }
664 }
665 }
666
667 private void updateManagedServerControls() {
668 if(selectedCsii.isLocalhostMgd()) {
669 if(isSelectedCdmInstanceRunningInManagedServer()) {
670 txtCdmInstanceStatus.setText(STATUS_STARTED);
671 } else {
672 txtCdmInstanceStatus.setText(STATUS_NOT_STARTED);
673 }
674 btnConnect.setEnabled(true);
675 selectedCsii.setPort(getManagedServerPort());
676 updatePort();
677 }
678
679
680 if(isManagedServerRunning()) {
681 btnStopServer.setEnabled(true);
682 } else {
683 btnStopServer.setEnabled(false);
684 }
685 }
686
687 private boolean isManagedServerRunning() {
688 return CdmStore.getManagedServer() != null && CdmStore.getManagedServer().isAlive();
689 }
690
691 private boolean isSelectedCdmInstanceRunningInManagedServer() {
692 return CdmStore.getManagedServer() != null &&
693 CdmStore.getManagedServer().isAlive() &&
694 selectedCsii.isLocalhostMgd() &&
695 CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName());
696 }
697
698 private void startManagedServer() {
699 if(isManagedServerRunning()) {
700 if(CdmStore.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName())) {
701 return;
702 } else {
703 Display.getDefault().syncExec(new Runnable() {
704 @Override
705 public void run() {
706 stopManagedServer();
707 }
708 });
709 }
710 }
711
712
713 Job job = new Job("Managed CDM Server Launch") {
714
715 @Override
716 public IStatus run(IProgressMonitor monitor) {
717 String mgdServerConfigFileName = "mgd.datasources.xml";
718 String config = CDMServerUtils.convertEditorToServerConfig();
719 File managedServerConfigFile;
720 int maxUnits = 50;
721 monitor.beginTask("Launching Managed CDM Server", maxUnits);
722 try {
723 monitor.subTask("Generating datasources config file for " + selectedCdmInstance.getName());
724 managedServerConfigFile = CDMServerUtils.writeManagedServerConfig(config, mgdServerConfigFileName);
725 monitor.worked(1);
726 CdmStore.setManagedServer(new CDMServer(selectedCdmInstance.getName(), managedServerConfigFile));
727 monitor.subTask("Starting Managed CDM Server. This may take a while.");
728 CdmStore.getManagedServer().start(false, RemotingLoginDialog.this);
729 int serverUnits = 0;
730
731 // the following loop is a 'fake' progress monitoring where the progress
732 // bar is advanced by one unit every second until maxUnits -2
733 while(!CdmStore.getManagedServer().isStarted()) {
734 if(serverUnits < maxUnits - 2) {
735 try {
736 Thread.sleep(1000);
737 } catch (InterruptedException e) {
738 }
739 monitor.worked(1);
740 serverUnits++;
741 }
742 }
743 Display.getDefault().asyncExec(new Runnable() {
744 @Override
745 public void run() {
746 hide(false);
747 updateManagedServerControls();
748 connect();
749 }
750 });
751 } catch (IOException ioe) {
752 MessagingUtils.errorDialog("Error generating server config file",
753 this,
754 ioe.getMessage(),
755 TaxeditorStorePlugin.PLUGIN_ID,
756 ioe,
757 true);
758 } catch (CDMEmbeddedServerException cse) {
759 MessagingUtils.errorDialog("Error starting managed server",
760 this,
761 cse.getMessage(),
762 TaxeditorStorePlugin.PLUGIN_ID,
763 cse,
764 true);
765 } finally {
766 monitor.done();
767 }
768 return Status.OK_STATUS;
769 }
770 };
771
772 // configure the job
773 job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
774 job.setUser(true);
775 // schedule job
776 hide(true);
777 job.schedule();
778 }
779
780 private void stopManagedServer() {
781 try {
782 CdmStore.getManagedServer().stop();
783 } catch (Exception e) {
784 MessagingUtils.errorDialog("Error stopping managed server",
785 this,
786 "Could not stop managed server running at port " + CdmStore.getManagedServer().getPort() + ". Please stop it manually",
787 TaxeditorStorePlugin.PLUGIN_ID,
788 e,
789 true);
790 }
791 CdmStore.setManagedServer(null);
792 updateManagedServerControls();
793 }
794
795 private int getManagedServerPort() {
796 return CdmStore.getManagedServer() == null ? CdmServerInfo.NULL_PORT : CdmStore.getManagedServer().getPort();
797 }
798
799 private void checkSelectedCdmInstance() {
800 boolean available = false;
801 String status = STATUS_NOT_AVAILABLE;
802 String message = null;
803
804 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
805 try {
806 if(selectedCsii.pingInstance(selectedCdmInstance, getPort())) {
807 status = STATUS_AVAILABLE;
808 available = true;
809 } else {
810 status = STATUS_NOT_AVAILABLE;
811 available = false;
812 }
813
814 if(available) {
815 txtServerCDMVersion.setText(selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort()).getDbSchemaVersion());
816 int compareDbSchemaVersion = selectedCsii.compareDbSchemaVersion(selectedCdmInstance, getPort());
817 int compareCdmlibServicesVersion = 0;
818 boolean disableServicesApiTimestampCheck =
819 PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
820 if(!disableServicesApiTimestampCheck) {
821 compareCdmlibServicesVersion = selectedCsii.compareCdmlibServicesVersion();
822 }
823 if(compareDbSchemaVersion > 0 || compareCdmlibServicesVersion > 0) {
824 status = STATUS_NOT_COMPATIBLE;
825 available = false;
826 message = MESG_COMPATIBLE_EDITOR_OLD;
827 } else if(compareDbSchemaVersion < 0 || compareCdmlibServicesVersion < 0) {
828 status = STATUS_NOT_COMPATIBLE;
829 available = false;
830 message = MESG_COMPATIBLE_SERVER_OLD;
831 } else {
832 status = STATUS_AVAILABLE;
833 available = true;
834 message = "";
835 }
836 }
837 } catch (Exception e) {
838 txtCdmInstanceStatus.setToolTipText(e.getMessage());
839 } finally {
840 btnConnect.setEnabled(available);
841 txtCdmInstanceStatus.setText(status);
842 if(!StringUtils.isBlank(message)) {
843 setMessage(message);
844 }
845 }
846 }
847 }
848
849 private void connect() {
850 checkSelectedCdmInstance();
851
852 if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
853 return;
854 }
855
856 ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
857
858 if(!validateLogin()) {
859 return;
860 }
861
862 try {
863 CdmStore.connect(source, this);
864 } catch (Exception e) {
865 // Do not expect anything to go wrong at this point, so we throw a runtime exception
866 // if any problems
867 throw new RuntimeException(e);
868 }
869
870 }
871
872
873 public boolean isRememberMe() {
874 return btnRememberMe.getSelection();
875 }
876
877 private void persistPrefLastServerInstance() {
878 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
879 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
880
881 lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedCsii.getName());
882 lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
883
884 flushPreferences(lastServerInstancePrefs);
885 }
886
887 private void persistPrefCredentials() {
888 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
889 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
890 credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
891 credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
892 flushPreferences(credentialsPrefs);
893 }
894
895 private void flushPreferences(Preferences prefs) {
896 try {
897 prefs.flush();
898 } catch (BackingStoreException bse) {
899 setMessage(bse.getMessage());
900 }
901 }
902
903 private void readPrefCredentials() {
904 String username, password;
905 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
906 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
907 username = credentialsPrefs.get(getUsernamePrefKey(), "");
908 txtLogin.setText(username);
909 password = credentialsPrefs.get(getPasswordPrefKey(),"");
910 txtPassword.setText(password);
911 if(username.isEmpty() || password.isEmpty()) {
912 autoConnect = false;
913 }
914 }
915
916 private void readPrefLastServerInstance() {
917 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
918 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
919
920 serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
921 instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
922 }
923
924 private void emptyCredentials() {
925 txtLogin.setText("");
926 txtPassword.setText("");
927 }
928
929 private String getUsernamePrefKey() {
930 return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + USERNAME_SUFFIX;
931 }
932
933 private String getPasswordPrefKey() {
934 return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
935 }
936
937 private boolean validateLogin() {
938 if(getUsername() == null || getUsername().isEmpty()) {
939 setMessage("User login cannot be empty");
940 return false;
941 }
942 if(getPassword() == null || getPassword().isEmpty()) {
943 setMessage("Password cannot be empty");
944 return false;
945 }
946 return true;
947 }
948 public String getUsername() {
949 return txtLogin.getText();
950 }
951
952 public String getPassword() {
953 return txtPassword.getText();
954 }
955
956 public void setMessage(String message) {
957 if(message != null && !message.isEmpty()) {
958 if(message.length() > 60) {
959 styledTxtMessage.setToolTipText(message);
960 message = message.substring(0, 60) + "...";
961 }
962 styledTxtMessage.setText(message);
963 styledTxtMessage.setVisible(true);
964 ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
965 shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
966 shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
967 } else {
968 styledTxtMessage.setText("");
969 styledTxtMessage.setVisible(false);
970 ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
971 shlConnect.setSize(MIN_WIDTH, getHeightWithoutMessage());
972 shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage());
973 }
974 remotingComposite.layout();
975 }
976
977 private int getHeightWithoutMessage() {
978 if(xpndblcmpstAdvanced.isExpanded()) {
979 return MIN_EXP_HEIGHT;
980 } else {
981 return MIN_HEIGHT;
982 }
983 }
984
985
986 public void hide(boolean isHidden) {
987 if(shlConnect != null && shlConnect.getDisplay() != null) {
988 shlConnect.setVisible(!isHidden);
989 }
990 }
991 public void dispose() {
992 if(shlConnect != null && shlConnect.getDisplay() != null) {
993 shlConnect.dispose();
994 }
995 }
996
997 public void onComplete() {
998 Display.getDefault().asyncExec(new Runnable() {
999 @Override
1000 public void run() {
1001 if(!isDevRemoteSource) {
1002 if(isRememberMe()) {
1003 persistPrefCredentials();
1004 }
1005 persistPrefLastServerInstance();
1006 }
1007 dispose();
1008 }
1009 });
1010 }
1011
1012 private String generateLastModifiedTooltip(String cdmlibLastModified) {
1013 if(StringUtils.isBlank(cdmlibLastModified)) {
1014 return "";
1015 }
1016 SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.z");
1017 Date cdmlibLastModifiedDate;
1018 String cdmlibLastModifiedTimestamp = "";
1019
1020 cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
1021 cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
1022
1023 return cdmlibLastModifiedTimestamp;
1024 }
1025
1026 private void setEditorInfo() {
1027 txtEditorCDMVersion.setText(CdmMetaData.getDbSchemaVersion());
1028 String editorVersionTimestamp = generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified());
1029 txtEditorVersion.setText(CdmApplicationState.getCdmlibVersion() + ":" + editorVersionTimestamp);
1030 }
1031
1032 private void clearOnServerChange() {
1033 setMessage("");
1034 txtServerCDMVersion.setText("");
1035 txtServerVersion.setText("");
1036 txtServerVersion.setToolTipText("");
1037 txtServerCDMVersion.setText("");
1038 txtPort.setEditable(false);
1039 txtPort.setEnabled(false);
1040 }
1041
1042 private void clearOnInstanceChange() {
1043 setMessage("");
1044 txtServerCDMVersion.setText("");
1045 }
1046
1047 /**
1048 * {@inheritDoc}
1049 */
1050 @Override
1051 public void handleError(final Throwable t) {
1052
1053 Display.getDefault().syncExec(new Runnable() {
1054 @Override
1055 public void run() {
1056 serverJob.cancel();
1057
1058 String title = "CDM Server launch error";
1059 String message = t.getMessage();
1060
1061
1062 MessagingUtils.errorDialog(title,
1063 this,
1064 message,
1065 TaxeditorStorePlugin.PLUGIN_ID,
1066 t,
1067 true);
1068 }
1069 });
1070 }
1071
1072
1073 }