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

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

Some more work on replacing the parameter mechanism.

File size: 18.7 KB
Line 
1package eu.clarin.cmdi.vlo.config;
2
3import java.io.UnsupportedEncodingException;
4import java.net.URLEncoder;
5import javax.servlet.GenericServlet;
6import javax.servlet.ServletContext;
7import org.simpleframework.xml.Element;
8import org.simpleframework.xml.ElementArray;
9import org.simpleframework.xml.Root;
10import org.slf4j.LoggerFactory;
11
12/**
13 * Web application configuration<br><br>
14 *
15 * A parameter that is part of the configuration of the VLO web application can
16 * be of two types: it is either a parameter that is defined from within the
17 * application, an application parameter, or it is a parameter that is defined
18 * in the context in which the application will live as a {@literal servlet}.
19 * Application parameters reside in the {@literal WebAppConfig.xml} file, while
20 * {@literal servlet} context parameters are part of the Tomcat server
21 * configuration. <br><br>
22 *
23 * An application parameter is defined an XML file in the resources directory of
24 * the application package. For every application parameter, the WebApplication
25 * class contains a member that is annotated according to the Simple framework
26 * specification. So<br><br>
27 *
28 * {@literal <parameterMember>}"the value of the
29 * parameter"{@literal </parameterMember>}<br><br>
30 *
31 * in the XML file is accompanied by<br><br>
32 *
33 * {@literal
34 *
35 * @element}<br> {@literal parameterMember}<br><br>
36 *
37 * in the WebAppConfig class. When the application invokes Simple by<br><br>
38 *
39 * WebAppConfig.open();<br><br>
40 *
41 * the parameter itself is accessed by<br><br>
42 *
43 * WebAppConfig.open().getParameterMember();<br><br>
44 *
45 * If you want to add a type of member that is not included in the class yet,
46 * refer to the Simple framework's specification.<br><br>
47 *
48 * A context parameter also resides in an XML file. For more information on the
49 * location and the format of this file, please refer to the Apache Tomcat
50 * configuration reference.<br><br>
51 *
52 * Because the application is indifferent to the origin of a parameter, the
53 * WebAppConfig class is the place to switch from one type of parameter to the
54 * other. In other words: you do not have to change the application if you
55 * replace an application parameter by a context parameter. The change is
56 * reflected here, and not in the application. In a sense, this relieves the
57 * need to use get and read methods. Such methods still have an advantage
58 * though, because you can change a parameter without changing the rest of the
59 * application.<br><br>
60 *
61 * Please note on the explanation of the meaning of the parameters. Because the
62 * meaning of a parameter is not local to this class, or even not local to the
63 * configuration package, they are described in the general VLO
64 * documentation.<br><br>
65 *
66 * @author keeloo
67 */
68@Root
69public class VloConfig extends ConfigFromFile {
70
71    // create a reference to the application logging
72    private final static org.slf4j.Logger LOG =
73            LoggerFactory.getLogger(VloConfig.class);
74
75    // connect to the logging framework
76    private class WebAppConfigLogger implements ConfigFilePersister.Logger {
77
78        @Override
79        public void log(Object data) {
80
81            LOG.error(data.toString());
82        }
83    }
84    private static WebAppConfigLogger logger;
85
86    /**
87     * Constructor method
88     */
89    public VloConfig() {
90        // let the superclass know about the logger defined here
91        // ConfigFileParam.logger = webAppConfigLogger;
92
93        logger = new WebAppConfigLogger();
94
95        ConfigFilePersister.setLogger(VloConfig.logger);
96    }
97   
98    /**
99     * XML File in which the application configuration is stored
100     */
101    public static final String CONFIG_FILE =
102            VloConfig.class.getResource("/WebAppConfig.xml").getFile();
103
104    /**
105     * Get the name of the XML file.
106     *
107     * Represent the filename by a method primarily for making it available to
108     * the superclass. In other words: this method overrides a method in the
109     * superclass.
110     *
111     * @return the name of the application parameter XML file
112     */
113    @Override
114    public String getFileName() {
115        /**
116         * Check the name of the web application parameter file. May be turn it
117         * into a {@literal maven} parameter.
118         */
119        return CONFIG_FILE;
120    }
121   
122    /**
123     * Make the configuration statically accessible<br><br>
124     *
125     * Both the Simple framework and the methods in the web application need to
126     * access the configuration. Access is granted by defining a member holding
127     * the configuration, that is, by defining a member of type WebAppConfig.
128     */
129    private static VloConfig config = null;
130
131    /**
132     * kj: change the annotation. Instead of opening a context, it is now a
133     * matter of initialising it. Make a new method for referencing.
134     *
135     * Open a static context of WebAppConfig members, and assign values to
136     * them.<br><br>
137     *
138     * The web application can access a parameter by invoking one of the get or
139     * set methods defined below. Because these methods return a non-static
140     * value, while WebAppConfig on the other hand denotes a static
141     * context,<br><br>
142     *
143     * WebAppConfig.getParameterMember()<br><br>
144     *
145     * for example, would not be valid. On encountering open() however, a new
146     * static context is opened, and from that, getParameterMember() can be
147     * invoked:<br><br>
148     *
149     * WebAppConfig.open().getParameterMember()<br><br>
150     *
151     * @return the web application configuration in a new static context
152     */
153    public static VloConfig setWebApp(ServletContext context) {
154        if (config == null) {
155            // the configuration is not there yet; create it now
156            config = new VloConfig();
157        }
158
159        /**
160         * get the XML file configuration from the file by invoking the
161         * superclass method
162         */
163        config = (VloConfig) read(config);
164
165        /* Contrary to Simple, the servlet context parameters are not retrieved
166         * by annotation. Get them by invoking a local method.
167         *
168         * Here also invoke a superclass method
169         */
170        config = addServletContext(config, context);
171
172        return config;
173    }
174
175    /**
176     * kj: add comment, much in the same way as the annotation of the WepApp
177     * method.
178     *
179     * @return
180     */
181    public static VloConfig WebAppTest() {
182        if (config == null) {
183            // the configuration is not there yet; create it now
184            config = new VloConfig();
185        }
186
187        /**
188         * get the XML file configuration from the file by invoking the
189         * superclass method
190         */
191        config = (VloConfig) read(config);
192
193        return config;
194    }
195   
196    /**
197     * kj: this is the new get context method
198     *
199     * @return
200     */
201    public static VloConfig get (){
202        return config;
203    }
204
205    /**
206     * Close the static context of WebAppConfig members<br><cr>
207     *
208     * As a counterpart to open() the close() method just writes the value of
209     * the parameters in the XML file back to this file.
210     */
211    public void close() {
212        if (config == null) {
213            // no configuration is not there yet; no need to do anything
214        } else {
215            // put it away by invoking the superclass write method
216
217            write (config);
218
219            /**
220             * Contrary to web the application parameters, servlet context
221             * parameters can not be written back to where they reside, so there
222             * is nothing left to do here.
223             */
224        }
225    }
226   
227    /**
228     * Web application parameter members<br><br>
229     *
230     * Initialize a member corresponding to application parameters with an empty
231     * string at least, for this will allow them to be linearized to
232     * corresponding elements in the XML file.
233     *
234     * Please refer to the general VLO documentation for a description of the
235     * member parameters.
236     */
237    @Element // annotation directive as defined by Simple
238    private String vloHomeLink = "";
239   
240    @Element
241    private String solrUrl = "";
242    // In the XML file, the value of the parameter is expanded by Maven.
243   
244    @Element
245    private String profileSchemaUrl = "";
246
247    @Element
248    private String componentRegistryRESTURL = "";
249   
250    @Element
251    private String handleServerUrl = "";
252   
253    @Element
254    private String imdiBrowserUrl = "";
255   
256    /**
257     * Note: the national project mapping itself is not part of the web
258     * application configuration.
259     */
260    @Element
261    private String nationalProjectMapping = "";
262   
263    /**
264     * An array of facetFields<br><br>
265     *
266     * In case of an array of elements, the number of elements in the array
267     * needs to be communicated to Simple. The following would be a correct
268     * description of an array of three facet fields<br><br>
269     *
270     * {@literal <facetFields length="3">}<br> {@literal    <facetField>}<br>
271     * {@literal       fieldOne}<br> {@literal    </facetField>}<br>
272     * {@literal    <facetField>}<br> {@literal       fieldTwo}<br>
273     * {@literal    </facetField>}<br> {@literal    <facetField>}<br>
274     * {@literal       fieldThree}<br> {@literal    </facetField>}<br>
275     * {@literal </facetFields>}<br><br>
276     *
277     * To let Spring now it has to interpret the facetFields element as an
278     * array, use the ElementArray directive. Use the directive to let Spring
279     * know that the elements inside 'facetFields' are named 'facetField'.
280     */
281    @ElementArray(entry = "facetField")
282    private String[] facetFields = {"", "", ""};
283   
284    @Element
285    private String countryComponentUrl = "";
286   
287    @Element
288    private String language2LetterCodeComponentUrl = "";
289   
290    @Element
291    private String language3LetterCodeComponentUrl = "";
292   
293    @Element
294    private String silToISO639CodesUrl = "";
295   
296    @Element
297    private String FederatedContentSearchUrl = " ";
298
299    /**
300     * Get and read methods for web application parameter members<br><br>
301     *
302     * By using a get or read method, you can apply an operation to a parameter
303     * here, in the WebAppConfig class, without the need to make changes in
304     * different parts of the application.
305     */
306   
307    /**
308     * Get the VloHomeLink parameter<br><br>
309     *
310     * For a description of the parameter, refer to the general VLO
311     * documentation.
312     *
313     * @return the parameter
314     */
315    public String getVloHomeLink() {
316        return vloHomeLink;
317    }
318
319    /**
320     * Set the VloHomeLink parameter<br><br>
321     *
322     * For a description of the parameter, refer to the general VLO
323     * documentation.
324     *
325     * @param vloHomeLink the parameter
326     */
327    public void setVloHomeLink(String link) {
328        this.vloHomeLink = link;
329    }
330
331    /**
332     * Get the SolrUrl parameter<br><br>
333     *
334     * For a description of the parameter, refer to the general VLO
335     * documentation.
336     *
337     * @param solrUrl the parameter
338     */
339    public String getSolrUrl() {
340        return solrUrl;
341    }
342
343    /**
344     * Get the ProfileSchemaUrl by profileId parameter<br><br>
345     *
346     * For a description of the schema, refer to the general VLO documentation.
347     * Note: the profileId needs to be expanded.
348     *
349     * @return the parameter
350     */
351    public String getComponentRegistryProfileSchema(String id) {
352        return profileSchemaUrl.replace("${PROFILE_ID}", id);
353    }
354
355    /**
356     * Set the ProfileSchemaUrl parameter<br><br>
357     *
358     * For a description of the schema, refer to the general VLO documentation.
359     * Note: the profileId needs to be expanded.
360     *
361     * @param profileId the parameter
362     */
363    public void setProfileSchemaUrl(String url) {
364        this.profileSchemaUrl = url;
365    }
366
367    /**
368     * Get the ComponentRegisteryRESTURL parameter<br><br>
369     *
370     * For a description of the parameter, refer to the general VLO
371     * documentation.
372     *
373     * @return the parameter
374     */
375    public String getComponentRegistryRESTURL() {
376        return componentRegistryRESTURL;
377    }
378
379    /**
380     * Set the ComponentRegisteryRESTURL parameter<br><br>
381     *
382     * For a description of the parameter, refer to the general VLO
383     * documentation.
384     *
385     * @param componentRegistryRESTURL the parameter
386     */
387    public void setComponentRegistryRESTURL(String url) {
388        this.componentRegistryRESTURL = url;
389    }
390
391    /**
392     * Get the HandleServerUrl parameter<br><br>
393     *
394     * For a description of the parameter, refer to the general VLO
395     * documentation.
396     *
397     * @return the parameter
398     */
399    public String getHandleServerUrl() {
400        return handleServerUrl;
401    }
402
403    /**
404     * Set the HandleServerUrl parameter<br><br>
405     *
406     * For a description of the parameter, refer to the general VLO
407     * documentation.
408     *
409     * @param handleServerUrl the parameter
410     */
411    public void setHandleServerUrl(String url) {
412        this.handleServerUrl = url;
413    }
414
415    /**
416     * Set the IMDIBrowserUrl parameter<br><br>
417     *
418     * For a description of the parameter, refer to the general VLO
419     * documentation.
420     *
421     * @param imdiBrowserUrl the parameter
422     */
423    public void setIMDIBrowserUrl(String url) {
424        this.imdiBrowserUrl = url;
425    }
426
427    /**
428     * Get ProfileSchemaUrl parameter combined with a handle<br><br>
429     *
430     * For a description of the schema, refer to the general VLO documentation.
431     *
432     * @param handle the handle to be combined with the parameter
433     */
434    public String getIMDIBrowserUrl(String handle) {
435        String result;
436        try {
437            result = imdiBrowserUrl + URLEncoder.encode(handle, "UTF-8");
438        } catch (UnsupportedEncodingException e) {
439            result = imdiBrowserUrl + handle;
440        }
441        return result;
442    }
443
444    /**
445     * Get the FederatedContentSearchUrl parameter<br><br>
446     *
447     * For a description of the parameter, refer to the general VLO
448     * documentation.
449     *
450     * @return the parameter
451     */
452    public void setFederatedContentSearchUrl(String url) {
453        this.FederatedContentSearchUrl = url;
454    }
455
456    /**
457     * Set the FederatedContentSearchUrl parameter<br><br>
458     *
459     * For a description of the parameter, refer to the general VLO
460     * documentation.
461     *
462     * @param FederatedContentSearchUrl the parameter
463     */
464    public String getFederatedContentSearchUrl() {
465        return FederatedContentSearchUrl;
466    }
467
468    /**
469     * Set the NationalProjectMapping parameter<br><br>
470     *
471     * For a description of the parameter, refer to the general VLO
472     * documentation.
473     *
474     * @param nationalProjectMapping the parameter
475     */
476    public void setNationalProjectMapping(String mapping) {
477        this.nationalProjectMapping = mapping;
478    }
479
480    /**
481     * Get the NationalProjectMapping parameter<br><br>
482     *
483     * For a description of the parameter, refer to the general VLO
484     * documentation.
485     *
486     * @return the parameter
487     */
488    public String getNationalProjectMapping() {
489        return nationalProjectMapping;
490    }
491
492    /**
493     * Get the FacetFields parameter<br><br>
494     *
495     * For a description of the parameter, refer to the general VLO
496     * documentation.
497     *
498     * @return the parameter
499     */
500    public String[] getFacetFields() {
501        return facetFields;
502    }
503
504    /**
505     * Set the FacetFields parameter<br><br>
506     *
507     * For a description of the parameter, refer to the general VLO
508     * documentation.
509     *
510     * @param facetFields the parameter, a list of facet fields
511     */
512    public void setFacetFields(String[] fields) {
513        this.facetFields = fields;
514    }
515
516    /**
517     * Get the CountryComponentUrl parameter<br><br>
518     *
519     * For a description of the parameter, refer to the general VLO
520     * documentation.
521     *
522     * @return the parameter
523     */
524    public String getCountryComponentUrl() {
525        return countryComponentUrl;
526    }
527
528    /**
529     * Set the CountryComponentUrl parameter<br><br>
530     *
531     * For a description of the parameter, refer to the general VLO
532     * documentation.
533     *
534     * @param countryComponentUrl the parameter
535     */
536    public void setCountryComponentUrl(String url) {
537        this.countryComponentUrl = url;
538    }
539
540    /**
541     * Get the Language2LetterCodeComponentUrl parameter<br><br>
542     *
543     * For a description of the parameter, refer to the general VLO
544     * documentation.
545     *
546     * @return the parameter
547     */
548    public String getLanguage2LetterCodeComponentUrl() {
549        return language2LetterCodeComponentUrl;
550    }
551
552    /**
553     * Set the Language2LetterCodeComponentUrl parameter<br><br>
554     *
555     * For a description of the parameter, refer to the general VLO
556     * documentation.
557     *
558     * @param language2LetterCodeComponentUrl the parameter
559     */
560    public void setLanguage2LetterCodeComponentUrl(String url) {
561        this.language2LetterCodeComponentUrl = url;
562    }
563
564    /**
565     * Get the Language3LetterCodeComponentUrl parameter<br><br> For a
566     * description of the parameter, refer to the general VLO documentation.
567     *
568     * @return the parameter
569     */
570    public String getLanguage3LetterCodeComponentUrl() {
571        return language3LetterCodeComponentUrl;
572    }
573
574    /**
575     * Set the Language3LetterCodeComponentUrl parameter<br><br> For a
576     * description of the parameter, refer to the general VLO documentation.
577     *
578     * @param language3LetterCodeComponentUrl the parameter
579     */
580    public void setLanguage3LetterCodeComponentUrl(String url) {
581        this.language3LetterCodeComponentUrl = url;
582    }
583
584    /**
585     * Get the SilToISO639CodesUrl parameter<br><br>
586     *
587     * For a description of the parameter, refer to the general VLO
588     * documentation.
589     *
590     * @return the parameter
591     */
592    public String getSilToISO639CodesUrl() {
593        return silToISO639CodesUrl;
594    }
595
596    /**
597     * Set the SilToISO639CodesUrl parameter<br><br>
598     *
599     * For a description of the parameter, refer to the general VLO
600     * documentation.
601     *
602     * @param silToISO639CodesUrl the parameter
603     */
604    public void setSilToISO639CodesUrl(String url) {
605        this.silToISO639CodesUrl = url;
606    }
607           
608    /**
609     * Add properties of the {@literal servlet's} context<br><br>
610     *
611     * Keep the properties in the static context of the WebAppConfig class, next
612     * to the members representing the values in WebAppConfig.xml file.<br><br>
613     *
614     * @param config static configuration
615     *
616     * @return the static WebAppConfig member
617     */
618    static VloConfig addServletContext(VloConfig config, ServletContext context) {
619
620        // retrieve parameter valies from the servlet context
621
622        config.solrUrl = context.getInitParameter("solrUrl");
623
624        return config;
625    }
626   
627    /**
628     * {@literal Servlet} context members<br><br>
629     *
630     * The following defines the members corresponding to {@servlet} context
631     * parameters.
632     */   
633}
Note: See TracBrowser for help on using the repository browser.