3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.ui
.dialog
;
13 import java
.io
.IOException
;
14 import java
.text
.SimpleDateFormat
;
15 import java
.util
.Date
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
20 import org
.apache
.commons
.lang
.StringUtils
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.Status
;
24 import org
.eclipse
.core
.runtime
.jobs
.Job
;
25 import org
.eclipse
.core
.runtime
.preferences
.ConfigurationScope
;
26 import org
.eclipse
.core
.runtime
.preferences
.IEclipsePreferences
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.custom
.StyledText
;
29 import org
.eclipse
.swt
.events
.MouseAdapter
;
30 import org
.eclipse
.swt
.events
.MouseEvent
;
31 import org
.eclipse
.swt
.events
.SelectionAdapter
;
32 import org
.eclipse
.swt
.events
.SelectionEvent
;
33 import org
.eclipse
.swt
.graphics
.Point
;
34 import org
.eclipse
.swt
.layout
.FillLayout
;
35 import org
.eclipse
.swt
.layout
.GridData
;
36 import org
.eclipse
.swt
.layout
.GridLayout
;
37 import org
.eclipse
.swt
.widgets
.Button
;
38 import org
.eclipse
.swt
.widgets
.Combo
;
39 import org
.eclipse
.swt
.widgets
.Composite
;
40 import org
.eclipse
.swt
.widgets
.Dialog
;
41 import org
.eclipse
.swt
.widgets
.Display
;
42 import org
.eclipse
.swt
.widgets
.Label
;
43 import org
.eclipse
.swt
.widgets
.Shell
;
44 import org
.eclipse
.swt
.widgets
.Text
;
45 import org
.eclipse
.ui
.forms
.events
.ExpansionEvent
;
46 import org
.eclipse
.ui
.forms
.events
.IExpansionListener
;
47 import org
.eclipse
.ui
.forms
.widgets
.ExpandableComposite
;
48 import org
.eclipse
.ui
.progress
.IProgressConstants
;
49 import org
.eclipse
.wb
.swt
.SWTResourceManager
;
50 import org
.osgi
.service
.prefs
.BackingStoreException
;
51 import org
.osgi
.service
.prefs
.Preferences
;
52 import org
.springframework
.security
.authentication
.BadCredentialsException
;
53 import org
.springframework
.security
.authentication
.LockedException
;
54 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
56 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteConfiguration
;
57 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
58 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
59 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
60 import eu
.etaxonomy
.taxeditor
.Messages
;
61 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
62 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
63 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
64 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
65 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerUtils
;
66 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
67 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmServerInfo
;
68 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmServerInfo
.CdmInstanceInfo
;
69 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
70 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
71 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
72 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
73 import eu
.etaxonomy
.taxeditor
.webapp
.CDMEmbeddedServerException
;
74 import eu
.etaxonomy
.taxeditor
.webapp
.CDMServer
;
75 import eu
.etaxonomy
.taxeditor
.webapp
.ICDMServerError
;
83 public class RemotingLoginDialog
extends Dialog
implements ICDMServerError
{
85 private static final String UBUNTU
= "Ubuntu"; //$NON-NLS-1$
86 protected Object result
;
87 protected Shell shlConnect
;
88 private Text txtCdmServerStatus
;
89 private Text txtCdmInstanceStatus
;
90 private Combo comboCdmServer
;
91 private Combo comboCdmInstance
;
92 private Button btnConnect
;
94 private final Map
<String
, CdmServerInfo
> csiiMap
= new HashMap
<String
, CdmServerInfo
>();
96 private final static String STATUS_AVAILABLE
= Messages
.RemotingLoginDialog_STATUS_AVAILABLE
;
97 private final static String STATUS_NOT_AVAILABLE
= Messages
.RemotingLoginDialog_STATUS_NOT_AVAILABLE
;
98 private final static String STATUS_STARTED
= Messages
.RemotingLoginDialog_STATUS_STARTED
;
99 private final static String STATUS_NOT_STARTED
= Messages
.RemotingLoginDialog_STATUS_NOT_STARTED
;
100 private final static String STATUS_RETRIEVING
= Messages
.RemotingLoginDialog_STATUS_RETRIEVING
;
101 private final static String STATUS_CHECKING_AVAILABILITY
= Messages
.RemotingLoginDialog_STATUS_CHECKING
;
102 private final static String STATUS_NO_INSTANCES
= Messages
.RemotingLoginDialog_STATUS_NO_INSTANCES_FOUND
;
103 private final static String STATUS_ERROR
= Messages
.RemotingLoginDialog_STATUS_ERROR
;
104 private final static String STATUS_REMOTING_NOT_ACTIVATED
= Messages
.RemotingLoginDialog_STATUS_REMOTING_NOT_ACTIVATED
;
105 private final static String STATUS_NOT_COMPATIBLE
= Messages
.RemotingLoginDialog_STATUS_NOT_COMPATIBLE
;
107 private final static String MESG_COMPATIBLE_EDITOR_OLD
= Messages
.RemotingLoginDialog_UPDATE_EDITOR
;
108 private final static String MESG_COMPATIBLE_SERVER_OLD
= Messages
.RemotingLoginDialog_CHOOSE_COMPATIBLE_CDM_SERVER
;
110 private final static String STORE_PREFERENCES_NODE
= "eu.etaxonomy.taxeditor.store"; //$NON-NLS-1$
112 private final static String LOGIN_NODE
= "login"; //$NON-NLS-1$
113 private final static String USERNAME_SUFFIX
= "_username"; //$NON-NLS-1$
114 private final static String PASSWORD_SUFFIX
= "_password"; //$NON-NLS-1$
116 private final static String LAST_SERVER_INSTANCE_NODE
= "lastServerInstance"; //$NON-NLS-1$
117 private final static String LAST_SERVER_KEY
= "lastServerKey"; //$NON-NLS-1$
118 private final static String LAST_INSTANCE_KEY
= "lastInstanceKey"; //$NON-NLS-1$
120 private final static String REFRESH_LABEL
= Messages
.RemotingLoginDialog_LABEL_REFRESH
;
122 private Composite remotingComposite
;
123 private CdmServerInfo selectedCsii
;
124 private CdmInstanceInfo selectedCdmInstance
;
125 private Button btnCdmServerRefresh
;
126 private Button btnCdmInstanceRefresh
;
127 private Button btnStopServer
;
128 private Composite loginComposite
;
129 private Label lblLogin
;
130 private Text txtLogin
;
131 private Label lblPassword
;
132 private Text txtPassword
;
133 private Button btnRememberMe
;
134 private Composite compAdvanced
;
135 private Label lblPort
;
136 private Text txtPort
;
137 private Label lblServerVersion
;
138 private Text txtServerVersion
;
139 private ExpandableComposite xpndblcmpstAdvanced
;
140 private StyledText styledTxtMessage
;
143 private final int MIN_WIDTH
= 530;
144 private final int MIN_HEIGHT
= 220;
145 private final int MIN_EXP_HEIGHT
= 380;
146 private final int MESSAGE_HEIGHT
= 25;
147 private Label lblEditorVersion
;
148 private Text txtEditorVersion
;
149 private Label lblServerCDMVersion
;
150 private Text txtServerCDMVersion
;
151 private Label lblEditorCDMVersion
;
152 private Text txtEditorCDMVersion
;
154 private String serverName
, instanceName
;
155 private boolean autoConnect
= false;
156 private boolean loadLoginPrefs
= true;
157 private boolean isDevRemoteSource
= false;
158 private Job serverJob
;
164 public RemotingLoginDialog(Shell parent
, int style
) {
165 super(parent
, style
);
166 setText(Messages
.RemotingLoginDialog_LABEL_LOGIN
);
169 public Object
open(CdmRemoteSource source
, boolean loadLoginPrefs
, boolean autoConnect
) {
170 this.loadLoginPrefs
= loadLoginPrefs
;
171 this.serverName
= source
.getName();
172 String contextPath
= source
.getContextPath();
173 this.instanceName
= contextPath
.substring(contextPath
.lastIndexOf("/") + 1); //$NON-NLS-1$
174 return open(serverName
, instanceName
, loadLoginPrefs
, autoConnect
);
178 public Object
open(String serverName
, String instanceName
, boolean loadLoginPrefs
, boolean autoConnect
) {
179 this.serverName
= serverName
;
180 this.instanceName
= instanceName
;
181 this.loadLoginPrefs
= loadLoginPrefs
;
182 this.autoConnect
= autoConnect
;
190 public Object
open() {
193 if(serverName
== null && instanceName
== null) {
194 readPrefLastServerInstance();
198 populateCdmServerCombo();
202 xpndblcmpstAdvanced
.setExpanded(false);
204 Display display
= getParent().getDisplay();
206 while (!shlConnect
.isDisposed()) {
207 if (!display
.readAndDispatch()) {
216 * Create contents of the dialog.
218 private void createContents() {
219 shlConnect
= new Shell(getParent(), SWT
.DIALOG_TRIM
);
220 shlConnect
.setMinimumSize(new Point(MIN_WIDTH
, MIN_HEIGHT
));
221 shlConnect
.setSize(MIN_WIDTH
, MIN_HEIGHT
);
222 shlConnect
.setText(Messages
.RemotingLoginDialog_LABEL_CONNECT
);
223 shlConnect
.setLayout(new FillLayout(SWT
.HORIZONTAL
));
225 remotingComposite
= new Composite(shlConnect
, SWT
.NONE
);
226 remotingComposite
.setLayout(new GridLayout(1, false));
228 Composite cdmServerComposite
= new Composite(remotingComposite
, SWT
.NONE
);
229 GridData gd_cdmServerComposite
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 1, 1);
230 gd_cdmServerComposite
.heightHint
= 68;
231 cdmServerComposite
.setLayoutData(gd_cdmServerComposite
);
232 cdmServerComposite
.setLayout(new GridLayout(4, false));
234 Label lblCdmServer
= new Label(cdmServerComposite
, SWT
.NONE
);
235 lblCdmServer
.setText(Messages
.RemotingLoginDialog_LABEL_CDM_SERVER
);
236 lblCdmServer
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
237 lblCdmServer
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
239 comboCdmServer
= new Combo(cdmServerComposite
, SWT
.READ_ONLY
);
240 comboCdmServer
.addSelectionListener(new SelectionAdapter() {
242 public void widgetSelected(SelectionEvent e
) {
247 GridData gd_comboCdmServer
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
248 gd_comboCdmServer
.widthHint
= 150;
249 comboCdmServer
.setLayoutData(gd_comboCdmServer
);
250 comboCdmServer
.select(0);
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, false, 1, 1);
256 gd_txtCdmServerStatus
.widthHint
= 100;
257 txtCdmServerStatus
.setLayoutData(gd_txtCdmServerStatus
);
259 btnCdmServerRefresh
= new Button(cdmServerComposite
, SWT
.NONE
);
260 btnCdmServerRefresh
.addSelectionListener(new SelectionAdapter() {
262 public void widgetSelected(SelectionEvent e
) {
266 btnCdmServerRefresh
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1));
267 btnCdmServerRefresh
.setText(Messages
.RemotingLoginDialog_LABEL_REFRESH
);
269 Label lblCdmInstance
= new Label(cdmServerComposite
, SWT
.NONE
);
270 GridData gd_lblCdmInstance
= new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1);
271 gd_lblCdmInstance
.heightHint
= 30;
272 lblCdmInstance
.setLayoutData(gd_lblCdmInstance
);
273 lblCdmInstance
.setText(Messages
.RemotingLoginDialog_LABEL_CDM_INSTANCE
);
274 lblCdmInstance
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
276 comboCdmInstance
= new Combo(cdmServerComposite
, SWT
.READ_ONLY
);
277 comboCdmInstance
.addSelectionListener(new SelectionAdapter() {
279 public void widgetSelected(SelectionEvent e
) {
280 refreshCdmInstance();
283 GridData gd_comboCdmInstance
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
284 gd_comboCdmInstance
.widthHint
= 150;
285 comboCdmInstance
.setLayoutData(gd_comboCdmInstance
);
286 comboCdmInstance
.select(0);
288 txtCdmInstanceStatus
= new Text(cdmServerComposite
, SWT
.BORDER
);
289 txtCdmInstanceStatus
.setBackground(SWTResourceManager
.getColor(SWT
.COLOR_INFO_BACKGROUND
));
290 txtCdmInstanceStatus
.setEditable(false);
291 GridData gd_txtCdmInstanceStatus
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1);
292 gd_txtCdmInstanceStatus
.widthHint
= 100;
293 txtCdmInstanceStatus
.setLayoutData(gd_txtCdmInstanceStatus
);
295 btnCdmInstanceRefresh
= new Button(cdmServerComposite
, SWT
.FLAT
);
296 btnCdmInstanceRefresh
.addSelectionListener(new SelectionAdapter() {
298 public void widgetSelected(SelectionEvent e
) {
299 refreshCdmInstance();
302 GridData gd_btnCdmInstanceRefresh
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1);
303 gd_btnCdmInstanceRefresh
.widthHint
= 110;
304 gd_btnCdmInstanceRefresh
.heightHint
= 30;
305 btnCdmInstanceRefresh
.setLayoutData(gd_btnCdmInstanceRefresh
);
306 btnCdmInstanceRefresh
.setText(Messages
.RemotingLoginDialog_LABEL_REFRESH
);
308 loginComposite
= new Composite(remotingComposite
, SWT
.NONE
);
309 GridData gd_loginComposite
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 1, 1);
310 gd_loginComposite
.widthHint
= 487;
311 gd_loginComposite
.heightHint
= 70;
312 loginComposite
.setLayoutData(gd_loginComposite
);
313 GridLayout gl_loginComposite
= new GridLayout(6, false);
314 gl_loginComposite
.marginTop
= 5;
315 loginComposite
.setLayout(gl_loginComposite
);
317 lblLogin
= new Label(loginComposite
, SWT
.CENTER
);
318 GridData gd_lblLogin
= new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1);
319 gd_lblLogin
.widthHint
= 50;
320 lblLogin
.setLayoutData(gd_lblLogin
);
321 lblLogin
.setText(Messages
.RemotingLoginDialog_LABEL_LOGIN_COLON
);
322 lblLogin
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
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 gd_txtLogin
.widthHint
= 80;
328 gd_txtLogin
.heightHint
= 15;
329 txtLogin
.setLayoutData(gd_txtLogin
);
331 lblPassword
= new Label(loginComposite
, SWT
.CENTER
);
332 lblPassword
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
333 lblPassword
.setText(Messages
.RemotingLoginDialog_LABEL_PASSWORD
);
334 lblPassword
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
336 txtPassword
= new Text(loginComposite
, SWT
.BORDER
| SWT
.PASSWORD
);
337 GridData gd_txtPassword
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
338 gd_txtPassword
.minimumWidth
= 80;
339 gd_txtPassword
.widthHint
= 80;
340 gd_txtPassword
.heightHint
= 15;
341 txtPassword
.setLayoutData(gd_txtPassword
);
342 new Label(loginComposite
, SWT
.NONE
);
344 btnConnect
= new Button(loginComposite
, SWT
.FLAT
);
345 btnConnect
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1));
346 btnConnect
.addMouseListener(new MouseAdapter() {
348 public void mouseUp(MouseEvent e
) {
349 if(selectedCsii
.isLocalhostMgd() && !isSelectedCdmInstanceRunningInManagedServer()) {
350 startManagedServer();
356 btnConnect
.setText(Messages
.RemotingLoginDialog_LABEL_CONNECT
);
358 btnRememberMe
= new Button(loginComposite
, SWT
.CHECK
);
359 btnRememberMe
.setSelection(true);
360 GridData gd_btnRememberMe
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 2, 1);
361 btnRememberMe
.setLayoutData(gd_btnRememberMe
);
362 btnRememberMe
.setText(Messages
.RemotingLoginDialog_LABEL_REMEMBER_ME
);
363 new Label(loginComposite
, SWT
.NONE
);
364 new Label(loginComposite
, SWT
.NONE
);
365 new Label(loginComposite
, SWT
.NONE
);
366 new Label(loginComposite
, SWT
.NONE
);
368 styledTxtMessage
= new StyledText(remotingComposite
, SWT
.NONE
);
369 styledTxtMessage
.setBackground(SWTResourceManager
.getColor(SWT
.COLOR_INFO_BACKGROUND
));
370 styledTxtMessage
.setForeground(SWTResourceManager
.getColor(SWT
.COLOR_DARK_RED
));
371 styledTxtMessage
.setFont(SWTResourceManager
.getFont(UBUNTU
, 12, SWT
.BOLD
));
372 styledTxtMessage
.setSelectionBackground(SWTResourceManager
.getColor(SWT
.COLOR_LIST_SELECTION_TEXT
));
373 styledTxtMessage
.setSelectionForeground(SWTResourceManager
.getColor(SWT
.COLOR_DARK_RED
));
374 styledTxtMessage
.setDoubleClickEnabled(false);
375 styledTxtMessage
.setEditable(false);
376 GridData gd_styledTxtMessage
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
377 gd_styledTxtMessage
.exclude
= true;
378 gd_styledTxtMessage
.minimumHeight
= MESSAGE_HEIGHT
;
379 gd_styledTxtMessage
.heightHint
= MESSAGE_HEIGHT
;
380 styledTxtMessage
.setLayoutData(gd_styledTxtMessage
);
382 xpndblcmpstAdvanced
= new ExpandableComposite(remotingComposite
, SWT
.NONE
, ExpandableComposite
.TWISTIE
);
383 GridData gd_xpndblcmpstAdvanced
= new GridData(SWT
.FILL
, SWT
.FILL
, false, true, 1, 1);
384 gd_xpndblcmpstAdvanced
.heightHint
= 19;
385 xpndblcmpstAdvanced
.setLayoutData(gd_xpndblcmpstAdvanced
);
386 xpndblcmpstAdvanced
.addExpansionListener(new IExpansionListener() {
388 public void expansionStateChanged(ExpansionEvent e
) {
389 GridData gridData
= (GridData
) xpndblcmpstAdvanced
.getLayoutData();
391 shlConnect
.setSize(MIN_WIDTH
, MIN_EXP_HEIGHT
);
393 shlConnect
.setSize(MIN_WIDTH
, MIN_HEIGHT
);
398 public void expansionStateChanging(ExpansionEvent e
) {
401 xpndblcmpstAdvanced
.setText(Messages
.RemotingLoginDialog_LABEL_ADVANCED
);
402 xpndblcmpstAdvanced
.setExpanded(true);
404 compAdvanced
= new Composite(xpndblcmpstAdvanced
, SWT
.NONE
);
405 xpndblcmpstAdvanced
.setClient(compAdvanced
);
406 compAdvanced
.setLayout(new GridLayout(4, false));
408 lblPort
= new Label(compAdvanced
, SWT
.CENTER
);
409 lblPort
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
410 lblPort
.setSize(0, 0);
411 lblPort
.setText(Messages
.RemotingLoginDialog_LABEL_PORT
);
412 lblPort
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
414 txtPort
= new Text(compAdvanced
, SWT
.BORDER
);
415 GridData gd_txtPort
= new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1);
416 gd_txtPort
.minimumWidth
= 50;
417 gd_txtPort
.widthHint
= 50;
418 txtPort
.setLayoutData(gd_txtPort
);
420 lblServerVersion
= new Label(compAdvanced
, SWT
.CENTER
);
421 lblServerVersion
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
422 lblServerVersion
.setText(Messages
.RemotingLoginDialog_LABEL_SERVER_CDMLIB_VERSION
);
423 lblServerVersion
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
425 txtServerVersion
= new Text(compAdvanced
, SWT
.BORDER
);
426 txtServerVersion
.setEditable(false);
427 txtServerVersion
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1));
428 btnStopServer
= new Button(compAdvanced
, SWT
.FLAT
);
429 btnStopServer
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1));
430 btnStopServer
.addMouseListener(new MouseAdapter() {
432 public void mouseUp(MouseEvent e
) {
436 btnStopServer
.setText(Messages
.RemotingLoginDialog_LABEL_STOP_MANAGED_SERVER
);
438 lblEditorVersion
= new Label(compAdvanced
, SWT
.CENTER
);
439 lblEditorVersion
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
440 lblEditorVersion
.setText(Messages
.RemotingLoginDialog_LABEL_EDITOR_CDMLIB_VERSION
);
441 lblEditorVersion
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
443 txtEditorVersion
= new Text(compAdvanced
, SWT
.BORDER
);
444 txtEditorVersion
.setEditable(false);
445 txtEditorVersion
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1));
446 new Label(compAdvanced
, SWT
.NONE
);
447 new Label(compAdvanced
, SWT
.NONE
);
449 lblServerCDMVersion
= new Label(compAdvanced
, SWT
.CENTER
);
450 lblServerCDMVersion
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
451 lblServerCDMVersion
.setText(Messages
.RemotingLoginDialog_LABEL_SERVER_CDM_VERSION
);
452 lblServerCDMVersion
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
454 txtServerCDMVersion
= new Text(compAdvanced
, SWT
.BORDER
);
455 txtServerCDMVersion
.setEditable(false);
456 txtServerCDMVersion
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1));
457 new Label(compAdvanced
, SWT
.NONE
);
458 new Label(compAdvanced
, SWT
.NONE
);
460 lblEditorCDMVersion
= new Label(compAdvanced
, SWT
.CENTER
);
461 lblEditorCDMVersion
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
462 lblEditorCDMVersion
.setText(Messages
.RemotingLoginDialog_LABEL_EDITOR_CDM_VERSION
);
463 lblEditorCDMVersion
.setFont(SWTResourceManager
.getFont(UBUNTU
, 9, SWT
.NORMAL
));
465 txtEditorCDMVersion
= new Text(compAdvanced
, SWT
.BORDER
);
466 txtEditorCDMVersion
.setEditable(false);
467 txtEditorCDMVersion
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1));
473 private void populateCdmServerCombo() {
474 Job job
= new Job(Messages
.RemotingLoginDialog_RETRIEVE_SERVER_INSTANCES
) {
476 protected IStatus
run(IProgressMonitor monitor
) {
477 Display
.getDefault().syncExec(new Runnable() {
480 for(CdmServerInfo csii
: CdmServerInfo
.getCdmServers()) {
481 csiiMap
.put(csii
.getName(), csii
);
482 comboCdmServer
.add(csii
.getName());
484 int serverIndex
= -1;
485 if(serverName
!= null) {
486 serverIndex
= comboCdmServer
.indexOf(serverName
);
488 if(serverIndex
== -1) {
489 comboCdmServer
.select(0);
492 comboCdmServer
.select(serverIndex
);
494 CdmRemoteSource devRemoteSource
= CdmServerInfo
.getDevServerRemoteSource();
495 if(devRemoteSource
!= null) {
496 isDevRemoteSource
= true;
497 String username
= System
.getProperty("cdm.server.dev.username"); //$NON-NLS-1$
498 String password
= System
.getProperty("cdm.server.dev.password"); //$NON-NLS-1$
499 if(username
!= null && !username
.isEmpty() && password
!= null && !password
.isEmpty()) {
500 txtLogin
.setText(username
);
501 txtPassword
.setText(password
);
502 CdmStore
.connect(devRemoteSource
, RemotingLoginDialog
.this);
509 return Status
.OK_STATUS
;
516 private void refreshCdmServer() {
517 txtCdmServerStatus
.setText(STATUS_CHECKING_AVAILABILITY
);
518 clearOnServerChange();
520 updateSelectedCdmServer();
521 Display
.getDefault().asyncExec(new Runnable() {
524 checkSelectedCdmServer();
530 private void updateSelectedCdmServer() {
531 int selIndex
= comboCdmServer
.getSelectionIndex();
533 selectedCsii
= csiiMap
.get(comboCdmServer
.getItem(selIndex
));
537 private void updatePort() {
539 if(selectedCsii
!= null) {
540 int port
= selectedCsii
.getPort();
541 if(port
== CdmServerInfo
.NULL_PORT
) {
542 txtPort
.setText(CdmServerInfo
.NULL_PORT_STRING
);
544 txtPort
.setText(String
.valueOf(port
));
549 private int getPort() {
550 int port
= CdmServerInfo
.NULL_PORT
;
552 port
= Integer
.valueOf(txtPort
.getText());
553 } catch (NumberFormatException nfe
) {
554 if(!CdmServerInfo
.NULL_PORT_STRING
.equals(txtPort
.getText())) {
555 setMessage(Messages
.RemotingLoginDialog_MESSAGE_PORT_SHOULD_BE_INTEGER
);
561 private void checkSelectedCdmServer() {
562 if(selectedCsii
!= null) {
563 if(selectedCsii
.isLocalhost()) {
564 txtPort
.setEditable(true);
565 txtPort
.setEnabled(true);
567 if(selectedCsii
.pingServer()) {
568 txtCdmServerStatus
.setText(STATUS_AVAILABLE
);
569 populateCdmInstanceCombo(true);
570 String serverVersionTimestamp
= generateLastModifiedTooltip(selectedCsii
.getCdmlibLastModified());
571 txtServerVersion
.setText(selectedCsii
.getCdmlibServicesVersion() + ":" + serverVersionTimestamp
); //$NON-NLS-1$
573 txtCdmServerStatus
.setText(STATUS_NOT_AVAILABLE
);
574 comboCdmInstance
.removeAll();
575 disableCdmInstanceControls("", "");
582 private void populateCdmInstanceCombo(final boolean forceRefresh
) {
583 comboCdmInstance
.removeAll();
584 comboCdmInstance
.setEnabled(false);
585 btnConnect
.setEnabled(false);
586 txtCdmInstanceStatus
.setText(STATUS_RETRIEVING
);
587 txtCdmInstanceStatus
.setToolTipText("");
589 serverJob
= new Job(Messages
.RemotingLoginDialog_RETRIEVE_SERVER_INSTANCES
) {
591 protected IStatus
run(IProgressMonitor monitor
) {
593 if(selectedCsii
!= null) {
595 selectedCsii
.refreshInstances();
597 final List
<CdmInstanceInfo
> instances
= selectedCsii
.getInstances();
598 Display
.getDefault().asyncExec(new Runnable() {
601 if(!instances
.isEmpty()) {
602 for(CdmInstanceInfo cdmInstance
: instances
) {
603 comboCdmInstance
.add(cdmInstance
.getName());
605 int instanceIndex
= -1;
606 if(instanceName
!= null) {
607 instanceIndex
= comboCdmInstance
.indexOf(instanceName
);
609 if(instanceIndex
== -1) {
610 comboCdmInstance
.select(0);
613 comboCdmInstance
.select(instanceIndex
);
615 refreshCdmInstance();
616 comboCdmInstance
.setEnabled(true);
622 txtCdmInstanceStatus
.setText(STATUS_NO_INSTANCES
);
623 btnConnect
.setEnabled(false);
628 } catch (final CDMServerException e
) {
629 MessagingUtils
.warn(getClass(), e
);
630 Display
.getDefault().asyncExec(new Runnable() {
633 disableCdmInstanceControls(STATUS_NOT_AVAILABLE
, e
.getMessage());
637 return Status
.OK_STATUS
;
641 if(txtCdmServerStatus
.getText().equals(STATUS_AVAILABLE
) && !isDevRemoteSource
) {
643 serverJob
.schedule();
647 private void disableCdmInstanceControls(String cdmInstanceStatus
, String tooltip
) {
648 txtCdmInstanceStatus
.setText(cdmInstanceStatus
);
649 txtCdmInstanceStatus
.setToolTipText(tooltip
);
650 comboCdmInstance
.setEnabled(false);
651 btnConnect
.setEnabled(false);
654 private void refreshCdmInstance() {
655 txtCdmInstanceStatus
.setText(STATUS_CHECKING_AVAILABILITY
);
656 clearOnInstanceChange();
657 updateSelectedCdmInstance();
658 checkSelectedCdmInstance();
659 updateManagedServerControls();
662 private void updateSelectedCdmInstance() {
663 int selIndex
= comboCdmInstance
.getSelectionIndex();
665 selectedCdmInstance
= selectedCsii
.getInstanceFromName(comboCdmInstance
.getItem(selIndex
));
667 if(loadLoginPrefs
&& !isDevRemoteSource
) {
668 readPrefCredentials();
673 private void updateManagedServerControls() {
674 if(selectedCsii
.isLocalhostMgd()) {
675 if(isSelectedCdmInstanceRunningInManagedServer()) {
676 txtCdmInstanceStatus
.setText(STATUS_STARTED
);
678 txtCdmInstanceStatus
.setText(STATUS_NOT_STARTED
);
680 btnConnect
.setEnabled(true);
681 selectedCsii
.setPort(getManagedServerPort());
686 if(isManagedServerRunning()) {
687 btnStopServer
.setEnabled(true);
689 btnStopServer
.setEnabled(false);
693 private boolean isManagedServerRunning() {
694 return CdmStore
.getManagedServer() != null && CdmStore
.getManagedServer().isAlive();
697 private boolean isSelectedCdmInstanceRunningInManagedServer() {
698 return CdmStore
.getManagedServer() != null &&
699 CdmStore
.getManagedServer().isAlive() &&
700 selectedCsii
.isLocalhostMgd() &&
701 CdmStore
.getManagedServer().getDataSourceName().equals(selectedCdmInstance
.getName());
704 private void startManagedServer() {
705 if(isManagedServerRunning()) {
706 if(CdmStore
.getManagedServer().getDataSourceName().equals(selectedCdmInstance
.getName())) {
709 Display
.getDefault().syncExec(new Runnable() {
719 Job job
= new Job(Messages
.RemotingLoginDialog_JOB_SERVER_LAUNCH
) {
722 public IStatus
run(IProgressMonitor monitor
) {
723 String mgdServerConfigFileName
= "mgd.datasources.xml"; //$NON-NLS-1$
724 String config
= CDMServerUtils
.convertEditorToServerConfig();
725 File managedServerConfigFile
;
727 monitor
.beginTask(Messages
.RemotingLoginDialog_TASK_LAUNCHING_SERVER
, maxUnits
);
729 monitor
.subTask(String
.format(Messages
.RemotingLoginDialog_GENERATING_CONFIG_FILE
, selectedCdmInstance
.getName()));
730 managedServerConfigFile
= CDMServerUtils
.writeManagedServerConfig(config
, mgdServerConfigFileName
);
732 CdmStore
.setManagedServer(new CDMServer(selectedCdmInstance
.getName(), managedServerConfigFile
));
733 monitor
.subTask(Messages
.RemotingLoginDialog_STARTING_MGD_SERVER
);
734 CdmStore
.getManagedServer().start(false, RemotingLoginDialog
.this);
737 // the following loop is a 'fake' progress monitoring where the progress
738 // bar is advanced by one unit every second until maxUnits -2
739 while(!CdmStore
.getManagedServer().isStarted() && !CdmStore
.getManagedServer().isFailed()) {
740 if(serverUnits
< maxUnits
- 2) {
743 } catch (InterruptedException e
) {
749 Display
.getDefault().asyncExec(new Runnable() {
753 updateManagedServerControls();
757 } catch (IOException ioe
) {
758 MessagingUtils
.errorDialog(Messages
.RemotingLoginDialog_ERROR_GENERATING_CONFIG_FILE
,
761 TaxeditorStorePlugin
.PLUGIN_ID
,
764 } catch (CDMEmbeddedServerException cse
) {
765 MessagingUtils
.errorDialog(Messages
.RemotingLoginDialog_ERROR_STARTING_SERVER
,
768 TaxeditorStorePlugin
.PLUGIN_ID
,
774 return Status
.OK_STATUS
;
779 job
.setProperty(IProgressConstants
.KEEP_PROPERTY
, true);
786 private void stopManagedServer() {
788 CdmStore
.getManagedServer().stop();
789 } catch (Exception e
) {
790 MessagingUtils
.errorDialog(Messages
.RemotingLoginDialog_ERROR_STOPPING_SERVER
,
792 String
.format(Messages
.RemotingLoginDialog_COULD_NOT_STOP_SERVER
, CdmStore
.getManagedServer().getPort()),
793 TaxeditorStorePlugin
.PLUGIN_ID
,
797 CdmStore
.setManagedServer(null);
798 updateManagedServerControls();
801 private int getManagedServerPort() {
802 return CdmStore
.getManagedServer() == null ? CdmServerInfo
.NULL_PORT
: CdmStore
.getManagedServer().getPort();
805 private void checkSelectedCdmInstance() {
806 boolean available
= false;
807 String status
= STATUS_NOT_AVAILABLE
;
808 String message
= null;
810 if(txtCdmServerStatus
.getText().equals(STATUS_AVAILABLE
)) {
812 if(selectedCsii
.pingInstance(selectedCdmInstance
, getPort())) {
813 status
= STATUS_AVAILABLE
;
816 status
= STATUS_NOT_AVAILABLE
;
821 txtServerCDMVersion
.setText(selectedCsii
.getCdmRemoteSource(selectedCdmInstance
, getPort()).getDbSchemaVersion());
822 int compareDbSchemaVersion
= selectedCsii
.compareDbSchemaVersion(selectedCdmInstance
, getPort());
823 int compareCdmlibServicesVersion
= 0;
824 boolean disableServicesApiTimestampCheck
=
825 PreferencesUtil
.getPreferenceStore().getBoolean((IPreferenceKeys
.DISABLE_SERVICES_API_TIMESTAMP_CHECK
));
826 if(!disableServicesApiTimestampCheck
) {
827 compareCdmlibServicesVersion
= selectedCsii
.compareCdmlibServicesVersion();
829 if(compareDbSchemaVersion
> 0 || compareCdmlibServicesVersion
> 0) {
830 status
= STATUS_NOT_COMPATIBLE
;
832 message
= MESG_COMPATIBLE_EDITOR_OLD
;
833 } else if(compareDbSchemaVersion
< 0 || compareCdmlibServicesVersion
< 0) {
834 status
= STATUS_NOT_COMPATIBLE
;
836 message
= MESG_COMPATIBLE_SERVER_OLD
;
838 status
= STATUS_AVAILABLE
;
840 message
= ""; //$NON-NLS-1$
843 } catch (Exception e
) {
844 txtCdmInstanceStatus
.setToolTipText(e
.getMessage());
846 btnConnect
.setEnabled(available
);
847 txtCdmInstanceStatus
.setText(status
);
848 if(!StringUtils
.isBlank(message
)) {
855 private void connect() {
856 checkSelectedCdmInstance();
858 if(!txtCdmInstanceStatus
.getText().equals(STATUS_AVAILABLE
)) {
862 ICdmRemoteSource source
= selectedCsii
.getCdmRemoteSource(selectedCdmInstance
, getPort());
864 if(!validateLogin(source
)) {
869 CdmStore
.connect(source
, this);
870 } catch (Exception e
) {
871 // Do not expect anything to go wrong at this point, so we throw a runtime exception
873 throw new RuntimeException(e
);
879 public boolean isRememberMe() {
880 return btnRememberMe
.getSelection();
883 private void persistPrefLastServerInstance() {
884 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
885 Preferences lastServerInstancePrefs
= preferences
.node(LAST_SERVER_INSTANCE_NODE
);
887 lastServerInstancePrefs
.put(LAST_SERVER_KEY
, selectedCsii
.getName());
888 lastServerInstancePrefs
.put(LAST_INSTANCE_KEY
, selectedCdmInstance
.getName());
890 flushPreferences(lastServerInstancePrefs
);
893 private void persistPrefCredentials() {
894 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
895 Preferences credentialsPrefs
= preferences
.node(LOGIN_NODE
);
896 credentialsPrefs
.put(getUsernamePrefKey(), txtLogin
.getText());
897 credentialsPrefs
.put(getPasswordPrefKey(), txtPassword
.getText());
898 flushPreferences(credentialsPrefs
);
901 private void flushPreferences(Preferences prefs
) {
904 } catch (BackingStoreException bse
) {
905 setMessage(bse
.getMessage());
909 private void readPrefCredentials() {
910 String username
, password
;
911 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
912 Preferences credentialsPrefs
= preferences
.node(LOGIN_NODE
);
913 username
= credentialsPrefs
.get(getUsernamePrefKey(), ""); //$NON-NLS-1$
914 txtLogin
.setText(username
);
915 password
= credentialsPrefs
.get(getPasswordPrefKey(),""); //$NON-NLS-1$
916 txtPassword
.setText(password
);
917 if(username
.isEmpty() || password
.isEmpty()) {
922 private void readPrefLastServerInstance() {
923 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
924 Preferences lastServerInstancePrefs
= preferences
.node(LAST_SERVER_INSTANCE_NODE
);
926 serverName
= lastServerInstancePrefs
.get(LAST_SERVER_KEY
, null);
927 instanceName
= lastServerInstancePrefs
.get(LAST_INSTANCE_KEY
, null);
930 private void emptyCredentials() {
931 txtLogin
.setText("");
932 txtPassword
.setText("");
935 private String
getUsernamePrefKey() {
936 return selectedCsii
.toString(selectedCdmInstance
.getName(), getPort()) + USERNAME_SUFFIX
;
939 private String
getPasswordPrefKey() {
940 return selectedCsii
.toString(selectedCdmInstance
.getName(), getPort()) + PASSWORD_SUFFIX
;
943 private boolean validateLogin(ICdmRemoteSource remoteSource
) {
944 if(getUsername() == null || getUsername().isEmpty()) {
945 setMessage(Messages
.RemotingLoginDialog_LOGIN_CANNOT_BE_EMPTY
);
948 if(getPassword() == null || getPassword().isEmpty()) {
949 setMessage(Messages
.RemotingLoginDialog_PASSWORD_CANNOT_BE_EMPTY
);
955 IUserService userService
= CdmApplicationRemoteConfiguration
.getUserService(remoteSource
);
956 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(getUsername(), getPassword());
957 CdmApplicationRemoteConfiguration
.getAuthenticationManager(userService
).authenticate(token
);
958 } catch(BadCredentialsException e
){
959 setMessage(LoginManager
.INCORRECT_CREDENTIALS_MESSAGE
);
961 } catch(LockedException e
){
962 setMessage(LoginManager
.ACCOUNT_LOCKED_MESSAGE
);
964 } catch(IllegalArgumentException e
){
965 setMessage(LoginManager
.INCORRECT_CREDENTIALS_MESSAGE
);
971 public String
getUsername() {
972 return txtLogin
.getText();
975 public String
getPassword() {
976 return txtPassword
.getText();
979 public void setMessage(String message
) {
980 if(message
!= null && !message
.isEmpty()) {
981 if(message
.length() > 60) {
982 styledTxtMessage
.setToolTipText(message
);
983 message
= message
.substring(0, 60) + "..."; //$NON-NLS-1$
985 styledTxtMessage
.setText(message
);
986 styledTxtMessage
.setVisible(true);
987 ((GridData
)styledTxtMessage
.getLayoutData()).exclude
= false;
988 shlConnect
.setSize(MIN_WIDTH
, getHeightWithoutMessage() + MESSAGE_HEIGHT
);
989 shlConnect
.setMinimumSize(MIN_WIDTH
, getHeightWithoutMessage() + MESSAGE_HEIGHT
);
991 styledTxtMessage
.setText(""); //$NON-NLS-1$
992 styledTxtMessage
.setVisible(false);
993 ((GridData
)styledTxtMessage
.getLayoutData()).exclude
= true;
994 shlConnect
.setSize(MIN_WIDTH
, getHeightWithoutMessage());
995 shlConnect
.setMinimumSize(MIN_WIDTH
, getHeightWithoutMessage());
997 remotingComposite
.layout();
1000 private int getHeightWithoutMessage() {
1001 if(xpndblcmpstAdvanced
.isExpanded()) {
1002 return MIN_EXP_HEIGHT
;
1009 public void hide(boolean isHidden
) {
1010 if(shlConnect
!= null && shlConnect
.getDisplay() != null) {
1011 shlConnect
.setVisible(!isHidden
);
1014 public void dispose() {
1015 if(shlConnect
!= null && shlConnect
.getDisplay() != null) {
1016 shlConnect
.dispose();
1020 public void onComplete() {
1021 Display
.getDefault().asyncExec(new Runnable() {
1024 if(!isDevRemoteSource
) {
1025 if(isRememberMe()) {
1026 persistPrefCredentials();
1028 persistPrefLastServerInstance();
1035 private String
generateLastModifiedTooltip(String cdmlibLastModified
) {
1036 if(StringUtils
.isBlank(cdmlibLastModified
)) {
1037 return ""; //$NON-NLS-1$
1039 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy.MM.dd.HH.mm.z"); //$NON-NLS-1$
1040 Date cdmlibLastModifiedDate
;
1041 String cdmlibLastModifiedTimestamp
= ""; //$NON-NLS-1$
1043 cdmlibLastModifiedDate
= new Date(Long
.valueOf(cdmlibLastModified
));
1044 cdmlibLastModifiedTimestamp
= sdf
.format(cdmlibLastModifiedDate
);
1046 return cdmlibLastModifiedTimestamp
;
1049 private void setEditorInfo() {
1050 txtEditorCDMVersion
.setText(CdmMetaData
.getDbSchemaVersion());
1051 String editorVersionTimestamp
= generateLastModifiedTooltip(CdmApplicationState
.getCdmlibLastModified());
1052 txtEditorVersion
.setText(CdmApplicationState
.getCdmlibVersion() + ":" + editorVersionTimestamp
); //$NON-NLS-1$
1055 private void clearOnServerChange() {
1056 setMessage(""); //$NON-NLS-1$
1057 txtServerCDMVersion
.setText(""); //$NON-NLS-1$
1058 txtServerVersion
.setText(""); //$NON-NLS-1$
1059 txtServerVersion
.setToolTipText(""); //$NON-NLS-1$
1060 txtServerCDMVersion
.setText(""); //$NON-NLS-1$
1061 comboCdmInstance
.removeAll();
1062 txtCdmInstanceStatus
.setText(""); //$NON-NLS-1$
1063 txtPort
.setEditable(false);
1064 txtPort
.setEnabled(false);
1067 private void clearOnInstanceChange() {
1068 setMessage(""); //$NON-NLS-1$
1069 txtServerCDMVersion
.setText(""); //$NON-NLS-1$
1076 public void handleError(final Throwable t
) {
1078 Display
.getDefault().syncExec(new Runnable() {
1083 String title
= Messages
.RemotingLoginDialog_SERVER_LAUNCH_ERROR
;
1084 String message
= t
.getMessage();
1087 MessagingUtils
.errorDialog(title
,
1090 TaxeditorStorePlugin
.PLUGIN_ID
,