Project

General

Profile

Download (7.54 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.session.ICdmEntitySessionManager;
32

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

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

    
45

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

    
51

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

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

    
93
    }
94

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

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

    
106
    }
107

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

    
128
    }
129

    
130

    
131

    
132

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

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

    
147
        GenericApplicationContext applicationContext =  new GenericApplicationContext();
148

    
149
        int nTasks = 2;
150

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

    
153
        progressMonitor.subTask("Loading context beans ...");
154
        // initialising the cdm model cache
155
        CdmRemoteCacheManager.getInstance();
156

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

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

    
186

    
187

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

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

    
199

    
200

    
201
    }
202

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

    
207

    
208
    public ITestService getTestService(){
209
        return (ITestService) getBean("testService");
210
    }
211
}
(3-3/8)