|
1 |
/**
|
|
2 |
* Copyright (C) 2014 EDIT
|
|
3 |
* European Distributed Institute of Taxonomy
|
|
4 |
* http://www.e-taxonomy.eu
|
|
5 |
*
|
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms.
|
|
8 |
*/
|
|
9 |
package eu.etaxonomy.cdm.config;
|
|
10 |
|
|
11 |
import static eu.etaxonomy.cdm.common.XmlHelp.getBeansRoot;
|
|
12 |
import static eu.etaxonomy.cdm.common.XmlHelp.saveToXml;
|
|
13 |
|
|
14 |
import java.io.File;
|
|
15 |
import java.io.FileInputStream;
|
|
16 |
import java.io.FileNotFoundException;
|
|
17 |
import java.io.FileOutputStream;
|
|
18 |
import java.io.IOException;
|
|
19 |
import java.util.ArrayList;
|
|
20 |
import java.util.List;
|
|
21 |
|
|
22 |
import org.apache.log4j.Logger;
|
|
23 |
import org.jdom.Document;
|
|
24 |
import org.jdom.Element;
|
|
25 |
import org.jdom.output.Format;
|
|
26 |
|
|
27 |
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
|
|
28 |
import eu.etaxonomy.cdm.common.XmlHelp;
|
|
29 |
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Utility class which manages the persistent source settings
|
|
33 |
* in the datasource datasource config
|
|
34 |
*
|
|
35 |
* @author cmathew
|
|
36 |
*
|
|
37 |
*/
|
|
38 |
public class CdmPersistentSourceUtils {
|
|
39 |
private static final Logger logger = Logger.getLogger(CdmPersistentSourceUtils.class);
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Returns the directory containing the datasource config file
|
|
45 |
* @return
|
|
46 |
*/
|
|
47 |
public static String getResourceDirectory(){
|
|
48 |
try {
|
|
49 |
File f = CdmApplicationUtils.getWritableResourceDir();
|
|
50 |
return f.getPath();
|
|
51 |
} catch (IOException e) {
|
|
52 |
logger.error(e);
|
|
53 |
throw new RuntimeException(e);
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Returns the datasource config file input stream.
|
|
59 |
*
|
|
60 |
* @return data source config file input stream
|
|
61 |
*/
|
|
62 |
public static FileInputStream getCdmSourceInputStream(){
|
|
63 |
String dir = CdmPersistentSourceUtils.getResourceDirectory();
|
|
64 |
File file = new File(dir + File.separator + CdmPersistentXMLSource.CDMSOURCE_FILE_NAME);
|
|
65 |
return fileInputStream(file);
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Returns the datasource config file outputStream.
|
|
70 |
*
|
|
71 |
* @return data source config file outputStream
|
|
72 |
*/
|
|
73 |
public static FileOutputStream getCdmSourceOutputStream(){
|
|
74 |
String dir = CdmPersistentSourceUtils.getResourceDirectory();
|
|
75 |
File file = new File(dir + File.separator + CdmPersistentXMLSource.CDMSOURCE_FILE_NAME);
|
|
76 |
return fileOutputStream(file);
|
|
77 |
}
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Returns a FileInputStream object from a File object
|
|
81 |
*
|
|
82 |
* @param file
|
|
83 |
* @return FileInputStream object
|
|
84 |
*/
|
|
85 |
public static FileInputStream fileInputStream(File file){
|
|
86 |
try {
|
|
87 |
FileInputStream fis = new FileInputStream(file);
|
|
88 |
return fis;
|
|
89 |
} catch (FileNotFoundException e) {
|
|
90 |
logger.warn("File " + file == null?"null":file.getAbsolutePath() + " does not exist in the file system");
|
|
91 |
return null;
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Returns a FileOututStream object from a File object
|
|
97 |
*
|
|
98 |
* @param file
|
|
99 |
* @return FileOututStream object
|
|
100 |
*/
|
|
101 |
public static FileOutputStream fileOutputStream(File file){
|
|
102 |
try {
|
|
103 |
FileOutputStream fos = new FileOutputStream(file);
|
|
104 |
return fos;
|
|
105 |
} catch (FileNotFoundException e) {
|
|
106 |
logger.warn("File " + (file == null?"null":file.getAbsolutePath()) + " does not exist in the file system");
|
|
107 |
return null;
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Returns the jdom Element representing the data source bean in the config file.
|
|
114 |
*
|
|
115 |
* @param beanName , of the element to be retrieved
|
|
116 |
* @return jdom Element representing the data source bean in the config file.
|
|
117 |
*/
|
|
118 |
public static Element getCdmSourceBeanXml(String beanName){
|
|
119 |
FileInputStream inStream = getCdmSourceInputStream();
|
|
120 |
Element root = getBeansRoot(inStream);
|
|
121 |
if (root == null){
|
|
122 |
return null;
|
|
123 |
}else{
|
|
124 |
Element xmlBean = XmlHelp.getFirstAttributedChild(root, "bean", "id", beanName);
|
|
125 |
if (xmlBean == null){
|
|
126 |
//TODO warn or info
|
|
127 |
logger.debug("Unknown Element 'bean id=" + beanName + "' ");
|
|
128 |
}
|
|
129 |
return xmlBean;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Returns the jdom Element corresponding to the cdm source name and type in the config file.
|
|
136 |
*
|
|
137 |
* @param cdmSourceName , of the element to be retrieved
|
|
138 |
* @param postFix , indicating the type of cdm source
|
|
139 |
* @return jdom Element corresponding to the cdm source name and type in the config file.
|
|
140 |
*/
|
|
141 |
public static Element getCdmSourceBeanXml(String cdmSourceName, String postFix){
|
|
142 |
return getCdmSourceBeanXml(getBeanName(cdmSourceName, postFix));
|
|
143 |
}
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Converts input parameters to bean name.
|
|
149 |
*
|
|
150 |
* @param cdmSourceName , of the element to be retrieved
|
|
151 |
* @param postFix , indicating the type of cdm source
|
|
152 |
* @return bean name corresponding to the cdm source name and type in the config file.
|
|
153 |
*/
|
|
154 |
public static String getBeanName(String cdmSourceName, String postFix){
|
|
155 |
return cdmSourceName == null? null : cdmSourceName + postFix;
|
|
156 |
}
|
|
157 |
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Deletes a CDM persistent source from the source config file.
|
|
161 |
*
|
|
162 |
* @param cdmPersistentSource , CDM persistent source object
|
|
163 |
*/
|
|
164 |
public static void delete (ICdmPersistentSource cdmPersistentSource) {
|
|
165 |
delete(cdmPersistentSource.getBeanName());
|
|
166 |
}
|
|
167 |
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Deletes a CDM persistent source from the source config file.
|
|
171 |
*
|
|
172 |
* @param beanName , CDM persistent source bean name
|
|
173 |
*/
|
|
174 |
public static void delete (String beanName){
|
|
175 |
Element bean = getCdmSourceBeanXml(beanName);
|
|
176 |
if (bean != null){
|
|
177 |
Document doc = bean.getDocument();
|
|
178 |
bean.detach();
|
|
179 |
saveToXml(doc,
|
|
180 |
CdmPersistentSourceUtils.getCdmSourceOutputStream(),
|
|
181 |
XmlHelp.prettyFormat );
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Returns the property value of a CDM persistent source object bean
|
|
188 |
* from the source config file.
|
|
189 |
*
|
|
190 |
* @param bean , CDM persistent source object bean
|
|
191 |
* @param property , whose value is to be retrieved
|
|
192 |
* @return value of requested property
|
|
193 |
*/
|
|
194 |
public static String getPropertyValue(Element bean, String property){
|
|
195 |
Element driverProp = XmlHelp.getFirstAttributedChild(bean, "property", "name", property);
|
|
196 |
if (driverProp == null){
|
|
197 |
logger.debug("Unknown property: " + property);
|
|
198 |
return null;
|
|
199 |
}else{
|
|
200 |
String strProperty = driverProp.getAttributeValue("value");
|
|
201 |
return strProperty;
|
|
202 |
}
|
|
203 |
}
|
|
204 |
|
|
205 |
}
|
CdmPersistentSourceUtils : new class which contains all functionality to manage the persistent cdm sources config (taken from CdmPersistentDataSource)
CdmSourceException : wraps around exceptions when interacting with the Cdm Sources