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;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * AbstractConfigReader
43 * $LastChangedRevision: $
46 public abstract class AbstractConfigReaderImpl<T>
47 implements ConfigReader<T> {
49 private final Logger logger = LoggerFactory.getLogger(AbstractConfigReaderImpl.class);
50 private String serverRootDir;
52 AbstractConfigReaderImpl(String serverRootDir) {
53 this.serverRootDir = serverRootDir;
57 abstract public String getFileName();
60 abstract public void read() throws Exception;
63 abstract public void read(String configFile) throws Exception;
66 abstract public T getConfiguration();
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.
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.
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()) {
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);
99 * Gets a list of files/documents in the specified folder -does not return directories/folders.
101 * @param rootDir the root dir
103 * @throws IOException Signals that an I/O exception has occurred.
105 List<File> getFiles(File rootDir) throws IOException {
106 return getFileChildren(rootDir, false);
109 List<File> getDirectories(File rootDir) throws IOException {
110 return getFileChildren(rootDir, true);
113 protected Object parse(File configFile, Class<?> clazz) throws FileNotFoundException, JAXBException {
114 Object result = null;
116 InputStream inputStream = new FileInputStream(configFile);
117 result = parse(inputStream, clazz);
118 if (logger.isDebugEnabled()) {
119 logger.debug("read() read file " + configFile.getAbsolutePath());
126 * parse parses given configuration file from the disk based on given class
130 * @return A JAXB object
131 * @throws JAXBException
134 protected Object parse(InputStream configFileStream, Class<?> clazz) throws JAXBException {
135 Object result = null;
137 JAXBContext jc = JAXBContext.newInstance(clazz);
138 Unmarshaller um = jc.createUnmarshaller();
139 result = um.unmarshal(configFileStream);
144 protected String getAbsoluteFileName(String configFileName) {
146 + File.separator + CSPACE_DIR_NAME
147 + File.separator + CONFIG_DIR_PATH
148 + File.separator + configFileName;
151 protected String getServerRootDir() {
152 return serverRootDir;
155 protected String getConfigRootDir() {
157 + File.separator + CSPACE_DIR_NAME
158 + File.separator + CONFIG_DIR_PATH;