Skip to content

Commit 444f26b

Browse files
authored
Merge pull request #4847 from evolvedbinary/refactor/simplify-source-interface
Simplify Source interface
2 parents b8c34d9 + cdb2052 commit 444f26b

File tree

24 files changed

+97
-135
lines changed

24 files changed

+97
-135
lines changed

exist-core/src/main/java/org/exist/http/AuditTrailSessionListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void executeXQuery(String xqueryResourcePath) {
110110
if (LOG.isTraceEnabled()) {
111111
LOG.trace("Resource [{}] exists.", xqueryResourcePath);
112112
}
113-
source = new DBSource(broker, (BinaryDocument) lockedResource.getDocument(), true);
113+
source = new DBSource(pool, (BinaryDocument) lockedResource.getDocument(), true);
114114
} else {
115115
LOG.error("Resource [{}] does not exist.", xqueryResourcePath);
116116
return;

exist-core/src/main/java/org/exist/http/RESTServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ private void executeXQuery(final DBBroker broker, final Txn transaction, final D
15401540
final Properties outputProperties, final String servletPath, final String pathInfo)
15411541
throws XPathException, BadRequestException, PermissionDeniedException {
15421542

1543-
final Source source = new DBSource(broker, (BinaryDocument) resource, true);
1543+
final Source source = new DBSource(broker.getBrokerPool(), (BinaryDocument) resource, true);
15441544
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
15451545
CompiledXQuery compiled = null;
15461546
try {

exist-core/src/main/java/org/exist/http/urlrewrite/XQueryURLRewrite.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ private ModelAndView getFromCache(final String url, final Subject user) throws E
479479
((DBSource) model.getSourceInfo().source).validate(Permission.EXECUTE);
480480
}
481481

482-
if (model.getSourceInfo().source.isValid(broker) != Source.Validity.VALID) {
482+
if (model.getSourceInfo().source.isValid() != Source.Validity.VALID) {
483483
urlCache.remove(url);
484484
return null;
485485
}
@@ -758,7 +758,7 @@ SourceInfo findSourceFromDb(final DBBroker broker, final String basePath, final
758758
}
759759

760760
final String controllerPath = controllerDoc.getCollection().getURI().getRawCollectionPath();
761-
return new SourceInfo(new DBSource(broker, (BinaryDocument) controllerDoc, true), "xmldb:exist://" + controllerPath, controllerPath.substring(locationUri.getCollectionPath().length()));
761+
return new SourceInfo(new DBSource(broker.getBrokerPool(), (BinaryDocument) controllerDoc, true), "xmldb:exist://" + controllerPath, controllerPath.substring(locationUri.getCollectionPath().length()));
762762

763763
} catch (final URISyntaxException e) {
764764
LOG.warn("Bad URI for base path: {}", e.getMessage(), e);
@@ -906,7 +906,7 @@ private SourceInfo getSource(final DBBroker broker, final String moduleLoadPath)
906906
throw new ServletException("XQuery resource: " + query + " is not an XQuery or " +
907907
"declares a wrong mime-type");
908908
}
909-
sourceInfo = new SourceInfo(new DBSource(broker, (BinaryDocument) sourceDoc, true),
909+
sourceInfo = new SourceInfo(new DBSource(broker.getBrokerPool(), (BinaryDocument) sourceDoc, true),
910910
locationUri.toString());
911911
} catch (final PermissionDeniedException e) {
912912
throw new ServletException("permission denied to read module source from " + query);

exist-core/src/main/java/org/exist/scheduler/UserXQueryJob.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public final void execute(final JobExecutionContext jec) throws JobExecutionExce
159159
final XmldbURI pathUri = XmldbURI.create(xqueryResource);
160160
try(final LockedDocument lockedResource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
161161
if (lockedResource != null) {
162-
final Source source = new DBSource(broker, (BinaryDocument) lockedResource.getDocument(), true);
162+
final Source source = new DBSource(pool, (BinaryDocument) lockedResource.getDocument(), true);
163163
executeXQuery(pool, broker, source, params);
164164
return;
165165
}

exist-core/src/main/java/org/exist/security/internal/SMEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private Source getQuerySource(DBBroker broker, String scriptURI, String script)
158158
final XmldbURI pathUri = XmldbURI.create(scriptURI);
159159
try(final LockedDocument lockedResource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
160160
if (lockedResource != null) {
161-
return new DBSource(broker, (BinaryDocument)lockedResource.getDocument(), true);
161+
return new DBSource(broker.getBrokerPool(), (BinaryDocument)lockedResource.getDocument(), true);
162162
}
163163
} catch (final PermissionDeniedException e) {
164164
//XXX: log

exist-core/src/main/java/org/exist/source/BinarySource.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
*/
2222
package org.exist.source;
2323

24-
import org.exist.security.Subject;
25-
import org.exist.storage.DBBroker;
2624
import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
2725

2826
import java.io.*;
@@ -54,12 +52,7 @@ public String type() {
5452
}
5553

5654
@Override
57-
public Validity isValid(final DBBroker broker) {
58-
return Source.Validity.VALID;
59-
}
60-
61-
@Override
62-
public Validity isValid(final Source other) {
55+
public Validity isValid() {
6356
return Source.Validity.VALID;
6457
}
6558

exist-core/src/main/java/org/exist/source/DBSource.java

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323

2424
import java.io.*;
2525

26+
import org.exist.EXistException;
2627
import org.exist.dom.persistent.BinaryDocument;
2728
import org.exist.dom.QName;
2829
import org.exist.dom.persistent.LockedDocument;
2930
import org.exist.security.Permission;
3031
import org.exist.security.PermissionDeniedException;
3132
import org.exist.security.Subject;
3233
import org.exist.security.internal.aider.UnixStylePermissionAider;
34+
import org.exist.storage.BrokerPool;
3335
import org.exist.storage.DBBroker;
3436
import org.exist.storage.lock.Lock.LockMode;
3537
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
@@ -49,11 +51,11 @@ public class DBSource extends AbstractSource {
4951
private final long lastModified;
5052
private String encoding = UTF_8.name();
5153
private final boolean checkEncoding;
52-
private final DBBroker broker;
54+
private final BrokerPool brokerPool;
5355

54-
public DBSource(final DBBroker broker, final BinaryDocument doc, final boolean checkXQEncoding) {
56+
public DBSource(final BrokerPool brokerPool, final BinaryDocument doc, final boolean checkXQEncoding) {
5557
super(hashKey(doc.getURI().toString()));
56-
this.broker = broker;
58+
this.brokerPool = brokerPool;
5759
this.doc = doc;
5860
this.lastModified = doc.getLastModified();
5961
this.checkEncoding = checkXQEncoding;
@@ -78,50 +80,45 @@ public long getLastModified() {
7880
}
7981

8082
@Override
81-
public Validity isValid(final DBBroker broker) {
83+
public Validity isValid() {
8284
Validity result;
83-
try (final LockedDocument lockedDoc = broker.getXMLResource(doc.getURI(), LockMode.READ_LOCK)) {
85+
try (final DBBroker broker = brokerPool.getBroker();
86+
final LockedDocument lockedDoc = broker.getXMLResource(doc.getURI(), LockMode.READ_LOCK)) {
8487
if (lockedDoc == null) {
8588
result = Validity.INVALID;
8689
} else if(lockedDoc.getDocument().getLastModified() > lastModified) {
8790
result = Validity.INVALID;
8891
} else {
8992
result = Validity.VALID;
9093
}
91-
} catch (final PermissionDeniedException pde) {
94+
} catch (final EXistException | PermissionDeniedException pde) {
9295
result = Validity.INVALID;
9396
}
9497

9598
return result;
9699
}
97100

98-
@Override
99-
public Validity isValid(final Source other) {
100-
final Validity result;
101-
if (!(other instanceof DBSource)) {
102-
result = Validity.INVALID;
103-
} else if (((DBSource)other).getLastModified() > lastModified) {
104-
result = Validity.INVALID;
105-
} else {
106-
result = Validity.VALID;
107-
}
108-
109-
return result;
110-
}
111-
112101
@Override
113102
public Reader getReader() throws IOException {
114-
final InputStream is = broker.getBinaryResource(doc);
115-
final BufferedInputStream bis = new BufferedInputStream(is);
116-
bis.mark(64);
117-
checkEncoding(bis);
118-
bis.reset();
119-
return new InputStreamReader(bis, encoding);
103+
try (final DBBroker broker = brokerPool.getBroker()) {
104+
final InputStream is = broker.getBinaryResource(doc);
105+
final BufferedInputStream bis = new BufferedInputStream(is);
106+
bis.mark(64);
107+
checkEncoding(bis);
108+
bis.reset();
109+
return new InputStreamReader(bis, encoding);
110+
} catch (final EXistException e) {
111+
throw new IOException(e.getMessage(), e);
112+
}
120113
}
121114

122115
@Override
123116
public InputStream getInputStream() throws IOException {
124-
return broker.getBinaryResource(doc);
117+
try (final DBBroker broker = brokerPool.getBroker()) {
118+
return broker.getBinaryResource(doc);
119+
} catch (final EXistException e) {
120+
throw new IOException(e.getMessage(), e);
121+
}
125122
}
126123

127124
@Override
@@ -131,20 +128,26 @@ public String getContent() throws IOException {
131128
throw new IOException("Resource too big to be read using this method.");
132129
}
133130

134-
try (final InputStream raw = broker.getBinaryResource(doc);
131+
try (final DBBroker broker = brokerPool.getBroker();
132+
final InputStream raw = broker.getBinaryResource(doc);
135133
final UnsynchronizedByteArrayOutputStream buf = new UnsynchronizedByteArrayOutputStream((int)binaryLength)) {
136134
buf.write(raw);
137135
try (final InputStream is = buf.toInputStream()) {
138136
checkEncoding(is);
139137
return buf.toString(encoding);
140138
}
139+
} catch (final EXistException e) {
140+
throw new IOException(e.getMessage(), e);
141141
}
142142
}
143143

144144
@Override
145145
public QName isModule() throws IOException {
146-
try (final InputStream is = broker.getBinaryResource(doc)) {
146+
try (final DBBroker broker = brokerPool.getBroker();
147+
final InputStream is = broker.getBinaryResource(doc)) {
147148
return getModuleDecl(is);
149+
} catch (final EXistException e) {
150+
throw new IOException(e.getMessage(), e);
148151
}
149152
}
150153

@@ -173,9 +176,13 @@ public String toString() {
173176
@Deprecated
174177
public void validate(final int mode) throws PermissionDeniedException {
175178
//TODO(AR) This check should not even be here! Its up to the database to refuse access not requesting source
176-
final Subject subject = broker.getCurrentSubject();
177-
if (subject != null) {
178-
doValidation(subject, mode);
179+
try (final DBBroker broker = brokerPool.getBroker()) {
180+
final Subject subject = broker.getCurrentSubject();
181+
if (subject != null) {
182+
doValidation(subject, mode);
183+
}
184+
} catch (final EXistException e) {
185+
throw new PermissionDeniedException(e.getMessage(), e);
179186
}
180187
}
181188

exist-core/src/main/java/org/exist/source/FileSource.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import org.apache.logging.log4j.LogManager;
3434
import org.apache.logging.log4j.Logger;
3535
import org.exist.dom.QName;
36-
import org.exist.security.Subject;
37-
import org.exist.storage.DBBroker;
3836

3937
/**
4038
* A source implementation reading from the path system.
@@ -82,7 +80,7 @@ public Path getPath() {
8280
}
8381

8482
@Override
85-
public Validity isValid(final DBBroker broker) {
83+
public Validity isValid() {
8684
final long currentLastModified = lastModifiedSafe(path);
8785
if (currentLastModified == -1 || currentLastModified > lastModified) {
8886
return Validity.INVALID;
@@ -91,11 +89,6 @@ public Validity isValid(final DBBroker broker) {
9189
}
9290
}
9391

94-
@Override
95-
public Validity isValid(final Source other) {
96-
return Validity.INVALID;
97-
}
98-
9992
@Override
10093
public Reader getReader() throws IOException {
10194
checkEncoding();

exist-core/src/main/java/org/exist/source/Source.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.exist.dom.QName;
3030
import org.exist.security.PermissionDeniedException;
3131
import org.exist.security.Subject;
32-
import org.exist.storage.DBBroker;
3332

3433
import javax.annotation.Nullable;
3534

@@ -45,8 +44,7 @@ public interface Source {
4544

4645
enum Validity {
4746
VALID,
48-
INVALID,
49-
UNKNOWN
47+
INVALID
5048
}
5149

5250
/**
@@ -73,32 +71,10 @@ enum Validity {
7371

7472
/**
7573
* Is this source object still valid?
76-
*
77-
* Returns {@link Validity#UNKNOWN} if the validity of
78-
* the source cannot be determined.
79-
*
80-
* The {@link DBBroker} parameter is required by
81-
* some implementations as they have to read
82-
* resources from the database.
83-
*
84-
* @param broker eXist-db broker
74+
*
8575
* @return Validity of the source object
8676
*/
87-
Validity isValid(DBBroker broker);
88-
89-
/**
90-
* Checks if the source object is still valid
91-
* by comparing it to another version of the
92-
* same source. It depends on the concrete
93-
* implementation how the sources are compared.
94-
*
95-
* Use this method if {@link #isValid(DBBroker)}
96-
* return {@link Validity#UNKNOWN}.
97-
*
98-
* @param other source
99-
* @return Validity of the other source object
100-
*/
101-
Validity isValid(Source other);
77+
Validity isValid();
10278

10379
/**
10480
* Returns a {@link Reader} to read the contents

exist-core/src/main/java/org/exist/source/SourceFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private static Source getSource_fromClasspath(final String contextPath, final St
185185
if (lockedResource != null) {
186186
final DocumentImpl resource = lockedResource.getDocument();
187187
if (resource.getResourceType() == DocumentImpl.BINARY_FILE) {
188-
source = new DBSource(broker, (BinaryDocument) resource, true);
188+
source = new DBSource(broker.getBrokerPool(), (BinaryDocument) resource, true);
189189
} else {
190190
final Serializer serializer = broker.borrowSerializer();
191191
try {

0 commit comments

Comments
 (0)