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