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