Search in sources :

Example 1 with NSArray

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

the class WOOgnl method convertOgnlConstantAssociations.

public void convertOgnlConstantAssociations(NSMutableDictionary associations) {
    for (Enumeration e = associations.keyEnumerator(); e.hasMoreElements(); ) {
        String name = (String) e.nextElement();
        WOAssociation association = (WOAssociation) associations.objectForKey(name);
        boolean isConstant = false;
        String keyPath = null;
        if (association instanceof WOConstantValueAssociation) {
            WOConstantValueAssociation constantAssociation = (WOConstantValueAssociation) association;
            // AK: this sucks, but there is no API to get at the value
            Object value = constantAssociation.valueInComponent(null);
            keyPath = value != null ? value.toString() : null;
            isConstant = true;
        } else if (association instanceof WOKeyValueAssociation) {
            keyPath = association.keyPath();
        } else if (association instanceof WOBindingNameAssociation) {
            WOBindingNameAssociation b = (WOBindingNameAssociation) association;
            // AK: strictly speaking, this is not correct, as we only get the first part of
            // the path. But take a look at WOBindingNameAssociation for a bit of fun...
            keyPath = "^" + b._parentBindingName;
        }
        if (keyPath != null) {
            if (!associationMappings.isEmpty()) {
                int index = name.indexOf(':');
                if (index > 0) {
                    String prefix = name.substring(0, index);
                    if (prefix != null) {
                        Class c = associationMappings.get(prefix);
                        if (c != null) {
                            String postfix = name.substring(index + 1);
                            WOAssociation newAssociation = createAssociationForClass(c, keyPath, isConstant);
                            associations.removeObjectForKey(name);
                            associations.setObjectForKey(newAssociation, postfix);
                        }
                    }
                }
            }
            if (isConstant && keyPath.startsWith(ognlBindingFlag())) {
                String ognlExpression = keyPath.substring(ognlBindingFlag().length(), keyPath.length());
                if (ognlExpression.length() > 0) {
                    WOAssociation newAssociation = new WOOgnlAssociation(ognlExpression);
                    NSArray keys = associations.allKeysForObject(association);
                    // + (keys.count() == 1 ? keys.lastObject() : keys) + " expression: " + ognlExpression);
                    if (keys.count() == 1) {
                        associations.setObjectForKey(newAssociation, keys.lastObject());
                    } else {
                        for (Enumeration ee = keys.objectEnumerator(); ee.hasMoreElements(); ) {
                            associations.setObjectForKey(newAssociation, e.nextElement());
                        }
                    }
                }
            }
        }
    }
}
Also used : WOAssociation(com.webobjects.appserver.WOAssociation) Enumeration(java.util.Enumeration) NSArray(com.webobjects.foundation.NSArray) WOBindingNameAssociation(com.webobjects.appserver._private.WOBindingNameAssociation) WOConstantValueAssociation(com.webobjects.appserver._private.WOConstantValueAssociation) WOKeyValueAssociation(com.webobjects.appserver._private.WOKeyValueAssociation)

Example 2 with NSArray

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

the class ERXExceptionUtilities method _printSingleStackTrace.

