Search in sources :

Example 1 with IModificationWatcher

use of org.apache.wicket.util.watch.IModificationWatcher in project wicket by apache.

the class PropertiesFactory method load.

/**
 * @see org.apache.wicket.resource.IPropertiesFactory#load(java.lang.Class, java.lang.String)
 */
@Override
public Properties load(final Class<?> clazz, final String path) {
    // Check the cache
    Properties properties = null;
    if (propertiesCache != null) {
        properties = propertiesCache.get(path);
    }
    if (properties == null) {
        Iterator<IPropertiesLoader> iter = propertiesLoader.iterator();
        while ((properties == null) && iter.hasNext()) {
            IPropertiesLoader loader = iter.next();
            String fullPath = path + "." + loader.getFileExtension();
            // If not in the cache than try to load properties
            IResourceStream resourceStream = context.getResourceStreamLocator().locate(clazz, fullPath);
            if (resourceStream == null) {
                continue;
            }
            // Watch file modifications
            final IModificationWatcher watcher = context.getResourceWatcher(true);
            if (watcher != null) {
                addToWatcher(path, resourceStream, watcher);
            }
            ValueMap props = loadFromLoader(loader, resourceStream);
            if (props != null) {
                properties = new Properties(path, props);
            }
        }
        // Cache the lookup
        if (propertiesCache != null) {
            if (properties == null) {
                // Could not locate properties, store a placeholder
                propertiesCache.put(path, Properties.EMPTY_PROPERTIES);
            } else {
                propertiesCache.put(path, properties);
            }
        }
    }
    if (properties == Properties.EMPTY_PROPERTIES) {
        // Translate empty properties placeholder to null prior to returning
        properties = null;
    }
    return properties;
}
Also used : IModificationWatcher(org.apache.wicket.util.watch.IModificationWatcher) IResourceStream(org.apache.wicket.util.resource.IResourceStream) ValueMap(org.apache.wicket.util.value.ValueMap)

Example 2 with IModificationWatcher

use of org.apache.wicket.util.watch.IModificationWatcher in project wicket by apache.

the class MarkupCache method loadMarkupAndWatchForChanges.

/**
 * Load markup from an IResourceStream and add an {@link IChangeListener}to the
 * {@link ModificationWatcher} so that if the resource changes, we can remove it from the cache
 * automatically and subsequently reload when needed.
 *
 * @param container
 *            The original requesting markup container
 * @param markupResourceStream
 *            The markup stream to load and begin to watch
 * @param enforceReload
 *            The cache will be ignored and all, including inherited markup files, will be
 *            reloaded. Whatever is in the cache, it will be ignored
 * @return The markup in the stream
 */
private Markup loadMarkupAndWatchForChanges(final MarkupContainer container, final MarkupResourceStream markupResourceStream, final boolean enforceReload) {
    // @TODO the following code sequence looks very much like in loadMarkup. Can it be
    // optimized?
    final String cacheKey = markupResourceStream.getCacheKey();
    if (cacheKey != null) {
        if (enforceReload == false) {
            // get the location String
            String locationString = markupResourceStream.locationAsString();
            if (locationString == null) {
                // set the cache key as location string, because location string
                // couldn't be resolved.
                locationString = cacheKey;
            }
            Markup markup = markupCache.get(locationString);
            if (markup != null) {
                markupKeyCache.put(cacheKey, locationString);
                return markup;
            }
        }
        // Watch file in the future
        final IModificationWatcher watcher = application.getResourceSettings().getResourceWatcher(true);
        if (watcher != null) {
            watcher.add(markupResourceStream, new IChangeListener<IModifiable>() {

                @Override
                public void onChange(IModifiable modifiable) {
                    if (log.isDebugEnabled()) {
                        log.debug("Remove markup from watcher: " + markupResourceStream);
                    }
                    // Remove the markup from the cache. It will be reloaded
                    // next time when the markup is requested.
                    watcher.remove(markupResourceStream);
                    removeMarkup(cacheKey);
                }
            });
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Loading markup from " + markupResourceStream);
    }
    return loadMarkup(container, markupResourceStream, enforceReload);
}
Also used : IModificationWatcher(org.apache.wicket.util.watch.IModificationWatcher) IModifiable(org.apache.wicket.util.watch.IModifiable)

Example 3 with IModificationWatcher

use of org.apache.wicket.util.watch.IModificationWatcher in project wicket by apache.

the class MarkupCache method removeMarkup.

/**
 * Note that this method will be called from a "cleanup" thread which might not have a thread
 * local application.
 */
@Override
public final IMarkupFragment removeMarkup(final String cacheKey) {
    Args.notNull(cacheKey, "cacheKey");
    if (log.isDebugEnabled()) {
        log.debug("Removing from cache: " + cacheKey);
    }
    // Remove the markup from the cache
    String locationString = markupKeyCache.get(cacheKey);
    IMarkupFragment markup = (locationString != null ? markupCache.get(locationString) : null);
    if (markup == null) {
        return null;
    }
    // Found an entry: actual markup or Markup.NO_MARKUP. Null values are not possible
    // because of ConcurrentHashMap.
    markupCache.remove(locationString);
    if (log.isDebugEnabled()) {
        log.debug("Removed from cache: " + locationString);
    }
    // If a base markup file has been removed from the cache then
    // the derived markup should be removed as well.
    removeMarkupWhereBaseMarkupIsNoLongerInTheCache();
    // And now remove all watcher entries associated with markup
    // resources no longer in the cache.
    // Note that you can not use Application.get() since removeMarkup() will be called from a
    // ModificationWatcher thread which has no associated Application.
    IModificationWatcher watcher = application.getResourceSettings().getResourceWatcher(false);
    if (watcher != null) {
        Iterator<IModifiable> iter = watcher.getEntries().iterator();
        while (iter.hasNext()) {
            IModifiable modifiable = iter.next();
            if (modifiable instanceof MarkupResourceStream) {
                if (!isMarkupCached((MarkupResourceStream) modifiable)) {
                    iter.remove();
                    if (log.isDebugEnabled()) {
                        log.debug("Removed from watcher: " + modifiable);
                    }
                }
            }
        }
    }
    return markup;
}
Also used : IModificationWatcher(org.apache.wicket.util.watch.IModificationWatcher) IModifiable(org.apache.wicket.util.watch.IModifiable)

Example 4 with IModificationWatcher

use of org.apache.wicket.util.watch.IModificationWatcher in project wicket by apache.

the class WebApplication method internalDestroy.

/**
 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT.
 */
@Override
public void internalDestroy() {
    // destroy the resource watcher
    IModificationWatcher resourceWatcher = getResourceSettings().getResourceWatcher(false);
    if (resourceWatcher != null) {
        resourceWatcher.destroy();
    }
    IFileCleaner fileCleaner = getResourceSettings().getFileCleaner();
    if (fileCleaner != null) {
        fileCleaner.destroy();
    }
    super.internalDestroy();
}
Also used : IModificationWatcher(org.apache.wicket.util.watch.IModificationWatcher) IFileCleaner(org.apache.wicket.util.file.IFileCleaner)

Aggregations

IModificationWatcher (org.apache.wicket.util.watch.IModificationWatcher)4 IModifiable (org.apache.wicket.util.watch.IModifiable)2 IFileCleaner (org.apache.wicket.util.file.IFileCleaner)1 IResourceStream (org.apache.wicket.util.resource.IResourceStream)1 ValueMap (org.apache.wicket.util.value.ValueMap)1