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
|
|
20
|
import org.apache.logging.log4j.LogManager;
|
21
|
import org.apache.logging.log4j.Logger;
|
22
|
import org.jdom.Document;
|
23
|
import org.jdom.Element;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
|
26
|
import eu.etaxonomy.cdm.common.XmlHelp;
|
27
|
|
28
|
/**
|
29
|
* Utility class which manages the persistent source settings
|
30
|
* in the datasource datasource config
|
31
|
*
|
32
|
* @author cmathew
|
33
|
*/
|
34
|
public class CdmPersistentSourceUtils {
|
35
|
|
36
|
private static final Logger logger = LogManager.getLogger(CdmPersistentSourceUtils.class);
|
37
|
|
38
|
/**
|
39
|
* Returns the directory containing the datasource config file
|
40
|
*/
|
41
|
public static String getResourceDirectory(){
|
42
|
try {
|
43
|
File f = CdmApplicationUtils.getWritableResourceDir();
|
44
|
return f.getPath();
|
45
|
} catch (IOException e) {
|
46
|
logger.error(e);
|
47
|
throw new RuntimeException(e);
|
48
|
}
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Returns the datasource config file input stream.
|
53
|
*
|
54
|
* @return data source config file input stream
|
55
|
*/
|
56
|
public static FileInputStream getCdmSourceInputStream(){
|
57
|
String dir = CdmPersistentSourceUtils.getResourceDirectory();
|
58
|
File file = new File(dir + File.separator + CdmPersistentXMLSource.CDMSOURCE_FILE_NAME);
|
59
|
return fileInputStream(file);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Returns the datasource config file outputStream.
|
64
|
*
|
65
|
* @return data source config file outputStream
|
66
|
*/
|
67
|
public static FileOutputStream getCdmSourceOutputStream(){
|
68
|
String dir = CdmPersistentSourceUtils.getResourceDirectory();
|
69
|
File file = new File(dir + File.separator + CdmPersistentXMLSource.CDMSOURCE_FILE_NAME);
|
70
|
return fileOutputStream(file);
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Returns a FileInputStream object from a File object
|
75
|
*
|
76
|
* @param file
|
77
|
* @return FileInputStream object
|
78
|
*/
|
79
|
public static FileInputStream fileInputStream(File file){
|
80
|
try {
|
81
|
FileInputStream fis = new FileInputStream(file);
|
82
|
return fis;
|
83
|
} catch (FileNotFoundException e) {
|
84
|
logger.warn("File " + (file == null?"null":file.getAbsolutePath()) + " does not exist in the file system");
|
85
|
return null;
|
86
|
}
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Returns a FileOututStream object from a File object
|
91
|
*
|
92
|
* @param file
|
93
|
* @return FileOututStream object
|
94
|
*/
|
95
|
public static FileOutputStream fileOutputStream(File file){
|
96
|
try {
|
97
|
FileOutputStream fos = new FileOutputStream(file);
|
98
|
return fos;
|
99
|
} catch (FileNotFoundException e) {
|
100
|
logger.warn("File " + (file == null?"null":file.getAbsolutePath()) + " does not exist in the file system");
|
101
|
return null;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
|
106
|
/**
|
107
|
* Returns the jdom Element representing the data source bean in the config file.
|
108
|
*
|
109
|
* @param beanName , of the element to be retrieved
|
110
|
* @return jdom Element representing the data source bean in the config file.
|
111
|
*/
|
112
|
public static Element getCdmSourceBeanXml(String beanName){
|
113
|
FileInputStream inStream = getCdmSourceInputStream();
|
114
|
Element root = getBeansRoot(inStream);
|
115
|
if (root == null){
|
116
|
return null;
|
117
|
}else{
|
118
|
Element xmlBean = XmlHelp.getFirstAttributedChild(root, "bean", "id", beanName);
|
119
|
if (xmlBean == null){
|
120
|
//TODO warn or info
|
121
|
logger.debug("Unknown Element 'bean id=" + beanName + "' ");
|
122
|
}
|
123
|
return xmlBean;
|
124
|
}
|
125
|
}
|
126
|
|
127
|
|
128
|
/**
|
129
|
* Returns the jdom Element corresponding to the cdm source name and type in the config file.
|
130
|
*
|
131
|
* @param cdmSourceName , of the element to be retrieved
|
132
|
* @param postFix , indicating the type of cdm source
|
133
|
* @return jdom Element corresponding to the cdm source name and type in the config file.
|
134
|
*/
|
135
|
public static Element getCdmSourceBeanXml(String cdmSourceName, String postFix){
|
136
|
return getCdmSourceBeanXml(getBeanName(cdmSourceName, postFix));
|
137
|
}
|
138
|
|
139
|
|
140
|
|
141
|
/**
|
142
|
* Converts input parameters to bean name.
|
143
|
*
|
144
|
* @param cdmSourceName , of the element to be retrieved
|
145
|
* @param postFix , indicating the type of cdm source
|
146
|
* @return bean name corresponding to the cdm source name and type in the config file.
|
147
|
*/
|
148
|
public static String getBeanName(String cdmSourceName, String postFix){
|
149
|
return cdmSourceName == null? null : cdmSourceName + postFix;
|
150
|
}
|
151
|
|
152
|
|
153
|
/**
|
154
|
* Deletes a CDM persistent source from the source config file.
|
155
|
*
|
156
|
* @param cdmPersistentSource , CDM persistent source object
|
157
|
*/
|
158
|
public static void delete (ICdmPersistentSource cdmPersistentSource) {
|
159
|
delete(cdmPersistentSource.getBeanName());
|
160
|
}
|
161
|
|
162
|
|
163
|
/**
|
164
|
* Deletes a CDM persistent source from the source config file.
|
165
|
*
|
166
|
* @param beanName , CDM persistent source bean name
|
167
|
*/
|
168
|
public static void delete (String beanName){
|
169
|
Element bean = getCdmSourceBeanXml(beanName);
|
170
|
if (bean != null){
|
171
|
Document doc = bean.getDocument();
|
172
|
bean.detach();
|
173
|
saveToXml(doc,
|
174
|
CdmPersistentSourceUtils.getCdmSourceOutputStream(),
|
175
|
XmlHelp.prettyFormat );
|
176
|
}
|
177
|
}
|
178
|
|
179
|
|
180
|
/**
|
181
|
* Returns the property value of a CDM persistent source object bean
|
182
|
* from the source config file.
|
183
|
*
|
184
|
* @param bean , CDM persistent source object bean
|
185
|
* @param property , whose value is to be retrieved
|
186
|
* @return value of requested property
|
187
|
*/
|
188
|
public static String getPropertyValue(Element bean, String property){
|
189
|
Element driverProp = XmlHelp.getFirstAttributedChild(bean, "property", "name", property);
|
190
|
if (driverProp == null){
|
191
|
logger.debug("Unknown property: " + property);
|
192
|
return null;
|
193
|
}else{
|
194
|
String strProperty = driverProp.getAttributeValue("value");
|
195
|
return strProperty;
|
196
|
}
|
197
|
}
|
198
|
}
|