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.");
}
}
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;
}
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();
}
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;
}
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;
}
Aggregations