Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id: CdmApplicationController.java 11680 2011-04-04 17:07:39Z a.mueller $
2
/**
3
 * Copyright (C) 2007 EDIT
4
 * European Distributed Institute of Taxonomy 
5
 * http://www.e-taxonomy.eu
6
 * 
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.
9
 */
10

    
11

    
12
package eu.etaxonomy.cdm.api.application;
13

    
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
18
import org.springframework.context.support.AbstractApplicationContext;
19
import org.springframework.context.support.GenericApplicationContext;
20
import org.springframework.core.io.ClassPathResource;
21
import org.springframework.core.io.Resource;
22
import org.springframework.security.authentication.ProviderManager;
23

    
24
import eu.etaxonomy.cdm.api.service.IAgentService;
25
import eu.etaxonomy.cdm.api.service.IClassificationService;
26
import eu.etaxonomy.cdm.api.service.ICollectionService;
27
import eu.etaxonomy.cdm.api.service.ICommonService;
28
import eu.etaxonomy.cdm.api.service.IDatabaseService;
29
import eu.etaxonomy.cdm.api.service.IDescriptionService;
30
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
31
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
32
import eu.etaxonomy.cdm.api.service.IGroupService;
33
import eu.etaxonomy.cdm.api.service.IIdentificationKeyService;
34
import eu.etaxonomy.cdm.api.service.ILocationService;
35
import eu.etaxonomy.cdm.api.service.IMediaService;
36
import eu.etaxonomy.cdm.api.service.INameService;
37
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
38
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
39
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
40
import eu.etaxonomy.cdm.api.service.IReferenceService;
41
import eu.etaxonomy.cdm.api.service.IService;
42
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
43
import eu.etaxonomy.cdm.api.service.ITaxonService;
44
import eu.etaxonomy.cdm.api.service.ITermService;
45
import eu.etaxonomy.cdm.api.service.IUserService;
46
import eu.etaxonomy.cdm.api.service.IVocabularyService;
47
import eu.etaxonomy.cdm.api.service.IWorkingSetService;
48
import eu.etaxonomy.cdm.common.IProgressMonitor;
49
import eu.etaxonomy.cdm.common.NullProgressMonitor;
50
import eu.etaxonomy.cdm.model.common.CdmBase;
51
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
52

    
53

    
54
/**
55
 * @author a.mueller
56
 * @author j.koch
57
 *
58
 */
59
public class CdmApplicationRemoteController implements ICdmApplicationRemoteConfiguration{
60
	private static final Logger logger = Logger.getLogger(CdmApplicationRemoteController.class);
61
	
62
	public static final String DEFAULT_APPLICATION_CONTEXT_RESOURCE = "/eu/etaxonomy/cdm/remoteApplicationContext.xml";
63
	
64
	public AbstractApplicationContext applicationContext;
65
	private ICdmApplicationRemoteConfiguration configuration; 
66
	private Resource applicationContextResource;
67
	private IProgressMonitor progressMonitor;
68
	
69
	/**
70
	 * Constructor, opens a spring ApplicationContext with defaults
71
	 */
72
	public static CdmApplicationRemoteController NewInstance() {
73
		logger.info("Configure CdmApplicationRemoteController with defaults");
74
		return new CdmApplicationRemoteController(null, null);
75
	}
76
	
77
	/**
78
	 * Constructor, opens a spring ApplicationContext with given application context
79
	 * @param applicationContextResource
80
	 */
81
	public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource, IProgressMonitor progressMonitor) {
82
		logger.info("Configure CdmApplicationRemoteController with given application context");
83
		return new CdmApplicationRemoteController(applicationContextResource, progressMonitor);
84
	}
85

    
86
	/**
87
	 * Constructor, starts the application remote controller
88
	 * @param applicationContextResource
89
	 */
90
	private CdmApplicationRemoteController(Resource applicationContextResource, IProgressMonitor progressMonitor){
91
		logger.info("Start CdmApplicationRemoteController");
92
		this.applicationContextResource = applicationContextResource != null ? applicationContextResource : new ClassPathResource(DEFAULT_APPLICATION_CONTEXT_RESOURCE);
93
		this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
94
		setNewApplicationContext();
95
	}
96
		
97
	/**
98
	 * Sets the application context to a new spring ApplicationContext and initializes the Controller
99
	 */
100
	private boolean setNewApplicationContext(){
101
		logger.info("Set new application context");
102
		progressMonitor.beginTask("Start application context.", 6);
103
		progressMonitor.worked(1);
104

    
105
		GenericApplicationContext applicationContext =  new GenericApplicationContext();
106
		
107
		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
108
		progressMonitor.subTask("Registering resources.");
109
		xmlReader.loadBeanDefinitions(applicationContextResource);
110
		progressMonitor.worked(1);
111
				
112
		progressMonitor.subTask("This might take a while ...");
113
		applicationContext.refresh();
114
		applicationContext.start();
115
		progressMonitor.worked(1);
116
		
117
		progressMonitor.subTask("Cleaning up.");
118
		setApplicationContext(applicationContext);
119
		progressMonitor.done();
120
		return true;
121
	}
122
	
123
	/**
124
	 * Tests if some DefinedTermsAreMissing.
125
	 * @return true, if at least one is missing, else false
126
	 */
127
	public boolean testDefinedTermsAreMissing(){
128
		UUID englishUuid = UUID.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
129
		DefinedTermBase<?> english = this.getTermService().getByUri(englishUuid.toString());
130
		if ( english == null || ! english.getUuid().equals(englishUuid)){
131
			return true;
132
		}else{
133
			return false;
134
		}
135
	}
