Passenger: ResourceLocator.h Source File

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List

# ResourceLocator.h

    00001 /*
    00002  *  Phusion Passenger - http://www.modrails.com/
    00003  *  Copyright (c) 2010 Phusion
    00004  *
    00005  *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
    00006  *
    00007  *  Permission is hereby granted, free of charge, to any person obtaining a copy
    00008  *  of this software and associated documentation files (the "Software"), to deal
    00009  *  in the Software without restriction, including without limitation the rights
    00010  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00011  *  copies of the Software, and to permit persons to whom the Software is
    00012  *  furnished to do so, subject to the following conditions:
    00013  *
    00014  *  The above copyright notice and this permission notice shall be included in
    00015  *  all copies or substantial portions of the Software.
    00016  *
    00017  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00018  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00019  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00020  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00021  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00022  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00023  *  THE SOFTWARE.
    00024  */
    00025 #ifndef _PASSENGER_RESOURCE_LOCATOR_H_
    00026 #define _PASSENGER_RESOURCE_LOCATOR_H_
    00027
    00028 #include <string>
    00029 #include <IniFile.h>
    00030 #include <Exceptions.h>
    00031 #include <Utils.h>
    00032
    00033 namespace Passenger {
    00034
    00035 using namespace boost;
    00036
    00037
    00038 /**
    00039  * Locates various Phusion Passenger resources on the filesystem.
    00040  */
    00041 class ResourceLocator {
    00042 private:
    00043         string agentsDir;
    00044         string helperScriptsDir;
    00045         string resourcesDir;
    00046         string docDir;
    00047         string rubyLibDir;
    00048         string compilableSourceDir;
    00049         string apache2Module;
    00050
    00051         string getOption(const string &file, const IniFileSectionPtr &section, const string &key) const {
    00052                 if (section->hasKey(key)) {
    00053                         return section->get(key);
    00054                 } else {
    00055                         throw RuntimeException("Option '" + key + "' missing in file " + file);
    00056                 }
    00057         }
    00058
    00059 public:
    00060         ResourceLocator(const string &rootOrFile) {
    00061                 FileType rootOrFileType = getFileType(rootOrFile);
    00062                 if (rootOrFileType == FT_DIRECTORY || rootOrFileType == FT_NONEXISTANT) {
    00063                         string root = rootOrFile;
    00064                         bool nativelyPackaged = !fileExists(root + "/Rakefile") ||
    00065                                 !fileExists(root + "/DEVELOPERS.TXT");
    00066
    00067                         if (nativelyPackaged) {
    00068                                 agentsDir           = "/usr/lib/phusion-passenger/agents";
    00069                                 helperScriptsDir    = "/usr/share/phusion-passenger/helper-scripts";
    00070                                 resourcesDir        = "/usr/share/phusion-passenger";
    00071                                 docDir              = "/usr/share/doc/phusion-passenger";
    00072                                 rubyLibDir          = "";
    00073                                 compilableSourceDir = "/usr/share/phusion-passenger/compilable-source";
    00074                                 apache2Module       = "/usr/lib/apache2/modules/mod_passenger.so";
    00075                         } else {
    00076                                 agentsDir           = root + "/agents";
    00077                                 helperScriptsDir    = root + "/helper-scripts";
    00078                                 resourcesDir        = root + "/resources";
    00079                                 docDir              = root + "/doc";
    00080                                 rubyLibDir          = root + "/lib";
    00081                                 compilableSourceDir = root;
    00082                                 apache2Module       = root + "ext/apache2/mod_passenger.so";
    00083                         }
    00084
    00085                 } else {
    00086                         string file = rootOrFile;
    00087                         IniFileSectionPtr options = IniFile(file).section("locations");
    00088                         agentsDir           = getOption(file, options, "agents");
    00089                         helperScriptsDir    = getOption(file, options, "helper_scripts");
    00090                         resourcesDir        = getOption(file, options, "resources");
    00091                         docDir              = getOption(file, options, "doc");
    00092                         rubyLibDir          = getOption(file, options, "rubylib");
    00093                         compilableSourceDir = getOption(file, options, "compilable_source");
    00094                         apache2Module       = getOption(file, options, "apache2_module");
    00095                 }
    00096         }
    00097
    00098         string getAgentsDir() const {
    00099                 return agentsDir;
    00100         }
    00101
    00102         string getHelperScriptsDir() const {
    00103                 return helperScriptsDir;
    00104         }
    00105
    00106         string getSpawnServerFilename() const {
    00107                 return getHelperScriptsDir() + "/passenger-spawn-server";
    00108         }
    00109
    00110         string getResourcesDir() const {
    00111                 return resourcesDir;
    00112         }
    00113
    00114         string getDocDir() const {
    00115                 return docDir;
    00116         }
    00117
    00118         // Can be empty.
    00119         string getRubyLibDir() const {
    00120                 return rubyLibDir;
    00121         }
    00122
    00123         string getCompilableSourceDir() const {
    00124                 return compilableSourceDir;
    00125         }
    00126
    00127         string getApache2ModuleFilename() const {
    00128                 return apache2Module;
    00129         }
    00130 };
    00131
    00132
    00133 }
    00134
    00135 #endif /* _PASSENGER_RESOURCE_LOCATOR_H_ */