Search in sources :

Example 1 with CacheMapping

use of com.sun.appserv.web.cache.mapping.CacheMapping in project Payara by payara.

the class CacheManager method start.

/**
 * Start this Context component.
 * @exception LifecycleException if a startup error occurs
 */
public void start() throws LifecycleException {
    if (!enabled)
        return;
    // create the default cache
    try {
        defaultCache = createCache(maxEntries, cacheClassName);
    } catch (Exception e) {
        _logger.log(Level.WARNING, LogFacade.CACHE_MANAGER_EXCEPTION_CREATING_CACHE, e);
        throw new LifecycleException(_rb.getString(LogFacade.CACHE_MANAGER_EXCEPTION_CREATING_CACHE), e);
    }
    // initialize the "default" helper
    defaultHelper = new DefaultCacheHelper();
    defaultHelper.setCacheManager(this);
    defaultHelper.init(context, defaultHelperProps);
    // initialize the custom cache-helpers
    Iterator<String> helperNames = helperDefs.keySet().iterator();
    while (helperNames.hasNext()) {
        String name = helperNames.next();
        HashMap<String, String> map = helperDefs.get(name);
        try {
            String className = map.get("class-name");
            CacheHelper helper = loadCacheHelper(className);
            helper.init(context, map);
            cacheHelpers.put(name, helper);
        } catch (Exception e) {
            String msg = _rb.getString(LogFacade.CACHE_MANAGER_EXCEPTION_INITIALIZING_CACHE_HELPER);
            Object[] params = { name };
            msg = MessageFormat.format(msg, params);
            throw new LifecycleException(msg, e);
        }
    }
    // cache-mappings are ordered by the associated filter name
    Iterator<String> filterNames = cacheMappings.keySet().iterator();
    while (filterNames.hasNext()) {
        String name = filterNames.next();
        CacheMapping mapping = cacheMappings.get(name);
        String helperNameRef = mapping.getHelperNameRef();
        CacheHelper helper;
        if (helperNameRef == null || helperNameRef.equals("default")) {
            helper = defaultHelper;
        } else {
            helper = cacheHelpers.get(helperNameRef);
        }
        cacheHelpersByFilterName.put(name, helper);
    }
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) CacheMapping(com.sun.appserv.web.cache.mapping.CacheMapping) String(java.lang.String) LifecycleException(org.apache.catalina.LifecycleException)

Example 2 with CacheMapping

use of com.sun.appserv.web.cache.mapping.CacheMapping in project Payara by payara.

the class DefaultCacheHelper method getCacheKey.

/**
 * getCacheKey: generate the key to be used to cache this request
 *  @param request incoming <code>HttpServletRequest</code>
 *  @return key string used to access the cache entry.
 *  Key is composed of: servletPath + a concatenation of the field values in
 *  the request; all key field names must be found in the appropriate scope.
 */
public String getCacheKey(HttpServletRequest request) {
    // cache mapping associated with the request
    CacheMapping mapping = lookupCacheMapping(request);
    if (isKeyGeneratorChecked == false && attrKeyGenerator != null) {
        try {
            keyGenerator = (CacheKeyGenerator) context.getAttribute(attrKeyGenerator);
        } catch (ClassCastException cce) {
            _logger.log(Level.WARNING, LogFacade.CACHE_DEFAULT_HELP_ILLEGAL_KET_GENERATOR, cce);
        }
        isKeyGeneratorChecked = true;
    }
    if (keyGenerator != null) {
        String key = keyGenerator.getCacheKey(context, request);
        if (key != null)
            return key;
    }
    StringBuilder sb = new StringBuilder(128);
    sb.append(request.getServletPath());
    // append the key fields
    Field[] keys = mapping.getKeyFields();
    for (int i = 0; i < keys.length; i++) {
        Object value = keys[i].getValue(context, request);
        // all defined key field must be present
        if (value == null) {
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, LogFacade.REQUIRED_KEY_FIELDS_NOT_FOUND, request.getServletPath());
            }
            return null;
        }
        sb.append(";");
        sb.append(KEY_PREFIXES[keys[i].getScope()]);
        sb.append(keys[i].getName());
        sb.append("=");
        sb.append(value);
    }
    return sb.toString();
}
Also used : Field(com.sun.appserv.web.cache.mapping.Field) ConstraintField(com.sun.appserv.web.cache.mapping.ConstraintField) CacheMapping(com.sun.appserv.web.cache.mapping.CacheMapping)

Example 3 with CacheMapping

use of com.sun.appserv.web.cache.mapping.CacheMapping in project Payara by payara.

the class DefaultCacheHelper method isRefreshNeeded.

