|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.client.xmldb.embed; |
|
21 |
| |
|
22 |
| import org.apache.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| import org.apache.xindice.client.xmldb.CommonConfigurable; |
|
25 |
| import org.apache.xindice.core.DBException; |
|
26 |
| import org.apache.xindice.core.Database; |
|
27 |
| import org.apache.xindice.core.FaultCodes; |
|
28 |
| import org.apache.xindice.server.Xindice; |
|
29 |
| import org.apache.xindice.util.Configuration; |
|
30 |
| import org.apache.xindice.util.ReadOnlyException; |
|
31 |
| import org.apache.xindice.util.XindiceException; |
|
32 |
| import org.apache.xindice.util.XindiceRuntimeException; |
|
33 |
| import org.apache.xindice.xml.dom.DOMParser; |
|
34 |
| |
|
35 |
| import org.xmldb.api.base.Collection; |
|
36 |
| import org.xmldb.api.base.ErrorCodes; |
|
37 |
| import org.xmldb.api.base.XMLDBException; |
|
38 |
| |
|
39 |
| import java.io.File; |
|
40 |
| import java.io.FileInputStream; |
|
41 |
| import java.io.FileNotFoundException; |
|
42 |
| import java.io.IOException; |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| public class DatabaseImpl extends CommonConfigurable implements org.xmldb.api.base.Database { |
|
85 |
| |
|
86 |
| private static final Log log = LogFactory.getLog(DatabaseImpl.class); |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| public static final String PROP_XINDICE_DB_HOME = "db-home"; |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| public static final String SYSPROP_XINDICE_DB_HOME = Xindice.PROP_XINDICE_DB_HOME; |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| public static final String PROP_XINDICE_CONFIGURATION = "configuration"; |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| public static final String SYSPROP_XINDICE_CONFIGURATION = Xindice.PROP_XINDICE_CONFIGURATION; |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| public static final String PROP_XINDICE_MANAGED = "db-managed"; |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| public static final String SYSPROP_XINDICE_MANAGED = "xindice.db.managed"; |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| public static final String DRIVER_NAME = "xindice-embed"; |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| private static final String CONFORMANCE_LEVEL = "0"; |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| private String configFile; |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
0
| public DatabaseImpl() {
|
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
3
| public DatabaseImpl(CommonConfigurable config) {
|
|
155 |
3
| super(config);
|
|
156 |
| } |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
2020
| private Database getDatabase(String dbName, String uri) throws XMLDBException {
|
|
168 |
2020
| Database database = Database.getDatabase(dbName);
|
|
169 |
2020
| if (database == null && !isManaged()) {
|
|
170 |
4
| Configuration dbConfig = getConfiguration(dbName);
|
|
171 |
4
| if (log.isDebugEnabled()) {
|
|
172 |
0
| log.debug("Mounting database: '" + dbName + "'");
|
|
173 |
| } |
|
174 |
| |
|
175 |
4
| if (dbConfig == null) {
|
|
176 |
2
| throw new XMLDBException(ErrorCodes.NO_SUCH_DATABASE,
|
|
177 |
| "Database '" + dbName + "' not found: " + uri); |
|
178 |
| } |
|
179 |
| |
|
180 |
2
| try {
|
|
181 |
2
| database = Database.getDatabase(dbConfig);
|
|
182 |
| } catch (DBException e) { |
|
183 |
0
| throw FaultCodes.createXMLDBException(e);
|
|
184 |
| } |
|
185 |
2
| if (log.isDebugEnabled()) {
|
|
186 |
0
| log.info("Mounted database: '" + database.getName() + "'");
|
|
187 |
| } |
|
188 |
| } |
|
189 |
| |
|
190 |
2018
| return database;
|
|
191 |
| } |
|
192 |
| |
|
193 |
4
| protected boolean isManaged() {
|
|
194 |
4
| String managed = null;
|
|
195 |
4
| try {
|
|
196 |
| |
|
197 |
4
| managed = getProperty(PROP_XINDICE_MANAGED);
|
|
198 |
| } catch (XMLDBException e) { |
|
199 |
| |
|
200 |
| } |
|
201 |
4
| if (managed == null) {
|
|
202 |
| |
|
203 |
4
| managed = System.getProperty(SYSPROP_XINDICE_MANAGED);
|
|
204 |
| } |
|
205 |
4
| return Boolean.valueOf(managed).booleanValue();
|
|
206 |
| } |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
4
| protected Configuration getConfiguration(String dbName) throws XMLDBException {
|
|
216 |
4
| Configuration config = loadConfiguration();
|
|
217 |
| |
|
218 |
| |
|
219 |
4
| Configuration[] roots = config.getChildren("root-collection");
|
|
220 |
4
| config = null;
|
|
221 |
4
| for (int i = 0; i < roots.length; i++) {
|
|
222 |
4
| if (dbName.equals(roots[i].getAttribute(Database.NAME))) {
|
|
223 |
2
| config = roots[i];
|
|
224 |
2
| break;
|
|
225 |
| } |
|
226 |
| } |
|
227 |
| |
|
228 |
4
| if (config == null) {
|
|
229 |
2
| return null;
|
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
2
| String dbRoot = config.getAttribute(Database.DBROOT, Database.DBROOT_DEFAULT);
|
|
234 |
2
| if (!new File(dbRoot).isAbsolute()) {
|
|
235 |
| |
|
236 |
2
| String home = null;
|
|
237 |
2
| try {
|
|
238 |
| |
|
239 |
2
| home = getProperty(PROP_XINDICE_DB_HOME);
|
|
240 |
| } catch (XMLDBException e) { |
|
241 |
| |
|
242 |
| } |
|
243 |
2
| if (home == null) {
|
|
244 |
| |
|
245 |
2
| home = System.getProperty(SYSPROP_XINDICE_DB_HOME);
|
|
246 |
| } |
|
247 |
| |
|
248 |
2
| if (home != null) {
|
|
249 |
2
| try {
|
|
250 |
2
| dbRoot = new File(home + File.separator + dbRoot).getCanonicalPath();
|
|
251 |
| } catch (IOException e) { |
|
252 |
0
| log.warn("getCanonicalPath failed", e);
|
|
253 |
0
| dbRoot = new File(home + File.separator + dbRoot).getAbsolutePath();
|
|
254 |
| } |
|
255 |
0
| } else if (configFile != null) {
|
|
256 |
0
| dbRoot = new File(configFile).getAbsoluteFile().getParent() + File.separator + dbRoot;
|
|
257 |
| } else { |
|
258 |
0
| log.warn("The database configuration file is not specified and there was no "
|
|
259 |
| + SYSPROP_XINDICE_DB_HOME + " property set, " |
|
260 |
| + "so Xindice was unable to determine a database location. " |
|
261 |
| + "Database will be created relative to the current directory."); |
|
262 |
0
| try {
|
|
263 |
0
| dbRoot = new File("." + File.separator + dbRoot).getCanonicalPath();
|
|
264 |
| } catch (IOException e) { |
|
265 |
0
| log.warn("getCanonicalPath failed", e);
|
|
266 |
0
| dbRoot = new File("." + File.separator + dbRoot).getAbsolutePath();
|
|
267 |
| } |
|
268 |
| } |
|
269 |
2
| try {
|
|
270 |
2
| config.setAttribute(Database.DBROOT, dbRoot);
|
|
271 |
| } catch (ReadOnlyException e) { |
|
272 |
| |
|
273 |
| } |
|
274 |
| } |
|
275 |
| |
|
276 |
2
| return config;
|
|
277 |
| } |
|
278 |
| |
|
279 |
4
| private Configuration loadConfiguration() throws XMLDBException {
|
|
280 |
4
| Configuration config;
|
|
281 |
| |
|
282 |
4
| if (configFile == null) {
|
|
283 |
2
| try {
|
|
284 |
| |
|
285 |
2
| configFile = getProperty(PROP_XINDICE_CONFIGURATION);
|
|
286 |
| } catch (XMLDBException e) { |
|
287 |
| |
|
288 |
| } |
|
289 |
2
| if (configFile == null) {
|
|
290 |
| |
|
291 |
2
| configFile = System.getProperty(SYSPROP_XINDICE_CONFIGURATION);
|
|
292 |
| } |
|
293 |
2
| if ("".equals(configFile)) {
|
|
294 |
0
| configFile = null;
|
|
295 |
| } |
|
296 |
| } |
|
297 |
| |
|
298 |
4
| if (configFile != null) {
|
|
299 |
4
| if (log.isInfoEnabled()) {
|
|
300 |
0
| log.info("Specified configuration file: '" + configFile + "'");
|
|
301 |
| } |
|
302 |
4
| try {
|
|
303 |
4
| FileInputStream configXMLFile = new FileInputStream(configFile);
|
|
304 |
4
| config = new Configuration(DOMParser.toDocument(configXMLFile), false);
|
|
305 |
| } catch (XindiceException e) { |
|
306 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR,
|
|
307 |
| "Unable to parse database configuration file: " + configFile, e); |
|
308 |
| } catch (FileNotFoundException e) { |
|
309 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR,
|
|
310 |
| "Database configuration file not found: " + configFile); |
|
311 |
| } |
|
312 |
| } else { |
|
313 |
0
| if (log.isInfoEnabled()) {
|
|
314 |
0
| log.info("No configuration file specified, going with the default configuration");
|
|
315 |
| } |
|
316 |
| |
|
317 |
0
| try {
|
|
318 |
0
| config = new Configuration(DOMParser.toDocument(Xindice.DEFAULT_CONFIGURATION), false);
|
|
319 |
| } catch (XindiceException e) { |
|
320 |
0
| throw new XindiceRuntimeException("Unable to parse default database configuration", e);
|
|
321 |
| } |
|
322 |
| } |
|
323 |
| |
|
324 |
4
| return config;
|
|
325 |
| } |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
2023
| public boolean acceptsURI(String uri) throws XMLDBException {
|
|
340 |
2023
| return ((uri != null) && uri.startsWith(getName() + "://"));
|
|
341 |
| } |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
2020
| public Collection getCollection(String uri, String userName, String password) throws XMLDBException {
|
|
367 |
| |
|
368 |
| |
|
369 |
2020
| if (!acceptsURI(uri)) {
|
|
370 |
0
| throw new XMLDBException(ErrorCodes.INVALID_URI,
|
|
371 |
| "Invalid URL: " + uri); |
|
372 |
| } |
|
373 |
| |
|
374 |
| |
|
375 |
2020
| uri = uri.substring(getName().length() + 3);
|
|
376 |
| |
|
377 |
| |
|
378 |
2020
| int firstSlash = uri.indexOf('/');
|
|
379 |
2020
| if (firstSlash == -1) {
|
|
380 |
0
| throw new XMLDBException(ErrorCodes.INVALID_URI,
|
|
381 |
| "Invalid URL (must have '/'): " + uri); |
|
382 |
| } |
|
383 |
| |
|
384 |
| |
|
385 |
2020
| String collPath = uri.substring(firstSlash);
|
|
386 |
2020
| if (!collPath.startsWith("/")) {
|
|
387 |
0
| throw new XMLDBException(ErrorCodes.INVALID_URI,
|
|
388 |
| "Invalid URL (collection name must start with '/'): " + uri); |
|
389 |
| } |
|
390 |
| |
|
391 |
| |
|
392 |
2020
| int colIndex = collPath.indexOf('/', 1);
|
|
393 |
| |
|
394 |
| |
|
395 |
2020
| String dbName = collPath.substring(1);
|
|
396 |
| |
|
397 |
| |
|
398 |
2020
| if (colIndex != -1) {
|
|
399 |
2010
| dbName = collPath.substring(1, colIndex);
|
|
400 |
| } |
|
401 |
| |
|
402 |
2020
| Database database = getDatabase(dbName, uri);
|
|
403 |
2018
| if (database == null) {
|
|
404 |
0
| throw new XMLDBException(ErrorCodes.NO_SUCH_DATABASE,
|
|
405 |
| "Database '" + dbName + "' not found. URL: " + uri); |
|
406 |
| } |
|
407 |
| |
|
408 |
2018
| try {
|
|
409 |
2018
| return new CollectionImpl(database, collPath);
|
|
410 |
| } catch (XMLDBException e) { |
|
411 |
2
| if (e.errorCode == ErrorCodes.NO_SUCH_COLLECTION) {
|
|
412 |
| |
|
413 |
2
| return null;
|
|
414 |
| } |
|
415 |
0
| throw e;
|
|
416 |
| } |
|
417 |
| } |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
4043
| public String getName() throws XMLDBException {
|
|
429 |
4043
| return DRIVER_NAME;
|
|
430 |
| } |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
0
| public String[] getNames() throws XMLDBException {
|
|
441 |
0
| return new String[]{ getName() };
|
|
442 |
| } |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
0
| public String getConformanceLevel() throws XMLDBException {
|
|
455 |
0
| return CONFORMANCE_LEVEL;
|
|
456 |
| } |
|
457 |
| } |