3 * Copyright (C) 2007 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.
11 package eu
.etaxonomy
.taxeditor
.store
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.sql
.SQLException
;
15 import java
.util
.concurrent
.CancellationException
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.core
.runtime
.jobs
.Job
;
21 import org
.eclipse
.jface
.dialogs
.ProgressMonitorDialog
;
22 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
23 import org
.eclipse
.swt
.widgets
.Display
;
24 import org
.springframework
.core
.io
.Resource
;
26 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
27 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
28 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
29 import eu
.etaxonomy
.cdm
.config
.CdmSourceException
;
30 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
31 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
32 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
33 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
34 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
35 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
36 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
37 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
38 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.LoginDialog
;
39 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.RemotingLoginDialog
;
40 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
45 * @created Dec 8, 2010
48 class CdmStoreConnector
extends Job
{
49 private final Display display
;
50 private final ICdmSource cdmSource
;
51 private DbSchemaValidation dbSchemaValidation
;
52 private final Resource applicationContextBean
;
57 * @param dbSchemaValidation
58 * @param applicationContextBean
60 public CdmStoreConnector(Display display
,
62 DbSchemaValidation dbSchemaValidation
,
63 Resource applicationContextBean
) {
64 super("Connecting to datasource: " + cdmSource
);
65 this.display
= display
;
66 this.cdmSource
= cdmSource
;
67 this.dbSchemaValidation
= dbSchemaValidation
;
68 this.applicationContextBean
= applicationContextBean
;
74 public IStatus
run(final IProgressMonitor monitor
) {
76 monitor
.beginTask(getConnectionMessage(), 10);
78 // check if database is up and running
79 checkDatabaseReachable(monitor
);
81 if (!monitor
.isCanceled()) {
82 // check if the datasource actually holds data
83 checkIsNonEmptyCdmDatabase(monitor
);
86 if (dbSchemaValidation
!= DbSchemaValidation
.CREATE
87 && !monitor
.isCanceled()) {
88 // if we do not create the datasource, we want to check if the
89 // datasource is compatible with this editor
90 checkDbSchemaVersionCompatibility(monitor
);
93 // we are done with our low level checking and will free resources now
94 cdmSource
.closeOpenConnections();
96 if (!monitor
.isCanceled()) {
97 CdmStore
.close(monitor
);
100 ICdmApplicationConfiguration applicationController
= null;
102 if (!monitor
.isCanceled()) {
103 CdmProgressMonitorAdapter subprogressMonitor
= CdmProgressMonitorAdapter
104 .CreateSubMonitor(monitor
, 7);
105 // This is where we instantiate the application controller
106 int oldPriority
= Thread
.currentThread().getPriority();
108 Thread
.currentThread().setPriority(10);
109 applicationController
= getApplicationController(cdmSource
,subprogressMonitor
);
110 } catch (Exception e
) {
111 if(! causeIsCancelationExceptionRecursive(e
)){
112 return new Status(IStatus
.ERROR
, "Could not connect to CDM Store", "An error occurred while trying to connect to datasource: " + cdmSource
.getName(), e
);
116 Thread
.currentThread().setPriority(oldPriority
);
122 if (!monitor
.isCanceled()) {
123 CdmStore
.setInstance(applicationController
, cdmSource
);
125 display
.asyncExec(new Runnable() {
129 * @see java.lang.Runnable#run()
139 MessagingUtils
.info("Application context initialized.");
140 return Status
.OK_STATUS
;
142 // Show datasource view if not shown yet
143 display
.asyncExec(new Runnable() {
147 * @see java.lang.Runnable#run()
151 StoreUtil
.showView(CdmDataSourceViewPart
.ID
);
154 return Status
.CANCEL_STATUS
;
159 public void start(final RemotingLoginDialog loginDialog
) {
160 // hide login dialog and start connection dialog
161 loginDialog
.setMessage(null);
162 loginDialog
.hide(true);
165 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
168 dialog
.run(true, true, new IRunnableWithProgress() {
170 public void run(final IProgressMonitor monitor
) {
172 monitor
.beginTask(getConnectionMessage(), 7);
174 // check if database is up and running
175 checkDatabaseReachable(monitor
);
177 // check if the datasource actually holds data
178 checkIsNonEmptyCdmDatabase(monitor
);
180 if (dbSchemaValidation
!= DbSchemaValidation
.CREATE
) {
181 // if we do not create the datasource, we want to check if the
182 // datasource is compatible with this editor
183 checkDbSchemaVersionCompatibility(monitor
);
186 // we are done with our low level checking and will free resources now
187 cdmSource
.closeOpenConnections();
189 display
.syncExec(new Runnable() {
193 * @see java.lang.Runnable#run()
197 // close the current context
198 CdmStore
.close(monitor
, false);
202 ICdmApplicationConfiguration applicationController
= null;
204 if (!monitor
.isCanceled()) {
205 CdmProgressMonitorAdapter subprogressMonitor
= CdmProgressMonitorAdapter
206 .CreateSubMonitor(monitor
, 3);
207 // genrerate new application controller
208 applicationController
= getApplicationController(cdmSource
,subprogressMonitor
);
212 if (!monitor
.isCanceled()) {
213 CdmStore
.setInstance(applicationController
, cdmSource
);
214 monitor
.subTask("Authenticating user");
215 display
.syncExec(new Runnable() {
219 * @see java.lang.Runnable#run()
225 // create new security context
226 CdmStore
.getLoginManager().doAuthenticate(loginDialog
.getUsername(), loginDialog
.getPassword());
227 loginDialog
.onComplete();
228 CdmStore
.getContextManager().notifyContextStart();
229 Rank
.initDefaultTerms();
230 } catch(CdmAuthenticationException cae
) {
231 loginDialog
.hide(false);
232 loginDialog
.setMessage(cae
.getMessage());
238 throw new RuntimeException("Login cancelled");
245 } catch (InvocationTargetException e
) {
246 loginDialog
.hide(false);
247 loginDialog
.setMessage(e
.getMessage());
248 } catch (InterruptedException e
) {
249 loginDialog
.hide(false);
250 loginDialog
.setMessage(e
.getMessage());
255 private ICdmApplicationConfiguration
getApplicationController(ICdmSource cdmSource
, CdmProgressMonitorAdapter subprogressMonitor
) {
256 if(cdmSource
instanceof ICdmDataSource
) {
257 return CdmApplicationController
.NewInstance(applicationContextBean
,
258 (ICdmDataSource
)cdmSource
,
262 } else if(cdmSource
instanceof ICdmRemoteSource
) {
263 return CdmApplicationRemoteController
.NewInstance((ICdmRemoteSource
)cdmSource
,
267 throw new UnsupportedOperationException("Cannot create application controller for " + cdmSource
.getName());
271 private void authenticate() {
272 LoginDialog saloginDialog
= new LoginDialog(StoreUtil
.getShell());
273 saloginDialog
.open();
276 private void startContext() {
277 CdmStore
.getContextManager().notifyContextStart();
283 private String
getConnectionMessage() {
284 return cdmSource
.getConnectionMessage();
289 * @throws SQLException
291 private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor
) {
292 monitor
.subTask("Checking if datasource is compatible with this editor.");
293 String dbSchemaVersion
;
295 String message
= null;
297 dbSchemaVersion
= cdmSource
.getDbSchemaVersion();
298 // we assume that empty dbSchemaVersion means an empty database and
299 // skip version checking
301 if(dbSchemaVersion
!= null) {
302 int compareVersion
= CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
303 // if the datasource version is greater than the taxeditor compatible version then the taxeditor needs to
304 // be updated else the datasource needs to be updated
305 if(compareVersion
> 0) {
306 message
= "Please update the Taxonomic Editor (Help->Check for Updates) or choose a compatible datasource";
307 } else if (compareVersion
< 0) {
308 message
= "Please update the chosen datasource or choose a new data source to connect to in the Datasource View.";
312 } catch (CdmSourceException e
) {
316 if (message
!= null) {
317 // Show an error message
320 "Datasource Compatibility Check failed",
322 "The database schema for the chosen "
325 + "' \n is not compatible for this version of the taxonomic editor. \n\n"
329 monitor
.setCanceled(true);
334 private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor
) {
335 monitor
.subTask("Checking if datasource is a non empty CDM database.");
336 boolean isDbEmpty
= false;
338 isDbEmpty
= cdmSource
.isDbEmpty();
339 } catch (CdmSourceException e
) {
343 dbSchemaValidation
= DbSchemaValidation
.CREATE
;
347 private boolean causeIsCancelationExceptionRecursive(Throwable throwable
){
348 if(throwable
== null){
350 }else if(throwable
instanceof CancellationException
){
353 return causeIsCancelationExceptionRecursive(throwable
.getCause());
357 private void checkDatabaseReachable(IProgressMonitor monitor
) {
359 monitor
.subTask("Checking if datasource is reachable.");
360 cdmSource
.checkConnection();
362 } catch (CdmSourceException e
) {
363 MessagingUtils
.messageDialog("Could not connect to chosen datasource",
364 this, "Reason: " + e
.getMessage(), e
);
365 monitor
.setCanceled(true);