private static void _printSingleStackTrace(Throwable t, PrintWriter writer, int exceptionDepth, boolean cleanupStackTrace) {
    NSArray<Pattern> skipPatterns = ERXExceptionUtilities._skipPatterns;
    if (cleanupStackTrace && skipPatterns == null) {
        String skipPatternsFile = ERXProperties.stringForKey("er.extensions.stackTrace.skipPatternsFile");
        if (skipPatternsFile != null) {
            NSMutableArray<Pattern> mutableSkipPatterns = new NSMutableArray<>();
            Enumeration<String> frameworksEnum = ERXLocalizer.frameworkSearchPath().reverseObjectEnumerator();
            while (frameworksEnum.hasMoreElements()) {
                String framework = frameworksEnum.nextElement();
                URL path = WOApplication.application().resourceManager().pathURLForResourceNamed(skipPatternsFile, framework, null);
                if (path != null) {
                    try {
                        NSArray<String> skipPatternStrings = (NSArray<String>) ERXUtilities.readPropertyListFromFileInFramework(skipPatternsFile, framework, (NSArray) null, "utf-8");
                        if (skipPatternStrings != null) {
                            for (String skipPatternString : skipPatternStrings) {
                                try {
                                    mutableSkipPatterns.addObject(Pattern.compile(skipPatternString));
                                } catch (Throwable patternThrowable) {
                                    log.error("Skipping invalid exception pattern '{}' in '{}' in the framework '{}' ({})", skipPatternString, skipPatternsFile, framework, ERXExceptionUtilities.toParagraph(patternThrowable));
                                }
                            }
                        }
                    } catch (Throwable patternThrowable) {
                        log.error("Failed to read pattern file '{}' in the framework '{}' ({})", skipPatternsFile, framework, ERXExceptionUtilities.toParagraph(patternThrowable));
                    }
                }
            }
            skipPatterns = mutableSkipPatterns;
        }
        if (ERXProperties.booleanForKeyWithDefault("er.extensions.stackTrace.cachePatterns", true)) {
            if (skipPatterns == null) {
                ERXExceptionUtilities._skipPatterns = NSArray.EmptyArray;
            } else {
                ERXExceptionUtilities._skipPatterns = skipPatterns;
            }
        }
    }
    StackTraceElement[] elements = t.getStackTrace();
    indent(writer, exceptionDepth);
    if (exceptionDepth > 0) {
        writer.print("Caused by a ");
    }
    if (cleanupStackTrace) {
        writer.print(t.getClass().getSimpleName());
    } else {
        writer.print(t.getClass().getName());
    }
    String message = t.getLocalizedMessage();
    if (message != null) {
        writer.print(": ");
        writer.print(message);
    }
    writer.println();
    int stackDepth = 0;
    int skippedCount = 0;
    for (StackTraceElement element : elements) {
        boolean showElement = true;
        if (stackDepth > 0 && cleanupStackTrace && skipPatterns != null && !skipPatterns.isEmpty()) {
            String elementName = element.getClassName() + "." + element.getMethodName();
            for (Pattern skipPattern : skipPatterns) {
                if (skipPattern.matcher(elementName).matches()) {
                    showElement = false;
                    break;
                }
            }
        }
        if (!showElement) {
            skippedCount++;
        } else {
            if (skippedCount > 0) {
                indent(writer, exceptionDepth + 1);
                writer.println("   ... skipped " + skippedCount + " stack elements");
                skippedCount = 0;
            }
            indent(writer, exceptionDepth + 1);
            writer.print("at ");
            writer.print(element.getClassName());
            writer.print(".");
            writer.print(element.getMethodName());
            writer.print("(");
            if (element.isNativeMethod()) {
                writer.print("Native Method");
            } else if (element.getLineNumber() < 0) {
                writer.print(element.getFileName());
                writer.print(":Unknown");
            } else {
                writer.print(element.getFileName());
                writer.print(":");
                writer.print(element.getLineNumber());
            }
            writer.print(")");
            writer.println();
        }
        stackDepth++;
    }
    if (skippedCount > 0) {
        indent(writer, exceptionDepth + 1);
        writer.println("... skipped " + skippedCount + " stack elements");
    }
}
Also used : Pattern(java.util.regex.Pattern) NSArray(com.webobjects.foundation.NSArray) URL(java.net.URL) NSMutableArray(com.webobjects.foundation.NSMutableArray)

Example 3 with NSArray

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

the class ERXComponentUtilities method pathUrlForResourceNamed.

private static URL pathUrlForResourceNamed(WOResourceManager resourceManager, String resourceName, NSArray languages) {
    URL templateUrl = resourceManager.pathURLForResourceNamed(resourceName, null, languages);
    if (templateUrl == null) {
        NSArray frameworkBundles = NSBundle.frameworkBundles();
        if (frameworkBundles != null) {
            Enumeration frameworksEnum = frameworkBundles.objectEnumerator();
            while (templateUrl == null && frameworksEnum.hasMoreElements()) {
                NSBundle frameworkBundle = (NSBundle) frameworksEnum.nextElement();
                templateUrl = resourceManager.pathURLForResourceNamed(resourceName, frameworkBundle.name(), languages);
            }
        }
    }
    return templateUrl;
}
Also used : Enumeration(java.util.Enumeration) NSBundle(com.webobjects.foundation.NSBundle) NSArray(com.webobjects.foundation.NSArray) URL(java.net.URL)

Example 4 with NSArray

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

the class WOExceptionParser method _parseException.

