/** Adds the given listener to this ServletContext. */
 @Override
 public <T extends EventListener> void addListener(T t) {
   if (SecurityUtil.isPackageProtectionEnabled()) {
     doPrivileged(
         "addListener", new Class[] {EventListener.class}, new Object[] {t.getClass().getName()});
   } else {
     context.addListener(t);
   }
 }
Beispiel #2
0
  /**
   * Gets the value of the given parameter from the request converted to an Object. If the parameter
   * is not set or not parseable, the default value is returned.
   *
   * @param pReq the servlet request
   * @param pName the parameter name
   * @param pType the type of object (class) to return
   * @param pFormat the format to use (might be {@code null} in many cases)
   * @param pDefault the default value
   * @return the value of the parameter converted to a boolean, or the default value, if the
   *     parameter is not set.
   * @throws IllegalArgumentException if {@code pDefault} is non-{@code null} and not an instance of
   *     {@code pType}
   * @throws NullPointerException if {@code pReq}, {@code pName} or {@code pType} is {@code null}.
   * @todo Well, it's done. Need some thinking... We probably don't want default if conversion
   *     fails...
   * @see Converter#toObject
   */
  static <T> T getParameter(
      final ServletRequest pReq,
      final String pName,
      final Class<T> pType,
      final String pFormat,
      final T pDefault) {
    // Test if pDefault is either null or instance of pType
    if (pDefault != null && !pType.isInstance(pDefault)) {
      throw new IllegalArgumentException(
          "default value not instance of " + pType + ": " + pDefault.getClass());
    }

    String str = pReq.getParameter(pName);

    if (str == null) {
      return pDefault;
    }

    try {
      return pType.cast(Converter.getInstance().toObject(str, pType, pFormat));
    } catch (ConversionException ce) {
      return pDefault;
    }
  }