Skip to content

Commit

Permalink
Fixed idents and other code style in VitroHomeDirectory. Removed Vitr…
Browse files Browse the repository at this point in the history
…oHomeDirectory from checkstyle suppressions
  • Loading branch information
litvinovg committed Dec 17, 2024
1 parent 3543dd5 commit a1d7b2b
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,37 @@
* Encapsulates some of the info relating to the Vitro home directory.
*/
public class VitroHomeDirectory {
private static final Log log = LogFactory.getLog(VitroHomeDirectory.class);
private static final Log log = LogFactory.getLog(VitroHomeDirectory.class);

public static VitroHomeDirectory find(ServletContext ctx) {
HomeDirectoryFinder finder = new HomeDirectoryFinder(ctx);
return new VitroHomeDirectory(ctx, finder.getPath(),
finder.getMessage());
}
public static VitroHomeDirectory find(ServletContext ctx) {
HomeDirectoryFinder finder = new HomeDirectoryFinder(ctx);
return new VitroHomeDirectory(ctx, finder.getPath(), finder.getMessage());
}

private final ServletContext ctx;
private final Path path;
private final String discoveryMessage;
private final ServletContext ctx;
private final Path path;
private final String discoveryMessage;
private Set<String> excludedHomeFiles = new HashSet<>(Arrays.asList("rdf"));
private String homeSourcePath;

public VitroHomeDirectory(ServletContext ctx, Path path, String discoveryMessage) {
this.ctx = ctx;
this.path = path;
this.discoveryMessage = discoveryMessage;
setHomeSourcePath(ctx);
}

public VitroHomeDirectory(ServletContext ctx, Path path,
String discoveryMessage) {
this.ctx = ctx;
this.path = path;
this.discoveryMessage = discoveryMessage;
setHomeSourcePath(ctx);
}

public ServletContext getCtx() {
return ctx;
}
public ServletContext getCtx() {
return ctx;
}

public Path getPath() {
return path;
}
public Path getPath() {
return path;
}

public String getDiscoveryMessage() {
return discoveryMessage;
}
public String getDiscoveryMessage() {
return discoveryMessage;
}

/**
* Get source home file directory path
Expand All @@ -79,162 +76,142 @@ private void setHomeSourcePath(ServletContext context) {
}
}

/**
* Find something that specifies the location of the Vitro home directory.
* Look in the JDNI environment, the system properties, and the
* build.properties file.
*
* If we don't find it, fail. If we find it more than once, use the first
* one (with a warning). If it is not an existing, readable directory, fail.
*/
private static class HomeDirectoryFinder {
/** JNDI path that defines the Vitro home directory */
private static final String VHD_JNDI_PATH = "java:comp/env/vitro/home";

/** System property that defines the Vitro home directory */
private static final String VHD_SYSTEM_PROPERTY = "vitro.home";

/** build.properties property that defines the Vitro home directory */
private static final String VHD_BUILD_PROPERTY = "vitro.home";

private final ServletContext ctx;
private final List<Found> foundLocations = new ArrayList<>();

public HomeDirectoryFinder(ServletContext ctx) {
this.ctx = ctx;

getVhdFromJndi();
getVhdFromSystemProperties();
getVhdFromBuildProperties();
confirmExactlyOneResult();
confirmValidDirectory();
}

public String getMessage() {
return foundLocations.get(0).getMessage();
}

public Path getPath() {
return foundLocations.get(0).getPath();
}

public void getVhdFromJndi() {
try {
String vhdPath = (String) new InitialContext()
.lookup(VHD_JNDI_PATH);
if (vhdPath == null) {
log.debug("Didn't find a JNDI value at '" + VHD_JNDI_PATH
+ "'.");
} else {
log.debug("'" + VHD_JNDI_PATH + "' as specified by JNDI: "
+ vhdPath);
String message = String.format(
"JNDI environment '%s' was set to '%s'",
VHD_JNDI_PATH, vhdPath);
foundLocations.add(new Found(Paths.get(vhdPath), message));
}
} catch (Exception e) {
log.debug("JNDI lookup failed. " + e);
}
}

private void getVhdFromSystemProperties() {
String vhdPath = System.getProperty(VHD_SYSTEM_PROPERTY);
if (vhdPath == null) {
log.debug("Didn't find a system property value at '"
+ VHD_SYSTEM_PROPERTY + "'.");
} else {
log.debug("'" + VHD_SYSTEM_PROPERTY
+ "' as specified by system property: " + vhdPath);
String message = String.format(
"System property '%s' was set to '%s'",
VHD_SYSTEM_PROPERTY, vhdPath);
foundLocations.add(new Found(Paths.get(vhdPath), message));
}
}

private void getVhdFromBuildProperties() {
try {
Map<String, String> buildProps = new BuildProperties(ctx)
.getMap();
String vhdPath = buildProps.get(VHD_BUILD_PROPERTY);
if (vhdPath == null) {
log.debug("build properties doesn't contain a value for '"
+ VHD_BUILD_PROPERTY + "'.");
} else {
log.debug("'" + VHD_BUILD_PROPERTY
+ "' as specified by build.properties: " + vhdPath);
String message = String.format(
"In resource '%s', '%s' was set to '%s'.",
WEBAPP_PATH_BUILD_PROPERTIES, VHD_BUILD_PROPERTY,
vhdPath);
foundLocations.add(new Found(Paths.get(vhdPath), message));
}
} catch (Exception e) {
log.warn("Reading build properties failed. " + e);
}
}

private void confirmExactlyOneResult() {
if (foundLocations.isEmpty()) {
String message = String.format("Can't find a value "
+ "for the Vitro home directory. "
+ "Looked in JNDI environment at '%s'. "
+ "Looked for a system property named '%s'. "
+ "Looked in 'WEB-INF/resources/build.properties' "
+ "for '%s'.", VHD_JNDI_PATH, VHD_SYSTEM_PROPERTY,
VHD_BUILD_PROPERTY);
throw new IllegalStateException(message);
} else if (foundLocations.size() > 1) {
String message = "Found multiple values for the "
+ "Vitro home directory: " + foundLocations;
log.warn(message);
}
}

private void confirmValidDirectory() {
Path vhd = getPath();
if (!Files.exists(vhd)) {
createHomeDirectory(vhd);
}
if (!Files.isDirectory(vhd)) {
throw new IllegalStateException("Vitro home directory '" + vhd
+ "' is not a directory.");
}
if (!Files.isReadable(vhd)) {
throw new IllegalStateException(
"Cannot read Vitro home directory '" + vhd + "'.");
}
if (!Files.isWritable(vhd)) {
throw new IllegalStateException(
"Can't write to Vitro home directory: '" + vhd + "'.");
}
}

/** We found it: where and how. */
private static class Found {
private final Path path;
private final String message;

public Found(Path path, String message) {
this.path = path;
this.message = message;
}

public Path getPath() {
return path;
}

public String getMessage() {
return message;
}

@Override
public String toString() {
return "Found[path=" + path + ", message=" + message + "]";
}
}
}
/**
* Find something that specifies the location of the Vitro home directory. Look in the JDNI environment, the system
* properties, and the build.properties file.
*
* If we don't find it, fail. If we find it more than once, use the first one (with a warning). If it is not an
* existing, readable directory, fail.
*/
private static class HomeDirectoryFinder {
/** JNDI path that defines the Vitro home directory */
private static final String VHD_JNDI_PATH = "java:comp/env/vitro/home";

/** System property that defines the Vitro home directory */
private static final String VHD_SYSTEM_PROPERTY = "vitro.home";

/** build.properties property that defines the Vitro home directory */
private static final String VHD_BUILD_PROPERTY = "vitro.home";

private final ServletContext ctx;
private final List<Found> foundLocations = new ArrayList<>();

public HomeDirectoryFinder(ServletContext ctx) {
this.ctx = ctx;

getVhdFromJndi();
getVhdFromSystemProperties();
getVhdFromBuildProperties();
confirmExactlyOneResult();
confirmValidDirectory();
}

public String getMessage() {
return foundLocations.get(0).getMessage();
}

public Path getPath() {
return foundLocations.get(0).getPath();
}

public void getVhdFromJndi() {
try {
String vhdPath = (String) new InitialContext().lookup(VHD_JNDI_PATH);
if (vhdPath == null) {
log.debug("Didn't find a JNDI value at '" + VHD_JNDI_PATH + "'.");
} else {
log.debug("'" + VHD_JNDI_PATH + "' as specified by JNDI: " + vhdPath);
String message = String.format("JNDI environment '%s' was set to '%s'", VHD_JNDI_PATH, vhdPath);
foundLocations.add(new Found(Paths.get(vhdPath), message));
}
} catch (Exception e) {
log.debug("JNDI lookup failed. " + e);
}
}

private void getVhdFromSystemProperties() {
String vhdPath = System.getProperty(VHD_SYSTEM_PROPERTY);
if (vhdPath == null) {
log.debug("Didn't find a system property value at '" + VHD_SYSTEM_PROPERTY + "'.");
} else {
log.debug("'" + VHD_SYSTEM_PROPERTY + "' as specified by system property: " + vhdPath);
String message = String.format("System property '%s' was set to '%s'", VHD_SYSTEM_PROPERTY, vhdPath);
foundLocations.add(new Found(Paths.get(vhdPath), message));
}
}

private void getVhdFromBuildProperties() {
try {
Map<String, String> buildProps = new BuildProperties(ctx).getMap();
String vhdPath = buildProps.get(VHD_BUILD_PROPERTY);
if (vhdPath == null) {
log.debug("build properties doesn't contain a value for '" + VHD_BUILD_PROPERTY + "'.");
} else {
log.debug("'" + VHD_BUILD_PROPERTY + "' as specified by build.properties: " + vhdPath);
String message = String.format("In resource '%s', '%s' was set to '%s'.",
WEBAPP_PATH_BUILD_PROPERTIES, VHD_BUILD_PROPERTY, vhdPath);
foundLocations.add(new Found(Paths.get(vhdPath), message));
}
} catch (Exception e) {
log.warn("Reading build properties failed. " + e);
}
}

private void confirmExactlyOneResult() {
if (foundLocations.isEmpty()) {
String message = String.format("Can't find a value " +
"for the Vitro home directory. " +
"Looked in JNDI environment at '%s'. " +
"Looked for a system property named '%s'. " +
"Looked in 'WEB-INF/resources/build.properties' " +
"for '%s'.", VHD_JNDI_PATH, VHD_SYSTEM_PROPERTY, VHD_BUILD_PROPERTY);
throw new IllegalStateException(message);
} else if (foundLocations.size() > 1) {
String message = "Found multiple values for the " + "Vitro home directory: " + foundLocations;
log.warn(message);
}
}

private void confirmValidDirectory() {
Path vhd = getPath();
if (!Files.exists(vhd)) {
createHomeDirectory(vhd);
}
if (!Files.isDirectory(vhd)) {
throw new IllegalStateException("Vitro home directory '" + vhd + "' is not a directory.");
}
if (!Files.isReadable(vhd)) {
throw new IllegalStateException("Cannot read Vitro home directory '" + vhd + "'.");
}
if (!Files.isWritable(vhd)) {
throw new IllegalStateException("Can't write to Vitro home directory: '" + vhd + "'.");
}
}

/** We found it: where and how. */
private static class Found {
private final Path path;
private final String message;

public Found(Path path, String message) {
this.path = path;
this.message = message;
}

public Path getPath() {
return path;
}

public String getMessage() {
return message;
}

@Override
public String toString() {
return "Found[path=" + path + ", message=" + message + "]";
}
}
}

/**
* Populates home directory with home files, excluding the rdf directory
Expand All @@ -250,7 +227,7 @@ public void populate() {
}
try {
copy(homeDestination);
} catch(Exception e) {
} catch (Exception e) {
throw new IllegalStateException("Failed to copy home files! " + homeDestination, e);
}
log.info("Copied home files to " + homeDestination.toPath());
Expand Down
1 change: 0 additions & 1 deletion checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
<suppress files="src[\\/]main[\\/]java[\\/]edu[\\/]cornell[\\/]mannlib[\\/]vitro[\\/]webapp[\\/]application[\\/]ApplicationSetup\.java" checks="."/>
<suppress files="src[\\/]main[\\/]java[\\/]edu[\\/]cornell[\\/]mannlib[\\/]vitro[\\/]webapp[\\/]application[\\/]ApplicationUtils\.java" checks="."/>
<suppress files="src[\\/]main[\\/]java[\\/]edu[\\/]cornell[\\/]mannlib[\\/]vitro[\\/]webapp[\\/]application[\\/]BuildProperties\.java" checks="."/>
<suppress files="src[\\/]main[\\/]java[\\/]edu[\\/]cornell[\\/]mannlib[\\/]vitro[\\/]webapp[\\/]application[\\/]VitroHomeDirectory\.java" checks="."/>
<suppress files="src[\\/]main[\\/]java[\\/]edu[\\/]cornell[\\/]mannlib[\\/]vitro[\\/]webapp[\\/]auth[\\/]identifier[\\/]ActiveIdentifierBundleFactories\.java" checks="."/>
<suppress files="src[\\/]main[\\/]java[\\/]edu[\\/]cornell[\\/]mannlib[\\/]vitro[\\/]webapp[\\/]auth[\\/]identifier[\\/]ArrayIdentifierBundle\.java" checks="."/>
<suppress files="src[\\/]main[\\/]java[\\/]edu[\\/]cornell[\\/]mannlib[\\/]vitro[\\/]webapp[\\/]auth[\\/]identifier[\\/]Identifier\.java" checks="."/>
Expand Down

0 comments on commit a1d7b2b

Please sign in to comment.