Example usage for javax.servlet.jsp PageContext APPLICATION_SCOPE

List of usage examples for javax.servlet.jsp PageContext APPLICATION_SCOPE

Introduction

In this page you can find the example usage for javax.servlet.jsp PageContext APPLICATION_SCOPE.

Prototype

int APPLICATION_SCOPE

To view the source code for javax.servlet.jsp PageContext APPLICATION_SCOPE.

Click Source Link

Document

Application scope: named reference remains available in the ServletContext until it is reclaimed.

Usage

From source file:org.apache.sling.scripting.jsp.taglib.IncludeTagHandler.java

/**
 * Gets the int code for a valid scope, must be one of 'page', 'request',
 * 'session' or 'application'. If an invalid or no scope is specified page
 * scope is returned.//from w  w  w.jav  a2 s. c  o m
 * 
 * @param scope
 * @return
 */
private Integer validScope(String scope) {
    scope = (scope != null && scope.trim().length() > 0 ? scope.trim().toUpperCase() : null);
    if (scope == null) {
        return PageContext.PAGE_SCOPE;
    }

    String[] scopes = { "PAGE", "REQUEST", "SESSION", "APPLICATION" };
    Integer[] iaScopes = { PageContext.PAGE_SCOPE, PageContext.REQUEST_SCOPE, PageContext.SESSION_SCOPE,
            PageContext.APPLICATION_SCOPE };

    for (int ndx = 0, len = scopes.length; ndx < len; ndx++) {
        if (scopes[ndx].equals(scope)) {
            return iaScopes[ndx];
        }
    }
    return PageContext.PAGE_SCOPE;
}

From source file:org.apache.struts.faces.taglib.JavascriptValidatorTag.java

/**
 * Render the JavaScript for to perform validations based on the form name.
 *
 * @exception JspException if a JSP exception has occurred
 *//*w w w.j a v a2  s .c om*/
public int doStartTag() throws JspException {
    StringBuffer results = new StringBuffer();

    ModuleConfig config = RequestUtils.getModuleConfig(pageContext);
    ValidatorResources resources = (ValidatorResources) pageContext
            .getAttribute(ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix(), PageContext.APPLICATION_SCOPE);

    Locale locale = RequestUtils.retrieveUserLocale(this.pageContext, null);

    Form form = resources.get(locale, formName);
    if (form != null) {
        if ("true".equalsIgnoreCase(dynamicJavascript)) {
            MessageResources messages = (MessageResources) pageContext.getAttribute(bundle + config.getPrefix(),
                    PageContext.APPLICATION_SCOPE);

            List lActions = new ArrayList();
            List lActionMethods = new ArrayList();

            // Get List of actions for this Form
            for (Iterator i = form.getFields().iterator(); i.hasNext();) {
                Field field = (Field) i.next();

                for (Iterator x = field.getDependencies().iterator(); x.hasNext();) {
                    Object o = x.next();

                    if (o != null && !lActionMethods.contains(o)) {
                        lActionMethods.add(o);
                    }
                }

            }

            // Create list of ValidatorActions based on lActionMethods
            for (Iterator i = lActionMethods.iterator(); i.hasNext();) {
                String depends = (String) i.next();
                ValidatorAction va = resources.getValidatorAction(depends);

                // throw nicer NPE for easier debugging
                if (va == null) {
                    throw new NullPointerException(
                            "Depends string \"" + depends + "\" was not found in validator-rules.xml.");
                }

                String javascript = va.getJavascript();
                if (javascript != null && javascript.length() > 0) {
                    lActions.add(va);
                } else {
                    i.remove();
                }
            }

            Collections.sort(lActions, new Comparator() {
                public int compare(Object o1, Object o2) {
                    ValidatorAction va1 = (ValidatorAction) o1;
                    ValidatorAction va2 = (ValidatorAction) o2;

                    if ((va1.getDepends() == null || va1.getDepends().length() == 0)
                            && (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                        return 0;
                    } else if ((va1.getDepends() != null && va1.getDepends().length() > 0)
                            && (va2.getDepends() == null || va2.getDepends().length() == 0)) {
                        return 1;
                    } else if ((va1.getDepends() == null || va1.getDepends().length() == 0)
                            && (va2.getDepends() != null && va2.getDepends().length() > 0)) {
                        return -1;
                    } else {
                        return va1.getDependencies().size() - va2.getDependencies().size();
                    }
                }
            });

            String methods = null;
            for (Iterator i = lActions.iterator(); i.hasNext();) {
                ValidatorAction va = (ValidatorAction) i.next();

                if (methods == null) {
                    methods = va.getMethod() + "(form)";
                } else {
                    methods += " && " + va.getMethod() + "(form)";
                }
            }

            results.append(getJavascriptBegin(methods));

            for (Iterator i = lActions.iterator(); i.hasNext();) {
                ValidatorAction va = (ValidatorAction) i.next();
                String jscriptVar = null;
                String functionName = null;

                if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) {
                    functionName = va.getJsFunctionName();
                } else {
                    functionName = va.getName();
                }

                if (isStruts11()) {
                    results.append("    function " + functionName + " () { \n");
                } else {
                    results.append("    function " + formName + "_" + functionName + " () { \n");
                }
                for (Iterator x = form.getFields().iterator(); x.hasNext();) {
                    Field field = (Field) x.next();

                    // Skip indexed fields for now until there is a good way to handle 
                    // error messages (and the length of the list (could retrieve from scope?))
                    if (field.isIndexed() || field.getPage() != page || !field.isDependency(va.getName())) {

                        continue;
                    }

                    String message = Resources.getMessage(messages, locale, va, field);

                    message = (message != null) ? message : "";

                    jscriptVar = this.getNextVar(jscriptVar);

                    results.append("     this." + jscriptVar + " = new Array(\"" + getFormClientId() + ":"
                            + field.getKey() + "\", \"" + message + "\", ");

                    results.append("new Function (\"varName\", \"");

                    Map vars = field.getVars();
                    // Loop through the field's variables.
                    Iterator varsIterator = vars.keySet().iterator();
                    while (varsIterator.hasNext()) {
                        String varName = (String) varsIterator.next();
                        Var var = (Var) vars.get(varName);
                        String varValue = var.getValue();
                        String jsType = var.getJsType();

                        // skip requiredif variables field, fieldIndexed, fieldTest, fieldValue
                        if (varName.startsWith("field")) {
                            continue;
                        }

                        if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "="
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "; ");
                        } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "=/"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "/; ");
                        } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "='"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "'; ");
                            // So everyone using the latest format doesn't need to change their xml files immediately.
                        } else if ("mask".equalsIgnoreCase(varName)) {
                            results.append("this." + varName + "=/"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "/; ");
                        } else {
                            results.append("this." + varName + "='"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "'; ");
                        }
                    }

                    results.append(" return this[varName];\"));\n");
                }
                results.append("    } \n\n");
            }
        } else if ("true".equalsIgnoreCase(staticJavascript)) {
            results.append(this.getStartElement());
            if ("true".equalsIgnoreCase(htmlComment)) {
                results.append(htmlBeginComment);
            }
        }
    }

    if ("true".equalsIgnoreCase(staticJavascript)) {
        results.append(getJavascriptStaticMethods(resources));
    }

    if (form != null
            && ("true".equalsIgnoreCase(dynamicJavascript) || "true".equalsIgnoreCase(staticJavascript))) {

        results.append(getJavascriptEnd());
    }

    JspWriter writer = pageContext.getOut();
    try {
        writer.print(results.toString());
    } catch (IOException e) {
        throw new JspException(e.getMessage());
    }

    return (EVAL_BODY_TAG);

}