/**
 * isRefreshNeeded: is the response to given request be refreshed?
 *  @param request incoming <code>HttpServletRequest</code> object
 *  @return <code>true</code> if the response needs to be refreshed.
 *  or return <code>false</code> if the results of this request
 *  don't need to be refreshed.
 *
 *  XXX: 04/16/02 right now there is no configurability for this in
 *  ias-web.xml; should add a refresh-field element there:
 *  <refresh-field name="refresh" scope="request.parameter" />
 */
public boolean isRefreshNeeded(HttpServletRequest request) {
    boolean result = false;
    // cache mapping associated with the request
    CacheMapping mapping = lookupCacheMapping(request);
    Field field = mapping.getRefreshField();
    if (field != null) {
        Object value = field.getValue(context, request);
        // the field's string representation must be "true" or "false"
        if (value != null && "true".equals(value.toString())) {
            result = true;
        }
    }
    return result;
}
Also used : Field(com.sun.appserv.web.cache.mapping.Field) ConstraintField(com.sun.appserv.web.cache.mapping.ConstraintField) CacheMapping(com.sun.appserv.web.cache.mapping.CacheMapping)

Example 4 with CacheMapping

use of com.sun.appserv.web.cache.mapping.CacheMapping in project Payara by payara.

the class DefaultCacheHelper method isCacheable.

/**
 * isCacheable: is the response to given request cachebale?
 *  @param request incoming <code>HttpServletRequest</code> object
 *  @return <code>true</code> if the response could be cached.
 *  or return <code>false</code> if the results of this request
 *  must not be cached.
 *
 *  Applies pre-configured cacheability constraints in the cache-mapping;
 *  all constraints must pass for this to be cacheable.
 */
public boolean isCacheable(HttpServletRequest request) {
    boolean result = false;
    // cache mapping associated with the request
    CacheMapping mapping = lookupCacheMapping(request);
    // check if the method is in the allowed methods list
    if (mapping.findMethod(request.getMethod())) {
        result = true;
        ConstraintField[] fields = mapping.getConstraintFields();
        // apply all the constraints
        for (int i = 0; i < fields.length; i++) {
            if (!fields[i].applyConstraints(context, request)) {
                result = false;
                break;
            }
        }
    }
    return result;
}
Also used : ConstraintField(com.sun.appserv.web.cache.mapping.ConstraintField) CacheMapping(com.sun.appserv.web.cache.mapping.CacheMapping)

Example 5 with CacheMapping

use of com.sun.appserv.web.cache.mapping.CacheMapping in project Payara by payara.

the class CacheModule method configureResponseCache.

/**
 * configure ias-web response cache
 * @param app WebModule containing the cache
 * @param bean ias-web app config bean
 * @throws Exception
 *
 * read the configuration and setup the runtime support for caching in a
 * application.
 */
