]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a03e118f3555339d3222635d90ce371d86c5c091
[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 void read(List<String> configFiles, boolean useAppGeneratedBindings) throws Exception;
68
69         @Override
70         abstract public T getConfiguration();
71
72         /**
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.
76          * 
77          * @param rootDir
78          *            the root dir
79          * @param isDirectory
80          *            the is directory
81          * @return the file children
82          * @throws IOException
83          *             Signals that an I/O exception has occurred.
84          */
85         List<File> getFileChildren(File rootDir, boolean getDirectories)
86                         throws IOException {
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()) {
93                                                 result.add(child);
94                                         }
95                                 }
96                         }
97                 } else {
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);
102                 }
103                 return result;
104         }
105
106         /**
107          * Gets a list of files/documents in the specified folder -does not return
108          * directories/folders.
109          * 
110          * @param rootDir
111          *            the root dir
112          * @return the files
113          * @throws IOException
114          *             Signals that an I/O exception has occurred.
115          */
116         List<File> getFiles(File rootDir) throws IOException {
117                 return getFileChildren(rootDir, false);
118         }
119
120         List<File> getDirectories(File rootDir) throws IOException {
121                 return getFileChildren(rootDir, true);
122         }
123
124         public Object parse(File configFile, Class<?> clazz)
125                         throws FileNotFoundException, JAXBException {
126                 Object result = null;
127
128                 InputStream inputStream = new FileInputStream(configFile);
129                 result = parse(inputStream, clazz);
130                 if (logger.isDebugEnabled()) {
131                         logger.debug("read() read file " + configFile.getAbsolutePath());
132                 }
133
134                 return result;
135         }
136
137         /**
138          * parse parses given configuration file from the disk based on given class
139          * definition
140          * 
141          * @param configFile
142          * @param clazz
143          * @return A JAXB object
144          * @throws JAXBException
145          * @throws Exception
146          */
147         public Object parse(InputStream configFileStream, Class<?> clazz)
148                         throws JAXBException {
149                 Object result = null;
150
151                 JAXBContext jc = JAXBContext.newInstance(clazz);
152                 Unmarshaller um = jc.createUnmarshaller();
153                 result = um.unmarshal(configFileStream);
154
155                 return result;
156         }
157
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
162                                 + configFileName;
163         }
164
165         protected String getServerRootDir() {
166                 return serverRootDir;
167         }
168
169         protected String getConfigRootDir() {
170                 return serverRootDir + File.separator
171                                 + JEEServerDeployment.CSPACE_DIR_NAME + File.separator
172                                 + JEEServerDeployment.CONFIG_DIR_PATH;
173         }
174 }