Project

General

Profile

Download (7.61 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.common.monitor.IProgressMonitor;
27
import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
28
import eu.etaxonomy.taxeditor.remoting.cache.CdmModelCacher;
29
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
30
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
31

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

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

    
44

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

    
50

    
51
    /**
52
     * Creates new instance of CdmApplicationRemoteController
53
     *
54
     * @param applicationContextResource
55
     * @param remoteSource
56
     * @param omitTermLoading
57
     * @param progressMonitor
58
     * @param listeners
59
     * @return
60
     */
61
    public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
62
            ICdmRemoteSource remoteSource,
63
            boolean omitTermLoading,
64
            IProgressMonitor progressMonitor,
65
            List<ApplicationListener> listeners) {
66
        return new CdmApplicationRemoteController(applicationContextResource,
67
                remoteSource,
68
                omitTermLoading,
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
            boolean omitTermLoading,
85
            IProgressMonitor progressMonitor,
86
            List<ApplicationListener> listeners) {
87

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

    
94
    }
95

    
96
    /**
97
     * Constructs CdmApplicationRemoteController
98
     *
99
     * @param applicationContextResource
100
     * @param remoteSource
101
     * @param omitTermLoading
102
     * @param progressMonitor
103
     * @param listeners
104
     */
105
    private CdmApplicationRemoteController(Resource applicationContextResource,
106
            ICdmRemoteSource remoteSource,
107
            boolean omitTermLoading,
108
            IProgressMonitor progressMonitor,
109
            List<ApplicationListener> listeners){
110
        logger.info("Start CdmApplicationRemoteController with remote source: " + remoteSource.getName());
111
        this.applicationContextResource =
112
                applicationContextResource != null ? applicationContextResource : DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE;
113
        this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
114

    
115
        setNewRemoteSource(remoteSource, omitTermLoading, listeners);
116

    
117
    }
118

    
119

    
120

    
121

    
122
    /**
123
     * Creates and starts a new spring application context
124
     *
125
     * @param remoteSource object for connecting to an http-invoker server
126
     * @param omitTermLoading
127
     * @param listeners
128
     * @return
129
     */
130
    protected boolean setNewRemoteSource(ICdmRemoteSource remoteSource,
131
            boolean omitTermLoading,
132
            List<ApplicationListener> listeners){
133

    
134
        logger.info("Connecting to '" + remoteSource.getName() + "'");
135

    
136
        GenericApplicationContext applicationContext
137
            = generateApplicationContext(applicationContextResource, listeners, progressMonitor, false);
138

    
139

    
140
        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
141
        Properties properties = new Properties();
142
        properties.setProperty("remoteServer", remoteSource.getServer());
143
        properties.setProperty("remotePort", String.valueOf(remoteSource.getPort()));
144
        properties.setProperty("remoteContext", remoteSource.getContextPath());
145
        pspc.setProperties(properties);
146
        applicationContext.addBeanFactoryPostProcessor(pspc);
147
        //progressMonitor.beginTask("Connecting to '" + remoteSource.getName() + "'", nTasks);
148
        applicationContext.refresh();
149
        applicationContext.start();
150

    
151

    
152
        progressMonitor.subTask("Cleaning up.");
153
        setApplicationContext(applicationContext);
154
        progressMonitor.worked(1);
155

    
156
        progressMonitor.done();
157
        return true;
158
    }
159

    
160
    public static GenericApplicationContext generateApplicationContext(Resource applicationContextResource,
161
            List<ApplicationListener> listeners,
162
            IProgressMonitor progressMonitor,
163
            boolean validateXml) {
164
        RemotingMonitoredGenericApplicationContext applicationContext =  new RemotingMonitoredGenericApplicationContext();
165
        int refreshTasks = 45;
166
        int nTasks = 5 + refreshTasks;
167

    
168
        progressMonitor.subTask("Registering remote source.");
169
        applicationContext.getEnvironment().setActiveProfiles("remoting");
170
        progressMonitor.worked(1);
171

    
172
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
173
        if(!validateXml) {
174
            xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
175
        }
176
        progressMonitor.subTask("Registering resources.");
177
        xmlReader.loadBeanDefinitions(applicationContextResource);
178
        progressMonitor.worked(1);
179

    
180
        if (listeners != null){
181
            for(ApplicationListener listener : listeners){
182
                applicationContext.addApplicationListener(listener);
183
            }
184
        }
185

    
186
        return applicationContext;
187
    }
188

    
189
    /* (non-Javadoc)
190
     * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
191
     */
192
    @Override
193
    protected void init(){
194
        configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationRemoteConfiguration");
195
        AbstractLazyInitializer.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
196
        AbstractPersistentCollection.setConfiguration((CdmApplicationRemoteConfiguration)configuration);
197

    
198
        CdmModelCacher cmdmc = new CdmModelCacher();
199
        cmdmc.cacheGetterFields();
200

    
201
    }
202

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