Skip to content

Commit 8bd8732

Browse files
authored
Merge pull request #5012 from evolvedbinary/6.x.x/hotfix/expath-import-xquery-transient
[6.x.x] Fix an issue with XQuery transient imports within EXPath Package
2 parents 7430778 + a79be27 commit 8bd8732

File tree

7 files changed

+82
-23
lines changed

7 files changed

+82
-23
lines changed

exist-core/src/main/java/org/exist/client/InteractiveClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ public boolean run(final String args[]) throws Exception {
21902190
configFile = Optional.ofNullable(ConfigurationHelper.lookup((String) CONF_XML.get("")));
21912191
}
21922192
}
2193-
configFile.ifPresent(value -> properties.setProperty(CONFIGURATION, value.toAbsolutePath().toString()));
2193+
configFile.ifPresent(value -> properties.setProperty(CONFIGURATION, value.toString()));
21942194

21952195
properties.putAll(loadClientProperties());
21962196

exist-core/src/main/java/org/exist/repo/ExistRepository.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323

2424
import org.apache.logging.log4j.LogManager;
2525
import org.apache.logging.log4j.Logger;
26+
import org.exist.dom.persistent.BinaryDocument;
27+
import org.exist.security.PermissionDeniedException;
28+
import org.exist.source.DBSource;
2629
import org.exist.storage.BrokerPool;
2730
import org.exist.storage.BrokerPoolService;
2831
import org.exist.storage.BrokerPoolServiceException;
32+
import org.exist.storage.DBBroker;
2933
import org.exist.storage.NativeBroker;
3034
import org.exist.util.Configuration;
3135
import org.exist.util.FileUtils;
36+
import org.exist.xmldb.XmldbURI;
3237
import org.exist.xquery.ErrorCodes;
3338
import org.exist.xquery.Expression;
3439
import org.exist.xquery.Module;
@@ -41,7 +46,9 @@
4146
import org.expath.pkg.repo.PackageException;
4247
import org.expath.pkg.repo.Repository;
4348
import org.expath.pkg.repo.URISpace;
49+
import org.w3c.dom.Document;
4450

51+
import javax.annotation.Nullable;
4552
import javax.xml.transform.Source;
4653
import javax.xml.transform.stream.StreamSource;
4754
import java.io.IOException;
@@ -273,6 +280,38 @@ public Path resolveXQueryModule(final String namespace) throws XPathException {
273280
return null;
274281
}
275282

