Project

General

Profile

Download (4.98 KB) Statistics
| Branch: | Tag: | Revision:
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

    
10
package eu.etaxonomy.cdm.config;
11

    
12
import java.util.ArrayList;
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Properties;
16

    
17
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
18
import org.jdom.Attribute;
19
import org.jdom.Element;
20

    
21
import eu.etaxonomy.cdm.common.XmlHelp;
22

    
23
/**
24
 * Cdm Source class which represents a persisted CDM source object
25
 * as configured in the CDM sources config file.
26
 *
27
 * @author cmathew
28
 *
29
 */
30
public class CdmPersistentXMLSource {
31
	private static final Logger logger = LogManager.getLogger(CdmPersistentXMLSource.class);
32

    
33

    
34
	/**
35
	 * CDM sources config file name
36
	 */
37
	public final static String CDMSOURCE_FILE_NAME = "cdm.datasources.xml";
38

    
39
	/**
40
	 * Directory path of the template CDM sources config file - this file is used
41
	 * in the case of the very first call to persist sources see {@link eu.etaxonomy.cdm.api.application.CdmApplicationUtils#getWritableResourceDir()}
42
	 */
43
	public final static String CDMSOURCE_PATH = "/eu/etaxonomy/cdm/";
44

    
45
	/**
46
	 * jDom Element represeting the CDM source as stored in the source config file
47
	 */
48
	private Element bean;
49

    
50

    
51
	/**
52
	 * Post fix represeting the type of source (data / remote)
53
	 */
54
	private String postFix;
55

    
56
	/**
57
	 * Enumeration containg all possible properties of all types of
58
	 * CDM Sources
59
	 *
60
	 *
61
	 */
62
	public enum CdmSourceProperties {
63
		URL,
64
		SERVER,
65
		PORT,
66
		FILEPATH,
67
		CONTEXTPATH,
68
		USERNAME,
69
		PASSWORD,
70
		DRIVER_CLASS,
71
		DATABASE,
72
		MODE;
73

    
74
		@Override
75
		public String toString(){
76
			switch (this){
77
				case URL:
78
					return "url";
79
				case SERVER:
80
					return "server";
81
				case PORT:
82
					return "port";
83
				case FILEPATH:
84
					return "filePath";
85
				case CONTEXTPATH:
86
					return "contextPath";
87
				case USERNAME:
88
					return "username";
89
				case PASSWORD:
90
					return "password";
91
				case DRIVER_CLASS:
92
					return "driverClassName";
93
				case DATABASE:
94
					return "database";
95
				case MODE:
96
					return "mode";
97
				default:
98
					throw new IllegalArgumentException( "Unknown enumeration type" );
99
			}
100
		}
101
	}
102

    
103
	private String cdmSourceName;
104

    
105
	/**
106
	 * Constructor which uses the given CDM source name and post fix to initialze the
107
	 * jDom element from the source config file.
108
	 *
109
	 * @param cdmSourceName
110
	 * @param postFix
111
	 */
112
	private CdmPersistentXMLSource(String cdmSourceName, String postFix) {
113
		this.cdmSourceName = cdmSourceName;
114
		this.postFix = postFix;
115
		bean = CdmPersistentSourceUtils.getCdmSourceBeanXml(cdmSourceName, postFix);
116
	}
117

    
118
	/**
119
	 * Constructor which uses the given CDM source name and post fix to initialze the
120
	 * jDom element from the source config file.
121
	 *
122
	 * @param strDataSourceName
123
	 * @param postFix
124
	 * @return new CdmPersistentXMLSource object
125
	 */
126
	public final static CdmPersistentXMLSource NewInstance(String strDataSourceName, String postFix) {
127
		return new CdmPersistentXMLSource(strDataSourceName, postFix);
128
	}
129

    
130

    
131
	/**
132
	 * Returns the loaded jDom element representing this object
133
	 *
134
	 * @return
135
	 */
136
	public Element getElement() {
137
		return bean;
138
	}
139

    
140
	/**
141
	 * Returns the name of the bean Element in the cdm source xml config file.
142
	 * @return bean name
143
	 */
144
	public String getBeanName(){
145
		return CdmPersistentSourceUtils.getBeanName(cdmSourceName, postFix);
146
	}
147

    
148
	/**
149
	 * Returns the list of attributes of this CDM source
150
	 * that are defined in the cdm source xml config file.
151
	 * @return
152
	 */
153
	@SuppressWarnings("unchecked")
154
	public List<Attribute> getCdmSourceAttributes(){
155
		List<Attribute> result = new ArrayList<Attribute>();
156
		if (bean == null){
157
			return null;
158
		}else{
159
			result = bean.getAttributes();
160
		}
161
		return result;
162
	}
163

    
164
	/**
165
	 * Returns a defined property of the cdm source xml config file.
166
	 * @return the property of the cdm source or null if the datasource bean or the property does not exist.
167
	 */
168
	public String getCdmSourceProperty(String property){
169

    
170
		if (bean == null){
171
			return null;
172
		}else{
173
			Element elProperty = XmlHelp.getFirstAttributedChild(bean, "property", "name", property);
174
			if (elProperty == null){
175
				logger.warn("Unknown property: " + property);
176
		    	return null;
177
			}else{
178
				String strValue = elProperty.getAttributeValue("value");
179
				return strValue;
180
			}
181
		}
182
	}
183

    
184
	/**
185
	 * Returns the list of properties that are defined in the
186
	 * cdm source xml config file.
187
	 *
188
	 * @return
189
	 */
190
	public Properties getCdmSourceProperties(){
191
		Properties result = new Properties();
192
		if (bean == null){
193
			return null;
194
		}else{
195
			List<Element> elProperties = XmlHelp.getAttributedChildList(bean, "property", "name");
196
			Iterator<Element> iterator = elProperties.iterator();
197
			while(iterator.hasNext()){
198
				Element next = iterator.next();
199
				String strName = next.getAttributeValue("name");
200
				String strValue = next.getAttributeValue("value");
201
				result.put(strName, strValue);
202
			}
203
		}
204
		return result;
205
	}
206

    
207
}
(5-5/11)