Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions eo-parser/src/main/java/org/eolang/parser/StrictXmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.xml.namespace.NamespaceContext;
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.IoCheckedBytes;
Expand All @@ -44,14 +46,8 @@
* then encapsulate it into this decorator.</p>
*
* @since 0.49.0
* @todo #4884:30min Replace synchronized statements with ReentrantLock.
* We are getting the following complaint about {@ling StrictXmir}:
* StrictXmir.java[244-289]: Use ReentrantLock rather than synchronization
* (AvoidSynchronizedStatement). It's better to consider this replacement.
* Don't forget to remove PMD.AvoidSynchronizedStatement from the
* SuppressWarnings annotation of this class.
*/
@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidSynchronizedStatement"})
@SuppressWarnings("PMD.TooManyMethods")
public final class StrictXmir implements XML {
/**
* XSD for current EO version.
Expand All @@ -61,6 +57,11 @@ public final class StrictXmir implements XML {
Manifests.read("EO-Version")
);

/**
* Lock to synchronize by.
*/
private static final Lock LOCK = new ReentrantLock();

/**
* The XML.
*/
Expand Down Expand Up @@ -167,8 +168,7 @@ private static XML reset(final XML xml, final Path tmp) {
before,
tmp.resolve(
before.substring(before.lastIndexOf('/') + 1)
),
tmp
)
).getAbsoluteFile().toString().replace("\\", "/")
);
} else {
Expand All @@ -190,15 +190,14 @@ private static XML reset(final XML xml, final Path tmp) {
* Fetch the XSD and place into the path.
* @param uri The URI
* @param path The file
* @param tmp Original directory
* @return Where it was saved
*/
private static File fetch(final String uri, final Path path, final Path tmp) {
private static File fetch(final String uri, final Path path) {
final File ret;
if (StrictXmir.MINE.equals(uri)) {
ret = StrictXmir.copied(uri, path, tmp);
ret = StrictXmir.copied(uri, path);
} else {
ret = StrictXmir.downloaded(uri, path, tmp);
ret = StrictXmir.downloaded(uri, path);
}
return ret;
}
Expand All @@ -207,12 +206,12 @@ private static File fetch(final String uri, final Path path, final Path tmp) {
* Copy URI from local resource and save to file.
* @param uri The URI
* @param path The file
* @param tmp Directory to synchronize by
* @return Where it was saved
*/
private static File copied(final String uri, final Path path, final Path tmp) {
private static File copied(final String uri, final Path path) {
final File file = path.toFile();
synchronized (tmp) {
StrictXmir.LOCK.lock();
try {
if (!file.exists()) {
if (file.getParentFile().mkdirs()) {
Logger.debug(StrictXmir.class, "Directory for %[file]s created", path);
Expand All @@ -232,6 +231,8 @@ private static File copied(final String uri, final Path path, final Path tmp) {
);
}
}
} finally {
StrictXmir.LOCK.unlock();
}
return file;
}
Expand All @@ -240,13 +241,13 @@ private static File copied(final String uri, final Path path, final Path tmp) {
* Download URI from Internet and save to file.
* @param uri The URI
* @param path The file
* @param tmp Directory to synchronize by
* @return Where it was saved
*/
@SuppressWarnings("PMD.CognitiveComplexity")
private static File downloaded(final String uri, final Path path, final Path tmp) {
private static File downloaded(final String uri, final Path path) {
final File abs = path.toFile().getAbsoluteFile();
synchronized (tmp) {
StrictXmir.LOCK.lock();
try {
if (!abs.exists()) {
if (abs.getParentFile().mkdirs()) {
Logger.debug(StrictXmir.class, "Directory for %[file]s created", path);
Expand Down Expand Up @@ -291,6 +292,8 @@ private static File downloaded(final String uri, final Path path, final Path tmp
}
}
}
} finally {
StrictXmir.LOCK.unlock();
}
return abs;
}
Expand Down
Loading