Search in sources :

Example 1 with NSNotification

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

the class ERXLoader method bundleDidLoad.

/**
 * Will be called after each bundle load. We use it to know when the last
 * bundle loaded so we can post a notification for it. Note that the bundles
 * will get loaded in the order of the classpath but the main bundle will
 * get loaded last. So in order to set the properties correctly, we first
 * add all the props that are not already set, then we add the main bundle
 * and the WebObjects.properties and finally the command line props.
 */
public void bundleDidLoad(NSNotification n) {
    NSBundle bundle = (NSBundle) n.object();
    if (allFrameworks.contains(bundle.name())) {
        allFrameworks.remove(bundle.name());
        debugMsg("Loaded " + bundle.name() + ". Remaining: " + allFrameworks);
    } else if (bundle.isFramework()) {
        debugMsg("Loaded unexpected framework bundle '" + bundle.name() + "'. Ensure your build.properties settings like project.name match the bundle name (including case).");
    }
    if (allBundleProps == null) {
        allBundleProps = new Properties();
    }
    String userName = propertyFromCommandLineFirst("user.name");
    applyIfUnset(readProperties(bundle, "Properties." + userName));
    applyIfUnset(readProperties(bundle, null));
    if (allFrameworks.size() == 0) {
        mainProps = null;
        mainUserProps = null;
        collectMainProps(userName);
        allBundleProps.putAll(mainProps);
        if (mainUserProps != null) {
            allBundleProps.putAll(mainUserProps);
        }
        String userHome = propertyFromCommandLineFirst("user.home");
        Properties userHomeProps = null;
        if (userHome != null && userHome.length() > 0) {
            userHomeProps = readProperties(new File(userHome, "WebObjects.properties"));
        }
        if (userHomeProps != null) {
            allBundleProps.putAll(userHomeProps);
        }
        Properties props = NSProperties._getProperties();
        props.putAll(allBundleProps);
        NSProperties._setProperties(props);
        insertCommandLineArguments();
        if (userHomeProps != null) {
            urls.add(0, urls.remove(urls.size() - 1));
        }
        if (mainUserProps != null) {
            urls.add(0, urls.remove(urls.size() - 1));
        }
        urls.add(0, urls.remove(urls.size() - 1));
        // System.out.print(urls);
        NSNotificationCenter.defaultCenter().postNotification(new NSNotification(ERXApplication.AllBundlesLoadedNotification, NSKeyValueCoding.NullValue));
    }
}
Also used : NSBundle(com.webobjects.foundation.NSBundle) NSNotification(com.webobjects.foundation.NSNotification) NSProperties(com.webobjects.foundation.NSProperties) Properties(java.util.Properties) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with NSNotification

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

the class ERXLowMemoryHandler method shouldQuit.

/**
 * Handles the potentially fatal OutOfMemoryError by quitting the
 * application ASAP. Broken out into a separate method to make custom error
 * handling easier, ie. generating your own error pages in production, etc.
 *
 * @param throwable to check if it is a fatal exception.
 * @return true if we should quit
 */
public boolean shouldQuit(Throwable throwable) {
    boolean shouldQuit = false;
    if (throwable instanceof Error) {
        if (throwable instanceof OutOfMemoryError) {
            boolean shouldExitOnOOMError = ERXProperties.booleanForKeyWithDefault("er.extensions.AppShouldExitOnOutOfMemoryError", true);
            shouldQuit = shouldExitOnOOMError;
            // what we do is set up a last-resort buffer during startup
            if (lowMemBuffer != null) {
                Runtime.getRuntime().freeMemory();
                try {
                    lowMemBuffer = null;
                    System.gc();
                    log.error("Ran out of memory, sending notification to clear caches");
                    log.error("Ran out of memory, sending notification to clear caches", throwable);
                    NSNotificationCenter.defaultCenter().postNotification(new NSNotification(LowMemoryNotification, this));
                    shouldQuit = false;
                    // try to reclaim our twice of our buffer
                    // if this worked maybe we can continue running
                    lowMemBuffer = new byte[lowMemBufferSize * 2];
                    // shrink buffer to normal size
                    lowMemBuffer = new byte[lowMemBufferSize];
                } catch (Throwable ex) {
                    shouldQuit = shouldExitOnOOMError;
                }
            }
            // We first log just in case the log4j call puts us in a bad state.
            if (shouldQuit) {
                NSLog.err.appendln("Ran out of memory, killing this instance");
                log.error("Ran out of memory, killing this instance");
                log.error("Ran out of memory, killing this instance", throwable);
            }
        } else {
            // We log just in case the log4j call puts us in a bad state.
            NSLog.err.appendln("java.lang.Error \"" + throwable.getClass().getName() + "\" occured.");
            log.error("java.lang.Error \"" + throwable.getClass().getName() + "\" occured.", throwable);
        }
    }
    return shouldQuit;
}
Also used : NSNotification(com.webobjects.foundation.NSNotification)

