]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ed79fa52a689abefb4c8d311a0f302ecdf86e9ed
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.common.config;
25
26 import java.io.File;
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.Unmarshaller;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * AbstractConfigReader
34  *
35  * $LastChangedRevision: $
36  * $LastChangedDate: $
37  */
38 public abstract class AbstractConfigReaderImpl<T>
39         implements ConfigReader<T> {
40
41     private final Logger logger = LoggerFactory.getLogger(AbstractConfigReaderImpl.class);
42     private String serverRootDir;
43
44     AbstractConfigReaderImpl(String serverRootDir) {
45         this.serverRootDir = serverRootDir;
46     }
47
48     @Override
49     abstract public String getFileName();
50
51     @Override
52     abstract public void read() throws Exception;
53
54     @Override
55     abstract public T getConfiguration();
56
57
58     /**
59      * parse parses given configuration file from the disk based on given class
60      * definition
61      * @param configFile
62      * @param clazz
63      * @return A JAXB object
64      * @throws Exception
65      */
66     protected Object parse(File configFile, Class clazz) throws Exception {
67         JAXBContext jc = JAXBContext.newInstance(clazz);
68         Unmarshaller um = jc.createUnmarshaller();
69         Object readObject = um.unmarshal(configFile);
70         if(logger.isDebugEnabled()){
71             logger.debug("read() read file " + configFile.getAbsolutePath());
72         }
73         return readObject;
74     }
75
76     protected String getAbsoluteFileName(String configFileName) {
77         return serverRootDir +
78                 File.separator + CSPACE_DIR_NAME +
79                 File.separator + CONFIG_DIR_NAME +
80                 File.separator + configFileName;
81     }
82
83     protected String getServerRootDir() {
84         return serverRootDir;
85     }
86 }