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