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 void read(List<String> configFiles, boolean useAppGeneratedBindings) throws Exception;
70 abstract public T getConfiguration();
73 * Gets a list of File items in the specified directory. If 'isDirectory' is
74 * true, then this method will return a list of items that are
75 * directories/folders; otherwise, it returns a list of file/document items.
81 * @return the file children
83 * Signals that an I/O exception has occurred.
85 List<File> getFileChildren(File rootDir, boolean getDirectories)
87 ArrayList<File> result = new ArrayList<File>();
88 File[] children = rootDir.listFiles();
89 if (children != null) {
90 for (File child : children) {
91 if (child.isHidden() == false) {
92 if (getDirectories == child.isDirectory()) {
98 String errMessage = "An IO exception and/or error occurred while reading the directory: "
99 + rootDir.getAbsolutePath();
100 logger.debug(errMessage);
101 throw new IOException(errMessage);
107 * Gets a list of files/documents in the specified folder -does not return
108 * directories/folders.
113 * @throws IOException
114 * Signals that an I/O exception has occurred.
116 List<File> getFiles(File rootDir) throws IOException {
117 return getFileChildren(rootDir, false);
120 List<File> getDirectories(File rootDir) throws IOException {
121 return getFileChildren(rootDir, true);
124 public Object parse(File configFile, Class<?> clazz)
125 throws FileNotFoundException, JAXBException {
126 Object result = null;
128 InputStream inputStream = new FileInputStream(configFile);
129 result = parse(inputStream, clazz);
130 if (logger.isDebugEnabled()) {
131 logger.debug("read() read file " + configFile.getAbsolutePath());
138 * parse parses given configuration file from the disk based on given class
143 * @return A JAXB object
144 * @throws JAXBException
147 public Object parse(InputStream configFileStream, Class<?> clazz)
148 throws JAXBException {
149 Object result = null;
151 JAXBContext jc = JAXBContext.newInstance(clazz);
152 Unmarshaller um = jc.createUnmarshaller();
153 result = um.unmarshal(configFileStream);
158 protected String getAbsoluteFileName(String configFileName) {
159 return serverRootDir + File.separator
160 + JEEServerDeployment.CSPACE_DIR_NAME + File.separator
161 + JEEServerDeployment.CONFIG_DIR_PATH + File.separator
165 protected String getServerRootDir() {
166 return serverRootDir;
169 protected String getConfigRootDir() {
170 return serverRootDir + File.separator
171 + JEEServerDeployment.CSPACE_DIR_NAME + File.separator
172 + JEEServerDeployment.CONFIG_DIR_PATH;