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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.common.config;
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;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.Unmarshaller;
38 import org.collectionspace.services.common.api.JEEServerDeployment;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * AbstractConfigReader
45 * $LastChangedRevision: $ $LastChangedDate: $
47 public abstract class AbstractConfigReaderImpl<T> implements ConfigReader<T> {
49 private final Logger logger = LoggerFactory
50 .getLogger(AbstractConfigReaderImpl.class);
51 private String serverRootDir;
53 AbstractConfigReaderImpl(String serverRootDir) {
54 this.serverRootDir = serverRootDir;
58 abstract public String getFileName();
61 abstract public void read(boolean useAppGeneratedBindings) throws Exception;
64 abstract public void read(String configFile, boolean useAppGeneratedBindings) throws Exception;
67 abstract public T getConfiguration();
70 * Gets a list of File items in the specified directory. If 'isDirectory' is
71 * true, then this method will return a list of items that are
72 * directories/folders; otherwise, it returns a list of file/document items.
78 * @return the file children
80 * Signals that an I/O exception has occurred.
82 List<File> getFileChildren(File rootDir, boolean getDirectories)
84 ArrayList<File> result = new ArrayList<File>();
85 File[] children = rootDir.listFiles();
86 if (children != null) {
87 for (File child : children) {
88 if (child.isHidden() == false) {
89 if (getDirectories == child.isDirectory()) {
95 String errMessage = "An IO exception and/or error occurred while reading the directory: "
96 + rootDir.getAbsolutePath();
97 logger.debug(errMessage);
98 throw new IOException(errMessage);
104 * Gets a list of files/documents in the specified folder -does not return
105 * directories/folders.
110 * @throws IOException
111 * Signals that an I/O exception has occurred.
113 List<File> getFiles(File rootDir) throws IOException {
114 return getFileChildren(rootDir, false);
117 List<File> getDirectories(File rootDir) throws IOException {
118 return getFileChildren(rootDir, true);
121 protected Object parse(File configFile, Class<?> clazz)
122 throws FileNotFoundException, JAXBException {
123 Object result = null;
125 InputStream inputStream = new FileInputStream(configFile);
126 result = parse(inputStream, clazz);
127 if (logger.isDebugEnabled()) {
128 logger.debug("read() read file " + configFile.getAbsolutePath());
135 * parse parses given configuration file from the disk based on given class
140 * @return A JAXB object
141 * @throws JAXBException
144 protected Object parse(InputStream configFileStream, Class<?> clazz)
145 throws JAXBException {
146 Object result = null;
148 JAXBContext jc = JAXBContext.newInstance(clazz);
149 Unmarshaller um = jc.createUnmarshaller();
150 result = um.unmarshal(configFileStream);
155 protected String getAbsoluteFileName(String configFileName) {
156 return serverRootDir + File.separator
157 + JEEServerDeployment.CSPACE_DIR_NAME + File.separator
158 + JEEServerDeployment.CONFIG_DIR_PATH + File.separator
162 protected String getServerRootDir() {
163 return serverRootDir;
166 protected String getConfigRootDir() {
167 return serverRootDir + File.separator
168 + JEEServerDeployment.CSPACE_DIR_NAME + File.separator
169 + JEEServerDeployment.CONFIG_DIR_PATH;