]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
eb3439c2b0cb908d0231116f20cb60eb957c39d9
[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 java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.Unmarshaller;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * AbstractConfigReader
42  *
43  * $LastChangedRevision: $
44  * $LastChangedDate: $
45  */
46 public abstract class AbstractConfigReaderImpl<T>
47         implements ConfigReader<T> {
48
49     private final Logger logger = LoggerFactory.getLogger(AbstractConfigReaderImpl.class);
50     private String serverRootDir;
51
52     AbstractConfigReaderImpl(String serverRootDir) {
53         this.serverRootDir = serverRootDir;
54     }
55
56     @Override
57     abstract public String getFileName();
58
59     @Override
60     abstract public void read() throws Exception;
61
62     @Override
63     abstract public void read(String configFile) throws Exception;
64
65     @Override
66     abstract public T getConfiguration();
67
68         /**
69          * Gets a list of File items in the specified directory.  If 'isDirectory' is true, then this
70          * method will return a list of items that are directories/folders; otherwise, it returns a list
71          * of file/document items.
72          *
73          * @param rootDir the root dir
74          * @param isDirectory the is directory
75          * @return the file children
76          * @throws IOException Signals that an I/O exception has occurred.
77          */
78         List<File> getFileChildren(File rootDir, boolean getDirectories) throws IOException {
79                 ArrayList<File> result = new ArrayList<File>();
80                 File[] children = rootDir.listFiles();
81                 if (children != null) {
82                         for (File child : children) {
83                                 if (child.isHidden() == false) {
84                                         if (getDirectories == child.isDirectory()) {
85                                                 result.add(child);
86                                         }
87                                 }
88                         }
89                 } else {
90                         String errMessage = "An IO exception and/or error occurred while reading the directory: "
91                                 + rootDir.getAbsolutePath();
92                         logger.debug(errMessage);
93                         throw new IOException(errMessage);
94                 }
95                 return result;
96         }
97         
98         /**
99          * Gets a list of files/documents in the specified folder -does not return directories/folders.
100          *
101          * @param rootDir the root dir
102          * @return the files
103          * @throws IOException Signals that an I/O exception has occurred.
104          */
105         List<File> getFiles(File rootDir) throws IOException {
106                 return getFileChildren(rootDir, false);
107         }
108         
109         List<File> getDirectories(File rootDir) throws IOException {
110                 return getFileChildren(rootDir, true);
111         }
112     
113     protected Object parse(File configFile, Class<?> clazz) throws FileNotFoundException, JAXBException {
114         Object result = null;
115         
116         InputStream inputStream = new FileInputStream(configFile);
117         result = parse(inputStream, clazz);
118         if (logger.isDebugEnabled()) {
119             logger.debug("read() read file " + configFile.getAbsolutePath());
120         }
121         
122         return result;
123     }
124     
125     /**
126      * parse parses given configuration file from the disk based on given class
127      * definition
128      * @param configFile
129      * @param clazz
130      * @return A JAXB object
131      * @throws JAXBException 
132      * @throws Exception
133      */
134     protected Object parse(InputStream configFileStream, Class<?> clazz) throws JAXBException {
135         Object result = null;
136
137                 JAXBContext jc = JAXBContext.newInstance(clazz);
138                 Unmarshaller um = jc.createUnmarshaller();
139                 result = um.unmarshal(configFileStream);
140
141         return result;
142     }
143
144     protected String getAbsoluteFileName(String configFileName) {
145         return serverRootDir
146                 + File.separator + CSPACE_DIR_NAME
147                 + File.separator + CONFIG_DIR_PATH
148                 + File.separator + configFileName;
149     }
150
151     protected String getServerRootDir() {
152         return serverRootDir;
153     }
154     
155     protected String getConfigRootDir() {
156         return serverRootDir
157         + File.separator + CSPACE_DIR_NAME
158         + File.separator + CONFIG_DIR_PATH;
159     }
160 }