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

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

some work on the vlo tests

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