1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.util.Properties;
26
27 import org.apache.commons.jxpath.util.ClassLoaderUtil;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public abstract class JXPathContextFactory {
46
47
48 public static final String FACTORY_NAME_PROPERTY =
49 "org.apache.commons.jxpath.JXPathContextFactory";
50
51
52 private static final String DEFAULT_FACTORY_CLASS =
53 "org.apache.commons.jxpath.ri.JXPathContextFactoryReferenceImpl";
54
55
56
57
58
59 private static String factoryImplName = null;
60
61
62
63
64 protected JXPathContextFactory () {
65
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public static JXPathContextFactory newInstance() {
105 if (factoryImplName == null) {
106 factoryImplName =
107 findFactory(FACTORY_NAME_PROPERTY, DEFAULT_FACTORY_CLASS);
108 }
109
110 JXPathContextFactory factoryImpl;
111 try {
112 Class clazz = ClassLoaderUtil.getClass(factoryImplName, true);
113 factoryImpl = (JXPathContextFactory) clazz.newInstance();
114 }
115 catch (ClassNotFoundException cnfe) {
116 throw new JXPathContextFactoryConfigurationError(cnfe);
117 }
118 catch (IllegalAccessException iae) {
119 throw new JXPathContextFactoryConfigurationError(iae);
120 }
121 catch (InstantiationException ie) {
122 throw new JXPathContextFactoryConfigurationError(ie);
123 }
124 return factoryImpl;
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public abstract JXPathContext newContext(
138 JXPathContext parentContext,
139 Object contextBean);
140
141
142
143
144
145
146
147
148 private static boolean debug = false;
149 static {
150 try {
151 debug = System.getProperty("jxpath.debug") != null;
152 }
153 catch (SecurityException se) {
154
155 }
156 }
157
158
159
160
161
162
163
164
165
166 private static String findFactory(String property, String defaultFactory) {
167
168 try {
169 String systemProp = System.getProperty(property);
170 if (systemProp != null) {
171 if (debug) {
172 System.err.println(
173 "JXPath: found system property" + systemProp);
174 }
175 return systemProp;
176 }
177
178 }
179 catch (SecurityException se) {
180
181 }
182
183
184 try {
185 String javah = System.getProperty("java.home");
186 String configFile =
187 javah
188 + File.separator
189 + "lib"
190 + File.separator
191 + "jxpath.properties";
192 File f = new File(configFile);
193 if (f.exists()) {
194 Properties props = new Properties();
195 FileInputStream fis = new FileInputStream(f);
196 try {
197 props.load(fis);
198 }
199 finally {
200 if (fis != null) {
201 try {
202 fis.close();
203 }
204 catch (IOException e) {
205
206 }
207 }
208 }
209 String factory = props.getProperty(property);
210 if (factory != null) {
211 if (debug) {
212 System.err.println(
213 "JXPath: found java.home property " + factory);
214 }
215 return factory;
216 }
217 }
218 }
219 catch (IOException ex) {
220 if (debug) {
221 ex.printStackTrace();
222 }
223 }
224
225 String serviceId = "META-INF/services/" + property;
226
227 try {
228 ClassLoader cl = JXPathContextFactory.class.getClassLoader();
229 InputStream is = null;
230 if (cl == null) {
231 is = ClassLoader.getSystemResourceAsStream(serviceId);
232 }
233 else {
234 is = cl.getResourceAsStream(serviceId);
235 }
236
237 if (is != null) {
238 if (debug) {
239 System.err.println("JXPath: found " + serviceId);
240 }
241 BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
242
243 String factory = null;
244 try {
245 factory = rd.readLine();
246 }
247 finally {
248 try {
249 rd.close();
250 }
251 catch (IOException e) {
252
253 }
254 }
255
256 if (factory != null && !"".equals(factory)) {
257 if (debug) {
258 System.err.println(
259 "JXPath: loaded from services: " + factory);
260 }
261 return factory;
262 }
263 }
264 }
265 catch (Exception ex) {
266 if (debug) {
267 ex.printStackTrace();
268 }
269 }
270 return defaultFactory;
271 }
272 }