FileDocCategorySizeDatePackage
Field.javaAPI DocGlassfish v2 API6905Fri May 04 22:35:28 BST 2007com.sun.appserv.web.cache.mapping

Field

public class Field extends Object

Fields Summary
private static Logger
_logger
private static ResourceBundle
_rb
The resource bundle containing the localized message strings.
protected String
name
protected int
scope
Constructors Summary
public Field(String name, String scope)
create a new cache field, given a string representation of the scope

param
name name of this field
param
scope scope of this field

 

                                 
            
        // web container logger
        _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
        _rb = _logger.getResourceBundle();

        this.name = name;
        this.scope = parseScope(scope);
    
Methods Summary
public java.lang.StringgetName()
get the associated name

return
the name of this field

        return name;
    
public intgetScope()
get the associated scope

return
the scope of this field

        return scope;
    
public java.lang.ObjectgetValue(javax.servlet.ServletContext context, javax.servlet.http.HttpServletRequest request)
get the field value by looking up in the given scope

param
context ServletContext underlying web app context
param
request HttpServletRequest
return
field value in the scope


        Object value = null;
        switch (scope) {
            case Constants.SCOPE_CONTEXT_ATTRIBUTE:
                    value = context.getAttribute(name);
                    break;
            case Constants.SCOPE_REQUEST_HEADER:
                    value = request.getHeader(name);
                    break;
            case Constants.SCOPE_REQUEST_PARAMETER:
                    value = request.getParameter(name);
                    break;
            case Constants.SCOPE_REQUEST_COOKIE:
                    Cookie cookies[] = request.getCookies();
                    for (int i = 0; i < cookies.length; i++) {
                        if (name.equals(cookies[i].getName())) {
                            value = cookies[i].getValue();
                            break;
                        }
                    }
                    break;
            case Constants.SCOPE_REQUEST_ATTRIBUTE:
                    value = request.getAttribute(name);
                    break;
            case Constants.SCOPE_SESSION_ID:
                    {
                        HttpSession session = request.getSession(false);
                        if (session != null) {
                            value = session.getId();
                        }
                    }
                    break;
            case Constants.SCOPE_SESSION_ATTRIBUTE:
                    {
                        HttpSession session = request.getSession(false);
                        if (session != null) {
                            value = session.getAttribute(name);
                        }
                    }
                    break;
        }
        return value;
    
private intparseScope(java.lang.String value)
set the associated scope

param
scope scope of this field

        int scope;
        if ("context.attribute".equals(value))
            scope = Constants.SCOPE_CONTEXT_ATTRIBUTE;
        else if ("request.header".equals(value))
            scope = Constants.SCOPE_REQUEST_HEADER;
        else if ("request.parameter".equals(value))
            scope = Constants.SCOPE_REQUEST_PARAMETER;
        else if ("request.cookie".equals(value))
            scope = Constants.SCOPE_REQUEST_COOKIE;
        else if ("request.attribute".equals(value))
            scope = Constants.SCOPE_REQUEST_ATTRIBUTE;
        else if ("session.attribute".equals(value))
            scope = Constants.SCOPE_SESSION_ATTRIBUTE;
        else if ("session.id".equals(value))
            scope = Constants.SCOPE_SESSION_ID;
        else  {
            String msg = _rb.getString("cache.mapping.incorrectScope");
            Object[] params = { value, name };
            msg = MessageFormat.format(msg, params);

            throw new IllegalArgumentException(msg);
        }
        return scope;
    
public voidsetName(java.lang.String name)
set the associated name

param
name name of this field

        this.name = name;
    
public voidsetScope(int scope)
set the associated scope

param
scope scope of this field

        this.scope = scope;