public static CacheManager configureResponseCache(WebModule app, SunWebApp bean) throws Exception {
    Cache cacheConfig = ((SunWebAppImpl) bean).getCache();
    // is cache configured?
    if (cacheConfig == null) {
        return null;
    }
    if (logger.isLoggable(Level.FINE)) {
        logger.log(Level.FINE, LogFacade.CONFIGURE_CACHE, app.getPath());
    }
    // create the CacheManager object for this app
    CacheManager manager = new CacheManager();
    String name, value;
    value = cacheConfig.getAttributeValue(Cache.ENABLED);
    if (value != null) {
        boolean enabled = ConfigBeansUtilities.toBoolean(value);
        manager.setEnabled(enabled);
    }
    // set cache element's attributes and properties
    value = cacheConfig.getAttributeValue(Cache.MAX_ENTRIES);
    if (value != null) {
        try {
            int maxEntries = Integer.parseInt(value.trim());
            manager.setMaxEntries(maxEntries);
        } catch (NumberFormatException e) {
            // XXX need error message
            throw new Exception("invalid max-entries", e);
        }
    }
    value = cacheConfig.getAttributeValue(Cache.TIMEOUT_IN_SECONDS);
    if (value != null) {
        try {
            int defaultTimeout = Integer.parseInt(value.trim());
            manager.setDefaultTimeout(defaultTimeout);
        } catch (NumberFormatException e) {
            // XXX need error message
            throw new Exception("invalid timeout", e);
        }
    }
    WebProperty[] props = cacheConfig.getWebProperty();
    for (int i = 0; i < props.length; i++) {
        name = props[i].getAttributeValue(WebProperty.NAME);
        value = props[i].getAttributeValue(WebProperty.VALUE);
        manager.addProperty(name, value);
    }
    // configure the default cache-helper
    DefaultHelper defHelperConfig = cacheConfig.getDefaultHelper();
    HashMap<String, String> map = new HashMap<String, String>();
    if (defHelperConfig != null) {
        props = defHelperConfig.getWebProperty();
        for (int i = 0; i < props.length; i++) {
            name = props[i].getAttributeValue(WebProperty.NAME);
            value = props[i].getAttributeValue(WebProperty.VALUE);
            map.put(name, value);
        }
    }
    manager.setDefaultHelperProps(map);
    // configure custom cache-helper classes
    for (int i = 0; i < cacheConfig.sizeCacheHelper(); i++) {
        CacheHelper helperConfig = cacheConfig.getCacheHelper(i);
        String helperName = helperConfig.getAttributeValue(CacheHelper.NAME);
        HashMap<String, String> helperProps = new HashMap<String, String>();
        props = helperConfig.getWebProperty();
        for (int j = 0; j < props.length; j++) {
            name = props[i].getAttributeValue(WebProperty.NAME);
            value = props[i].getAttributeValue(WebProperty.VALUE);
            helperProps.put(name, value);
        }
        helperProps.put("class-name", helperConfig.getAttributeValue(CacheHelper.CLASS_NAME));
        manager.addCacheHelperDef(helperName, helperProps);
    }
    // for each cache-mapping, create CacheMapping, setup the filter
    for (int i = 0; i < cacheConfig.sizeCacheMapping(); i++) {
        org.glassfish.web.deployment.runtime.CacheMapping mapConfig = cacheConfig.getCacheMapping(i);
        CacheMapping mapping = new CacheMapping();
        configureCacheMapping(mapConfig, mapping, logger);
        // use filter's name to refer to setup the filter
        String filterName = CACHING_FILTER_CLASSNAME + i;
        /**
         * all cache-mapings are indexed by the unique filter-name;
         * DefaultCacheHelper uses this name to access the mapping.
         */
        manager.addCacheMapping(filterName, mapping);
        // setup the ias CachingFilter definition with the context
        FilterDef filterDef = new FilterDef();
        filterDef.setFilterName(filterName);
        filterDef.setFilterClassName(CACHING_FILTER_CLASSNAME);
        if (mapping.getServletName() != null) {
            filterDef.addInitParameter("servletName", mapping.getServletName());
        }
        if (mapping.getURLPattern() != null) {
            filterDef.addInitParameter("URLPattern", mapping.getURLPattern());
        }
        app.addFilterDef(filterDef);
        // setup the mapping for the specified servlet-name or url-pattern
        FilterMap filterMap = new FilterMap();
        filterMap.setServletName(mapping.getServletName());
        filterMap.setURLPattern(mapping.getURLPattern());
        String[] dispatchers = mapConfig.getDispatcher();
        if (dispatchers != null) {
            EnumSet<DispatcherType> dispatcherTypes = null;
            for (String dispatcher : dispatchers) {
                // calls to FilterMap.setDispatcher are cumulative
                if (dispatcherTypes == null) {
                    dispatcherTypes = EnumSet.of(Enum.valueOf(DispatcherType.class, dispatcher));
                } else {
                    dispatcherTypes.add(Enum.valueOf(DispatcherType.class, dispatcher));
                }
            }
            filterMap.setDispatcherTypes(dispatcherTypes);
        }
        filterMap.setFilterName(filterName);
        app.addFilterMap(filterMap);
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, LogFacade.CACHING_FILTER_ADDED, new Object[] { mapping.getServletName(), mapping.getURLPattern() });
        }
    }
    manager.setServletContext(app.getServletContext());
    return manager;
}
Also used : FilterDef(org.apache.catalina.deploy.FilterDef) HashMap(java.util.HashMap) org.glassfish.web.deployment.runtime(org.glassfish.web.deployment.runtime) FilterMap(org.apache.catalina.deploy.FilterMap) ValueConstraint(com.sun.appserv.web.cache.mapping.ValueConstraint) CacheManager(com.sun.appserv.web.cache.CacheManager) CacheMapping(com.sun.appserv.web.cache.mapping.CacheMapping) DispatcherType(javax.servlet.DispatcherType)

Aggregations

CacheMapping (com.sun.appserv.web.cache.mapping.CacheMapping)6 ConstraintField (com.sun.appserv.web.cache.mapping.ConstraintField)4 Field (com.sun.appserv.web.cache.mapping.Field)3 CacheManager (com.sun.appserv.web.cache.CacheManager)1 ValueConstraint (com.sun.appserv.web.cache.mapping.ValueConstraint)1 String (java.lang.String)1 HashMap (java.util.HashMap)1 DispatcherType (javax.servlet.DispatcherType)1 LifecycleException (org.apache.catalina.LifecycleException)1 FilterDef (org.apache.catalina.deploy.FilterDef)1 FilterMap (org.apache.catalina.deploy.FilterMap)1 org.glassfish.web.deployment.runtime (org.glassfish.web.deployment.runtime)1