Example 3 with NSNotification

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

the class ERXFileNotificationCenter method fileHasChanged.

/**
 * Only used internally. Notifies all of the observers who have been
 * registered for the given file.
 *
 * @param file
 *            file that has changed
 */
protected void fileHasChanged(File file) {
    NSMutableSet observers = (NSMutableSet) _observersByFilePath.objectForKey(cacheKeyForFile(file));
    if (observers == null)
        log.warn("Unable to find observers for file: {}", file);
    else {
        NSNotification notification = new NSNotification(FileDidChange, file);
        for (Enumeration e = observers.objectEnumerator(); e.hasMoreElements(); ) {
            _ObserverSelectorHolder holder = (_ObserverSelectorHolder) e.nextElement();
            try {
                holder.selector.invoke(holder.observer, notification);
            } catch (Exception ex) {
                log.error("Catching exception when invoking method on observer: {}", ex, ex);
            }
        }
        registerLastModifiedDateForFile(file);
    }
}
Also used : Enumeration(java.util.Enumeration) NSNotification(com.webobjects.foundation.NSNotification) NSMutableSet(com.webobjects.foundation.NSMutableSet) IOException(java.io.IOException)

Example 4 with NSNotification

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

the class ERXLowMemoryHandler method checkMemory.

/**
 * <p>
 * Checks if the free memory is less than the threshold given in
 * <code>er.extensions.ERXApplication.memoryStarvedThreshold</code> (should
 * be set to around 0.90 meaning 90% of total memory or 100 meaning 100 MB
 * of minimal available memory) and if it is greater start to refuse new
 * sessions until more memory becomes available. This helps when the
 * application is becoming unresponsive because it's more busy garbage
 * collecting than processing requests. The default is to do nothing unless
 * the property is set. This method is called on each request, but garbage
 * collection will be done only every minute.
 * </p>
 *
 * <p>
 * Additionally, you can set
 * <code>er.extensions.ERXApplication.memoryLowThreshold</code>, which you
 * can set at a higher "warning" level, before the situation is critical.
 * </p>
 *
 * <p>
 * Both of these methods post notifications both at the start of the event
 * as well as the end of the event
 * (LowMemoryNotification/LowMemoryResolvedNotification and
 * StarvedMemoryNotification and StarvedMemoryResolvedNotification).
 * </p>
 *
 * @author ak
 */
public void checkMemory() {
    boolean memoryLow = checkMemory(_memoryLowThreshold, false);
    if (memoryLow != _isMemoryLow) {
        if (!memoryLow) {
            log.warn("App is no longer low on memory");
            NSNotificationCenter.defaultCenter().postNotification(new NSNotification(LowMemoryResolvedNotification, this));
        } else {
            log.error("App is low on memory");
            NSNotificationCenter.defaultCenter().postNotification(new NSNotification(LowMemoryNotification, this));
        }
        _isMemoryLow = memoryLow;
    }
    boolean memoryStarved = checkMemory(_memoryStarvedThreshold, true);
    if (memoryStarved != _isMemoryStarved) {
        if (!memoryStarved) {
            log.warn("App is no longer starved, handling new sessions again");
            NSNotificationCenter.defaultCenter().postNotification(new NSNotification(StarvedMemoryResolvedNotification, this));
        } else {
            log.error("App is starved, starting to refuse new sessions");
            NSNotificationCenter.defaultCenter().postNotification(new NSNotification(StarvedMemoryNotification, this));
        }
        _isMemoryStarved = memoryStarved;
    }
}
Also used : NSNotification(com.webobjects.foundation.NSNotification)

Example 5 with NSNotification

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

the class ERXApplication method finishInitialization.

/**
 * Notification method called when the application posts the notification {@link WOApplication#ApplicationWillFinishLaunchingNotification}.
 * This method calls subclasses' {@link #finishInitialization} method.
 *
 * @param n notification posted after WOApplication has been constructed, but before the application is ready for accepting requests.
 */
public final void finishInitialization(NSNotification n) {
    finishInitialization();
    NSNotificationCenter.defaultCenter().postNotification(new NSNotification(ERXApplication.ApplicationDidFinishInitializationNotification, this));
}
Also used : NSNotification(com.webobjects.foundation.NSNotification)

Aggregations

NSNotification (com.webobjects.foundation.NSNotification)5 NSBundle (com.webobjects.foundation.NSBundle)1 NSMutableSet (com.webobjects.foundation.NSMutableSet)1 NSProperties (com.webobjects.foundation.NSProperties)1 File (java.io.File)1 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 Properties (java.util.Properties)1 JarFile (java.util.jar.JarFile)1