Search in sources :

Example 16 with NSBundle

use of com.webobjects.foundation.NSBundle in project wonder-slim by undur.

the class ERXProperties method pathsForUserAndBundleProperties.

public static NSArray<String> pathsForUserAndBundleProperties(boolean reportLoggingEnabled) {
    NSMutableArray<String> propertiesPaths = new NSMutableArray();
    NSMutableArray<String> projectsInfo = new NSMutableArray();
    /*  Properties for frameworks */
    NSArray frameworkNames = (NSArray) NSBundle.frameworkBundles().valueForKey("name");
    Enumeration e = frameworkNames.reverseObjectEnumerator();
    while (e.hasMoreElements()) {
        String frameworkName = (String) e.nextElement();
        String propertyPath = pathForResourceNamed("Properties", frameworkName, null);
        addIfPresent(frameworkName + ".framework", propertyPath, propertiesPaths, projectsInfo);
        /**
         * Properties.dev -- per-Framework-dev properties
         * This adds support for Properties.dev in your Frameworks new load order will be
         */
        String devPropertiesPath = ERXApplication.isDevelopmentModeSafe() ? ERXProperties.variantPropertiesInBundle("dev", frameworkName) : null;
        addIfPresent(frameworkName + ".framework.dev", devPropertiesPath, propertiesPaths, projectsInfo);
        /**
         * Properties.<userName> -- per-Framework-per-User properties
         */
        String userPropertiesPath = ERXProperties.variantPropertiesInBundle(NSProperties.getProperty("user.name"), frameworkName);
        addIfPresent(frameworkName + ".framework.user", userPropertiesPath, propertiesPaths, projectsInfo);
    }
    NSBundle mainBundle = NSBundle.mainBundle();
    if (mainBundle != null) {
        String mainBundleName = mainBundle.name();
        String appPath = pathForResourceNamed("Properties", "app", null);
        addIfPresent(mainBundleName + ".app", appPath, propertiesPaths, projectsInfo);
    }
    /*  WebObjects.properties in the user home directory */
    String userHome = NSProperties.getProperty("user.home");
    if (userHome != null && userHome.length() > 0) {
        File file = new File(userHome, "WebObjects.properties");
        if (file.exists() && file.isFile() && file.canRead()) {
            try {
                String userHomePath = file.getCanonicalPath();
                addIfPresent("{$user.home}/WebObjects.properties", userHomePath, propertiesPaths, projectsInfo);
            } catch (java.io.IOException ex) {
                log.error("Failed to load the configuration file '{}'.", file, ex);
            }
        }
    }
    /*  Optional properties files */
    if (optionalConfigurationFiles() != null && optionalConfigurationFiles().count() > 0) {
        for (Enumeration configEnumerator = optionalConfigurationFiles().objectEnumerator(); configEnumerator.hasMoreElements(); ) {
            String configFile = (String) configEnumerator.nextElement();
            File file = new File(configFile);
            if (file.exists() && file.isFile() && file.canRead()) {
                try {
                    String optionalPath = file.getCanonicalPath();
                    addIfPresent("Optional Configuration", optionalPath, propertiesPaths, projectsInfo);
                } catch (java.io.IOException ex) {
                    log.error("Failed to load configuration file '{}'.", file, ex);
                }
            } else {
                log.error("The optional configuration file '{}' either does not exist or could not be read.", file);
            }
        }
    }
    optionalPropertiesLoader(NSProperties.getProperty("user.name"), propertiesPaths, projectsInfo);
    /**
     * /etc/WebObjects/AppName/Properties -- per-Application-per-Machine properties
     */
    String applicationMachinePropertiesPath = ERXProperties.applicationMachinePropertiesPath("Properties");
    addIfPresent("Application-Machine Properties", applicationMachinePropertiesPath, propertiesPaths, projectsInfo);
    /**
     * Properties.dev -- per-Application-dev properties
     */
    String applicationDeveloperPropertiesPath = ERXProperties.applicationDeveloperProperties();
    addIfPresent("Application-Developer Properties", applicationDeveloperPropertiesPath, propertiesPaths, projectsInfo);
    /**
     * Properties.<userName> -- per-Application-per-User properties
     */
    String applicationUserPropertiesPath = ERXProperties.applicationUserProperties();
    addIfPresent("Application-User Properties", applicationUserPropertiesPath, propertiesPaths, projectsInfo);
    /*  Report the result */
    if (reportLoggingEnabled && projectsInfo.count() > 0 && log.isInfoEnabled()) {
        StringBuilder message = new StringBuilder();
        message.append("\n\n").append("ERXProperties has found the following Properties files: \n");
        message.append(projectsInfo.componentsJoinedByString("\n"));
        message.append('\n');
        message.append("ERXProperties currently has the following properties:\n");
        message.append(ERXProperties.logString(NSProperties._getProperties()));
        log.info(message.toString());
    }
    return propertiesPaths.immutableClone();
}
Also used : Enumeration(java.util.Enumeration) NSBundle(com.webobjects.foundation.NSBundle) NSArray(com.webobjects.foundation.NSArray) NSMutableArray(com.webobjects.foundation.NSMutableArray) IOException(java.io.IOException) File(java.io.File)

