KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > Directories


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config;
5
6 import org.apache.commons.lang.StringUtils;
7
8 import java.io.File JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10
11 /**
12  * Knows how to find certain directories. You should try <em>everything</em> you can to avoid using this class; using
13  * it requires the user to set various system properties when running Terracotta, and we try to avoid that if at all
14  * possible.
15  */

16 public class Directories {
17
18   /**
19    * This is <code>public</code> <strong>ONLY</strong> so that some entities can <strong>SET</strong> it. You should
20    * <strong>NOT</strong> set it yourself; that breaks the point of encapsulation. Use the method instead.
21    */

22   public static final String JavaDoc TC_INSTALL_ROOT_PROPERTY_NAME = "tc.install-root";
23   public static final String JavaDoc TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME = "tc.install-root.ignore-checks";
24   public static final String JavaDoc TC_LICENSE_LOCATION_PROPERTY_NAME = "tc.license-location";
25
26   public static File JavaDoc getLicenseLocation() throws FileNotFoundException JavaDoc {
27     String JavaDoc path = System.getProperty(TC_LICENSE_LOCATION_PROPERTY_NAME);
28     if (StringUtils.isBlank(path)) { throw new FileNotFoundException JavaDoc(
29                                                                      "The system property '"
30                                                                          + TC_LICENSE_LOCATION_PROPERTY_NAME
31                                                                          + "' has not been set. As such, the Terracotta license location directory cannot be located."); }
32     File JavaDoc licenseDir = new File JavaDoc(path).getAbsoluteFile();
33     if (!licenseDir.exists() || !licenseDir.isDirectory()) { throw new FileNotFoundException JavaDoc(
34                                                                                              "The specified Terracotta installation directory, '"
35                                                                                                  + licenseDir
36                                                                                                  + "', located via the value of the system property '"
37                                                                                                  + TC_LICENSE_LOCATION_PROPERTY_NAME
38                                                                                                  + "', does not actually exist."); }
39     return licenseDir;
40   }
41
42   public static File JavaDoc getInstallationRoot() throws FileNotFoundException JavaDoc {
43     String JavaDoc path = System.getProperty(TC_INSTALL_ROOT_PROPERTY_NAME);
44
45     if (StringUtils.isBlank(path)) {
46       // formatting
47
throw new FileNotFoundException JavaDoc(
48                                       "The system property '"
49                                           + TC_INSTALL_ROOT_PROPERTY_NAME
50                                           + "' has not been set. As such, the Terracotta installation directory cannot be located.");
51     }
52
53     File JavaDoc theFile = new File JavaDoc(path).getAbsoluteFile();
54
55     if (System.getProperty(TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME) == null) {
56       String JavaDoc absolutePath = theFile.getAbsolutePath();
57
58       if (!theFile.exists()) {
59         // formatting
60
throw new FileNotFoundException JavaDoc("The specified Terracotta installation directory, '" + absolutePath
61                                         + "', located via the value of the system property '"
62                                         + TC_INSTALL_ROOT_PROPERTY_NAME + "', does not actually exist.");
63       }
64
65       if (!theFile.isDirectory()) {
66         // formatting
67
throw new FileNotFoundException JavaDoc("The specified Terracotta installation directory, '" + absolutePath
68                                         + "', located via the value of the system property '"
69                                         + TC_INSTALL_ROOT_PROPERTY_NAME + "', does not actually exist.");
70       }
71
72       File JavaDoc searchFile = new File JavaDoc(new File JavaDoc(theFile, "lib"), "tc.jar");
73
74       if (!searchFile.exists() || !searchFile.isFile()) {
75         // This is just so we don't have to have tc.jar around in development configurations.
76
if (new File JavaDoc(theFile, ".force-is-terracotta-install-dir").exists()) return theFile;
77         else {
78           // formatting
79
throw new FileNotFoundException JavaDoc("The specified Terracotta installation directory, '" + absolutePath
80                                           + "', located via the value of the system property '"
81                                           + TC_INSTALL_ROOT_PROPERTY_NAME + "', does not seem to actually "
82                                           + "be the root of the Terracotta installation. (The required "
83                                           + "Terracotta JAR file, '" + searchFile.getAbsolutePath()
84                                           + "', does not exist or is not a file.)");
85         }
86       }
87     }
88
89     return theFile;
90   }
91
92 }
93
Popular Tags