private void _parseException() {
    StringWriter sWriter = new StringWriter();
    PrintWriter pWriter = new PrintWriter(sWriter, false);
    String string;
    NSArray lines;
    NSArray ignoredPackage;
    WOParsedErrorLine aLine;
    String line;
    int i, size;
    try {
        _exception.printStackTrace(pWriter);
        pWriter.close();
        // Added the try/catch as this throws in JDK 1.2
        sWriter.close();
        // aB.
        string = sWriter.toString();
        // We skip the name of the
        i = _exception.toString().length();
        // our parse
        if (string.length() > i + 2) {
            // certain errors don't contain a
            // stack trace
            // Skip the exception type and
            string = string.substring(i + 2);
            // message
            lines = NSArray.componentsSeparatedByString(string, "\n");
            ignoredPackage = _ignoredPackages();
            size = lines.count();
            _stackTrace = new NSMutableArray(size);
            for (i = 0; i < size; i++) {
                line = ((String) lines.objectAtIndex(i)).trim();
                if (line.startsWith("at ")) {
                    // If we don't have an open parenthesis it means that we
                    // have probably reach the latest stack trace.
                    aLine = new WOParsedErrorLine(line);
                    _verifyPackageForLine(aLine, ignoredPackage);
                    _stackTrace.add(aLine);
                }
            }
        }
    } catch (Throwable e) {
        NSLog.err.appendln("WOExceptionParser - exception collecting backtrace data " + e + " - Empty backtrace.");
        NSLog.err.appendln(e);
    }
    if (_stackTrace == null) {
        _stackTrace = new NSMutableArray();
    }
}
Also used : StringWriter(java.io.StringWriter) NSArray(com.webobjects.foundation.NSArray) NSMutableArray(com.webobjects.foundation.NSMutableArray) PrintWriter(java.io.PrintWriter)

Example 5 with NSArray

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

the class WOExceptionParser method _ignoredPackages.

private NSArray _ignoredPackages() {
    NSBundle bundle;
    String path, content;
    NSDictionary dic = null;
    NSMutableArray<NSBundle> allBundles = new NSMutableArray<>(NSBundle.frameworkBundles());
    NSMutableArray<String> ignored = new NSMutableArray<>();
    for (Enumeration enumerator = allBundles.objectEnumerator(); enumerator.hasMoreElements(); ) {
        bundle = (NSBundle) enumerator.nextElement();
        path = WOApplication.application().resourceManager().pathForResourceNamed("WOIgnoredPackage.plist", bundle.name(), null);
        if (path != null) {
            content = _stringFromFileSafely(path);
            if (content != null) {
                dic = (NSDictionary) NSPropertyListSerialization.propertyListFromString(content);
                if (dic != null && dic.containsKey("ignoredPackages")) {
                    @SuppressWarnings("unchecked") NSArray<String> tmpArray = (NSArray<String>) dic.objectForKey("ignoredPackages");
                    if (tmpArray != null && tmpArray.count() > 0) {
                        ignored.addObjectsFromArray(tmpArray);
                    }
                }
            }
        }
    }
    return ignored;
}
Also used : NSBundle(com.webobjects.foundation.NSBundle) Enumeration(java.util.Enumeration) NSArray(com.webobjects.foundation.NSArray) NSDictionary(com.webobjects.foundation.NSDictionary) NSMutableArray(com.webobjects.foundation.NSMutableArray)

Aggregations

NSArray (com.webobjects.foundation.NSArray)53 Enumeration (java.util.Enumeration)17 NSMutableArray (com.webobjects.foundation.NSMutableArray)13 NSBundle (com.webobjects.foundation.NSBundle)5 NSMutableDictionary (com.webobjects.foundation.NSMutableDictionary)5 File (java.io.File)4 List (java.util.List)4 WOAssociation (com.webobjects.appserver.WOAssociation)3 WOComponent (com.webobjects.appserver.WOComponent)3 NSDictionary (com.webobjects.foundation.NSDictionary)3 IOException (java.io.IOException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 WOElement (com.webobjects.appserver.WOElement)2 WOResponse (com.webobjects.appserver.WOResponse)2 WOConstantValueAssociation (com.webobjects.appserver._private.WOConstantValueAssociation)2 EOEvent (com.webobjects.eocontrol.EOEvent)2 EOSortOrdering (com.webobjects.eocontrol.EOSortOrdering)2 NSForwardException (com.webobjects.foundation.NSForwardException)2 NSKeyValueCoding (com.webobjects.foundation.NSKeyValueCoding)2