Example 17 with NSBundle

use of com.webobjects.foundation.NSBundle in project wonder-slim by undur.

the class ERXProperties method pathForResourceNamed.

/**
 * Determines the path of the specified Resource. This is done to get a
 * single entry point due to the deprecation of pathForResourceNamed
 *
 * @param fileName name of the file
 * @param frameworkName name of the framework, <code>null</code> or "app" for the application bundle
 * @param languages array of languages to get localized resource or <code>null</code>
 * @return the absolutePath method off of the file object
 */
private static String pathForResourceNamed(String fileName, String frameworkName, NSArray<String> languages) {
    String path = null;
    NSBundle bundle = "app".equals(frameworkName) ? NSBundle.mainBundle() : NSBundle.bundleForName(frameworkName);
    if (bundle != null && bundle.isJar()) {
        // FIXME: Changed log level to debug
        // This was emitting at every application startup, seemingly without purpose.
        // Since property loading seems to work fine anyway, I turned it to debug
        // and we're going to have to have a look at property loading in general later.
        log.debug("Can't get path when run as jar: {} - {}", frameworkName, fileName);
    } else {
        WOApplication application = WOApplication.application();
        if (application != null) {
            URL url = application.resourceManager().pathURLForResourceNamed(fileName, frameworkName, languages);
            if (url != null) {
                path = url.getFile();
            }
        } else if (bundle != null) {
            URL url = bundle.pathURLForResourcePath(fileName);
            if (url != null) {
                path = url.getFile();
            }
        }
    }
    return path;
}
Also used : NSBundle(com.webobjects.foundation.NSBundle) WOApplication(com.webobjects.appserver.WOApplication) URL(java.net.URL)

Aggregations

NSBundle (com.webobjects.foundation.NSBundle)17 NSArray (com.webobjects.foundation.NSArray)5 IOException (java.io.IOException)5 Enumeration (java.util.Enumeration)5 File (java.io.File)4 NSMutableArray (com.webobjects.foundation.NSMutableArray)3 URL (java.net.URL)3 WOApplication (com.webobjects.appserver.WOApplication)2 WODeployedBundle (com.webobjects.appserver._private.WODeployedBundle)2 NSDictionary (com.webobjects.foundation.NSDictionary)2 MalformedURLException (java.net.MalformedURLException)2 JarFile (java.util.jar.JarFile)2 NSForwardException (com.webobjects.foundation.NSForwardException)1 NSMutableDictionary (com.webobjects.foundation.NSMutableDictionary)1 NSNotification (com.webobjects.foundation.NSNotification)1 NSProperties (com.webobjects.foundation.NSProperties)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 Field (java.lang.reflect.Field)1