Project

General

Profile

Download (7.75 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2014 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10

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

    
13
import java.util.List;
14
import java.util.Properties;
15

    
16
import org.apache.log4j.Logger;
17
import org.hibernate.collection.internal.AbstractPersistentCollection;
18
import org.hibernate.proxy.AbstractLazyInitializer;
19
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
20
import org.springframework.context.ApplicationListener;
21
import org.springframework.context.support.GenericApplicationContext;
22
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
23
import org.springframework.core.io.ClassPathResource;
24
import org.springframework.core.io.Resource;
25

    
26
import eu.etaxonomy.cdm.api.service.ITestService;
27
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
28
import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
29
import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
30
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
31
import eu.etaxonomy.taxeditor.service.ICachedCommonService;
32
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
33

    
34
/**
35
 * CDM Application Controller class for remoting clients
36
 *
37
 * FIXME:Remoting extending {@link CdmApplicationController} is a temporary workaround.
38
 * The {@link CdmApplicationController} should be split into a CdmApplicationControllerBase
39
 * class with {@link CdmApplicationController} and this class as subclasses
40
 *
41
 */
42
public class CdmApplicationRemoteController  extends CdmApplicationController {
43

    
44
    private static final Logger logger = Logger.getLogger(CdmApplicationRemoteController.class);
45

    
46

    
47
    public static final Resource DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE =
48
            new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
49
    private final Resource applicationContextResource;
50
    private final IProgressMonitor progressMonitor;
51

    
52

    
53
    /**
54
     * Creates new instance of CdmApplicationRemoteController
55
     *
56
     * @param applicationContextResource
57
     * @param remoteSource
58
     * @param omitTermLoading
59
     * @param progressMonitor
60
     * @param listeners
61
     * @return
62
     */
63
//    public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
64
//            ICdmRemoteSource remoteSource,
65
//            IProgressMonitor progressMonitor,
66
//            List<ApplicationListener> listeners) {
67
//        return new CdmApplicationRemoteController(applicationContextResource,
68
//                remoteSource,
69
//                false,
70
//                progressMonitor,
71
//                listeners);
72
//
73
//    }
74
    /**
75
     * Creates new instance of CdmApplicationRemoteController
76
     *
77
     * @param applicationContextResource
78
     * @param remoteSource
79
     * @param omitTermLoading
80
     * @param progressMonitor
81
     * @param listeners
82
     * @return
83
     */
84
    public static CdmApplicationRemoteController NewInstance(ICdmRemoteSource remoteSource,
85
            IProgressMonitor progressMonitor,
86
            List<ApplicationListener> listeners) {
87

    
88
        return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE,
89
                remoteSource,
90
                false,
91
                progressMonitor,
92
                listeners);
93

    
94
    }
95

    
96
    public static CdmApplicationRemoteController NewInstance(ICdmRemoteSource remoteSource,
97
            boolean validateXml,
98
            IProgressMonitor progressMonitor,
99
            List<ApplicationListener> listeners) {
100

    
101
        return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE,
102
                remoteSource,
103
                validateXml,
104
                progressMonitor,
105
                listeners);
106

    
107
    }
108

    
109
    /**
110
     * Constructs CdmApplicationRemoteController
111
     *
112
     * @param applicationContextResource
113
     * @param remoteSource
114
     * @param omitTermLoading
115
     * @param progressMonitor
116
     * @param listeners
117
     */
118
    private CdmApplicationRemoteController(Resource applicationContextResource,
119
            ICdmRemoteSource remoteSource,
120
            boolean validateXml,
121
            IProgressMonitor progressMonitor,
122
            List<ApplicationListener> listeners){
123
        logger.info("Start CdmApplicationRemoteController with remote source: " + remoteSource.getName());
124
        this.applicationContextResource =
125
                applicationContextResource != null ? applicationContextResource : DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE;
126
        this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
127
        setNewRemoteSource(remoteSource, validateXml, listeners);
128

    
129
    }
130

    
131

    
132

    
133

    
134
    /**
135
     * Creates and starts a new spring application context
136
     *
137
     * @param remoteSource object for connecting to an http-invoker server
138
     * @param omitTermLoading
139
     * @param listeners
140
     * @return
141
     */
142
    public boolean setNewRemoteSource(ICdmRemoteSource remoteSource,
143
            boolean validateXml,
144
            List<ApplicationListener> listeners){
145

    
146
        logger.info("Connecting to '" + remoteSource.getName() + "'");
147

    
148
        GenericApplicationContext applicationContext =  new GenericApplicationContext();
149

    
150
        int nTasks = 3;
151

    
152
        progressMonitor.beginTask("Connecting to '" + remoteSource.getName() + "'", nTasks);
153

    
154
        progressMonitor.subTask("Initialising CDM Model Cache ...");
155
        CdmRemoteCacheManager.getInstance();
156

    
157
        progressMonitor.subTask("Loading context beans ...");
158
        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
159
        Properties properties = new Properties();
160
        properties.setProperty("remoteServer", remoteSource.getServer());
161
        properties.setProperty("remotePort", String.valueOf(remoteSource.getPort()));
162
        properties.setProperty("remoteContext", remoteSource.getContextPath());
163
        pspc.setProperties(properties);
164
        applicationContext.addBeanFactoryPostProcessor(pspc);
165
        applicationContext.getEnvironment().setActiveProfiles("remoting");
166
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
167
        if(!validateXml) {
168
            xmlReader.setValidating(false);
169
        }
170
        xmlReader.loadBeanDefinitions(applicationContextResource);
171
        if (listeners != null){
172
            for(ApplicationListener listener : listeners){
173
                applicationContext.addApplicationListener(listener);
174
            }
175
        }
176
        progressMonitor.worked(1);
177

    
178
        progressMonitor.subTask("Starting context ...");
179
        applicationContext.refresh();
180
        applicationContext.start();
181
        setApplicationContext(applicationContext);
182
        progressMonitor.worked(1);
183
        progressMonitor.done();
184
        return true;
185
    }
186

    
187

    
188

    
189
    /* (non-Javadoc)
190
     * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
191
     */
192
    @Override
193
    protected void init(){
194

    
195
        // retrieving the application configuration
196
        configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationRemoteConfiguration");
197
        AbstractLazyInitializer.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
198
        AbstractPersistentCollection.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
199

    
200

    
201

    
202
    }
203

    
204
    public ICdmEntitySessionManager getCdmEntitySessionManager() {
205
        return ((CdmApplicationRemoteConfiguration)configuration).getCdmEntitySessionManager();
206
    }
207

    
208

    
209
    public ITestService getTestService(){
210
        return (ITestService) getBean("testService");
211
    }
212

    
213
    public ICachedCommonService getCachedCommonService(){
214
        return (ICachedCommonService) getBean("cachedCommonService");
215
    }
216
}
(3-3/8)