From source file:org.apache.struts.taglib.bean.TestDefineTag.java

public void testDefineNameApplicationScope() {
    pageContext.setAttribute(TEST_KEY, TEST_VALUE, PageContext.APPLICATION_SCOPE);
    runMyTest("testDefineNameApplicationScope");
}

From source file:org.apache.struts.taglib.bean.TestDefineTag.java

public void testDefineNamePropertyApplicationScope() {
    LabelValueBean lvb = new LabelValueBean("key", TEST_VALUE);
    pageContext.setAttribute(TEST_KEY, lvb, PageContext.APPLICATION_SCOPE);
    runMyTest("testDefineNamePropertyApplicationScope");
}

From source file:org.apache.struts.taglib.bean.TestDefineTag.java

public void testDefineValueApplicationScope() {
    LabelValueBean lvb = new LabelValueBean("key", TEST_VALUE + " this will be set to the correct value");
    pageContext.setAttribute(TEST_KEY, lvb, PageContext.APPLICATION_SCOPE);
    runMyTest("testDefineValueApplicationScope");
}

From source file:org.apache.struts.taglib.bean.TestMessageTag.java

public void testMessageTagNoArgNamePropertyApplicationScopeDefaultBundle() {
    pageContext.setAttribute("key", new SimpleBeanForTesting("default.bundle.message"),
            PageContext.APPLICATION_SCOPE);
    runMyTest("testMessageTagNoArgNamePropertyApplicationScopeDefaultBundle", new Locale("", ""));
}

From source file:org.apache.struts.taglib.bean.TestMessageTag.java

public void testMessageTagNoArgNamePropertyApplicationScopeAlternateBundle() {
    pageContext.setAttribute("key", new SimpleBeanForTesting("alternate.bundle.message"),
            PageContext.APPLICATION_SCOPE);
    runMyTest("testMessageTagNoArgNamePropertyApplicationScopeAlternateBundle", new Locale("", ""));
}

From source file:org.apache.struts.taglib.bean.TestMessageTag1.java

public void testMessageTag1ArgNamePropertyApplicationScopeDefaultBundle() {
    pageContext.setAttribute("key", new SimpleBeanForTesting("default.bundle.message.1"),
            PageContext.APPLICATION_SCOPE);
    runMyTest("testMessageTag1ArgNamePropertyApplicationScopeDefaultBundle", new Locale("", ""));
}

From source file:org.apache.struts.taglib.bean.TestMessageTag1.java

public void testMessageTag1ArgNamePropertyApplicationScopeAlternateBundle() {
    pageContext.setAttribute("key", new SimpleBeanForTesting("alternate.bundle.message.1"),
            PageContext.APPLICATION_SCOPE);
    runMyTest("testMessageTag1ArgNamePropertyApplicationScopeAlternateBundle", new Locale("", ""));
}

From source file:org.apache.struts.taglib.bean.TestMessageTag1_fr.java

public void testMessageTag1ArgNamePropertyApplicationScopeDefaultBundle_fr() {
    pageContext.setAttribute("key", new SimpleBeanForTesting("default.bundle.message.1"),
            PageContext.APPLICATION_SCOPE);
    runMyTest("testMessageTag1ArgNamePropertyApplicationScopeDefaultBundle", new Locale("fr", "fr"));
}