Search in sources :

Example 6 with NSBundle

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

the class ERXLoader method collectMainProps.

private void collectMainProps(String userName) {
    NSBundle mainBundle = mainBundle();
    if (mainBundle != null) {
        mainUserProps = readProperties(mainBundle, "Properties." + userName);
        mainProps = readProperties(mainBundle, "Properties");
    }
    if (mainProps == null) {
        String woUserDir = NSProperties.getProperty("webobjects.user.dir");
        if (woUserDir == null) {
            woUserDir = System.getProperty("user.dir");
        }
        mainUserProps = readProperties(new File(woUserDir, "Contents" + File.separator + "Resources" + File.separator + "Properties." + userName));
        mainProps = readProperties(new File(woUserDir, "Contents" + File.separator + "Resources" + File.separator + "Properties"));
    }
    if (mainProps == null) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        try {
            Enumeration<URL> jarBundles = classLoader.getResources("Resources/Properties");
            URL propertiesPath = null;
            URL userPropertiesPath = null;
            String mainBundleName = NSProperties._mainBundleName();
            // Look for a jar file name like: myapp[-1.0][-SNAPSHOT].jar
            Pattern mainBundleJarPattern = Pattern.compile("\\b" + mainBundleName.toLowerCase() + "[-\\.\\d]*(snapshot)?\\.jar");
            while (jarBundles.hasMoreElements()) {
                URL url = jarBundles.nextElement();
                String urlAsString = url.toString();
                if (mainBundleJarPattern.matcher(urlAsString.toLowerCase()).find()) {
                    try {
                        propertiesPath = new URL(URLDecoder.decode(urlAsString, StandardCharsets.UTF_8));
                        userPropertiesPath = new URL(propertiesPath.toExternalForm() + userName);
                    } catch (MalformedURLException exception) {
                        exception.printStackTrace();
                    }
                    break;
                }
            }
            mainProps = readProperties(propertiesPath);
            mainUserProps = readProperties(userPropertiesPath);
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
    if (mainProps == null) {
        throw new IllegalStateException("Main bundle 'Properties' file can't be read.  Did you run as a Java Application instead of a WOApplication in WOLips?\nPlease post your deployment configuration in the Wonder mailing list.");
    }
}
Also used : Pattern(java.util.regex.Pattern) MalformedURLException(java.net.MalformedURLException) NSBundle(com.webobjects.foundation.NSBundle) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

Example 7 with NSBundle

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

the class ERXProperties method applicationMachinePropertiesPath.

/**
 * Returns the path to the application-specific system-wide file "fileName".  By default this path is /etc/WebObjects,
 * and the application name will be appended.  For instance, if you are asking for the MyApp Properties file for the
 * system, it would go in /etc/WebObjects/MyApp/Properties.
 *
 * @param fileName the Filename
 * @return the path, or null if the path does not exist
 */
private static String applicationMachinePropertiesPath(String fileName) {
    String applicationMachinePropertiesPath = null;
    String machinePropertiesPath = NSProperties.getProperty("er.extensions.ERXProperties.machinePropertiesPath", "/etc/WebObjects");
    WOApplication application = WOApplication.application();
    String applicationName;
    if (application != null) {
        applicationName = application.name();
    } else {
        applicationName = NSProperties.getProperty("WOApplicationName");
        if (applicationName == null) {
            NSBundle mainBundle = NSBundle.mainBundle();
            if (mainBundle != null) {
                applicationName = mainBundle.name();
            }
            if (applicationName == null) {
                applicationName = "Unknown";
            }
        }
    }
    File applicationPropertiesFile = new File(machinePropertiesPath + File.separator + fileName);
    if (!applicationPropertiesFile.exists()) {
        applicationPropertiesFile = new File(machinePropertiesPath + File.separator + applicationName + File.separator + fileName);
    }
    if (applicationPropertiesFile.exists()) {
        try {
            applicationMachinePropertiesPath = applicationPropertiesFile.getCanonicalPath();
        } catch (IOException e) {
            log.error("Failed to load machine Properties file '{}'.", fileName, e);
        }
    }
    return applicationMachinePropertiesPath;
}
Also used : NSBundle(com.webobjects.foundation.NSBundle) IOException(java.io.IOException) File(java.io.File) WOApplication(com.webobjects.appserver.WOApplication)

Example 8 with NSBundle

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

the class ERXUtilities method versionStringForFrameworkNamed.

/**
 * Returns the version string of the given framework.
 * It checks <code>CFBundleShortVersionString</code> property
 * in the <code>info.plist</code> resource and returns
 * a trimmed version of the value.
 *
 * @param frameworkName name
 * @return version number as string; can be null-string when the framework is not found or the framework doesn't have the value of <code>CFBundleShortVersionString</code> in its <code>info.plist</code> resource.
 * @see #webObjectsVersion()
 */
private static String versionStringForFrameworkNamed(String frameworkName) {
    NSBundle bundle = NSBundle.bundleForName(frameworkName);
    if (bundle == null) {
        return "";
    }
    final String dictString = new String(bundle.bytesForResourcePath("Info.plist"));
    final NSDictionary versionDictionary = NSPropertyListSerialization.dictionaryForString(dictString);
    final String versionString = (String) versionDictionary.objectForKey("CFBundleShortVersionString");
    // trim() removes the line ending char
    return versionString == null ? "" : versionString.trim();
}
Also used : NSBundle(com.webobjects.foundation.NSBundle) NSDictionary(com.webobjects.foundation.NSDictionary)

Example 9 with NSBundle

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

the class ERXUtilities method informationForBundles.

private static NSMutableDictionary<String, Object> informationForBundles() {
    NSMutableDictionary<String, Object> extraInfo = new NSMutableDictionary<>();
    NSMutableDictionary<String, Object> bundleVersions = new NSMutableDictionary<String, Object>();
    for (Enumeration bundles = NSBundle._allBundlesReally().objectEnumerator(); bundles.hasMoreElements(); ) {
        NSBundle bundle = (NSBundle) bundles.nextElement();
        String version = versionStringForFrameworkNamed(bundle.name());
        if (version == null) {
            version = "No version provided";
        }
        bundleVersions.setObjectForKey(version, bundle.name());
    }
    extraInfo.setObjectForKey(bundleVersions, "Bundles");
    return extraInfo;
}
Also used : Enumeration(java.util.Enumeration) NSBundle(com.webobjects.foundation.NSBundle) NSMutableDictionary(com.webobjects.foundation.NSMutableDictionary)

Example 10 with NSBundle

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

the class ERXUtilities method stringFromResource.

/**
 * Retrieves a given string for a given name, extension and bundle.
 *
 * @param name of the resource
 * @param extension of the resource, example: txt or rtf
 * @param bundle to look for the resource in
 * @return string of the given file specified in the bundle
 */
public static String stringFromResource(String name, String extension, String bundleName) {
    NSBundle bundle = NSBundle.bundleForName(bundleName);
    if (bundle == null) {
        bundle = NSBundle.mainBundle();
    }
    String path = bundle.resourcePathForLocalizedResourceNamed(name + (extension == null || extension.length() == 0 ? "" : "." + extension), null);
    if (path != null) {
        try (InputStream stream = bundle.inputStreamForResourcePath(path)) {
            byte[] bytes = stream.readAllBytes();
            return new String(bytes);
        } catch (IOException e) {
            log.warn("IOException when stringFromResource({}.{} in bundle {}", name, extension, bundle.name());
        }
    }
    return null;
}
Also used : NSBundle(com.webobjects.foundation.NSBundle) InputStream(java.io.InputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

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