-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
ExistRepository.java
271 lines (244 loc) · 11.4 KB
/
ExistRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/****************************************************************************/
/* File: ExistRepository.java */
/* Author: F. Georges - H2O Consulting */
/* Date: 2010-09-22 */
/* Tags: */
/* Copyright (c) 2010 Florent Georges (see end of file.) */
/* ------------------------------------------------------------------------ */
package org.exist.repo;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import javax.xml.transform.stream.StreamSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.storage.BrokerPool;
import org.exist.storage.BrokerPoolService;
import org.exist.storage.NativeBroker;
import org.exist.util.Configuration;
import org.exist.util.FileUtils;
import org.exist.xquery.Module;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.expath.pkg.repo.FileSystemStorage;
import org.expath.pkg.repo.FileSystemStorage.FileSystemResolver;
import org.expath.pkg.repo.Package;
import org.expath.pkg.repo.Packages;
import org.expath.pkg.repo.PackageException;
import org.expath.pkg.repo.Repository;
import org.expath.pkg.repo.URISpace;
/**
* A repository as viewed by eXist.
*
* @author Florent Georges
* @author Wolfgang Meier
* @author Adam Retter
* @since 2010-09-22
*/
public class ExistRepository extends Observable implements BrokerPoolService {
private final static Logger LOG = LogManager.getLogger(ExistRepository.class);
public final static String EXPATH_REPO_DIR = "expathrepo";
public final static String EXPATH_REPO_DEFAULT = "webapp/WEB-INF/" + EXPATH_REPO_DIR;
/** The wrapped EXPath repository. */
private final Repository myParent;
public ExistRepository(final FileSystemStorage storage) throws PackageException {
this.myParent = new Repository(storage);
myParent.registerExtension(new ExistPkgExtension());
}
public static ExistRepository getRepository(final Configuration config) throws PackageException {
try {
final Path expathDir = getRepositoryDir(config);
LOG.info("Using directory " + expathDir.toAbsolutePath().toString() + " for expath package repository");
final FileSystemStorage storage = new FileSystemStorage(expathDir.toFile());
return new ExistRepository(storage);
} catch(final IOException ioe) {
throw new PackageException("Unable to access EXPath repository", ioe);
}
}
public Repository getParentRepo() {
return myParent;
}
public Module resolveJavaModule(final String namespace, final XQueryContext ctxt) throws XPathException {
final URI uri;
try {
uri = new URI(namespace);
}
catch (final URISyntaxException ex) {
throw new XPathException("Invalid URI: " + namespace, ex);
}
for (final Packages pp : myParent.listPackages()) {
final Package pkg = pp.latest();
final ExistPkgInfo info = (ExistPkgInfo) pkg.getInfo("exist");
if (info != null) {
final String clazz = info.getJava(uri);
if (clazz != null) {
return getModule(clazz, namespace, ctxt);
}
}
}
return null;
}
/**
* Load a module instance from its class name. Check the namespace is consistent.
*/
private Module getModule(final String name, final String namespace, final XQueryContext ctxt)
throws XPathException {
try {
final Class clazz = Class.forName(name);
final Module module = instantiateModule(clazz);
final String ns = module.getNamespaceURI();
if (!ns.equals(namespace)) {
throw new XPathException("The namespace in the Java module " +
"does not match the namespace in the package descriptor: " +
namespace + " - " + ns);
}
return ctxt.loadBuiltInModule(namespace, name);
} catch (final ClassNotFoundException ex) {
throw new XPathException("Cannot find module class from EXPath repository: " + name, ex);
} catch (final InstantiationException | InvocationTargetException | IllegalAccessException ex) {
throw new XPathException("Problem instantiating module class from EXPath repository: " + name, ex);
} catch (final ClassCastException ex) {
throw new XPathException("The class configured in EXPath repository is not a Module: " + name, ex);
} catch (final IllegalArgumentException ex) {
throw new XPathException("Illegal argument passed to the module ctor", ex);
}
}
/**
* Try to instantiate the class using the constructor with a Map parameter,
* or the default constructor.
*/
private Module instantiateModule(final Class<Module> clazz) throws XPathException,
InstantiationException, IllegalAccessException, InvocationTargetException {
try {
final Constructor<Module> ctor = clazz.getConstructor(Map.class);
return ctor.newInstance(Collections.emptyMap());
} catch (final NoSuchMethodException ex) {
try {
final Constructor<Module> ctor = clazz.getConstructor();
return ctor.newInstance();
}
catch (final NoSuchMethodException exx) {
throw new XPathException("Cannot find suitable constructor " +
"for module from expath repository", exx);
}
}
}
public Path resolveXQueryModule(final String namespace) throws XPathException {
final URI uri;
try {
uri = new URI(namespace);
} catch (final URISyntaxException ex) {
throw new XPathException("Invalid URI: " + namespace, ex);
}
for (final Packages pp : myParent.listPackages()) {
final Package pkg = pp.latest();
// FIXME: Rely on having a file system storage, that's probably a bad design!
final FileSystemResolver resolver = (FileSystemResolver) pkg.getResolver();
final ExistPkgInfo info = (ExistPkgInfo) pkg.getInfo("exist");
if (info != null) {
final String f = info.getXQuery(uri);
if (f != null) {
return resolver.resolveComponentAsFile(f).toPath();
}
}
String sysid = null; // declared here to be used in catch
try {
final StreamSource src = pkg.resolve(namespace, URISpace.XQUERY);
if (src != null) {
sysid = src.getSystemId();
return Paths.get(new URI(sysid));
}
} catch (final URISyntaxException ex) {
throw new XPathException("Error parsing the URI of the query library: " + sysid, ex);
} catch (final PackageException ex) {
throw new XPathException("Error resolving the query library: " + namespace, ex);
}
}
return null;
}
public List<URI> getJavaModules() {
final List<URI> modules = new ArrayList<>();
for (final Packages pp : myParent.listPackages()) {
final Package pkg = pp.latest();
final ExistPkgInfo info = (ExistPkgInfo) pkg.getInfo("exist");
if (info != null) {
modules.addAll(info.getJavaModules());
}
}
return modules;
}
public static Path getRepositoryDir(final Configuration config) throws IOException {
final Path dataDir = Optional.ofNullable((Path) config.getProperty(BrokerPool.PROPERTY_DATA_DIR))
.orElse(Paths.get(NativeBroker.DEFAULT_DATA_DIR));
final Path expathDir = dataDir.resolve(EXPATH_REPO_DIR);
if(!Files.exists(expathDir)) {
moveOldRepo(config.getExistHome(), expathDir);
}
Files.createDirectories(expathDir);
return expathDir;
}
private static void moveOldRepo(final Optional<Path> home, final Path newRepo) {
final Path repo_dir = home.map(h -> {
if(FileUtils.fileName(h).equals("WEB-INF")) {
return h.resolve(EXPATH_REPO_DIR);
} else {
return h.resolve( EXPATH_REPO_DEFAULT);
}
}).orElse(Paths.get(System.getProperty("java.io.tmpdir")).resolve(EXPATH_REPO_DIR));
if (Files.isReadable(repo_dir)) {
LOG.info("Found old expathrepo directory. Moving to new default location: " + newRepo.toAbsolutePath().toString());
try {
Files.move(repo_dir, newRepo, StandardCopyOption.ATOMIC_MOVE);
} catch (final IOException e) {
LOG.error("Failed to move old expathrepo directory to new default location. Keeping it.", e);
}
}
}
public void reportAction(final Action action, final String packageURI) {
notifyObservers(new Notification(action, packageURI));
setChanged();
}
public enum Action {
INSTALL, UNINSTALL
}
public final static class Notification {
private final Action action;
private final String packageURI;
public Notification(final Action action, final String packageURI) {
this.action = action;
this.packageURI = packageURI;
}
public Action getAction() {
return action;
}
public String getPackageURI() {
return packageURI;
}
}
}
/* ------------------------------------------------------------------------ */
/* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT. */
/* */
/* The contents of this file are subject to the Mozilla Public License */
/* Version 1.0 (the "License"); you may not use this file except in */
/* compliance with the License. You may obtain a copy of the License at */
/* http://www.mozilla.org/MPL/. */
/* */
/* Software distributed under the License is distributed on an "AS IS" */
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
/* the License for the specific language governing rights and limitations */
/* under the License. */
/* */
/* The Original Code is: all this file. */
/* */
/* The Initial Developer of the Original Code is Florent Georges. */
/* */
/* Contributor(s): Wolfgang Meier, Adam Retter */
/* ------------------------------------------------------------------------ */