ref #9359 upgrade TaxEditor to log4j2
[taxeditor.git] / eu.etaxonomy.taxeditor.local / src / main / java / eu / etaxonomy / taxeditor / local / server / RemotingLoginDialogLocal.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.local.server;
10
11 import java.io.File;
12 import java.io.IOException;
13 import java.net.SocketTimeoutException;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16 import java.util.List;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.core.runtime.preferences.ConfigurationScope;
25 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
26 import org.eclipse.jface.layout.GridDataFactory;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.SWTException;
29 import org.eclipse.swt.custom.StyledText;
30 import org.eclipse.swt.events.KeyAdapter;
31 import org.eclipse.swt.events.KeyEvent;
32 import org.eclipse.swt.events.MouseAdapter;
33 import org.eclipse.swt.events.MouseEvent;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Color;
37 import org.eclipse.swt.graphics.Point;
38 import org.eclipse.swt.graphics.Rectangle;
39 import org.eclipse.swt.layout.FillLayout;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Button;
43 import org.eclipse.swt.widgets.Combo;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Label;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.swt.widgets.Text;
49 import org.eclipse.ui.forms.events.ExpansionEvent;
50 import org.eclipse.ui.forms.events.IExpansionListener;
51 import org.eclipse.ui.forms.widgets.ExpandableComposite;
52 import org.eclipse.ui.progress.IProgressConstants;
53 import org.eclipse.wb.swt.SWTResourceManager;
54 import org.osgi.service.prefs.BackingStoreException;
55 import org.osgi.service.prefs.Preferences;
56 import org.springframework.security.authentication.BadCredentialsException;
57 import org.springframework.security.authentication.LockedException;
58 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
59
60 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
61 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
62 import eu.etaxonomy.cdm.api.service.IUserService;
63 import eu.etaxonomy.cdm.common.CdmUtils;
64 import eu.etaxonomy.cdm.config.CdmSourceException;
65 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
66 import eu.etaxonomy.cdm.persistence.permission.Role;
67 import eu.etaxonomy.taxeditor.l10n.Messages;
68 import eu.etaxonomy.taxeditor.local.CdmEmbeddedServerException;
69 import eu.etaxonomy.taxeditor.local.CdmServer;
70 import eu.etaxonomy.taxeditor.model.MessagingUtils;
71 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
72 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
73 import eu.etaxonomy.taxeditor.remoting.server.CdmServerException;
74 import eu.etaxonomy.taxeditor.remoting.server.CdmServerUtils;
75 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteLocalhostSource;
76 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
77 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
78 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
79 import eu.etaxonomy.taxeditor.store.CdmStore;
80 import eu.etaxonomy.taxeditor.store.LoginManager;
81 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
82 import eu.etaxonomy.taxeditor.ui.dialog.RemotingLoginDialog;
83
84 /**
85 * @author cmathew
86 * @date 20 Jan 2015
87 */
88 public class RemotingLoginDialogLocal extends RemotingLoginDialog {
89
90 private Logger logger = LogManager.getLogger(getClass());
91
92 // protected Shell shlConnect;
93 // private Text txtCdmServerStatus;
94 // private Text txtCdmInstanceStatus;
95 //
96 // private Button btnConnect;
97 //
98 // private Composite remotingComposite;
99 // private Button btnCdmServerRefresh;
100 // private Button btnCdmInstanceRefresh;
101 // private Button btnStopServer;
102 // private Composite loginComposite;
103 // private Label lblLogin;
104 // private Text txtLogin;
105 // private Label lblPassword;
106 // private Text txtPassword;
107 // private Button btnRememberMe;
108 // private Label lblDefaultLogin;
109 // private Composite compAdvanced;
110 // private Label lblPort;
111 // private Text txtPort;
112 // private Label lblServerCdmlibVersion;
113 // private Text txtServerCdmlibVersion;
114 // private ExpandableComposite expandableCompositeAdvanced;
115 // private StyledText styledTxtMessage;
116 //
117 // private Label lblEditorCdmlibVersion;
118 // private Text txtEditorCdmlibVersion;
119 // private Label lblServerSchemaVersion;
120 // private Text txtServerSchemaVersion;
121 // private Label lblEditorCdmVersion;
122 // private Text txtEditorCdmVersion;
123 //
124 // private final int MESSAGE_HEIGHT = 50;
125 // private final int COMBO_MIN_WIDTH =200;
126 // private final int CONTROLS_MIN_HEIGHT = 23; //does not seem to work yet
127 //// private final int MIN_EXP_HEIGHT = 380;
128 //
129 // private boolean autoConnect = false;
130 // private boolean loadLoginPrefs = true;
131 // private boolean isDevRemoteSource = false;
132
133 // private Job serverJob;
134 // protected Object result;
135 //
136 // private final Map<String, CdmServerInfo> serverInfoMap = new HashMap<>();
137
138 // private String serverName;
139 // private String instanceName;
140 // private CdmServerInfo selectedServerInfo;
141 // private CdmInstanceInfo selectedCdmInstance;
142 // private String login;
143 // private String password;
144
145 /**
146 * Create the dialog.
147 * @param parent
148 * @param style
149 */
150 public RemotingLoginDialogLocal(Shell parent, int style) {
151 super(parent, style);
152 setText(Messages.RemotingLoginDialog_LABEL_LOGIN);
153 this.isLocal = true;
154 }
155
156
157 @Override
158 public Object open(ICdmRemoteSource source, boolean loadLoginPrefs, boolean autoConnect) {
159 this.loadLoginPrefs = loadLoginPrefs;
160 this.serverName = source.getName();
161 if (source instanceof CdmRemoteLocalhostSource){
162 this.instanceName = ((CdmRemoteLocalhostSource)source).getDatasourceName();
163 }else{
164 String contextPath = source.getContext();
165 this.instanceName = contextPath == null? "" : contextPath.substring(contextPath.lastIndexOf("/") + 1); //$NON-NLS-1$
166 }
167 return open(serverName, instanceName, loadLoginPrefs, autoConnect);
168 }
169
170
171 @Override
172 public Object open(String serverName, String instanceName, boolean loadLoginPrefs, boolean autoConnect) {
173 this.serverName = serverName;
174 this.instanceName = instanceName;
175 this.loadLoginPrefs = loadLoginPrefs;
176 this.autoConnect = autoConnect;
177 return open();
178 }
179
180 /**
181 * Open the dialog.
182 * @return the dialog result
183 */
184
185 @Override
186 public Object open() {
187
188 createContents();
189 if(serverName == null && instanceName == null) {
190 readPrefLastServerInstance();
191 }
192 expandableCompositeAdvanced.setExpanded(false);
193
194 setEditorInfo();
195 populateCdmServerCombo();
196 shlConnect.pack(true);
197
198 setCenterPoint();
199 shlConnect.open();
200
201 Display display = getParent().getDisplay();
202 while (!shlConnect.isDisposed()) {
203 if (!display.isDisposed()){
204 if (!display.readAndDispatch()) {
205 display.sleep();
206 }
207 }
208 }
209 return result;
210 }
211
212 /**
213 * Create contents of the dialog.
214 */
215 @Override
216 protected void createContents() {
217 shlConnect = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE | SWT.CENTER);
218 shlConnect.setText(LABEL_CONNECT);
219 shlConnect.setLayout(new FillLayout(SWT.HORIZONTAL));
220
221 remotingComposite = new Composite(shlConnect, SWT.NONE);
222 remotingComposite.setLayout(new GridLayout(1, false));
223
224 //server
225 Composite cdmServerComposite = new Composite(remotingComposite, SWT.NONE);
226 GridData gd_cdmServerComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
227 cdmServerComposite.setLayoutData(gd_cdmServerComposite);
228 cdmServerComposite.setLayout(new GridLayout(4, false));
229
230 Label lblCdmServer = new Label(cdmServerComposite, SWT.NONE);
231 lblCdmServer.setText(Messages.RemotingLoginDialog_LABEL_CDM_SERVER);
232 lblCdmServer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
233
234 comboCdmServer = new Combo(cdmServerComposite, SWT.READ_ONLY);
235 comboCdmServer.addSelectionListener(new SelectionAdapter() {
236 @Override
237 public void widgetSelected(SelectionEvent e) {
238 onRefreshCdmServer();
239 }
240 });
241
242 GridData gd_comboCdmServer = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
243
244 comboCdmServer.setLayoutData(gd_comboCdmServer);
245 comboCdmServer.select(0);
246
247 txtCdmServerStatus = new Text(cdmServerComposite, SWT.BORDER);
248 txtCdmServerStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
249 txtCdmServerStatus.setEditable(false);
250 GridData gd_txtCdmServerStatus = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
251 gd_txtCdmServerStatus.minimumHeight = CONTROLS_MIN_HEIGHT;
252 txtCdmServerStatus.setLayoutData(gd_txtCdmServerStatus);
253
254 btnCdmServerRefresh = new Button(cdmServerComposite, SWT.NONE);
255 btnCdmServerRefresh.addSelectionListener(new SelectionAdapter() {
256 @Override
257 public void widgetSelected(SelectionEvent e) {
258 onRefreshCdmServer();
259 }
260 });
261 GridData gd_btnCdmServerRefresh = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
262 gd_btnCdmServerRefresh.minimumHeight = CONTROLS_MIN_HEIGHT;
263 btnCdmServerRefresh.setLayoutData(gd_btnCdmServerRefresh);
264 btnCdmServerRefresh.setText(Messages.RemotingLoginDialog_LABEL_REFRESH);
265
266 //instance
267 Label lblCdmInstance = new Label(cdmServerComposite, SWT.NONE);
268 GridData gd_lblCdmInstance = new GridData(SWT.RIGHT, SWT.CENTER, false, true, 1, 1);
269 //gd_lblCdmInstance.heightHint = 30;
270 lblCdmInstance.setLayoutData(gd_lblCdmInstance);
271 lblCdmInstance.setText(Messages.RemotingLoginDialog_LABEL_CDM_INSTANCE);
272
273 comboCdmInstance = new Combo(cdmServerComposite, SWT.READ_ONLY);
274 comboCdmInstance.addSelectionListener(new SelectionAdapter() {
275 @Override
276 public void widgetSelected(SelectionEvent e) {
277 onRefreshCdmInstance();
278 }
279 });
280
281 GridData gd_comboCdmInstance = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
282 gd_comboCdmInstance.minimumWidth = COMBO_MIN_WIDTH;
283
284 comboCdmInstance.setLayoutData(gd_comboCdmInstance);
285 comboCdmInstance.select(0);
286
287 txtCdmInstanceStatus = new Text(cdmServerComposite, SWT.BORDER);
288 txtCdmInstanceStatus.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
289 txtCdmInstanceStatus.setEditable(false);
290 GridData gd_txtCdmInstanceStatus = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
291 gd_txtCdmInstanceStatus.minimumHeight = CONTROLS_MIN_HEIGHT;
292 txtCdmInstanceStatus.setLayoutData(gd_txtCdmInstanceStatus);
293
294 btnCdmInstanceRefresh = new Button(cdmServerComposite, SWT.FLAT);
295 btnCdmInstanceRefresh.addSelectionListener(new SelectionAdapter() {
296 @Override
297 public void widgetSelected(SelectionEvent e) {
298 onRefreshCdmInstance();
299 }
300 });
301 GridData gd_btnCdmInstanceRefresh = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
302 gd_btnCdmInstanceRefresh.minimumHeight = CONTROLS_MIN_HEIGHT;
303 btnCdmInstanceRefresh.setLayoutData(gd_btnCdmInstanceRefresh);
304 btnCdmInstanceRefresh.setText(Messages.RemotingLoginDialog_LABEL_REFRESH);
305
306 //login + connect
307 loginComposite = new Composite(remotingComposite, SWT.NONE);
308 GridData gd_loginComposite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
309 loginComposite.setLayoutData(gd_loginComposite);
310 GridLayout gl_loginComposite = new GridLayout(6, false);
311 gl_loginComposite.marginTop = 5;
312 loginComposite.setLayout(gl_loginComposite);
313
314 lblLogin = new Label(loginComposite, SWT.CENTER);
315 GridData gd_lblLogin = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
316 lblLogin.setLayoutData(gd_lblLogin);
317 lblLogin.setText(Messages.RemotingLoginDialog_LABEL_LOGIN_COLON);
318
319 txtLogin = new Text(loginComposite, SWT.BORDER);
320 GridData gd_txtLogin = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
321 gd_txtLogin.minimumWidth = 80;
322 txtLogin.setLayoutData(gd_txtLogin);
323
324 lblPassword = new Label(loginComposite, SWT.CENTER);
325 lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
326 lblPassword.setText(Messages.RemotingLoginDialog_LABEL_PASSWORD);
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 txtPassword.setLayoutData(gd_txtPassword);
332 @SuppressWarnings("unused")
333 Label nope = new Label(loginComposite, SWT.NONE);
334 txtPassword.addKeyListener(new KeyAdapter() {
335 @Override
336 public void keyPressed(KeyEvent e) {
337 if(e.character==SWT.CR){
338 if(btnConnect.getText().equals(LABEL_CONNECT)){
339 onConnectButtonPressed();
340 }
341 }
342 }
343 });
344
345 btnConnect = new Button(loginComposite, SWT.FLAT);
346 btnConnect.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
347 btnConnect.addSelectionListener(new SelectionAdapter() {
348 @Override
349 public void widgetSelected(SelectionEvent e) {
350 onConnectButtonPressed();
351 }
352 });
353 btnConnect.setText(LABEL_CONNECT);
354
355 //rememberMe + default values
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 btnRememberMe.setLayoutData(gd_btnRememberMe);
360 btnRememberMe.setText(Messages.RemotingLoginDialog_LABEL_REMEMBER_ME);
361
362 lblDefaultLogin = new Label(loginComposite, SWT.NONE);
363 GridDataFactory.fillDefaults().span(4, 1).align(SWT.TRAIL, SWT.CENTER).applyTo(lblDefaultLogin);
364 lblDefaultLogin.setText(String.format(Messages.RemotingLoginDialog_DEFAULT_LOGIN, DEFAULT_USER, DEFAULT_PASS));
365 lblDefaultLogin.setVisible(false);
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.setSelectionBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
371 styledTxtMessage.setSelectionForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
372 styledTxtMessage.setDoubleClickEnabled(false);
373 styledTxtMessage.setEditable(false);
374 styledTxtMessage.setWordWrap(true);
375
376 GridData gd_styledTxtMessage = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
377 gd_styledTxtMessage.exclude = true;
378 gd_styledTxtMessage.minimumHeight = MESSAGE_HEIGHT;
379
380 styledTxtMessage.setLayoutData(gd_styledTxtMessage);
381
382 //Advanced
383 expandableCompositeAdvanced = new ExpandableComposite(remotingComposite, SWT.NONE, ExpandableComposite.TWISTIE);
384 GridData gd_xpndblcmpstAdvanced = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
385 expandableCompositeAdvanced.setLayoutData(gd_xpndblcmpstAdvanced);
386 expandableCompositeAdvanced.addExpansionListener(new IExpansionListener() {
387 @Override
388 public void expansionStateChanged(ExpansionEvent e) {
389 shlConnect.pack(true);
390 }
391 @Override
392 public void expansionStateChanging(ExpansionEvent e) {
393 }
394 });
395 expandableCompositeAdvanced.setText(Messages.RemotingLoginDialog_LABEL_ADVANCED);
396 expandableCompositeAdvanced.setExpanded(true);
397
398 compAdvanced = new Composite(expandableCompositeAdvanced, SWT.NONE);
399 expandableCompositeAdvanced.setClient(compAdvanced);
400 compAdvanced.setLayout(new GridLayout(4, false));
401
402 lblPort = new Label(compAdvanced, SWT.CENTER);
403 lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
404 lblPort.setText(Messages.RemotingLoginDialog_LABEL_PORT);
405
406 txtPort = new Text(compAdvanced, SWT.BORDER);
407 GridData gd_txtPort = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
408 gd_txtPort.minimumWidth = 50;
409 txtPort.setLayoutData(gd_txtPort);
410
411 lblServerCdmlibVersion = new Label(compAdvanced, SWT.CENTER);
412 lblServerCdmlibVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
413 lblServerCdmlibVersion.setText(Messages.RemotingLoginDialog_LABEL_SERVER_CDMLIB_VERSION);
414
415 txtServerCdmlibVersion = new Text(compAdvanced, SWT.BORDER);
416 txtServerCdmlibVersion.setEditable(false);
417 txtServerCdmlibVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
418
419 btnStopServer = new Button(compAdvanced, SWT.FLAT);
420 btnStopServer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
421 btnStopServer.addMouseListener(new MouseAdapter() {
422 @Override
423 public void mouseUp(MouseEvent e) {
424 onStopManagedServer();
425 }
426 });
427 btnStopServer.setText(Messages.RemotingLoginDialog_LABEL_STOP_MANAGED_SERVER);
428
429 lblEditorCdmlibVersion = new Label(compAdvanced, SWT.CENTER);
430 lblEditorCdmlibVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
431 lblEditorCdmlibVersion.setText(Messages.RemotingLoginDialog_LABEL_EDITOR_CDMLIB_VERSION);
432
433 txtEditorCdmlibVersion = new Text(compAdvanced, SWT.BORDER);
434 txtEditorCdmlibVersion.setEditable(false);
435 txtEditorCdmlibVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
436 addEmptyCells(2);
437
438 lblServerSchemaVersion = new Label(compAdvanced, SWT.CENTER);
439 lblServerSchemaVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
440 lblServerSchemaVersion.setText(Messages.RemotingLoginDialog_LABEL_SERVER_CDM_VERSION);
441
442 txtServerSchemaVersion = new Text(compAdvanced, SWT.BORDER);
443 txtServerSchemaVersion.setEditable(false);
444 txtServerSchemaVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
445 addEmptyCells(2);
446
447 lblEditorCdmVersion = new Label(compAdvanced, SWT.CENTER);
448 lblEditorCdmVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
449 lblEditorCdmVersion.setText(Messages.RemotingLoginDialog_LABEL_EDITOR_CDM_VERSION);
450
451 txtEditorCdmVersion = new Text(compAdvanced, SWT.BORDER);
452 txtEditorCdmVersion.setEditable(false);
453 txtEditorCdmVersion.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
454 }
455
456
457 @Override
458 public void setCenterPoint() {
459 Rectangle parentSize = getParent().getBounds();
460 Rectangle shellSize = shlConnect.getBounds();
461 int locationX = (parentSize.width - shellSize.width)/2+parentSize.x;
462 int locationY = (parentSize.height - shellSize.height)/2+parentSize.y;
463 shlConnect.setLocation(new Point(locationX, locationY));
464
465 }
466
467
468
469
470
471
472 @SuppressWarnings("unused")
473 private void addEmptyCells(int cnt) {
474 for (int i = 0; i < cnt; i++) {
475 new Label(compAdvanced, SWT.NONE);
476 }
477 }
478
479 @Override
480 protected void onConnectButtonPressed() {
481 if(selectedServerInfo.isLocalhostMgd() && !isSelectedCdmInstanceRunningInManagedServer()) {
482 startManagedServer();
483 } else {
484 connect();
485 if (CdmStore.isActive() && CdmStore.currentAuthentiationHasOneOfRoles(Role.ROLE_REMOTING)){
486 PreferencesUtil.setNomenclaturalCodePreferences();
487 }
488 }
489 }
490
491 // private void populateCdmServerCombo() {
492 // Job job = new Job(Messages.RemotingLoginDialog_RETRIEVE_SERVER_INSTANCES) {
493 // @Override
494 // protected IStatus run(IProgressMonitor monitor) {
495 // Display.getDefault().syncExec(new Runnable() {
496 // @Override
497 // public void run() {
498 // //TODO performance
499 // for(CdmServerInfo csii : CdmServerInfo.getCdmServers(true)) {
500 // serverInfoMap.put(csii.getName(), csii);
501 // comboCdmServer.add(csii.getName());
502 // }
503 // int serverIndex = -1;
504 // if(serverName != null) {
505 // serverIndex = comboCdmServer.indexOf(serverName);
506 // }
507 // if(serverIndex == -1) {
508 // comboCdmServer.select(0);
509 // autoConnect = false;
510 // } else {
511 // comboCdmServer.select(serverIndex);
512 // }
513 // ICdmSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
514 // if(devRemoteSource != null) {
515 // isDevRemoteSource = true;
516 // String username = System.getProperty("cdm.server.dev.username"); //$NON-NLS-1$
517 // String password = System.getProperty("cdm.server.dev.password"); //$NON-NLS-1$
518 // if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
519 // txtLogin.setText(username);
520 // txtPassword.setText(password);
521 // CdmStoreLocal.connect(devRemoteSource, RemotingLoginDialogLocal.this);
522 // }
523 // } else {
524 // onRefreshCdmServer();
525 // }
526 // }
527 // });
528 // return Status.OK_STATUS;
529 // }
530 // };
531 // job.schedule();
532 // }
533
534 @Override
535 public void onRefreshCdmServer() {
536 try{
537 txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
538 clearOnServerChange();
539 emptyCredentials();
540 updateSelectedCdmServer();
541 Display.getDefault().asyncExec(new Runnable() {
542 @Override
543 public void run() {
544 checkSelectedCdmServer();
545 }
546 });
547 }
548 catch(SWTException e){
549 //catch widget is disposed exception which may occur if
550 //dialog is closed but the runnable tries to update
551 }
552 }
553
554 @Override
555 protected void updateSelectedCdmServer() {
556 int selIndex = comboCdmServer.getSelectionIndex();
557 if(selIndex != -1) {
558 selectedServerInfo = serverInfoMap.get(comboCdmServer.getItem(selIndex));
559 }
560 }
561
562 @Override
563 protected void updatePort() {
564 txtPort.setText(""); //$NON-NLS-1$
565 if(selectedServerInfo != null) {
566 int port = selectedServerInfo.getPort();
567 if(port == CdmServerInfo.NULL_PORT) {
568 txtPort.setText(CdmServerInfo.NULL_PORT_STRING);
569 } else {
570 txtPort.setText(String.valueOf(port));
571 }
572 }
573 }
574
575 @Override
576 protected int getPort() {
577 int port = CdmServerInfo.NULL_PORT;
578 try {
579 port = Integer.valueOf(txtPort.getText());
580 } catch (NumberFormatException nfe) {
581 if(!CdmServerInfo.NULL_PORT_STRING.equals(txtPort.getText())) {
582 setMessage(Messages.RemotingLoginDialog_MESSAGE_PORT_SHOULD_BE_INTEGER);
583 }
584 }
585 return port;
586 }
587
588 private void checkSelectedCdmServer() {
589 try{
590 if(selectedServerInfo != null) {
591 if(selectedServerInfo.isLocalhost()) {
592 txtPort.setEditable(true);
593 txtPort.setEnabled(true);
594 }
595 try {
596 if(selectedServerInfo.pingServer()) {
597 txtCdmServerStatus.setText(STATUS_AVAILABLE);
598 populateCdmInstanceCombo(true);
599 if(selectedServerInfo.isLocalhost()){
600 setCdmlibVersion(txtServerCdmlibVersion);
601 }else{
602 String serverVersionTimestamp = generateLastModifiedTooltip(selectedServerInfo.getCdmlibLastModified());
603 txtServerCdmlibVersion.setText(selectedServerInfo.getCdmlibServicesVersion() + ":" + serverVersionTimestamp); //$NON-NLS-1$
604 }
605 } else {
606 txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
607 comboCdmInstance.removeAll();
608 disableCdmInstanceControls("", ""); //$NON-NLS-1$ //$NON-NLS-2$
609 }
610 } catch (CdmServerException | IOException e) {
611 txtCdmServerStatus.setText(STATUS_NOT_AVAILABLE);
612 // TODO distinguish java.net.ConnectException: Connection refused, java.net.SocketTimeoutException: Read timed out
613 Throwable cause = e;
614 if(e instanceof CdmServerException && e.getCause() != null){
615 cause = e.getCause();
616 }
617 String message = Messages.RemotingLoginDialog_CONNECTION_FAILED_MESSAGE;
618 if(cause instanceof SocketTimeoutException){
619 message = Messages.RemotingLoginDialog_CONNECTION_TIMEOUT_MESSAGE;
620 }
621 MessagingUtils.warningDialog(Messages.RemotingLoginDialog_CONNECTION_FAILED_TITLE, this,
622 message);
623 logger.warn(Messages.RemotingLoginDialog_CONNECTION_FAILED_TITLE, e);
624 }
625 }
626 updatePort();
627 }
628 catch(SWTException e){
629 //catch widget is disposed exception which may occur if
630 //dialog is closed but the runnable tries to update
631 }
632 }
633
634 @Override
635 protected void populateCdmInstanceCombo(final boolean forceRefresh) {
636 comboCdmInstance.removeAll();
637 comboCdmInstance.setEnabled(false);
638 btnConnect.setEnabled(false);
639 txtCdmInstanceStatus.setText(STATUS_RETRIEVING);
640 txtCdmInstanceStatus.setToolTipText(""); //$NON-NLS-1$
641
642 serverJob = new Job(Messages.RemotingLoginDialog_RETRIEVE_SERVER_INSTANCES) {
643 @Override
644 protected IStatus run(IProgressMonitor monitor) {
645 try {
646 if(selectedServerInfo != null) {
647 if(forceRefresh) {
648 selectedServerInfo.refreshInstances();
649 }
650 final List<CdmInstanceInfo> instances = selectedServerInfo.getInstances();
651 Display.getDefault().asyncExec(()->{
652 if(!instances.isEmpty()) {
653 for(CdmInstanceInfo cdmInstance : instances) {
654 comboCdmInstance.add(cdmInstance.getName());
655 }
656 int instanceIndex = -1;
657 if(instanceName != null) {
658 instanceIndex = comboCdmInstance.indexOf(instanceName);
659 }
660 if(instanceIndex == -1) {
661 comboCdmInstance.select(0);
662 autoConnect = false;
663 } else {
664 comboCdmInstance.select(instanceIndex);
665 }
666 onRefreshCdmInstance();
667 comboCdmInstance.setEnabled(true);
668 if(autoConnect) {
669 connect();
670 }
671 } else {
672 txtCdmInstanceStatus.setText(STATUS_NO_INSTANCES);
673 btnConnect.setEnabled(false);
674 }
675 });
676 }
677 } catch (final CdmServerException e) {
678 MessagingUtils.warn(getClass(), e);
679 Display.getDefault().asyncExec(new Runnable() {
680 @Override
681 public void run() {
682 disableCdmInstanceControls(STATUS_NOT_AVAILABLE, e.getMessage());
683 }
684 });
685 }
686 return Status.OK_STATUS;
687 }
688 };
689
690 if(txtCdmServerStatus.getText().equals(STATUS_AVAILABLE) && !isDevRemoteSource) {
691 // Start the Job
692 serverJob.schedule();
693 }
694 }
695
696 @Override
697 public void disableCdmInstanceControls(String cdmInstanceStatus, String tooltip) {
698 txtCdmInstanceStatus.setText(cdmInstanceStatus);
699 txtCdmInstanceStatus.setToolTipText(tooltip);
700 comboCdmInstance.setEnabled(false);
701 btnConnect.setEnabled(false);
702 }
703
704 @Override
705 public void onRefreshCdmInstance() {
706 txtCdmInstanceStatus.setText(STATUS_CHECKING_AVAILABILITY);
707 clearOnInstanceChange();
708 updateSelectedCdmInstance();
709 checkSelectedCdmInstance();
710 updateManagedServerControls();
711 }
712
713 @Override
714 protected void updateSelectedCdmInstance() {
715 int selIndex = comboCdmInstance.getSelectionIndex();
716 if(selIndex != -1) {
717 selectedCdmInstance = selectedServerInfo.getInstanceFromName(comboCdmInstance.getItem(selIndex));
718 if(loadLoginPrefs) {
719 readPrefCredentials();
720 }
721 }
722 }
723
724 private void updateManagedServerControls() {
725 if(selectedServerInfo.isLocalhostMgd()) {
726 if(isSelectedCdmInstanceRunningInManagedServer()) {
727 txtCdmInstanceStatus.setText(STATUS_STARTED);
728 } else {
729 txtCdmInstanceStatus.setText(STATUS_NOT_STARTED);
730 }
731 btnConnect.setEnabled(true);
732 selectedServerInfo.setPort(getManagedServerPort());
733 updatePort();
734 }
735
736 if(isManagedServerRunning()) {
737 btnStopServer.setEnabled(true);
738 } else {
739 btnStopServer.setEnabled(false);
740 }
741 }
742
743 private boolean isManagedServerRunning() {
744 return CdmStoreLocal.getManagedServer() != null && CdmStoreLocal.getManagedServer().isAlive();
745 }
746
747 private boolean isSelectedCdmInstanceRunningInManagedServer() {
748 return CdmStoreLocal.getManagedServer() != null &&
749 CdmStoreLocal.getManagedServer().isAlive() &&
750 selectedServerInfo.isLocalhostMgd() &&
751 CdmStoreLocal.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName());
752 }
753
754 private void startManagedServer() {
755 if(isManagedServerRunning()) {
756 if(CdmStoreLocal.getManagedServer().getDataSourceName().equals(selectedCdmInstance.getName())) {
757 return;
758 } else {
759 Display.getDefault().syncExec(new Runnable() {
760 @Override
761 public void run() {
762 onStopManagedServer();
763 }
764 });
765 }
766 }
767
768 boolean forceSchemaCreate = !schemaExists(selectedCdmInstance);
769 boolean forceSchemaUpdate = LABEL_UPDATE_SCHEMA_VERSION.equals(btnConnect.getText());
770
771 Job job = new Job(Messages.RemotingLoginDialog_JOB_SERVER_LAUNCH) {
772
773 @Override
774 public IStatus run(IProgressMonitor monitor) {
775 String mgdServerConfigFileName = "mgd.datasources.xml"; //$NON-NLS-1$
776 String config = CdmServerUtils.convertEditorToServerConfig();
777 int maxUnits = 50;
778 monitor.beginTask(Messages.RemotingLoginDialog_TASK_LAUNCHING_SERVER, maxUnits);
779 try {
780 monitor.subTask(String.format(Messages.RemotingLoginDialog_GENERATING_CONFIG_FILE, selectedCdmInstance.getName()));
781 File managedServerConfigFile = CdmServerUtils.writeManagedServerConfig(config, mgdServerConfigFileName);
782 monitor.worked(1);
783
784
785 CdmServer cdmServer = new CdmServer(selectedCdmInstance.getName(), managedServerConfigFile);
786
787 // ICdmServer cdmServer = new CdmServer(selectedCdmInstance.getName(), managedServerConfigFile);
788 cdmServer.setForceSchemaCreate(forceSchemaCreate);
789 cdmServer.setForceSchemaUpdate(forceSchemaUpdate);
790 CdmStoreLocal.setManagedServer(cdmServer);
791 monitor.subTask(Messages.RemotingLoginDialog_STARTING_MGD_SERVER);
792 cdmServer.start(false, RemotingLoginDialogLocal.this);
793 int serverUnits = 0;
794
795 // the following loop is a 'fake' progress monitoring where the progress
796 // bar is advanced by one unit every second until maxUnits -2
797 while(!CdmStoreLocal.getManagedServer().isStarted() && !CdmStoreLocal.getManagedServer().isFailed()) {
798 if(serverUnits < maxUnits - 2) {
799 try {
800 Thread.sleep(1000);
801 } catch (InterruptedException e) {
802 }
803 monitor.worked(1);
804 serverUnits++;
805 }
806 }
807 Display.getDefault().syncExec(()->{
808 hide(false);
809 updateManagedServerControls();
810 connect();
811 });
812 } catch (IOException ioe) {
813 MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_GENERATING_CONFIG_FILE,
814 this,
815 ioe.getMessage(),
816 TaxeditorStorePlugin.PLUGIN_ID,
817 ioe,
818 true);
819 } catch (CdmEmbeddedServerException cese) {
820 MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_STARTING_SERVER,
821 this,
822 cese.getMessage(),
823 TaxeditorStorePlugin.PLUGIN_ID,
824 cese,
825 true);
826 } finally {
827 monitor.done();
828 }
829 // NOTE: Errors thrown during server startup are passed to the handleError() implementation
830 // TODO: is the above catch clause for CdmEmbeddedServerException still valuable?
831
832 return Status.OK_STATUS;
833 }
834 };
835
836 // configure the job
837 job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
838 job.setUser(true);
839 // schedule job
840 hide(true);
841 job.schedule();
842 }
843
844 private boolean schemaExists(CdmInstanceInfo instanceInfo) {
845 if (instanceInfo.getDataSource() != null){
846 try {
847 //TODO we need a new method in cdmlib to check for existing schema
848 String schemaVersion = instanceInfo.getDataSource().getDbSchemaVersion();
849 return schemaVersion != null;
850 } catch (CdmSourceException e) {
851 return false;
852 }
853 }else{
854 return true;
855 }
856 }
857
858 private String managedSchemaVersion(CdmInstanceInfo instanceInfo) throws CdmSourceException{
859 if (instanceInfo.getDataSource() != null){
860 return instanceInfo.getDataSource().getDbSchemaVersion();
861 }else{
862 throw new RuntimeException("ManagedSchemaVersion only available for managed server instances.");
863 }
864 }
865 private int compareSchemaVersion(CdmInstanceInfo instanceInfo) throws CdmSourceException {
866 return CdmMetaData.compareVersion(managedSchemaVersion(instanceInfo), CdmMetaData.getDbSchemaVersion(), 3, null);
867 }
868
869
870 @Override
871 public void handleError(final Throwable t) {
872
873 Display.getDefault().syncExec(()-> {
874 serverJob.cancel();
875
876 String title = Messages.RemotingLoginDialog_SERVER_LAUNCH_ERROR;
877 String message = t.getMessage();
878
879 MessagingUtils.errorDialog(title,
880 this,
881 message,
882 TaxeditorStorePlugin.PLUGIN_ID,
883 t,
884 true);
885 });
886 }
887
888 private void onStopManagedServer() {
889 try {
890 CdmStoreLocal.getManagedServer().stop();
891 } catch (Exception e) {
892 MessagingUtils.errorDialog(Messages.RemotingLoginDialog_ERROR_STOPPING_SERVER,
893 this,
894 String.format(Messages.RemotingLoginDialog_COULD_NOT_STOP_SERVER, CdmStoreLocal.getManagedServer().getPort()),
895 TaxeditorStorePlugin.PLUGIN_ID,
896 e,
897 true);
898 }
899 CdmStoreLocal.setManagedServer(null);
900 updateManagedServerControls();
901 }
902
903 private int getManagedServerPort() {
904 return CdmStoreLocal.getManagedServer() == null ? CdmServerInfo.NULL_PORT : CdmStoreLocal.getManagedServer().getPort();
905 }
906
907 @Override
908 protected void checkSelectedCdmInstance() {
909
910 if (selectedServerInfo.isLocalhostMgd()){
911 checkManagedSelectedInstance();
912 }else if (txtCdmServerStatus.getText().equals(STATUS_AVAILABLE)) {
913
914 boolean available = false;
915 String instanceStatus = STATUS_NOT_AVAILABLE;
916 String message = null;
917 try {
918 if(selectedServerInfo.pingInstance(selectedCdmInstance, getPort())) {
919 instanceStatus = STATUS_AVAILABLE;
920 available = true;
921 } else {
922 instanceStatus = STATUS_NOT_AVAILABLE;
923 available = false;
924 }
925
926 if(available) {
927 txtServerSchemaVersion.setText(selectedServerInfo.getCdmRemoteSource(selectedCdmInstance, getPort()).getDbSchemaVersion());
928
929 int compareDbSchemaVersion = selectedServerInfo.compareDbSchemaVersion(selectedCdmInstance, getPort());
930
931 int compareCdmlibServicesVersion = 0;
932 boolean disableServicesApiTimestampCheck =
933 PreferencesUtil.getBooleanValue((IPreferenceKeys.DISABLE_SERVICES_API_TIMESTAMP_CHECK));
934 if(!disableServicesApiTimestampCheck) {
935 compareCdmlibServicesVersion = selectedServerInfo.compareCdmlibServicesVersion();
936 }
937
938 if(compareDbSchemaVersion > 0 || compareCdmlibServicesVersion > 0) {
939 instanceStatus = STATUS_NOT_COMPATIBLE;
940 available = false;
941 message = MESG_COMPATIBLE_EDITOR_OLD;
942 } else if(compareDbSchemaVersion < 0 || compareCdmlibServicesVersion < 0) {
943 instanceStatus = STATUS_NOT_COMPATIBLE;
944 available = false;
945 message = MESG_COMPATIBLE_SERVER_OLD;
946 } else {
947 instanceStatus = STATUS_AVAILABLE;
948 available = true;
949 message = ""; //$NON-NLS-1$
950 }
951 }
952 } catch (Exception e) {
953 txtCdmInstanceStatus.setToolTipText(e.getMessage());
954 } finally {
955 lblDefaultLogin.setVisible(false);
956 btnConnect.setEnabled(available);
957 txtCdmInstanceStatus.setText(instanceStatus);
958 if(!StringUtils.isBlank(message)) {
959 setMessage(message);
960 }
961 }
962 }
963 }
964
965 private void checkManagedSelectedInstance() {
966 boolean available = false;
967 String instanceStatus = STATUS_NOT_AVAILABLE;
968 String message = null;
969 String connect = LABEL_CONNECT;
970 Integer color = null;
971 String schemaVersion = CdmMetaData.getDbSchemaVersion();
972
973 if (!schemaExists(selectedCdmInstance)){
974 message = MESG_SCHEMA_MISSING;
975 connect = LABEL_CREATE_SCHEMA;
976 color = BTN_COLOR_ATTENTION;
977 schemaVersion = MESG_NO_SCHEMA;
978 } else {
979 int compare;
980 try {
981 schemaVersion = managedSchemaVersion(selectedCdmInstance);
982 compare = compareSchemaVersion(selectedCdmInstance);
983 if (compare > 0){
984 instanceStatus = STATUS_NOT_COMPATIBLE;
985 message = MESG_COMPATIBLE_EDITOR_OLD;
986 available = false;
987 }else if (compare < 0){
988 instanceStatus = STATUS_NOT_COMPATIBLE;
989 message = MESG_UPDATE_SCHEMA_VERSION;
990 available = true;
991 connect = LABEL_UPDATE_SCHEMA_VERSION;
992 color = BTN_COLOR_ATTENTION;
993 }else{
994 available = true;
995 instanceStatus = STATUS_AVAILABLE;
996 }
997 } catch (CdmSourceException e) {
998 available = false;
999 }
1000 }
1001
1002 setManagedValues(available, instanceStatus, connect, color, schemaVersion);
1003 setMessage(message);
1004 }
1005
1006 private void setManagedValues(boolean available, String instanceStatus, String connect, Integer color,
1007 String schemaVersion) {
1008 Display.getDefault().syncExec(()->{
1009 btnConnect.setEnabled(available);
1010 btnConnect.setText(connect);
1011 Color systemColor = null;
1012 if(color != null) {
1013 systemColor = Display.getCurrent().getSystemColor(color);
1014 }
1015 btnConnect.setBackground(systemColor);
1016 btnConnect.requestLayout();
1017 txtCdmInstanceStatus.setText(instanceStatus);
1018 txtServerSchemaVersion.setText(schemaVersion);
1019 lblDefaultLogin.setVisible(true);
1020 });
1021 }
1022
1023 @Override
1024 public void connect() {
1025 checkSelectedCdmInstance();
1026
1027 if(!txtCdmInstanceStatus.getText().equals(STATUS_AVAILABLE)) {
1028 return;
1029 }
1030
1031 ICdmRemoteSource source = selectedServerInfo.getCdmRemoteSource(selectedCdmInstance, getPort());
1032
1033 if(!validateLogin(source)) {
1034 return;
1035 }
1036
1037 try {
1038 CdmStoreLocal.connect(source, RemotingLoginDialogLocal.this);
1039 } catch (Exception e) {
1040 // Do not expect anything to go wrong at this point, so we throw a runtime exception
1041 // if any problems
1042 throw new RuntimeException(e);
1043 }
1044 }
1045
1046
1047 @Override
1048 public boolean isRememberMe() {
1049 return btnRememberMe.getSelection();
1050 }
1051
1052 private void persistPrefLastServerInstance() {
1053 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
1054 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
1055
1056 lastServerInstancePrefs.put(LAST_SERVER_KEY, selectedServerInfo.getName());
1057 lastServerInstancePrefs.put(LAST_INSTANCE_KEY, selectedCdmInstance.getName());
1058
1059 flushPreferences(lastServerInstancePrefs);
1060 }
1061
1062 private void persistPrefCredentials() {
1063 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
1064 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
1065 credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
1066 credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
1067 flushPreferences(credentialsPrefs);
1068 }
1069
1070 private void removePrefCredentials() {
1071 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
1072 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
1073 credentialsPrefs.put(getUsernamePrefKey(), txtLogin.getText());
1074 credentialsPrefs.put(getPasswordPrefKey(), txtPassword.getText());
1075 removePreferences(credentialsPrefs);
1076 }
1077
1078 private void removePreferences(Preferences prefs) {
1079 try {
1080 prefs.removeNode();
1081 } catch (BackingStoreException bse) {
1082 setMessage(bse.getMessage());
1083 }
1084 }
1085
1086 private void flushPreferences(Preferences prefs) {
1087 try {
1088 prefs.flush();
1089 } catch (BackingStoreException bse) {
1090 setMessage(bse.getMessage());
1091 }
1092 }
1093
1094 @Override
1095 protected void readPrefCredentials() {
1096 String username, password;
1097 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
1098 Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
1099 username = credentialsPrefs.get(getUsernamePrefKey(), ""); //$NON-NLS-1$
1100 txtLogin.setText(username);
1101 password = credentialsPrefs.get(getPasswordPrefKey(),""); //$NON-NLS-1$
1102 txtPassword.setText(password);
1103 if(username.isEmpty() || password.isEmpty()) {
1104 autoConnect = false;
1105 }
1106 }
1107
1108 private void readPrefLastServerInstance() {
1109 IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
1110 Preferences lastServerInstancePrefs = preferences.node(LAST_SERVER_INSTANCE_NODE);
1111
1112 serverName = lastServerInstancePrefs.get(LAST_SERVER_KEY, null);
1113 instanceName = lastServerInstancePrefs.get(LAST_INSTANCE_KEY, null);
1114 }
1115
1116 private void emptyCredentials() {
1117 txtLogin.setText(""); //$NON-NLS-1$
1118 txtPassword.setText(""); //$NON-NLS-1$
1119 }
1120
1121 private String getUsernamePrefKey() {
1122 return selectedServerInfo.toString(selectedCdmInstance.getName(), isDevRemoteSource?getPort():-1) + USERNAME_SUFFIX;
1123 }
1124
1125 private String getPasswordPrefKey() {
1126 return selectedServerInfo.toString(selectedCdmInstance.getName(), isDevRemoteSource?getPort():-1) + PASSWORD_SUFFIX;
1127 }
1128
1129 private boolean validateLogin(ICdmRemoteSource remoteSource) {
1130 if(getUsername() == null || getUsername().isEmpty()) {
1131 setMessage(Messages.RemotingLoginDialog_LOGIN_CANNOT_BE_EMPTY);
1132 return false;
1133 }
1134 if(getPassword() == null || getPassword().isEmpty()) {
1135 setMessage(Messages.RemotingLoginDialog_PASSWORD_CANNOT_BE_EMPTY);
1136 return false;
1137 }
1138
1139 try {
1140 IUserService userService = CdmApplicationRemoteConfiguration.getUserService(remoteSource);
1141 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(getUsername(), getPassword());
1142 CdmApplicationRemoteConfiguration.getAuthenticationManager(userService).authenticate(token);
1143 } catch(BadCredentialsException e){
1144 setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
1145 return false;
1146 } catch(LockedException e){
1147 setMessage(LoginManager.ACCOUNT_LOCKED_MESSAGE);
1148 return false;
1149 } catch(IllegalArgumentException e){
1150 setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
1151 return false;
1152 }
1153 return true;
1154 }
1155
1156
1157 @Override
1158 public String getUsername() {
1159 return txtLogin.getText();
1160 }
1161
1162
1163 @Override
1164 public String getPassword() {
1165 return txtPassword.getText();
1166 }
1167
1168
1169 @Override
1170 public void setMessage(String message) {
1171 if(CdmUtils.isNotBlank(message)) {
1172 // if(message.length() > 50) {
1173 // styledTxtMessage.setToolTipText(message);
1174 // message = message.substring(0,50) + "..."; //$NON-NLS-1$
1175 // }
1176 styledTxtMessage.setText(message);
1177 styledTxtMessage.setVisible(true);
1178 ((GridData)styledTxtMessage.getLayoutData()).exclude = false;
1179 // shlConnect.setMinimumSize(MIN_WIDTH, getHeightWithoutMessage() + MESSAGE_HEIGHT);
1180 shlConnect.pack(true);
1181 } else {
1182 if (!styledTxtMessage.isDisposed()){
1183 styledTxtMessage.setText(""); //$NON-NLS-1$
1184 styledTxtMessage.setVisible(false);
1185 ((GridData)styledTxtMessage.getLayoutData()).exclude = true;
1186 }
1187 if(!shlConnect.isDisposed()){
1188 shlConnect.pack(true);
1189 }
1190 }
1191 if (!remotingComposite.isDisposed()){
1192 remotingComposite.layout();
1193 }
1194 }
1195
1196 // private int getHeightWithoutMessage() {
1197 // if(xpndblcmpstAdvanced.isExpanded()) {
1198 // return MIN_EXP_HEIGHT;
1199 // } else {
1200 // return MIN_HEIGHT;
1201 // }
1202 // }
1203
1204 @Override
1205 public void hide(boolean isHidden) {
1206 if (!shlConnect.isDisposed()){
1207 if(shlConnect != null && shlConnect.getDisplay() != null) {
1208 shlConnect.setVisible(!isHidden);
1209 }
1210 }
1211 }
1212
1213
1214 @Override
1215 public void dispose() {
1216 if (!shlConnect.isDisposed()){
1217 if(shlConnect != null && shlConnect.getDisplay() != null) {
1218 shlConnect.dispose();
1219 }
1220 }
1221 }
1222
1223
1224 @Override
1225 public void onComplete() {
1226 Display.getDefault().asyncExec(new Runnable() {
1227 @Override
1228 public void run() {
1229 if(selectedCdmInstance!=null){
1230 if(isRememberMe()) {
1231 persistPrefCredentials();
1232 }else{
1233 removePrefCredentials();
1234 }
1235 persistPrefLastServerInstance();
1236 }
1237 dispose();
1238 }
1239 });
1240 }
1241
1242 private String generateLastModifiedTooltip(String cdmlibLastModified) {
1243 if(StringUtils.isBlank(cdmlibLastModified)) {
1244 return ""; //$NON-NLS-1$
1245 }
1246 SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.z"); //$NON-NLS-1$
1247 Date cdmlibLastModifiedDate;
1248 String cdmlibLastModifiedTimestamp = ""; //$NON-NLS-1$
1249
1250 cdmlibLastModifiedDate = new Date(Long.valueOf(cdmlibLastModified));
1251 cdmlibLastModifiedTimestamp = sdf.format(cdmlibLastModifiedDate);
1252
1253 return cdmlibLastModifiedTimestamp;
1254 }
1255
1256 private void setEditorInfo() {
1257 txtEditorCdmVersion.setText(CdmMetaData.getDbSchemaVersion());
1258 setCdmlibVersion(txtEditorCdmlibVersion);
1259 }
1260
1261 private void setCdmlibVersion(Text txt) {
1262 String editorVersionTimestamp = generateLastModifiedTooltip(CdmApplicationState.getCdmlibLastModified());
1263 txt.setText(CdmApplicationState.getCdmlibVersion() + ":" + editorVersionTimestamp); //$NON-NLS-1$
1264 }
1265
1266 private void clearOnServerChange() {
1267 setMessage(""); //$NON-NLS-1$
1268 txtServerSchemaVersion.setText(""); //$NON-NLS-1$
1269 txtServerCdmlibVersion.setText(""); //$NON-NLS-1$
1270 txtServerCdmlibVersion.setToolTipText(""); //$NON-NLS-1$
1271 txtServerSchemaVersion.setText(""); //$NON-NLS-1$
1272 comboCdmInstance.removeAll();
1273 txtCdmInstanceStatus.setText(""); //$NON-NLS-1$
1274 txtPort.setEditable(false);
1275 txtPort.setEnabled(false);
1276 }
1277
1278 private void clearOnInstanceChange() {
1279 setMessage(""); //$NON-NLS-1$
1280 txtServerSchemaVersion.setText(""); //$NON-NLS-1$
1281 btnConnect.setText(LABEL_CONNECT);
1282 btnConnect.setBackground(null);
1283 btnConnect.requestLayout();
1284 }
1285
1286 @SuppressWarnings("unused") //in work, for future centralized handling of control's status
1287 private void updateControls(){
1288
1289 //server
1290 // comboCdmServer; //nothing to change
1291 String strServerStatus = this.selectedServerInfo != null? STATUS_NOT_AVAILABLE : STATUS_AVAILABLE;
1292 txtCdmServerStatus.setText(strServerStatus);
1293 // btnCdmServerRefresh; //nothing to change
1294
1295 //database
1296 // comboCdmInstance
1297 String strInstanceStatus = this.selectedCdmInstance != null? STATUS_NOT_AVAILABLE : STATUS_AVAILABLE;
1298 txtCdmInstanceStatus.setText(strInstanceStatus);
1299 // btnCdmInstanceRefresh; //nothing to change
1300
1301 //login + connect
1302 txtLogin.setText(Nz(login));
1303 txtPassword.setText(Nz(password));
1304 btnConnect.setText(LABEL_CONNECT); //TODO
1305 btnConnect.setEnabled(true);
1306
1307 //rememberMe + defaultValues
1308 // btnRememberMe;
1309 // lblDefaultLogin;
1310
1311 //message
1312 // styledTxtMessage;
1313
1314 //Advanced
1315 // txtPort
1316 // btnStopServer;
1317
1318 // txtServerCdmlibVersion;
1319 // txtEditorCdmlibVersion;
1320 // txtServerSchemaVersion;
1321 // txtEditorCdmVersion;
1322 }
1323
1324 private String Nz(String value) {
1325 return CdmUtils.Nz(value);
1326 }
1327
1328 }