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
;
53 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
54 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
55 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
56 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
57 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
58 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
59 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerUtils
;
60 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
61 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmServerInfo
;
62 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmServerInfo
.CdmInstanceInfo
;
63 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
64 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
65 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
66 import eu
.etaxonomy
.taxeditor
.webapp
.CDMEmbeddedServerException
;
67 import eu
.etaxonomy
.taxeditor
.webapp
.CDMServer
;
68 import eu
.etaxonomy
.taxeditor
.webapp
.ICDMServerError
;
76 public class RemotingLoginDialog
extends Dialog
implements ICDMServerError
{
78 protected Object result
;
79 protected Shell shlConnect
;
80 private Text txtCdmServerStatus
;
81 private Text txtCdmInstanceStatus
;
82 private Combo comboCdmServer
;
83 private Combo comboCdmInstance
;
84 private Button btnConnect
;
86 private final Map
<String
, CdmServerInfo
> csiiMap
= new HashMap
<String
, CdmServerInfo
>();
88 private final static String STATUS_AVAILABLE
= "Available";
89 private final static String STATUS_NOT_AVAILABLE
= "Not Available";
90 private final static String STATUS_STARTED
= "Started";
91 private final static String STATUS_NOT_STARTED
= "Not Started";
92 private final static String STATUS_RETRIEVING
= "Retrieving ...";
93 private final static String STATUS_CHECKING_AVAILABILITY
= "Checking ...";
94 private final static String STATUS_NO_INSTANCES
= "No Instances Found";
95 private final static String STATUS_ERROR
= "Error";
96 private final static String STATUS_REMOTING_NOT_ACTIVATED
= "Remoting not activated";
97 private final static String STATUS_NOT_COMPATIBLE
= "Not Compatible";
99 private final static String MESG_COMPATIBLE_EDITOR_OLD
= "Please update the Taxonomic Editor (Help->Check for Updates) or choose a compatible cdm-server";
100 private final static String MESG_COMPATIBLE_SERVER_OLD
= "Please choose a compatible cdm-server or update the chosen cdm-server";
102 private final static String STORE_PREFERENCES_NODE
= "eu.etaxonomy.taxeditor.store";
104 private final static String LOGIN_NODE
= "login";
105 private final static String USERNAME_SUFFIX
= "_username";
106 private final static String PASSWORD_SUFFIX
= "_password";
108 private final static String LAST_SERVER_INSTANCE_NODE
= "lastServerInstance";
109 private final static String LAST_SERVER_KEY
= "lastServerKey";
110 private final static String LAST_INSTANCE_KEY
= "lastInstanceKey";
112 private final static String REFRESH_LABEL
= "Refresh";
114 private Composite remotingComposite
;
115 private CdmServerInfo selectedCsii
;
116 private CdmInstanceInfo selectedCdmInstance
;
117 private Button btnCdmServerRefresh
;
118 private Button btnCdmInstanceRefresh
;
119 private Button btnStopServer
;
120 private Composite loginComposite
;
121 private Label lblLogin
;
122 private Text txtLogin
;
123 private Label lblPassword
;
124 private Text txtPassword
;
125 private Button btnRememberMe
;
126 private Composite compAdvanced
;
127 private Label lblPort
;
128 private Text txtPort
;
129 private Label lblServerVersion
;
130 private Text txtServerVersion
;
131 private ExpandableComposite xpndblcmpstAdvanced
;
132 private StyledText styledTxtMessage
;
135 private final int MIN_WIDTH
= 530;
136 private final int MIN_HEIGHT
= 220;
137 private final int MIN_EXP_HEIGHT
= 380;
138 private final int MESSAGE_HEIGHT
= 25;
139 private Label lblEditorVersion
;
140 private Text txtEditorVersion
;
141 private Label lblServerCDMVersion
;
142 private Text txtServerCDMVersion
;
143 private Label lblEditorCDMVersion
;
144 private Text txtEditorCDMVersion
;
146 private String serverName
, instanceName
;
147 private boolean autoConnect
= false;
148 private boolean loadLoginPrefs
= true;
149 private boolean isDevRemoteSource
= false;
150 private Job serverJob
;
156 public RemotingLoginDialog(Shell parent
, int style
) {
157 super(parent
, style
);
161 public Object
open(CdmRemoteSource source
, boolean loadLoginPrefs
, boolean autoConnect
) {
162 this.loadLoginPrefs
= loadLoginPrefs
;
163 this.serverName
= source
.getName();
164 String contextPath
= source
.getContextPath();
165 this.instanceName
= contextPath
.substring(contextPath
.lastIndexOf("/") + 1);
166 return open(serverName
, instanceName
, loadLoginPrefs
, autoConnect
);
170 public Object
open(String serverName
, String instanceName
, boolean loadLoginPrefs
, boolean autoConnect
) {
171 this.serverName
= serverName
;
172 this.instanceName
= instanceName
;
173 this.loadLoginPrefs
= loadLoginPrefs
;
174 this.autoConnect
= autoConnect
;
182 public Object
open() {
185 if(serverName
== null && instanceName
== null) {
186 readPrefLastServerInstance();
190 populateCdmServerCombo();
194 xpndblcmpstAdvanced
.setExpanded(false);
196 Display display
= getParent().getDisplay();
198 while (!shlConnect
.isDisposed()) {
199 if (!display
.readAndDispatch()) {
208 * Create contents of the dialog.
210 private void createContents() {
211 shlConnect
= new Shell(getParent(), SWT
.DIALOG_TRIM
);
212 shlConnect
.setMinimumSize(new Point(MIN_WIDTH
, MIN_HEIGHT
));
213 shlConnect
.setSize(MIN_WIDTH
, MIN_HEIGHT
);
214 shlConnect
.setText("Connect");
215 shlConnect
.setLayout(new FillLayout(SWT
.HORIZONTAL
));
217 remotingComposite
= new Composite(shlConnect
, SWT
.NONE
);
218 remotingComposite
.setLayout(new GridLayout(1, false));
220 Composite cdmServerComposite
= new Composite(remotingComposite
, SWT
.NONE
);
221 GridData gd_cdmServerComposite
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 1, 1);
222 gd_cdmServerComposite
.heightHint
= 68;
223 cdmServerComposite
.setLayoutData(gd_cdmServerComposite
);
224 cdmServerComposite
.setLayout(new GridLayout(4, false));
226 Label lblCdmServer
= new Label(cdmServerComposite
, SWT
.NONE
);
227 lblCdmServer
.setText("CDM Server : ");
228 lblCdmServer
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
229 lblCdmServer
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
231 comboCdmServer
= new Combo(cdmServerComposite
, SWT
.READ_ONLY
);
232 comboCdmServer
.addSelectionListener(new SelectionAdapter() {
234 public void widgetSelected(SelectionEvent e
) {
239 GridData gd_comboCdmServer
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
240 gd_comboCdmServer
.widthHint
= 150;
241 comboCdmServer
.setLayoutData(gd_comboCdmServer
);
242 comboCdmServer
.select(0);
244 txtCdmServerStatus
= new Text(cdmServerComposite
, SWT
.BORDER
);
245 txtCdmServerStatus
.setBackground(SWTResourceManager
.getColor(SWT
.COLOR_INFO_BACKGROUND
));
246 txtCdmServerStatus
.setEditable(false);
247 GridData gd_txtCdmServerStatus
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1);
248 gd_txtCdmServerStatus
.widthHint
= 100;
249 txtCdmServerStatus
.setLayoutData(gd_txtCdmServerStatus
);
251 btnCdmServerRefresh
= new Button(cdmServerComposite
, SWT
.NONE
);
252 btnCdmServerRefresh
.addSelectionListener(new SelectionAdapter() {
254 public void widgetSelected(SelectionEvent e
) {
258 btnCdmServerRefresh
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1));
259 btnCdmServerRefresh
.setText("Refresh");
261 Label lblCdmInstance
= new Label(cdmServerComposite
, SWT
.NONE
);
262 GridData gd_lblCdmInstance
= new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1);
263 gd_lblCdmInstance
.heightHint
= 30;
264 lblCdmInstance
.setLayoutData(gd_lblCdmInstance
);
265 lblCdmInstance
.setText("CDM Instance : ");
266 lblCdmInstance
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
268 comboCdmInstance
= new Combo(cdmServerComposite
, SWT
.READ_ONLY
);
269 comboCdmInstance
.addSelectionListener(new SelectionAdapter() {
271 public void widgetSelected(SelectionEvent e
) {
272 refreshCdmInstance();
275 GridData gd_comboCdmInstance
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
276 gd_comboCdmInstance
.widthHint
= 150;
277 comboCdmInstance
.setLayoutData(gd_comboCdmInstance
);
278 comboCdmInstance
.select(0);
280 txtCdmInstanceStatus
= new Text(cdmServerComposite
, SWT
.BORDER
);
281 txtCdmInstanceStatus
.setBackground(SWTResourceManager
.getColor(SWT
.COLOR_INFO_BACKGROUND
));
282 txtCdmInstanceStatus
.setEditable(false);
283 GridData gd_txtCdmInstanceStatus
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1);
284 gd_txtCdmInstanceStatus
.widthHint
= 100;
285 txtCdmInstanceStatus
.setLayoutData(gd_txtCdmInstanceStatus
);
287 btnCdmInstanceRefresh
= new Button(cdmServerComposite
, SWT
.FLAT
);
288 btnCdmInstanceRefresh
.addSelectionListener(new SelectionAdapter() {
290 public void widgetSelected(SelectionEvent e
) {
291 refreshCdmInstance();
294 GridData gd_btnCdmInstanceRefresh
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1);
295 gd_btnCdmInstanceRefresh
.widthHint
= 110;
296 gd_btnCdmInstanceRefresh
.heightHint
= 30;
297 btnCdmInstanceRefresh
.setLayoutData(gd_btnCdmInstanceRefresh
);
298 btnCdmInstanceRefresh
.setText("Refresh");
300 loginComposite
= new Composite(remotingComposite
, SWT
.NONE
);
301 GridData gd_loginComposite
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 1, 1);
302 gd_loginComposite
.widthHint
= 487;
303 gd_loginComposite
.heightHint
= 70;
304 loginComposite
.setLayoutData(gd_loginComposite
);
305 GridLayout gl_loginComposite
= new GridLayout(6, false);
306 gl_loginComposite
.marginTop
= 5;
307 loginComposite
.setLayout(gl_loginComposite
);
309 lblLogin
= new Label(loginComposite
, SWT
.CENTER
);
310 GridData gd_lblLogin
= new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1);
311 gd_lblLogin
.widthHint
= 50;
312 lblLogin
.setLayoutData(gd_lblLogin
);
313 lblLogin
.setText("Login : ");
314 lblLogin
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
316 txtLogin
= new Text(loginComposite
, SWT
.BORDER
);
317 GridData gd_txtLogin
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
318 gd_txtLogin
.minimumWidth
= 80;
319 gd_txtLogin
.widthHint
= 80;
320 gd_txtLogin
.heightHint
= 15;
321 txtLogin
.setLayoutData(gd_txtLogin
);
323 lblPassword
= new Label(loginComposite
, SWT
.CENTER
);
324 lblPassword
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
325 lblPassword
.setText("Password : ");
326 lblPassword
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
328 txtPassword
= new Text(loginComposite
, SWT
.BORDER
| SWT
.PASSWORD
);
329 GridData gd_txtPassword
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
330 gd_txtPassword
.minimumWidth
= 80;
331 gd_txtPassword
.widthHint
= 80;
332 gd_txtPassword
.heightHint
= 15;
333 txtPassword
.setLayoutData(gd_txtPassword
);
334 new Label(loginComposite
, SWT
.NONE
);
336 btnConnect
= new Button(loginComposite
, SWT
.FLAT
);
337 btnConnect
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1));
338 btnConnect
.addMouseListener(new MouseAdapter() {
340 public void mouseUp(MouseEvent e
) {
341 if(selectedCsii
.isLocalhostMgd() && !isSelectedCdmInstanceRunningInManagedServer()) {
342 startManagedServer();
348 btnConnect
.setText("Connect");
350 btnRememberMe
= new Button(loginComposite
, SWT
.CHECK
);
351 btnRememberMe
.setSelection(true);
352 GridData gd_btnRememberMe
= new GridData(SWT
.LEFT
, SWT
.CENTER
, false, false, 2, 1);
353 gd_btnRememberMe
.widthHint
= 107;
354 btnRememberMe
.setLayoutData(gd_btnRememberMe
);
355 btnRememberMe
.setText("Remember Me");
356 new Label(loginComposite
, SWT
.NONE
);
357 new Label(loginComposite
, SWT
.NONE
);
358 new Label(loginComposite
, SWT
.NONE
);
359 new Label(loginComposite
, SWT
.NONE
);
361 styledTxtMessage
= new StyledText(remotingComposite
, SWT
.NONE
);
362 styledTxtMessage
.setBackground(SWTResourceManager
.getColor(SWT
.COLOR_INFO_BACKGROUND
));
363 styledTxtMessage
.setForeground(SWTResourceManager
.getColor(SWT
.COLOR_DARK_RED
));
364 styledTxtMessage
.setFont(SWTResourceManager
.getFont("Ubuntu", 12, SWT
.BOLD
));
365 styledTxtMessage
.setSelectionBackground(SWTResourceManager
.getColor(SWT
.COLOR_LIST_SELECTION_TEXT
));
366 styledTxtMessage
.setSelectionForeground(SWTResourceManager
.getColor(SWT
.COLOR_DARK_RED
));
367 styledTxtMessage
.setDoubleClickEnabled(false);
368 styledTxtMessage
.setEditable(false);
369 GridData gd_styledTxtMessage
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 1, 1);
370 gd_styledTxtMessage
.exclude
= true;
371 gd_styledTxtMessage
.minimumHeight
= MESSAGE_HEIGHT
;
372 gd_styledTxtMessage
.heightHint
= MESSAGE_HEIGHT
;
373 styledTxtMessage
.setLayoutData(gd_styledTxtMessage
);
375 xpndblcmpstAdvanced
= new ExpandableComposite(remotingComposite
, SWT
.NONE
, ExpandableComposite
.TWISTIE
);
376 GridData gd_xpndblcmpstAdvanced
= new GridData(SWT
.FILL
, SWT
.FILL
, false, true, 1, 1);
377 gd_xpndblcmpstAdvanced
.heightHint
= 19;
378 xpndblcmpstAdvanced
.setLayoutData(gd_xpndblcmpstAdvanced
);
379 xpndblcmpstAdvanced
.addExpansionListener(new IExpansionListener() {
381 public void expansionStateChanged(ExpansionEvent e
) {
382 GridData gridData
= (GridData
) xpndblcmpstAdvanced
.getLayoutData();
384 shlConnect
.setSize(MIN_WIDTH
, MIN_EXP_HEIGHT
);
386 shlConnect
.setSize(MIN_WIDTH
, MIN_HEIGHT
);
391 public void expansionStateChanging(ExpansionEvent e
) {
394 xpndblcmpstAdvanced
.setText("advanced");
395 xpndblcmpstAdvanced
.setExpanded(true);
397 compAdvanced
= new Composite(xpndblcmpstAdvanced
, SWT
.NONE
);
398 xpndblcmpstAdvanced
.setClient(compAdvanced
);
399 compAdvanced
.setLayout(new GridLayout(4, false));
401 lblPort
= new Label(compAdvanced
, SWT
.CENTER
);
402 lblPort
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
403 lblPort
.setSize(0, 0);
404 lblPort
.setText("Port : ");
405 lblPort
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
407 txtPort
= new Text(compAdvanced
, SWT
.BORDER
);
408 GridData gd_txtPort
= new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1);
409 gd_txtPort
.minimumWidth
= 50;
410 gd_txtPort
.widthHint
= 50;
411 txtPort
.setLayoutData(gd_txtPort
);
413 lblServerVersion
= new Label(compAdvanced
, SWT
.CENTER
);
414 lblServerVersion
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
415 lblServerVersion
.setText("Server Cdmlib Version :");
416 lblServerVersion
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
418 txtServerVersion
= new Text(compAdvanced
, SWT
.BORDER
);
419 txtServerVersion
.setEditable(false);
420 txtServerVersion
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 1, 1));
421 btnStopServer
= new Button(compAdvanced
, SWT
.FLAT
);
422 btnStopServer
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1));
423 btnStopServer
.addMouseListener(new MouseAdapter() {
425 public void mouseUp(MouseEvent e
) {
429 btnStopServer
.setText("Stop Managed Server");
431 lblEditorVersion
= new Label(compAdvanced
, SWT
.CENTER
);
432 lblEditorVersion
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
433 lblEditorVersion
.setText("Editor Cdmlib Version :");
434 lblEditorVersion
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
436 txtEditorVersion
= new Text(compAdvanced
, SWT
.BORDER
);
437 txtEditorVersion
.setEditable(false);
438 txtEditorVersion
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1));
439 new Label(compAdvanced
, SWT
.NONE
);
440 new Label(compAdvanced
, SWT
.NONE
);
442 lblServerCDMVersion
= new Label(compAdvanced
, SWT
.CENTER
);
443 lblServerCDMVersion
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
444 lblServerCDMVersion
.setText("Server CDM Version :");
445 lblServerCDMVersion
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
447 txtServerCDMVersion
= new Text(compAdvanced
, SWT
.BORDER
);
448 txtServerCDMVersion
.setEditable(false);
449 txtServerCDMVersion
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1));
450 new Label(compAdvanced
, SWT
.NONE
);
451 new Label(compAdvanced
, SWT
.NONE
);
453 lblEditorCDMVersion
= new Label(compAdvanced
, SWT
.CENTER
);
454 lblEditorCDMVersion
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false, 1, 1));
455 lblEditorCDMVersion
.setText("Editor CDM Version :");
456 lblEditorCDMVersion
.setFont(SWTResourceManager
.getFont("Ubuntu", 9, SWT
.NORMAL
));
458 txtEditorCDMVersion
= new Text(compAdvanced
, SWT
.BORDER
);
459 txtEditorCDMVersion
.setEditable(false);
460 txtEditorCDMVersion
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 1, 1));
466 private void populateCdmServerCombo() {
467 Job job
= new Job("Retrieve Server Instances") {
469 protected IStatus
run(IProgressMonitor monitor
) {
470 Display
.getDefault().syncExec(new Runnable() {
473 for(CdmServerInfo csii
: CdmServerInfo
.getCdmServers()) {
474 csiiMap
.put(csii
.getName(), csii
);
475 comboCdmServer
.add(csii
.getName());
477 int serverIndex
= -1;
478 if(serverName
!= null) {
479 serverIndex
= comboCdmServer
.indexOf(serverName
);
481 if(serverIndex
== -1) {
482 comboCdmServer
.select(0);
485 comboCdmServer
.select(serverIndex
);
487 CdmRemoteSource devRemoteSource
= CdmServerInfo
.getDevServerRemoteSource();
488 if(devRemoteSource
!= null) {
489 isDevRemoteSource
= true;
490 String username
= System
.getProperty("cdm.server.dev.username");
491 String password
= System
.getProperty("cdm.server.dev.password");
492 if(username
!= null && !username
.isEmpty() && password
!= null && !password
.isEmpty()) {
493 txtLogin
.setText(username
);
494 txtPassword
.setText(password
);
495 CdmStore
.connect(devRemoteSource
, RemotingLoginDialog
.this);
502 return Status
.OK_STATUS
;
509 private void refreshCdmServer() {
510 Display
.getDefault().asyncExec(new Runnable() {
513 txtCdmServerStatus
.setText(STATUS_CHECKING_AVAILABILITY
);
514 updateSelectedCdmServer();
515 checkSelectedCdmServer();
521 private void updateSelectedCdmServer() {
522 int selIndex
= comboCdmServer
.getSelectionIndex();
524 selectedCsii
= csiiMap
.get(comboCdmServer
.getItem(selIndex
));
528 private void updatePort() {
530 if(selectedCsii
!= null) {
531 int port
= selectedCsii
.getPort();
532 if(port
== CdmServerInfo
.NULL_PORT
) {
533 txtPort
.setText(CdmServerInfo
.NULL_PORT_STRING
);
535 txtPort
.setText(String
.valueOf(port
));
540 private int getPort() {
541 int port
= CdmServerInfo
.NULL_PORT
;
543 port
= Integer
.valueOf(txtPort
.getText());
544 } catch (NumberFormatException nfe
) {
545 if(!CdmServerInfo
.NULL_PORT_STRING
.equals(txtPort
.getText())) {
546 setMessage("Port should be an integer");
552 private void checkSelectedCdmServer() {
554 clearOnServerChange();
557 if(selectedCsii
!= null) {
558 if(selectedCsii
.isLocalhost()) {
559 txtPort
.setEditable(true);
560 txtPort
.setEnabled(true);
562 if(selectedCsii
.pingServer()) {
563 txtCdmServerStatus
.setText(STATUS_AVAILABLE
);
564 populateCdmInstanceCombo(true);
565 String serverVersionTimestamp
= generateLastModifiedTooltip(selectedCsii
.getCdmlibLastModified());
566 txtServerVersion
.setText(selectedCsii
.getCdmlibServicesVersion() + ":" + serverVersionTimestamp
);
568 txtCdmServerStatus
.setText(STATUS_NOT_AVAILABLE
);
569 comboCdmInstance
.removeAll();
570 disableCdmInstanceControls("", "");
576 private void populateCdmInstanceCombo(final boolean forceRefresh
) {
577 comboCdmInstance
.removeAll();
578 comboCdmInstance
.setEnabled(false);
579 btnConnect
.setEnabled(false);
580 txtCdmInstanceStatus
.setText(STATUS_RETRIEVING
);
581 txtCdmInstanceStatus
.setToolTipText("");
583 serverJob
= new Job("Retrieve Server Instances") {
585 protected IStatus
run(IProgressMonitor monitor
) {
587 if(selectedCsii
!= null) {
589 selectedCsii
.refreshInstances();
591 final List
<CdmInstanceInfo
> instances
= selectedCsii
.getInstances();
592 Display
.getDefault().asyncExec(new Runnable() {
595 if(!instances
.isEmpty()) {
596 for(CdmInstanceInfo cdmInstance
: instances
) {
597 comboCdmInstance
.add(cdmInstance
.getName());
599 int instanceIndex
= -1;
600 if(instanceName
!= null) {
601 instanceIndex
= comboCdmInstance
.indexOf(instanceName
);
603 if(instanceIndex
== -1) {
604 comboCdmInstance
.select(0);
607 comboCdmInstance
.select(instanceIndex
);
609 refreshCdmInstance();
610 comboCdmInstance
.setEnabled(true);
616 txtCdmInstanceStatus
.setText(STATUS_NO_INSTANCES
);
617 btnConnect
.setEnabled(false);
622 } catch (final CDMServerException e
) {
623 MessagingUtils
.warn(getClass(), e
);
624 Display
.getDefault().asyncExec(new Runnable() {
627 disableCdmInstanceControls(STATUS_NOT_AVAILABLE
, e
.getMessage());
631 return Status
.OK_STATUS
;
635 if(txtCdmServerStatus
.getText().equals(STATUS_AVAILABLE
) && !isDevRemoteSource
) {
637 serverJob
.schedule();
641 private void disableCdmInstanceControls(String cdmInstanceStatus
, String tooltip
) {
642 txtCdmInstanceStatus
.setText(cdmInstanceStatus
);
643 txtCdmInstanceStatus
.setToolTipText(tooltip
);
644 comboCdmInstance
.setEnabled(false);
645 btnConnect
.setEnabled(false);
648 private void refreshCdmInstance() {
649 txtCdmInstanceStatus
.setText(STATUS_CHECKING_AVAILABILITY
);
650 clearOnInstanceChange();
651 updateSelectedCdmInstance();
652 checkSelectedCdmInstance();
653 updateManagedServerControls();
656 private void updateSelectedCdmInstance() {
657 int selIndex
= comboCdmInstance
.getSelectionIndex();
659 selectedCdmInstance
= selectedCsii
.getInstanceFromName(comboCdmInstance
.getItem(selIndex
));
661 if(loadLoginPrefs
&& !isDevRemoteSource
) {
662 readPrefCredentials();
667 private void updateManagedServerControls() {
668 if(selectedCsii
.isLocalhostMgd()) {
669 if(isSelectedCdmInstanceRunningInManagedServer()) {
670 txtCdmInstanceStatus
.setText(STATUS_STARTED
);
672 txtCdmInstanceStatus
.setText(STATUS_NOT_STARTED
);
674 btnConnect
.setEnabled(true);
675 selectedCsii
.setPort(getManagedServerPort());
680 if(isManagedServerRunning()) {
681 btnStopServer
.setEnabled(true);
683 btnStopServer
.setEnabled(false);
687 private boolean isManagedServerRunning() {
688 return CdmStore
.getManagedServer() != null && CdmStore
.getManagedServer().isAlive();
691 private boolean isSelectedCdmInstanceRunningInManagedServer() {
692 return CdmStore
.getManagedServer() != null &&
693 CdmStore
.getManagedServer().isAlive() &&
694 selectedCsii
.isLocalhostMgd() &&
695 CdmStore
.getManagedServer().getDataSourceName().equals(selectedCdmInstance
.getName());
698 private void startManagedServer() {
699 if(isManagedServerRunning()) {
700 if(CdmStore
.getManagedServer().getDataSourceName().equals(selectedCdmInstance
.getName())) {
703 Display
.getDefault().syncExec(new Runnable() {
713 Job job
= new Job("Managed CDM Server Launch") {
716 public IStatus
run(IProgressMonitor monitor
) {
717 String mgdServerConfigFileName
= "mgd.datasources.xml";
718 String config
= CDMServerUtils
.convertEditorToServerConfig();
719 File managedServerConfigFile
;
721 monitor
.beginTask("Launching Managed CDM Server", maxUnits
);
723 monitor
.subTask("Generating datasources config file for " + selectedCdmInstance
.getName());
724 managedServerConfigFile
= CDMServerUtils
.writeManagedServerConfig(config
, mgdServerConfigFileName
);
726 CdmStore
.setManagedServer(new CDMServer(selectedCdmInstance
.getName(), managedServerConfigFile
));
727 monitor
.subTask("Starting Managed CDM Server. This may take a while.");
728 CdmStore
.getManagedServer().start(false, RemotingLoginDialog
.this);
731 // the following loop is a 'fake' progress monitoring where the progress
732 // bar is advanced by one unit every second until maxUnits -2
733 while(!CdmStore
.getManagedServer().isStarted()) {
734 if(serverUnits
< maxUnits
- 2) {
737 } catch (InterruptedException e
) {
743 Display
.getDefault().asyncExec(new Runnable() {
747 updateManagedServerControls();
751 } catch (IOException ioe
) {
752 MessagingUtils
.errorDialog("Error generating server config file",
755 TaxeditorStorePlugin
.PLUGIN_ID
,
758 } catch (CDMEmbeddedServerException cse
) {
759 MessagingUtils
.errorDialog("Error starting managed server",
762 TaxeditorStorePlugin
.PLUGIN_ID
,
768 return Status
.OK_STATUS
;
773 job
.setProperty(IProgressConstants
.KEEP_PROPERTY
, true);
780 private void stopManagedServer() {
782 CdmStore
.getManagedServer().stop();
783 } catch (Exception e
) {
784 MessagingUtils
.errorDialog("Error stopping managed server",
786 "Could not stop managed server running at port " + CdmStore
.getManagedServer().getPort() + ". Please stop it manually",
787 TaxeditorStorePlugin
.PLUGIN_ID
,
791 CdmStore
.setManagedServer(null);
792 updateManagedServerControls();
795 private int getManagedServerPort() {
796 return CdmStore
.getManagedServer() == null ? CdmServerInfo
.NULL_PORT
: CdmStore
.getManagedServer().getPort();
799 private void checkSelectedCdmInstance() {
800 boolean available
= false;
801 String status
= STATUS_NOT_AVAILABLE
;
802 String message
= null;
804 if(txtCdmServerStatus
.getText().equals(STATUS_AVAILABLE
)) {
806 if(selectedCsii
.pingInstance(selectedCdmInstance
, getPort())) {
807 status
= STATUS_AVAILABLE
;
810 status
= STATUS_NOT_AVAILABLE
;
815 txtServerCDMVersion
.setText(selectedCsii
.getCdmRemoteSource(selectedCdmInstance
, getPort()).getDbSchemaVersion());
816 int compareDbSchemaVersion
= selectedCsii
.compareDbSchemaVersion(selectedCdmInstance
, getPort());
817 int compareCdmlibServicesVersion
= 0;
818 boolean disableServicesApiTimestampCheck
=
819 PreferencesUtil
.getPreferenceStore().getBoolean((IPreferenceKeys
.DISABLE_SERVICES_API_TIMESTAMP_CHECK
));
820 if(!disableServicesApiTimestampCheck
) {
821 compareCdmlibServicesVersion
= selectedCsii
.compareCdmlibServicesVersion();
823 if(compareDbSchemaVersion
> 0 || compareCdmlibServicesVersion
> 0) {
824 status
= STATUS_NOT_COMPATIBLE
;
826 message
= MESG_COMPATIBLE_EDITOR_OLD
;
827 } else if(compareDbSchemaVersion
< 0 || compareCdmlibServicesVersion
< 0) {
828 status
= STATUS_NOT_COMPATIBLE
;
830 message
= MESG_COMPATIBLE_SERVER_OLD
;
832 status
= STATUS_AVAILABLE
;
837 } catch (Exception e
) {
838 txtCdmInstanceStatus
.setToolTipText(e
.getMessage());
840 btnConnect
.setEnabled(available
);
841 txtCdmInstanceStatus
.setText(status
);
842 if(!StringUtils
.isBlank(message
)) {
849 private void connect() {
850 checkSelectedCdmInstance();
852 if(!txtCdmInstanceStatus
.getText().equals(STATUS_AVAILABLE
)) {
856 ICdmRemoteSource source
= selectedCsii
.getCdmRemoteSource(selectedCdmInstance
, getPort());
858 if(!validateLogin()) {
863 CdmStore
.connect(source
, this);
864 } catch (Exception e
) {
865 // Do not expect anything to go wrong at this point, so we throw a runtime exception
867 throw new RuntimeException(e
);
873 public boolean isRememberMe() {
874 return btnRememberMe
.getSelection();
877 private void persistPrefLastServerInstance() {
878 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
879 Preferences lastServerInstancePrefs
= preferences
.node(LAST_SERVER_INSTANCE_NODE
);
881 lastServerInstancePrefs
.put(LAST_SERVER_KEY
, selectedCsii
.getName());
882 lastServerInstancePrefs
.put(LAST_INSTANCE_KEY
, selectedCdmInstance
.getName());
884 flushPreferences(lastServerInstancePrefs
);
887 private void persistPrefCredentials() {
888 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
889 Preferences credentialsPrefs
= preferences
.node(LOGIN_NODE
);
890 credentialsPrefs
.put(getUsernamePrefKey(), txtLogin
.getText());
891 credentialsPrefs
.put(getPasswordPrefKey(), txtPassword
.getText());
892 flushPreferences(credentialsPrefs
);
895 private void flushPreferences(Preferences prefs
) {
898 } catch (BackingStoreException bse
) {
899 setMessage(bse
.getMessage());
903 private void readPrefCredentials() {
904 String username
, password
;
905 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
906 Preferences credentialsPrefs
= preferences
.node(LOGIN_NODE
);
907 username
= credentialsPrefs
.get(getUsernamePrefKey(), "");
908 txtLogin
.setText(username
);
909 password
= credentialsPrefs
.get(getPasswordPrefKey(),"");
910 txtPassword
.setText(password
);
911 if(username
.isEmpty() || password
.isEmpty()) {
916 private void readPrefLastServerInstance() {
917 IEclipsePreferences preferences
= ConfigurationScope
.INSTANCE
.getNode(STORE_PREFERENCES_NODE
);
918 Preferences lastServerInstancePrefs
= preferences
.node(LAST_SERVER_INSTANCE_NODE
);
920 serverName
= lastServerInstancePrefs
.get(LAST_SERVER_KEY
, null);
921 instanceName
= lastServerInstancePrefs
.get(LAST_INSTANCE_KEY
, null);
924 private void emptyCredentials() {
925 txtLogin
.setText("");
926 txtPassword
.setText("");
929 private String
getUsernamePrefKey() {
930 return selectedCsii
.toString(selectedCdmInstance
.getName(), getPort()) + USERNAME_SUFFIX
;
933 private String
getPasswordPrefKey() {
934 return selectedCsii
.toString(selectedCdmInstance
.getName(), getPort()) + PASSWORD_SUFFIX
;
937 private boolean validateLogin() {
938 if(getUsername() == null || getUsername().isEmpty()) {
939 setMessage("User login cannot be empty");
942 if(getPassword() == null || getPassword().isEmpty()) {
943 setMessage("Password cannot be empty");
948 public String
getUsername() {
949 return txtLogin
.getText();
952 public String
getPassword() {
953 return txtPassword
.getText();
956 public void setMessage(String message
) {
957 if(message
!= null && !message
.isEmpty()) {
958 if(message
.length() > 60) {
959 styledTxtMessage
.setToolTipText(message
);
960 message
= message
.substring(0, 60) + "...";
962 styledTxtMessage
.setText(message
);
963 styledTxtMessage
.setVisible(true);
964 ((GridData
)styledTxtMessage
.getLayoutData()).exclude
= false;
965 shlConnect
.setSize(MIN_WIDTH
, getHeightWithoutMessage() + MESSAGE_HEIGHT
);
966 shlConnect
.setMinimumSize(MIN_WIDTH
, getHeightWithoutMessage() + MESSAGE_HEIGHT
);
968 styledTxtMessage
.setText("");
969 styledTxtMessage
.setVisible(false);
970 ((GridData
)styledTxtMessage
.getLayoutData()).exclude
= true;
971 shlConnect
.setSize(MIN_WIDTH
, getHeightWithoutMessage());
972 shlConnect
.setMinimumSize(MIN_WIDTH
, getHeightWithoutMessage());
974 remotingComposite
.layout();
977 private int getHeightWithoutMessage() {
978 if(xpndblcmpstAdvanced
.isExpanded()) {
979 return MIN_EXP_HEIGHT
;
986 public void hide(boolean isHidden
) {
987 if(shlConnect
!= null && shlConnect
.getDisplay() != null) {
988 shlConnect
.setVisible(!isHidden
);
991 public void dispose() {
992 if(shlConnect
!= null && shlConnect
.getDisplay() != null) {
993 shlConnect
.dispose();
997 public void onComplete() {
998 Display
.getDefault().asyncExec(new Runnable() {
1001 if(!isDevRemoteSource
) {
1002 if(isRememberMe()) {
1003 persistPrefCredentials();
1005 persistPrefLastServerInstance();
1012 private String
generateLastModifiedTooltip(String cdmlibLastModified
) {
1013 if(StringUtils
.isBlank(cdmlibLastModified
)) {
1016 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy.MM.dd.HH.mm.z");
1017 Date cdmlibLastModifiedDate
;
1018 String cdmlibLastModifiedTimestamp
= "";
1020 cdmlibLastModifiedDate
= new Date(Long
.valueOf(cdmlibLastModified
));
1021 cdmlibLastModifiedTimestamp
= sdf
.format(cdmlibLastModifiedDate
);
1023 return cdmlibLastModifiedTimestamp
;
1026 private void setEditorInfo() {
1027 txtEditorCDMVersion
.setText(CdmMetaData
.getDbSchemaVersion());
1028 String editorVersionTimestamp
= generateLastModifiedTooltip(CdmApplicationState
.getCdmlibLastModified());
1029 txtEditorVersion
.setText(CdmApplicationState
.getCdmlibVersion() + ":" + editorVersionTimestamp
);
1032 private void clearOnServerChange() {
1034 txtServerCDMVersion
.setText("");
1035 txtServerVersion
.setText("");
1036 txtServerVersion
.setToolTipText("");
1037 txtServerCDMVersion
.setText("");
1038 txtPort
.setEditable(false);
1039 txtPort
.setEnabled(false);
1042 private void clearOnInstanceChange() {
1044 txtServerCDMVersion
.setText("");
1051 public void handleError(final Throwable t
) {
1053 Display
.getDefault().syncExec(new Runnable() {
1058 String title
= "CDM Server launch error";
1059 String message
= t
.getMessage();
1062 MessagingUtils
.errorDialog(title
,
1065 TaxeditorStorePlugin
.PLUGIN_ID
,