]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8afe5879515ac1dde1725be2a17f4bc45c9d04bb
[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.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;
33
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.Unmarshaller;
37
38 import org.collectionspace.services.common.api.JEEServerDeployment;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * AbstractConfigReader
44  * 
45  * $LastChangedRevision: $ $LastChangedDate: $
46  */
47 public abstract class AbstractConfigReaderImpl<T> implements ConfigReader<T> {
48
49         private final Logger logger = LoggerFactory
50                         .getLogger(AbstractConfigReaderImpl.class);
51         private String serverRootDir;
52
53         AbstractConfigReaderImpl(String serverRootDir) {
54                 this.serverRootDir = serverRootDir;
55         }
56
57         @Override
58         abstract public String getFileName();
59
60         @Override
61         abstract public void read(boolean useAppGeneratedBindings) throws Exception;
62
63         @Override
64         abstract public void read(String configFile, boolean useAppGeneratedBindings) throws Exception;
65
66         @Override
67         abstract public T getConfiguration();
68
69         /**
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.
73          * 
74          * @param rootDir
75          *            the root dir
76          * @param isDirectory
77          *            the is directory
78          * @return the file children
79          * @throws IOException
80          *             Signals that an I/O exception has occurred.
81          */
82         List<File> getFileChildren(File rootDir, boolean getDirectories)
83                         throws IOException {
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()) {
90                                                 result.add(child);
91                                         }
92                                 }
93                         }
94                 } else {
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);
99                 }
100                 return result;
101         }
102
103         /**
104          * Gets a list of files/documents in the specified folder -does not return
105          * directories/folders.
106          * 
107          * @param rootDir
108          *            the root dir
109          * @return the files
110          * @throws IOException
111          *             Signals that an I/O exception has occurred.
112          */
113         List<File> getFiles(File rootDir) throws IOException {
114                 return getFileChildren(rootDir, false);
115         }
116
117         List<File> getDirectories(File rootDir) throws IOException {
118                 return getFileChildren(rootDir, true);
119         }
120
121         protected Object parse(File configFile, Class<?> clazz)
122                         throws FileNotFoundException, JAXBException {
123                 Object result = null;
124
125                 InputStream inputStream = new FileInputStream(configFile);
126                 result = parse(inputStream, clazz);
127                 if (logger.isDebugEnabled()) {
128                         logger.debug("read() read file " + configFile.getAbsolutePath());
129                 }
130
131                 return result;
132         }
133
134         /**
135          * parse parses given configuration file from the disk based on given class
136          * definition
137          * 
138          * @param configFile
139          * @param clazz
140          * @return A JAXB object
141          * @throws JAXBException
142          * @throws Exception
143          */
144         protected Object parse(InputStream configFileStream, Class<?> clazz)
145                         throws JAXBException {
146                 Object result = null;
147
148                 JAXBContext jc = JAXBContext.newInstance(clazz);
149                 Unmarshaller um = jc.createUnmarshaller();
150                 result = um.unmarshal(configFileStream);
151
152                 return result;
153         }
154
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
159                                 + configFileName;
160         }
161
162         protected String getServerRootDir() {
163                 return serverRootDir;
164         }
165
166         protected String getConfigRootDir() {
167                 return serverRootDir + File.separator
168                                 + JEEServerDeployment.CSPACE_DIR_NAME + File.separator
169                                 + JEEServerDeployment.CONFIG_DIR_PATH;
170         }
171 }