283+
/**
284+
* Attempt to lookup an XQuery from the filesystem in the database.
285+
*
286+
* @param broker the database broker
287+
* @param xqueryPath the path to the xquery within the EXPath filesystem repo.
288+
*
289+
* @return the database source for the xquery, or null.
290+
*/
291+
public @Nullable org.exist.source.Source resolveStoredXQueryModuleFromDb(final DBBroker broker, final Path xqueryPath) throws PermissionDeniedException {
292+
if (!xqueryPath.startsWith(expathDir)) {
293+
return null;
294+
}
295+
296+
final String relXQueryPath = expathDir.relativize(xqueryPath).toString();
297+
298+
// 1. attempt to locate it within a library
299+
XmldbURI xqueryDbPath = XmldbURI.create("xmldb:exist:///db/system/repo/" + relXQueryPath);
300+
@Nullable Document doc = broker.getXMLResource(xqueryDbPath);
301+
if (doc != null && doc instanceof BinaryDocument) {
302+
return new DBSource(broker, (BinaryDocument) doc, false);
303+
}
304+
305+
// 2. attempt to locate it within an app
306+
xqueryDbPath = XmldbURI.create("xmldb:exist:///db/apps/" + relXQueryPath);
307+
doc = broker.getXMLResource(xqueryDbPath);
308+
if (doc != null && doc instanceof BinaryDocument) {
309+
return new DBSource(broker, (BinaryDocument) doc, false);
310+
}
311+
312+
return null;
313+
}
314+
276315
public Source resolveXSLTModule(final String namespace) throws PackageException {
277316
for (final Packages pp : myParent.listPackages()) {
278317
final Package pkg = pp.latest();

exist-core/src/main/java/org/exist/util/Configuration.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ public Configuration(String configFilename, Optional<Path> existHomeDirname) thr
127127
InputStream is = null;
128128
try {
129129

130+
existHomeDirname = existHomeDirname.map(Path::normalize);
131+
130132
if(configFilename == null) {
131133
// Default file name
132134
configFilename = DatabaseImpl.CONF_XML;
@@ -164,7 +166,6 @@ public Configuration(String configFilename, Optional<Path> existHomeDirname) thr
164166
}
165167
}
166168

167-
168169
Path configFile = Paths.get(configFilename);
169170

170171
if(!configFile.isAbsolute() && existHome.isPresent()) {
@@ -863,15 +864,15 @@ private void configureBackend( final Optional<Path> dbHome, Element con ) throws
863864
final String dataFiles = getConfigAttributeValue( con, BrokerPool.DATA_DIR_ATTRIBUTE );
864865

865866
if (dataFiles != null) {
866-
final Path df = ConfigurationHelper.lookup( dataFiles, dbHome );
867+
final Path df = ConfigurationHelper.lookup(dataFiles, dbHome);
867868
if (!Files.isReadable(df)) {
868869
try {
869870
Files.createDirectories(df);
870871
} catch (final IOException ioe) {
871-
throw new DatabaseConfigurationException("cannot read data directory: " + df.toAbsolutePath().toString(), ioe);
872+
throw new DatabaseConfigurationException("cannot read data directory: " + df, ioe);
872873
}
873874
}
874-
config.put(BrokerPool.PROPERTY_DATA_DIR, df.toAbsolutePath());
875+
config.put(BrokerPool.PROPERTY_DATA_DIR, df);
875876
LOG.debug(BrokerPool.PROPERTY_DATA_DIR + ": {}", config.get(BrokerPool.PROPERTY_DATA_DIR));
876877
}
877878

@@ -1090,9 +1091,9 @@ private void configureRecovery( final Optional<Path> dbHome, Element recovery )
10901091
final Path rf = ConfigurationHelper.lookup( option, dbHome );
10911092

10921093
if(!Files.isReadable(rf)) {
1093-
throw new DatabaseConfigurationException( "cannot read data directory: " + rf.toAbsolutePath());
1094+
throw new DatabaseConfigurationException( "cannot read data directory: " + rf);
10941095
}
1095-
setProperty(Journal.PROPERTY_RECOVERY_JOURNAL_DIR, rf.toAbsolutePath());
1096+
setProperty(Journal.PROPERTY_RECOVERY_JOURNAL_DIR, rf);
10961097
LOG.debug(Journal.PROPERTY_RECOVERY_JOURNAL_DIR + ": {}", config.get(Journal.PROPERTY_RECOVERY_JOURNAL_DIR));
10971098
}
10981099

exist-core/src/main/java/org/exist/util/ConfigurationHelper.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static Optional<Path> getExistHome(final String config) {
109109
final Path userHomeRelativeConfig = userHome.resolve(config);
110110
if (Files.isDirectory(userHome) && Files.isRegularFile(userHomeRelativeConfig)) {
111111
final Path existHome = userHomeRelativeConfig.getParent().normalize();
112-
LOG.debug("Got eXist home: {} from system property 'user.home': {}", existHome.toAbsolutePath().toString(), userHome.toAbsolutePath().toString());
112+
LOG.debug("Got eXist home: {} from system property 'user.home': {}", existHome.toAbsolutePath(), userHome.toAbsolutePath());
113113
return Optional.of(existHome);
114114
}
115115

@@ -119,7 +119,7 @@ public static Optional<Path> getExistHome(final String config) {
119119
final Path userDirRelativeConfig = userDir.resolve(config);
120120
if (Files.isDirectory(userDir) && Files.isRegularFile(userDirRelativeConfig)) {
121121
final Path existHome = userDirRelativeConfig.getParent().normalize();
122-
LOG.debug("Got eXist home: {} from system property 'user.dir': {}", existHome.toAbsolutePath().toString(), userDir.toAbsolutePath().toString());
122+
LOG.debug("Got eXist home: {} from system property 'user.dir': {}", existHome.toAbsolutePath(), userDir.toAbsolutePath());
123123
return Optional.of(existHome);
124124
}
125125

@@ -132,7 +132,7 @@ public static Optional<Path> getExistHome(final String config) {
132132
existHome = Paths.get(new URI(configUrl.getPath())).getParent().getParent().normalize();
133133
LOG.warn("{} file was found on the classpath, but inside a Jar file! Derived EXIST_HOME from Jar's parent folder: {}", config, existHome);
134134
} else {
135-
existHome = Paths.get(configUrl.toURI()).getParent();
135+
existHome = Paths.get(configUrl.toURI()).getParent().normalize();
136136
if (FileUtils.fileName(existHome).equals("etc")) {
137137
existHome = existHome.getParent().normalize();
138138
}
@@ -179,15 +179,14 @@ public static Path lookup(final String path) {
179179
* @return the file handle
180180
*/
181181
public static Path lookup(final String path, final Optional<Path> parent) {
182-
// resolvePath is used for things like ~user/folder
183-
final Path p = decodeUserHome(path);
184-
if (p.isAbsolute()) {
185-
return p;
182+
// attempt to first resolve the path that is used for things like ~user/folder
183+
Path p = decodeUserHome(path);
184+
if (!p.isAbsolute()) {
185+
p = parent
186+
.orElse(getExistHome().orElse(Paths.get(System.getProperty("user.dir"))))
187+
.resolve(path);
186188
}
187-
188-
return parent
189-
.orElse(getExistHome().orElse(Paths.get(System.getProperty("user.dir"))))
190-
.resolve(path);
189+
return p.normalize().toAbsolutePath();
191190
}
192191

193192

exist-core/src/main/java/org/exist/util/SingleInstanceConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public SingleInstanceConfiguration(String configFilename, Optional<Path> existHo
6666
public static Optional<Path> getPath() {
6767
if (!_configFile.isPresent()) {
6868
final Path f = ConfigurationHelper.lookup("conf.xml");
69-
return Optional.of(f.toAbsolutePath());
69+
return Optional.of(f);
7070
}
7171
return _configFile;
7272
}

exist-core/src/main/java/org/exist/xquery/XQueryContext.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.net.URISyntaxException;
3131
import java.nio.charset.Charset;
3232
import java.nio.file.Path;
33+
import java.nio.file.Paths;
3334
import java.util.*;
3435
import java.util.concurrent.CopyOnWriteArrayList;
3536
import java.util.concurrent.atomic.AtomicReference;
@@ -588,9 +589,28 @@ public Optional<ExistRepository> getRepository() {
588589

589590
// use the resolved file or return null
590591
if (resolved != null) {
591-
// build a module object from the file
592-
final Source src = new FileSource(resolved, false);
593-
return compileOrBorrowModule(prefix, namespace, "", src);
592+
593+
String location = "";
594+
595+
try {
596+
597+
// see if the src exists in the database and if so, use that instead
598+
Source src = repo.get().resolveStoredXQueryModuleFromDb(getBroker(), resolved);
599+
if (src != null) {
600+
// NOTE(AR) set the location of the module to import relative to this module's load path - so that transient imports of the imported module will resolve correctly!
601+
location = Paths.get(XmldbURI.create(moduleLoadPath).getCollectionPath()).relativize(Paths.get(((DBSource)src).getDocumentPath().getCollectionPath())).toString();
602+
} else {
603+
// else, fallback to the one from the filesystem
604+
src = new FileSource(resolved, false);
605+
}
606+
607+
// build a module object from the source
608+
final ExternalModule module = compileOrBorrowModule(prefix, namespace, location, src);
609+
return module;
610+
611+
} catch (final PermissionDeniedException e) {
612+
throw new XPathException(e.getMessage(), e);
613+
}
594614
}
595615
}
596616

exist-parent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@
394394
<dependency>
395395
<groupId>org.expath.packaging</groupId>
396396
<artifactId>pkg-java</artifactId>
397-
<version>2.0.0</version>
397+
<version>2.0.1</version>
398398
</dependency>
399399

400400
<dependency>

0 commit comments

Comments
 (0)