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.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
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;
38 * AbstractConfigReader
40 * $LastChangedRevision: $
43 public abstract class AbstractConfigReaderImpl<T>
44 implements ConfigReader<T> {
46 private final Logger logger = LoggerFactory.getLogger(AbstractConfigReaderImpl.class);
47 private String serverRootDir;
49 AbstractConfigReaderImpl(String serverRootDir) {
50 this.serverRootDir = serverRootDir;
54 abstract public String getFileName();
57 abstract public void read() throws Exception;
60 abstract public void read(String configFile) throws Exception;
63 abstract public T getConfiguration();
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.
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.
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()) {
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);
96 * Gets a list of files/documents in the specified folder -does not return directories/folders.
98 * @param rootDir the root dir
100 * @throws IOException Signals that an I/O exception has occurred.
102 List<File> getFiles(File rootDir) throws IOException {
103 return getFileChildren(rootDir, false);
106 List<File> getDirectories(File rootDir) throws IOException {
107 return getFileChildren(rootDir, true);
111 * parse parses given configuration file from the disk based on given class
115 * @return A JAXB object
118 protected Object parse(File configFile, Class clazz) {
119 Object result = null;
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());
127 } catch (JAXBException e) {
128 if (logger.isErrorEnabled() == true) {
129 logger.error("Could not unmarshal CollectionSpace config file: " +
130 configFile.getAbsolutePath(), e);
136 protected String getAbsoluteFileName(String configFileName) {
138 + File.separator + CSPACE_DIR_NAME
139 + File.separator + CONFIG_DIR_NAME
140 + File.separator + configFileName;
143 protected String getServerRootDir() {
144 return serverRootDir;
147 protected String getConfigRootDir() {
149 + File.separator + CSPACE_DIR_NAME
150 + File.separator + CONFIG_DIR_NAME;