source: vlo/branches/vlo-2.13-param/vlo_webapp/src/main/java/eu/clarin/cmdi/vlo/config/VloConfig.java @ 2627

Last change on this file since 2627 was 2627, checked in by keeloo, 11 years ago

Fixed a few tests, moved the SearchResultsDao? method to the init
method of the WebApplication? class. Tested the web application.

From now on, it is possible to override an optional external
configuration file in the context segment by a context definition of
the solrUrl parameter.

Removed parameters from application properties, removed application
properties files themselves, removed unused configuration files and
context segments, and finally: removed Spring dependencies.

File size: 19.5 KB
Line 
1package eu.clarin.cmdi.vlo.config;
2
3import java.io.UnsupportedEncodingException;
4import java.net.URLEncoder;
5import java.util.List;
6import javax.servlet.ServletContext;
7import org.simpleframework.xml.Element;
8import org.simpleframework.xml.ElementArray;
9import org.simpleframework.xml.ElementList;
10import org.simpleframework.xml.Root;
11import org.slf4j.LoggerFactory;
12
13/**
14 * Web application configuration<br><br>
15 *
16 * Map the elements in the packaged {@literal WebAppConfig.xml} file to the
17 * members in this class, the configuration of the VLO web application and
18 * importer according to the Simple framework specification. So<br><br>
19 *
20 * {@literal <parameterMember>}"the value of the
21 * parameter"{@literal </parameterMember>}<br><br>
22 *
23 * in the XML file is accompanied by<br><br>
24 *
25 * {@literal
26 *
27 * @element}<br> {@literal parameterMember}<br><br>
28 *
29 * in the VloConfig class. If you want to add a type of member that is not
30 * included in the class yet, refer to the Simple framework's
31 * specification.<br><br>
32 *
33 * The parameters are stored statically. This means that a parameter can be
34 * referenced from the application without first creating a configuration
35 * object. So get() in
36 *
37 * WebAppConfig.get().getSomeParameter();<br><br>
38 *
39 * will return the static configuration, and getSomeParameter() will return a
40 * specific parameter in this configuration.
41 *
42 * Through the get and set methods, the application is indifferent to the origin
43 * of a parameter: you can get and set the value of a parameter without having
44 * to worry about how the parameter was defined originally. By invoking the read
45 * method, and by querying the context, the web application, on initialization,
46 * determines which definition to use.
47 *
48 * Also, the get and set methods allow for a modification of the original value
49 * of the parameter. For example, if the format of a parameter changes, this
50 * change can be handled in the get and set method once, instead of having to
51 * modify every reference to the parameter in the application.
52 *
53 * Please note on the explanation of the meaning of the parameters. Because the
54 * meaning of a parameter is not local to this class, or even not local to the
55 * configuration package, they are described in the general VLO
56 * documentation.<br><br>
57 *
58 * @author keeloo
59 */
60@Root
61public class VloConfig extends ConfigFromFile {
62
63    // create a reference to the application logging
64    private final static org.slf4j.Logger LOG =
65            LoggerFactory.getLogger(VloConfig.class);
66
67    // connect to the logging framework
68    private class WebAppConfigLogger implements ConfigFilePersister.Logger {
69
70        @Override
71        public void log(Object data) {
72
73            LOG.error(data.toString());
74        }
75    }
76    private static WebAppConfigLogger logger;
77
78    /**
79     * Constructor method
80     */
81    public VloConfig() {
82        // let the superclass know about the logger defined here
83
84        logger = new WebAppConfigLogger();
85
86        ConfigFilePersister.setLogger(VloConfig.logger);
87    }
88   
89    /**
90     * Make the configuration statically accessible
91     */
92    private static VloConfig config = null;
93
94    /**
95     * Read the configuration from an XML file.
96     *
97     * Please invoke this method from the web application or from the importer;
98     * the readTestConfig method is intended for testing purposes.
99     *
100     * @param fileName
101     *
102     * @return the web application configuration
103     */
104    public static VloConfig readConfig(String fileName) {
105        if (config == null) {
106            // the configuration is not there yet; create it now
107            config = new VloConfig();
108        }
109
110        // get the XML file configuration from the file by invoking the
111
112        config = (VloConfig) read(fileName, config);
113
114        return config;
115    }
116
117    /**
118     * Read the configuration from an XML file.
119     *
120     * Please invoke this method from the package tests. If the tests invoke a
121     * method different from the one used by the web application and the
122     * important, you can make some test specific changes to the parameters
123     * here.
124     *
125     * @param fileName
126     *
127     * @return the web application configuration in a new static context
128     */
129    public static VloConfig readTestConfig(String fileName) {
130        if (config == null) {
131            // the configuration is not there yet; create it now
132            config = new VloConfig();
133        }
134
135        // get the XML file configuration from the file by invoking the
136
137        config = (VloConfig) read(fileName, config);
138
139        return config;
140    }
141   
142    /**
143     * Return the configuration
144     *
145     * @return
146     */
147    public static VloConfig get (){
148        return config;
149    }
150
151    /**
152     * VLO application parameter members<br><br>
153     *
154     * Initialize a member corresponding to application parameters with an empty
155     * string at least, for this will allow them to be linearized to
156     * corresponding elements in the XML file.
157     *
158     * Please refer to the general VLO documentation for a description of the
159     * member parameters.
160     */
161   
162    @Element // directive for Simple
163    private boolean deleteAllFirst = false;
164   
165    @Element
166    private boolean printMapping = false;
167   
168    @ElementList // directive for Simple
169    private List<DataRoot> dataRoots;
170   
171    public List<DataRoot> getDataRoots() {
172        return dataRoots;
173    }
174   
175    @Element
176    private String vloHomeLink = "";
177   
178    @Element
179    private String solrUrl = "";
180   
181    @Element
182    private String profileSchemaUrl = "";
183
184    @Element
185    private String componentRegistryRESTURL = "";
186   
187    @Element
188    private String handleServerUrl = "";
189   
190    @Element
191    private String imdiBrowserUrl = "";
192   
193    /**
194     * Note: the national project mapping itself is not part of the web
195     * application configuration.
196     */
197    @Element
198    private String nationalProjectMapping = "";
199   
200    /**
201     * An array of facetFields<br><br>
202     *
203     * In case of an array of elements, the number of elements in the array
204     * needs to be communicated to Simple. The following would be a correct
205     * description of an array of three facet fields<br><br>
206     *
207     * {@literal <facetFields length="3">}<br>
208     * {@literal    <facetField>}<br>
209     * {@literal       fieldOne}<br>
210     * {@literal    </facetField>}<br>
211     * {@literal    <facetField>}<br>
212     * {@literal       fieldTwo}<br>
213     * {@literal    </facetField>}<br>
214     * {@literal    <facetField>}<br>
215     * {@literal       fieldThree}<br>
216     * {@literal    </facetField>}<br>
217     * {@literal </facetFields>}<br><br>
218     *
219     * To let Simple now it has to interpret the facetFields element as an
220     * array, use the ElementArray directive. Use the directive to let Simple
221     * know that the elements inside 'facetFields' are named 'facetField'.
222     */
223    @ElementArray(entry = "facetField")
224    private String[] facetFields = {"", "", ""};
225   
226    @Element
227    private String countryComponentUrl = "";
228   
229    @Element
230    private String language2LetterCodeComponentUrl = "";
231   
232    @Element
233    private String language3LetterCodeComponentUrl = "";
234   
235    @Element
236    private String silToISO639CodesUrl = "";
237   
238    @Element
239    private String FederatedContentSearchUrl = " ";
240
241    /**
242     * Get and set methods for web application parameter members<br><br>
243     *
244     * By using a get or set method, you can apply an operation to a parameter
245     * here without the need to make changes in different parts of the
246     * application.
247     */
248   
249    /**
250     * Set the value of the dataRoots parameter<br><br>
251     *
252     * For a description of the parameter, refer to the general VLO
253     * documentation.
254     *
255     * @param the value
256     */
257    public void setDataRoots(List<DataRoot> dataRoots) {
258        this.dataRoots = dataRoots;
259    }
260
261    /**
262     * Set the value deleteAllFirst parameter<br><br>
263     *
264     * For a description of the parameter, refer to the general VLO
265     * documentation.
266     *
267     * @return the value
268     */
269    public void setDeleteAllFirst(boolean deleteAllFirst) {
270        this.deleteAllFirst = deleteAllFirst;
271    }
272
273    /**
274     * Get the value of the deleteAllFirst parameter<br><br>
275     *
276     * For a description of the parameter, refer to the general VLO
277     * documentation.
278     *
279     * @return the value
280     */
281    public boolean isDeleteAllFirst() {
282        return deleteAllFirst;
283    }
284
285    /**
286     * Set the value of the printMapping parameter<br><br>
287     *
288     * For a description of the parameter, refer to the general VLO
289     * documentation.
290     *
291     * @param the value
292     */
293    public void setPrintMapping(boolean printMapping) {
294        this.printMapping = printMapping;
295    }
296
297    /**
298     * Get the value of the printMapping parameter<br><br>
299     *
300     * For a description of the parameter, refer to the general VLO
301     * documentation.
302     *
303     * @return the value
304     */
305    public boolean isPrintMapping() {
306        return printMapping;
307    }
308   
309    /**
310     * Get the value of the VloHomeLink parameter<br><br>
311     *
312     * For a description of the parameter, refer to the general VLO
313     * documentation.
314     *
315     * @return the value
316     */
317    public String getVloHomeLink() {
318        return vloHomeLink;
319    }
320
321    /**
322     * Set the value of the VloHomeLink parameter<br><br>
323     *
324     * For a description of the parameter, refer to the general VLO
325     * documentation.
326     *
327     * @param vloHomeLink the value
328     */
329    public void setVloHomeLink(String link) {
330        this.vloHomeLink = link;
331    }
332
333    /**
334     * Get the value of the SolrUrl parameter<br><br>
335     *
336     * For a description of the parameter, refer to the general VLO
337     * documentation.
338     *
339     * @return the value
340     */
341    public String getSolrUrl() {
342        return solrUrl;
343    }
344
345    /**
346     * Set the value of the SolrUrl parameter<br><br>
347     *
348     * For a description of the parameter, refer to the general VLO
349     * documentation.
350     *
351     * @param the parameter
352     */
353    public void setSolrUrl(String url) {
354        this.solrUrl = url;
355    }
356
357    /**
358     * Get the value of the ProfileSchemaUrl by profileId parameter<br><br>
359     *
360     * For a description of the schema, refer to the general VLO documentation.
361     * Note: the profileId needs to be expanded.
362     *
363     * @return the value
364     */
365    public String getComponentRegistryProfileSchema(String id) {
366        return profileSchemaUrl.replace("${PROFILE_ID}", id);
367    }
368
369    /**
370     * Set the value of the ProfileSchemaUrl parameter<br><br>
371     *
372     * For a description of the schema, refer to the general VLO documentation.
373     * Note: the profileId needs to be expanded.
374     *
375     * @param profileId the value
376     */
377    public void setProfileSchemaUrl(String url) {
378        this.profileSchemaUrl = url;
379    }
380
381    /**
382     * Get the value of the ComponentRegisteryRESTURL parameter<br><br>
383     *
384     * For a description of the parameter, refer to the general VLO
385     * documentation.
386     *
387     * @return the value
388     */
389    public String getComponentRegistryRESTURL() {
390        return componentRegistryRESTURL;
391    }
392
393    /**
394     * Set the value of the ComponentRegisteryRESTURL parameter<br><br>
395     *
396     * For a description of the parameter, refer to the general VLO
397     * documentation.
398     *
399     * @param componentRegistryRESTURL the value
400     */
401    public void setComponentRegistryRESTURL(String url) {
402        this.componentRegistryRESTURL = url;
403    }
404
405    /**
406     * Get the value of the HandleServerUrl parameter<br><br>
407     *
408     * For a description of the parameter, refer to the general VLO
409     * documentation.
410     *
411     * @return the value
412     */
413    public String getHandleServerUrl() {
414        return handleServerUrl;
415    }
416
417    /**
418     * Set the value of the HandleServerUrl parameter<br><br>
419     *
420     * For a description of the parameter, refer to the general VLO
421     * documentation.
422     *
423     * @param handleServerUrl the value
424     */
425    public void setHandleServerUrl(String url) {
426        this.handleServerUrl = url;
427    }
428
429    /**
430     * Set the value of the IMDIBrowserUrl parameter<br><br>
431     *
432     * For a description of the parameter, refer to the general VLO
433     * documentation.
434     *
435     * @param imdiBrowserUrl the value
436     */
437    public void setIMDIBrowserUrl(String url) {
438        this.imdiBrowserUrl = url;
439    }
440
441    /**
442     * Get the value of the ProfileSchemaUrl parameter combined with a handle<br><br>
443     *
444     * For a description of the schema, refer to the general VLO documentation.
445     *
446     * @param handle the value
447     */
448    public String getIMDIBrowserUrl(String handle) {
449        String result;
450        try {
451            result = imdiBrowserUrl + URLEncoder.encode(handle, "UTF-8");
452        } catch (UnsupportedEncodingException e) {
453            result = imdiBrowserUrl + handle;
454        }
455        return result;
456    }
457
458    /**
459     * Get the value of the FederatedContentSearchUrl parameter<br><br>
460     *
461     * For a description of the parameter, refer to the general VLO
462     * documentation.
463     *
464     * @return the value
465     */
466    public void setFederatedContentSearchUrl(String url) {
467        this.FederatedContentSearchUrl = url;
468    }
469
470    /**
471     * Set the value of the FederatedContentSearchUrl parameter<br><br>
472     *
473     * For a description of the parameter, refer to the general VLO
474     * documentation.
475     *
476     * @param FederatedContentSearchUrl the value
477     */
478    public String getFederatedContentSearchUrl() {
479        return FederatedContentSearchUrl;
480    }
481
482    /**
483     * Set the value of the NationalProjectMapping parameter<br><br>
484     *
485     * For a description of the parameter, refer to the general VLO
486     * documentation.
487     *
488     * @param nationalProjectMapping the value
489     */
490    public void setNationalProjectMapping(String mapping) {
491        this.nationalProjectMapping = mapping;
492    }
493
494    /**
495     * Get the value of the NationalProjectMapping parameter<br><br>
496     *
497     * For a description of the parameter, refer to the general VLO
498     * documentation.
499     *
500     * @return the value
501     */
502    public String getNationalProjectMapping() {
503        return nationalProjectMapping;
504    }
505
506    /**
507     * Get the value of the FacetFields parameter<br><br>
508     *
509     * For a description of the parameter, refer to the general VLO
510     * documentation.
511     *
512     * @return the value
513     */
514    public String[] getFacetFields() {
515        return facetFields;
516    }
517
518    /**
519     * Set the value of the FacetFields parameter<br><br>
520     *
521     * For a description of the parameter, refer to the general VLO
522     * documentation.
523     *
524     * @param facetFields the value, a list of facet fields
525     */
526    public void setFacetFields(String[] fields) {
527        this.facetFields = fields;
528    }
529
530    /**
531     * Get the value of the CountryComponentUrl parameter<br><br>
532     *
533     * For a description of the parameter, refer to the general VLO
534     * documentation.
535     *
536     * @return the value
537     */
538    public String getCountryComponentUrl() {
539        return countryComponentUrl;
540    }
541
542    /**
543     * Set the value of the CountryComponentUrl parameter<br><br>
544     *
545     * For a description of the parameter, refer to the general VLO
546     * documentation.
547     *
548     * @param countryComponentUrl the value
549     */
550    public void setCountryComponentUrl(String url) {
551        this.countryComponentUrl = url;
552    }
553
554    /**
555     * Get the value of the Language2LetterCodeComponentUrl parameter<br><br>
556     *
557     * For a description of the parameter, refer to the general VLO
558     * documentation.
559     *
560     * @return the value
561     */
562    public String getLanguage2LetterCodeComponentUrl() {
563        return language2LetterCodeComponentUrl;
564    }
565
566    /**
567     * Set the value of the Language2LetterCodeComponentUrl parameter<br><br>
568     *
569     * For a description of the parameter, refer to the general VLO
570     * documentation.
571     *
572     * @param language2LetterCodeComponentUrl the value
573     */
574    public void setLanguage2LetterCodeComponentUrl(String url) {
575        this.language2LetterCodeComponentUrl = url;
576    }
577
578    /**
579     * Get the value of the Language3LetterCodeComponentUrl parameter<br><br>
580     *
581     * For a description of the parameter, refer to the general VLO
582     * documentation.
583     *
584     * @return the value
585     */
586    public String getLanguage3LetterCodeComponentUrl() {
587        return language3LetterCodeComponentUrl;
588    }
589
590    /**
591     * Set the value of the Language3LetterCodeComponentUrl parameter<br><br>
592     *
593     * For a description of the parameter, refer to the general VLO
594     * documentation.
595     *
596     * @param language3LetterCodeComponentUrl the value
597     */
598    public void setLanguage3LetterCodeComponentUrl(String url) {
599        this.language3LetterCodeComponentUrl = url;
600    }
601
602    /**
603     * Get the value of the SilToISO639CodesUrl parameter<br><br>
604     *
605     * For a description of the parameter, refer to the general VLO
606     * documentation.
607     *
608     * @return the value
609     */
610    public String getSilToISO639CodesUrl() {
611        return silToISO639CodesUrl;
612    }
613
614    /**
615     * Set the value of the SilToISO639CodesUrl parameter<br><br>
616     *
617     * For a description of the parameter, refer to the general VLO
618     * documentation.
619     *
620     * @param silToISO639CodesUrl the value
621     */
622    public void setSilToISO639CodesUrl(String url) {
623        this.silToISO639CodesUrl = url;
624    }
625           
626    /**
627     * Switch to external configuration.<br><br>
628     *
629     * In addition to the definition of the configuration by the packaged in the
630     * {@literal VloConfig.xml} file, you can configure the web application by
631     * means of an XML file that resides outside the package. By letting a
632     * parameter named<br><br>
633     *
634     * externalConfig<br><br>
635     *
636     * in the context reference an XML file similar to the packaged one, the
637     * parameters defined in this file will override the packaged parameters.
638     * Please note that the use of an external XML file is not
639     * compulsory.<br><br>
640     *
641     * Another way to externally configure the web application is to define
642     * parameters by including them in the context fragment not via an XML file,
643     * but directly. At the moment, only the packaged <br><br>
644     *
645     * solrUrl<br><br>
646     *
647     * parameter can be overridden in this way.
648     *
649     * @param config static configuration
650     *
651     * @return the static WebAppConfig member
652     */
653    public static VloConfig switchToExternalConfig(ServletContext context) {
654             
655        // assume that there is no file outside the package defining parameters
656
657        boolean externalConfig = false;
658       
659        // check for a reference to of such a file
660
661        String fileName;
662        fileName = context.getInitParameter("externalConfig");
663               
664        if (fileName == null) {
665            // no external configuration file
666        } else {
667            config = (VloConfig) read(fileName, config);
668        }
669       
670        /**
671         * In addition to modifications via an external configuration file,
672         * check if the current configuration needs to be modified because of a
673         * parameter defined in the context directly.
674         */       
675        String url = context.getInitParameter("solrUrl");
676       
677        if (url == null){
678            // no overruling parameter in the context
679        } else
680        {
681            // overrule the current value of solrUrl
682
683            VloConfig.get().setSolrUrl(url);
684        }
685       
686        // return the current configuration, modified or not
687
688        return config;
689    }   
690}
Note: See TracBrowser for help on using the repository browser.