001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.config;
005:
006: import org.apache.commons.lang.StringUtils;
007:
008: import java.io.File;
009: import java.io.FileNotFoundException;
010:
011: /**
012: * Knows how to find certain directories. You should try <em>everything</em> you can to avoid using this class; using
013: * it requires the user to set various system properties when running Terracotta, and we try to avoid that if at all
014: * possible.
015: */
016: public class Directories {
017:
018: /**
019: * This is <code>public</code> <strong>ONLY</strong> so that some entities can <strong>SET</strong> it. You should
020: * <strong>NOT</strong> set it yourself; that breaks the point of encapsulation. Use the method
021: * {@link Environment#inTest()} instead. The property name is "tc.install-root".
022: */
023: public static final String TC_INSTALL_ROOT_PROPERTY_NAME = "tc.install-root";
024:
025: /**
026: * The property "tc.install-root.ignore-checks", which is used for testing to ignore checks for the installation
027: * root directory.
028: */
029: public static final String TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME = "tc.install-root.ignore-checks";
030:
031: /**
032: * The property "tc.license-location", which indicates the directory containing the license.
033: */
034: public static final String TC_LICENSE_LOCATION_PROPERTY_NAME = "tc.license-location";
035:
036: /**
037: * Get the location of the license directory based on the value of {@link #TC_LICENSE_LOCATION_PROPERTY_NAME}.
038: * @return Directory containing license file, never null
039: * @throws FileNotFoundException If the tc.license-location directory has not been set, the license
040: * directory does not exist, or exists but is not a directory
041: */
042: public static File getLicenseLocation()
043: throws FileNotFoundException {
044: String path = System
045: .getProperty(TC_LICENSE_LOCATION_PROPERTY_NAME);
046: if (StringUtils.isBlank(path)) {
047: throw new FileNotFoundException(
048: "The system property '"
049: + TC_LICENSE_LOCATION_PROPERTY_NAME
050: + "' has not been set. As such, the Terracotta license location directory cannot be located.");
051: }
052: File licenseDir = new File(path).getAbsoluteFile();
053: if (!licenseDir.exists() || !licenseDir.isDirectory()) {
054: throw new FileNotFoundException(
055: "The specified Terracotta installation directory, '"
056: + licenseDir
057: + "', located via the value of the system property '"
058: + TC_LICENSE_LOCATION_PROPERTY_NAME
059: + "', does not actually exist.");
060: }
061: return licenseDir;
062: }
063:
064: /**
065: * Get installation root directory.
066: *
067: * @return Installation root directory or null if TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME is set and
068: * TC_INSTALL_ROOT_PROPERTY_NAME is not.
069: * @throws FileNotFoundException If {@link #TC_INSTALL_ROOT_PROPERTY_NAME} has not been set. If
070: * {@link #TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME} has not been set, this exception may be thrown if the
071: * installation root directory has not been set, is not a directory, or does not contain a lib/tc.jar that is
072: * a file.
073: */
074: public static File getInstallationRoot()
075: throws FileNotFoundException {
076: boolean ignoreCheck = System
077: .getProperty(TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME) != null;
078: String path = System.getProperty(TC_INSTALL_ROOT_PROPERTY_NAME);
079: File theFile = path != null ? new File(path).getAbsoluteFile()
080: : null;
081:
082: if (!ignoreCheck) {
083: if (StringUtils.isBlank(path)) {
084: // formatting
085: throw new FileNotFoundException(
086: "The system property '"
087: + TC_INSTALL_ROOT_PROPERTY_NAME
088: + "' has not been set. As such, the Terracotta installation directory cannot be located.");
089: }
090:
091: String absolutePath = theFile.getAbsolutePath();
092: if (!theFile.isDirectory()) {
093: // formatting
094: throw new FileNotFoundException(
095: "The specified Terracotta installation directory, '"
096: + absolutePath
097: + "', located via the value of the system property '"
098: + TC_INSTALL_ROOT_PROPERTY_NAME
099: + "', does not actually exist.");
100: }
101:
102: File searchFile = new File(new File(theFile, "lib"),
103: "tc.jar");
104:
105: if (!searchFile.exists() || !searchFile.isFile()) {
106: // This is just so we don't have to have tc.jar around in development configurations.
107: if (new File(theFile,
108: ".force-is-terracotta-install-dir").exists())
109: return theFile;
110: else {
111: // formatting
112: throw new FileNotFoundException(
113: "The specified Terracotta installation directory, '"
114: + absolutePath
115: + "', located via the value of the system property '"
116: + TC_INSTALL_ROOT_PROPERTY_NAME
117: + "', does not seem to actually "
118: + "be the root of the Terracotta installation. (The required "
119: + "Terracotta JAR file, '"
120: + searchFile.getAbsolutePath()
121: + "', does not exist or is not a file.)");
122: }
123: }
124: }
125:
126: return theFile;
127: }
128:
129: }
|