Skip to content

Commit

Permalink
Core, common and domain migrated to PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Eichar committed Nov 5, 2014
1 parent 3974b09 commit 05c2d89
Show file tree
Hide file tree
Showing 314 changed files with 2,893 additions and 2,359 deletions.
5 changes: 5 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@


<dependencies>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
Expand Down
57 changes: 47 additions & 10 deletions common/src/main/java/org/fao/geonet/utils/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@
import org.fao.geonet.Logger;

import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

//=============================================================================
Expand All @@ -49,6 +52,7 @@
*/
public final class IO {
private static FileSystem defaultFs = FileSystems.getDefault();
private static ThreadLocal<FileSystem> defaultFsThreadLocal = new InheritableThreadLocal<>();

public static final DirectoryStream.Filter<Path> DIRECTORIES_FILTER = new DirectoryStream.Filter<Path>() {
@Override
Expand Down Expand Up @@ -106,40 +110,61 @@ public static void closeQuietly(Statement stmt) {
}
}
public static void copyDirectoryOrFile(final Path from, final Path to) throws IOException {
Objects.requireNonNull(from);
Objects.requireNonNull(to);

final Path actualTo;
if (Files.isDirectory(to)) {
actualTo = to.resolve(from.getFileName().toString());
} else {
actualTo = to;
}

if (from.equals(to)) {
return;
}

if (Files.isDirectory(from)) {
Assert.isTrue(!Files.isRegularFile(to), "cannot copy a directory to a file. From: " + from + " to " + to);
Assert.isTrue(!Files.isRegularFile(actualTo), "cannot copy a directory to a file. From: " + from + " to " + actualTo);
Files.walkFileTree(from, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Files.createDirectory(relativeFile(dir, to, from));
Files.createDirectory(relativeFile(dir, actualTo, from));
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, relativeFile(file, to, from));
Files.copy(file, relativeFile(file, actualTo, from));
return FileVisitResult.CONTINUE;
}
});
} else {
final Path parent = to.getParent();
final Path parent = actualTo.getParent();
if (!Files.exists(parent)) {
Files.createDirectories(parent);
}
Files.copy(from, to);
Files.copy(from, actualTo);
}
}

protected static Path relativeFile(Path dir, Path to, Path from) {
return to.resolve(from.relativize(dir));
return to.resolve(from.relativize(dir).toString().replace('\\', '/'));
}

