Search in sources :

Example 6 with NSForwardException

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

the class ERXAppRunner method initApp.

/**
 * Initializes your WOApplication programmatically (for use in test cases and main methods)
 * with the assumption that the current directory is your main bundle URL.
 *
 * @param applicationSubclass your Application subclass
 * @param args the commandline arguments for your application
 */
public static void initApp(Class applicationSubclass, String[] args) {
    try {
        File woaFolder = new File(".").getCanonicalFile();
        if (!woaFolder.getName().endsWith(".woa")) {
            if (new File(woaFolder, ".project").exists()) {
                File buildFolder = new File(new File(woaFolder, "build"), woaFolder.getName() + ".woa");
                if (buildFolder.exists()) {
                    woaFolder = buildFolder;
                } else {
                    File distFolder = new File(new File(woaFolder, "dist"), woaFolder.getName() + ".woa");
                    if (distFolder.exists()) {
                        woaFolder = distFolder;
                    } else {
                    // Bundle-less builds. Yay!
                    // throw new IllegalArgumentException("You must run your application from a .woa folder to call this method.");
                    }
                }
            }
        }
        initApp(null, woaFolder.toURI().toURL(), applicationSubclass, args);
    } catch (IOException e) {
        throw new NSForwardException(e);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) NSForwardException(com.webobjects.foundation.NSForwardException)

Example 7 with NSForwardException

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

the class ERXSimpleTemplateParser method parseTemplateWithObject.

/**
 * This method replaces the keys enclosed between the
 * delimiter with the values found in object and otherObject.
 * It first looks for a value in object, and then in otherObject
 * if the key is not found in object. Therefore, otherObject is
 * a good place to store default values while object is a
 * good place to override default values.
 * <p>
 * When the value is not found in both object and otherObject,
 * it will replace the key with the undefined key label which
 * defaults to "?". You can set the label via the constructor
 * {@link #ERXSimpleTemplateParser(String)}. Note that a <code>null</code>
 * result will also output the label, so you might want to have the empty
 * string as the undefined key label.
 *
 * @param template to use to parse
 * @param delimiter to use to check for keys
 * @param object to resolve keys off of
 * @param otherObject object used to resolve default keys
 * @return parsed template with keys replaced
 */
public String parseTemplateWithObject(String template, String delimiter, Object object, Object otherObject) {
    if (template == null)
        throw new IllegalArgumentException("Attempting to parse null template!");
    if (object == null) {
        throw new IllegalArgumentException("Attempting to parse template with null object!");
    }
    if (delimiter == null) {
        delimiter = DEFAULT_DELIMITER;
    }
    if (!isLoggingDisabled) {
        log.debug("Parsing template: {} with delimiter: {} object: {}", template, delimiter, object);
        log.debug("Template: {}", template);
        log.debug("Delim: {}", delimiter);
        log.debug("otherObject: {}", otherObject);
    }
    NSArray components = NSArray.componentsSeparatedByString(template, delimiter);
    if (!isLoggingDisabled) {
        log.debug("Components: {}", components);
    }
    // if the template starts with delim, the first component will be a zero-length string
    boolean deriveElement = false;
    StringBuilder sb = new StringBuilder();
    Object[] objects;
    if (otherObject != null) {
        objects = new Object[] { object, otherObject };
    } else {
        objects = new Object[] { object };
    }
    for (Enumeration e = components.objectEnumerator(); e.hasMoreElements(); ) {
        String element = (String) e.nextElement();
        if (!isLoggingDisabled) {
            log.debug("Processing Element: {}", element);
        }
        if (deriveElement) {
            if (!isLoggingDisabled) {
                log.debug("Deriving value ...");
            }
            if (element.length() == 0) {
                throw new IllegalArgumentException("\"\" is not a valid keypath in template: " + template);
            }
            Object result = _undefinedKeyLabel;
            for (int i = 0; i < objects.length; i++) {
                Object o = objects[i];
                if (o != null && result == _undefinedKeyLabel) {
                    try {
                        if (!isLoggingDisabled) {
                            log.debug("calling valueForKeyPath({}, {})", o, element);
                        }
                        result = doGetValue(element, o);
                        // key is not defined. (NSDictionary doesn't seem to throw the exception.)
                        if (result == null) {
                            result = _undefinedKeyLabel;
                        }
                    } catch (NSKeyValueCoding.UnknownKeyException t) {
                        result = _undefinedKeyLabel;
                    } catch (Throwable t) {
                        throw new NSForwardException(t, "An exception occured while parsing element, " + element + ", of template, \"" + template + "\": " + t.getMessage());
                    }
                }
            }
            if (result == _undefinedKeyLabel) {
                if (!isLoggingDisabled) {
                    log.debug("Could not find a value for '{}' of template, '{}' in either the object or extra data.", element, template);
                }
            }
            sb.append(result.toString());
            deriveElement = false;
        } else {
            if (element.length() > 0) {
                sb.append(element);
            }
            deriveElement = true;
        }
        if (!isLoggingDisabled) {
            log.debug("Buffer: {}", sb);
        }
    }
    return sb.toString();
}
Also used : Enumeration(java.util.Enumeration) NSArray(com.webobjects.foundation.NSArray) NSKeyValueCoding(com.webobjects.foundation.NSKeyValueCoding) NSForwardException(com.webobjects.foundation.NSForwardException)

Example 8 with NSForwardException

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

the class ERXExtensions method bundleDidLoad.

public void bundleDidLoad(NSNotification n) {
    if (_initialized) {
        return;
    }
    _initialized = true;
    try {
        // This will load any optional configuration files, ensures that WOOutputPath's was processed with this @@ variable substitution.
        // WOApplication uses WOOutputPath in its constructor so we need to modify it before calling the constructor.
        ERXConfigurationManager.defaultManager().initialize();
        // GN: configure logging with optional custom subclass of ERXLogger
        String className = ERXProperties.stringForKey("er.extensions.erxloggerclass");
        if (className != null) {
            Class loggerClass = Class.forName(className);
            Method method = loggerClass.getDeclaredMethod(ERXLogger.CONFIGURE_LOGGING_WITH_SYSTEM_PROPERTIES, (Class[]) null);
            method.invoke(loggerClass, (Object[]) null);
        } else {
            // default behaviour:
            ERXLogger.configureLoggingWithSystemProperties();
        }
    } catch (Exception e) {
        throw NSForwardException._runtimeExceptionForThrowable(e);
    }
}
Also used : Method(java.lang.reflect.Method) NSForwardException(com.webobjects.foundation.NSForwardException)

Aggregations

NSForwardException (com.webobjects.foundation.NSForwardException)8 IOException (java.io.IOException)3 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 WOAssociation (com.webobjects.appserver.WOAssociation)1 WOComponent (com.webobjects.appserver.WOComponent)1 WODeployedBundle (com.webobjects.appserver._private.WODeployedBundle)1 NSArray (com.webobjects.foundation.NSArray)1 NSDictionary (com.webobjects.foundation.NSDictionary)1 NSKeyValueCoding (com.webobjects.foundation.NSKeyValueCoding)1 NSMutableDictionary (com.webobjects.foundation.NSMutableDictionary)1 ERXMutableURL (er.extensions.foundation.ERXMutableURL)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Method (java.lang.reflect.Method)1 MessageDigest (java.security.MessageDigest)1 Enumeration (java.util.Enumeration)1