136
		
137
	/**
138
	 * Sets a new application Context.
139
	 * @param ac
140
	 */
141
	public void setApplicationContext(AbstractApplicationContext ac){
142
		closeApplicationContext(); //closes old application context if necessary
143
		applicationContext = ac;
144
		applicationContext.registerShutdownHook();
145
		init();
146
	}
147
	
148
	/* (non-Javadoc)
149
	 * @see java.lang.Object#finalize()
150
	 */
151
	public void finalize(){
152
		close();
153
	}
154
	
155
	/**
156
	 * closes the application
157
	 */
158
	public void close(){
159
		closeApplicationContext();
160
	}
161
	
162
	/**
163
	 * closes the application context
164
	 */
165
	private void closeApplicationContext(){
166
		if (applicationContext != null){
167
			logger.info("Close ApplicationContext");
168
			applicationContext.close();
169
		}
170
	}
171
	
172
	private void init(){
173
		logger.info("Init " +  this.getClass().getName() + " ... ");
174
		if (logger.isInfoEnabled()){for (String beanName : applicationContext.getBeanDefinitionNames()){ logger.debug(beanName);}}
175
		//TODO delete next row (was just for testing)
176
		if (logger.isInfoEnabled()){
177
			logger.info("Registered Beans: ");
178
			String[] beanNames = applicationContext.getBeanDefinitionNames();
179
			for (String beanName : beanNames){
180
				logger.info(beanName);
181
			}
182
		}
183
		configuration = (ICdmApplicationRemoteConfiguration)applicationContext.getBean("cdmApplicationRemoteDefaultConfiguration");
184
	}
185
	
186

    
187
	/* ******  Services *********/
188
	
189
	public final INameService getNameService(){
190
		return configuration.getNameService();
191
	}
192

    
193
	public final ITaxonService getTaxonService(){
194
		return configuration.getTaxonService();
195
	}
196
	
197
	public final IClassificationService getClassificationService(){
198
		return configuration.getClassificationService();
199
	}
200
	
201
	public final ITaxonNodeService getTaxonNodeService(){
202
		return configuration.getTaxonNodeService();
203
	}
204

    
205
	public final IReferenceService getReferenceService(){
206
		return configuration.getReferenceService();
207
	}
208
	
209
	public final IAgentService getAgentService(){
210
		return configuration.getAgentService();
211
	}
212
	
213
//	public final IDatabaseService getDatabaseService(){
214
//		return null;
215
//	}
216
	
217
	public final ITermService getTermService(){
218
		return configuration.getTermService();
219
	}
220

    
221
	public final IDescriptionService getDescriptionService(){
222
		return configuration.getDescriptionService();
223
	}
224
	
225
	public final IOccurrenceService getOccurrenceService(){
226
		return configuration.getOccurrenceService();
227
	}
228

    
229
	public final IMediaService getMediaService(){
230
		return configuration.getMediaService();
231
	}
232

    
233
	public final ICommonService getCommonService(){
234
		return configuration.getCommonService();
235
	}
236
	
237
	public final ILocationService getLocationService(){
238
		return configuration.getLocationService();
239
	}
240
	
241
	public final IUserService getUserService(){
242
		return configuration.getUserService();
243
	}
244

    
245
	public IGroupService getGroupService() {
246
		return configuration.getGroupService();
247
	}
248
	
249
	public final ICollectionService getCollectionService(){
250
		return configuration.getCollectionService();
251
	}
252
	
253
	public final IFeatureTreeService getFeatureTreeService(){
254
		return configuration.getFeatureTreeService();
255
	}
256
	
257
	public final IFeatureNodeService getFeatureNodeService(){
258
		return configuration.getFeatureNodeService();
259
	}
260
	
261
	public final IVocabularyService getVocabularyService(){
262
		return configuration.getVocabularyService();
263
	}
264
	
265
	public final IIdentificationKeyService getIdentificationKeyService(){
266
		return configuration.getIdentificationKeyService();
267
	}
268

    
269
	public final IPolytomousKeyService getPolytomousKeyService(){
270
		return configuration.getPolytomousKeyService();
271
	}
272

    
273
	public final IPolytomousKeyNodeService getPolytomousKeyNodeService(){
274
		return configuration.getPolytomousKeyNodeService();
275
	}
276
	
277
	public final IService<CdmBase> getMainService(){
278
		return configuration.getMainService();
279
	}
280
	
281
	public final IWorkingSetService getWorkingSetService(){
282
		return configuration.getWorkingSetService();
283
	}
284
	
285
//	public final ConversationHolder NewConversation(){
286
//		//return (ConversationHolder)applicationContext.getBean("conversationHolder");
287
//		return configuration.NewConversation();
288
//	}
289
//	
290
//	public final ProviderManager getAuthenticationManager(){
291
//		return configuration.getAuthenticationManager();
292
//	}
293
//	
294
//	@Override
295
//	public final PlatformTransactionManager getTransactionManager() {
296
//		return configuration.getTransactionManager();
297
//	}
298
	
299
	public final Object getBean(String name){
300
		return this.applicationContext.getBean(name);
301
	}
302

    
303
	@Override
304
	public IDatabaseService getDatabaseService() {
305
		// TODO Auto-generated method stub
306
		return null;
307
	}
308

    
309
	@Override
310
	public ProviderManager getAuthenticationManager() {
311
		// TODO Auto-generated method stub
312
		return null;
313
	}
314

    
315
}
(3-3/7)