public static void moveDirectoryOrFile(final Path from, final Path to) throws IOException {
final Path parent = to.getParent();
final Path actualTo;
if (Files.isDirectory(to)) {
actualTo = to.resolve(from.getFileName());
} else {
actualTo = to;
}

final Path parent = actualTo.getParent();
if (!Files.exists(parent)) {
Files.createDirectories(parent);
}
Files.move(from, to);
Files.move(from, actualTo);
}
public static boolean isEmptyDir(Path dir) throws IOException {
try (DirectoryStream<Path> children = Files.newDirectoryStream(dir)) {
Expand Down Expand Up @@ -170,7 +195,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
}
public static void touch(Path file) throws IOException{
long timestamp = System.currentTimeMillis();
touch(file, FileTime.from(timestamp, TimeUnit.MILLISECONDS));
touch(file, FileTime.from(timestamp, TimeUnit.MILLISECONDS));
}

public static void touch(Path file, FileTime timestamp) throws IOException{
Expand All @@ -186,13 +211,25 @@ public static void touch(Path file, FileTime timestamp) throws IOException{
}

public static Path toPath(String firstPart, String... more) {
return defaultFs.getPath(firstPart, more);
FileSystem fileSystem = defaultFsThreadLocal.get();
if (fileSystem == null) {
fileSystem = defaultFs;
}
return fileSystem.getPath(firstPart, more);
}

public static Path toPath(URI uri) {
return Paths.get(uri);
}

public static void setFileSystem(FileSystem newFileSystem) {
defaultFs = newFileSystem;
}

public static void setFileSystemThreadLocal(FileSystem newFileSystem) {
defaultFsThreadLocal.set(newFileSystem);
}

}

//=============================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.fao.geonet.utils;

import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.tools.CatalogResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;

/**
* @author Jesse on 11/4/2014.
*/
class NioPathAwareCatalogResolver extends CatalogResolver {
public NioPathAwareCatalogResolver(CatalogManager catMan) {
super(catMan);
}

@Override
public String getResolvedEntity(String publicId, String systemId) {
return super.getResolvedEntity(publicId, systemId);
}

@Override
public InputSource resolveEntity(String publicId, String systemId) {
try {
final InputSource inputSource = Xml.PATH_RESOLVER.resolveEntity(publicId, systemId);
if (inputSource != null) {
return inputSource;
}
} catch (SAXException | IOException e) {
throw new RuntimeException(e);
}
return super.resolveEntity(publicId, systemId);
}

@Override
public Source resolve(String href, String base) throws TransformerException {
try {
final Path basePath = Paths.get(new URI(base));
final Path resolvedResource = basePath.getParent().resolve(href);
if (Files.isRegularFile(resolvedResource)) {
return new StreamSource(Files.newInputStream(resolvedResource), resolvedResource.toUri().toASCIIString());
}
} catch (Exception e) {
// ignore
}
return super.resolve(href, base);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.fao.geonet.utils;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;

/**
* An Xml Entity Resolver that resolves xml files relative to a java.nio.file.Path object. This means that
* the xml can be loaded from any Java NIO filesystem not just the default filesystem.
*
* @author Jesse on 11/4/2014.
*/
public class NioPathAwareEntityResolver implements EntityResolver {


@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return NioPathHolder.resolveEntity(publicId, systemId);
}
}
49 changes: 49 additions & 0 deletions common/src/main/java/org/fao/geonet/utils/NioPathHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.fao.geonet.utils;

import org.xml.sax.InputSource;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Holder the information required by {@link org.fao.geonet.utils.NioPathAwareCatalogResolver}
* and {@link org.fao.geonet.utils.NioPathAwareEntityResolver} for resolving paths.
*
* @author Jesse on 11/4/2014.
*/
public class NioPathHolder {
private static final ThreadLocal<Path> ACTUAL_RELATIVE_TO = new InheritableThreadLocal<>();
private static final ThreadLocal<Path> SYS_ID_RELATIVE_TO = new InheritableThreadLocal<>();

static void setBase(Path base) {
if (base != null) {
ACTUAL_RELATIVE_TO.set(base.getParent());
SYS_ID_RELATIVE_TO.set(new File(".").getAbsoluteFile().toPath().getParent());
} else {
ACTUAL_RELATIVE_TO.set(null);
SYS_ID_RELATIVE_TO.set(null);
}
}

static InputSource resolveEntity(String publicId, String systemId) throws IOException {
if (ACTUAL_RELATIVE_TO.get() != null && systemId.startsWith("file:/")) {
try {
final Path srcPath = Paths.get(new URI(systemId));

final Path relativePath = SYS_ID_RELATIVE_TO.get().relativize(srcPath);
Path finalPath = ACTUAL_RELATIVE_TO.get().resolve(relativePath.toString());
if (Files.isRegularFile(finalPath)) {
return new InputSource(Files.newInputStream(finalPath));
}
} catch (URISyntaxException e) {
// ignore
}
}
return null;
}
}
2 changes: 1 addition & 1 deletion common/src/main/java/org/fao/geonet/utils/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void setUpXmlResolver() {
if(Log.isDebugEnabled(Log.JEEVES)) Log.debug(Log.JEEVES,"Using catalog resolver verbosity "+iCatVerb);
catMan.setVerbosity(iCatVerb);

catResolver = new CatalogResolver(catMan);
catResolver = new NioPathAwareCatalogResolver(catMan);

@SuppressWarnings("unchecked")
Vector<String> catalogs = catResolver.getCatalog().getCatalogManager().getCatalogFiles();
Expand Down
Loading

0 comments on commit 05c2d89

Please sign in to comment.