JavaTypeExpr(Block block, String className) {
    super(block, null);

    this.type = TYPE_JAVA;
    _typeName = className;
    try {
      this.javaType = CauchoSystem.loadClass(className, false, block.getClassLoader());
    } catch (Exception e) {
      this.type = TYPE_ES;
    }
  }
  static Expr create(Block block, Expr rawExpr, String name) {
    ClassLoader loader = block.getClassLoader();

    try {
      Class cl;

      cl = CauchoSystem.loadClass(name, false, loader);

      if (Modifier.isPublic(cl.getModifiers())) return new JavaClassExpr(block, cl);
    } catch (Throwable e) {
    }

    return new PackageExpr(block, rawExpr, name);
  }
  public void init() throws ServletException {
    super.init();

    try {
      Class cl = _service.getClass();

      if (cl.isAnnotationPresent(WebService.class)) {
        WebService webService = (WebService) cl.getAnnotation(WebService.class);

        String endpoint = webService.endpointInterface();

        if (endpoint != null && !"".equals(endpoint))
          cl = CauchoSystem.loadClass(webService.endpointInterface());
      }

      ArrayList<Class> jaxbClasses = _jaxbClasses;

      for (Method method : cl.getMethods()) {
        if (method.getDeclaringClass().equals(Object.class)) continue;

        int modifiers = method.getModifiers();

        // Allow abstract for interfaces
        if (Modifier.isStatic(modifiers)
            || Modifier.isFinal(modifiers)
            || !Modifier.isPublic(modifiers)) continue;

        if (_context == null) JAXBUtil.introspectMethod(method, jaxbClasses);
      }

      if (_context != null) {
      } else if (_jaxbPackages != null) {
        _context = JAXBContext.newInstance(_jaxbPackages);
      } else {
        Class[] classes = jaxbClasses.toArray(new Class[jaxbClasses.size()]);
        _context = JAXBContext.newInstance(classes);
      }

      _marshaller = _context.createMarshaller();
      _marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
      _unmarshaller = _context.createUnmarshaller();
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }
Ejemplo n.º 4
0
  /** Initialize the servlet */
  public void init() throws ServletException {
    _ejbManager = EjbManager.getCurrent();

    if (_ejbManager == null) {
      throw new ServletException(
          L.l(
              "No <ejb-server> detected.  '{0}' requires a configured <ejb-server>",
              getClass().getName()));
    }

    _workPath = CauchoSystem.getWorkPath();

    _urlPrefix = getInitParameter("url-prefix");

    _localId = getInitParameter("local-prefix");

    String protocol = getInitParameter("protocol");
    if (protocol == null) protocol = getInitParameter("protocol-container");
    if (protocol == null) protocol = getInitParameter("protocol-factory");
    if (protocol == null) protocol = getDefaultProtocolContainer();

    try {
      Class cl = CauchoSystem.loadClass(protocol);

      _protocolContainer = (ProtocolContainer) cl.newInstance();
    } catch (Exception e) {
      throw new ServletException(e);
    }

    WebApp app = (WebApp) getServletContext();

    // need to initialize in the case of message driven beans.
    // if (containerId == null)
    //  containerId = app.getURL();

    initEjb();

    if (_urlPrefix != null) initProtocol();
  }
  /**
   * Adds an attribute.
   *
   * @param name the attribute name
   * @param value the attribute value
   */
  public void addAttribute(QName name, String value) throws JspParseException {
    if (IS_EL_IGNORED.equals(name)) {
      boolean isIgnored = value.equals("true");

      if (_parseState.isELIgnoredPageSpecified() && isIgnored != _parseState.isELIgnored())
        throw error(L.l("isELIgnored values conflict"));

      _parseState.setELIgnored(isIgnored);
      _parseState.setELIgnoredPageSpecified(true);
    }

    /*
    else if (name.equals("isScriptingInvalid"))
      _parseState.setScriptingInvalid(value.equals("true"));
    */
    else if (IS_VELOCITY_ENABLED.equals(name)) _parseState.setVelocityEnabled(value.equals("true"));
    else if (INFO.equals(name)) {
      String oldInfo = _parseState.getInfo();

      if (oldInfo != null && !value.equals(oldInfo))
        throw error(
            L.l(
                "info '{0}' conflicts with previous value of info '{1}'.  Check the .jsp and any included .jsp files for conflicts.",
                value, oldInfo));

      _parseState.setInfo(value);
    } else if (CONTENT_TYPE.equals(name)) {
      String oldContentType = _parseState.getContentType();

      if (oldContentType != null && !value.equals(oldContentType))
        throw error(
            L.l(
                "contentType '{0}' conflicts with previous value of contentType '{1}'.  Check the .jsp and any included .jsp files for conflicts.",
                value, oldContentType));

      _parseState.setContentType(value);
      String charEncoding = parseCharEncoding(value);
      if (charEncoding != null) _parseState.setCharEncoding(charEncoding);
    } else if (PAGE_ENCODING.equals(name)) {
      String oldEncoding = _parseState.getPageEncoding();

      /*
           // jsp/01f1
           if (oldEncoding != null) {
      String oldCanonical = Encoding.getMimeName(oldEncoding);
      String newCanonical = Encoding.getMimeName(value);

      if (! newCanonical.equals(oldCanonical))
        throw error(L.l("pageEncoding '{0}' conflicts with previous value of pageEncoding '{1}'.  Check the .jsp and any included .jsp files for conflicts.", value, oldEncoding));
           }
           */

      try {
        _parseState.setPageEncoding(value);
        // _parseState.setCharEncoding(value);
      } catch (JspParseException e) {
        log.log(Level.FINER, e.toString(), e);

        throw error(e.getMessage());
      }
    } else if (LANGUAGE.equals(name)) {
      if (!value.equals("java"))
        throw error(L.l("'{0}' is not supported as a JSP scripting language.", value));
    } else if (IMPORT.equals(name)) {
      _parseState.addImport(value);
    } else if (SESSION.equals(name)) {
      boolean isValid = false;

      if (value.equals("true")) isValid = _parseState.setSession(true);
      else if (value.equals("false")) isValid = _parseState.setSession(false);
      else throw error(L.l("session expects 'true' or 'false' at '{0}'", value));

      _parseState.markSessionSet();

      if (!isValid) throw error(L.l("session is assigned different values."));
    } else if (BUFFER.equals(name)) {
      boolean isValid = _parseState.setBuffer(processBufferSize(value));

      _parseState.markBufferSet();

      if (!isValid) throw error(L.l("buffer is assigned different values."));

      if (_parseState.getBuffer() == 0 && !_parseState.isAutoFlush())
        throw error(L.l("buffer must be non-zero when autoFlush is false."));
    } else if (ERROR_PAGE.equals(name)) {
      String errorPage = _parseState.getErrorPage();

      String newErrorPage = getRelativeUrl(value);

      _parseState.setErrorPage(newErrorPage);

      if (errorPage != null && !errorPage.equals(newErrorPage)) {
        _parseState.setErrorPage(null);
        throw error(L.l("errorPage is assigned different value '{0}'.", newErrorPage));
      }
    } else if (IS_ERROR_PAGE.equals(name)) {
      boolean isValid = false;

      if (value.equals("true")) isValid = _parseState.setErrorPage(true);
      else if (value.equals("false")) isValid = _parseState.setErrorPage(false);
      else throw error(L.l("isErrorPage expects 'true' or 'false' at '{0}'", value));

      _parseState.markErrorPage();

      if (!isValid) throw error(L.l("isErrorPage is assigned different values."));
    } else if (AUTO_FLUSH.equals(name)) {
      boolean isValid = false;

      if (value.equals("true")) isValid = _parseState.setAutoFlush(true);
      else if (value.equals("false")) isValid = _parseState.setAutoFlush(false);
      else throw error(L.l("autoFlush expects 'true' or 'false' at '{0}'", value));

      if (!isValid) throw error(L.l("autoFlush is assigned different values."));

      if (_parseState.getBuffer() == 0 && !_parseState.isAutoFlush())
        throw error(L.l("buffer must be non-zero when autoFlush is false."));

      _parseState.markAutoFlushSet();
    } else if (IS_THREAD_SAFE.equals(name)) {
      boolean isValid = false;

      if (value.equals("true")) isValid = _parseState.setThreadSafe(true);
      else if (value.equals("false")) isValid = _parseState.setThreadSafe(false);
      else throw error(L.l("isThreadSafe expects 'true' or 'false' at '{0}'", value));

      _parseState.markThreadSafeSet();

      if (!isValid) throw error(L.l("isThreadSafe is assigned different values."));
    } else if (EXTENDS.equals(name)) {
      Class cl = null;

      try {
        cl = CauchoSystem.loadClass(value);
      } catch (Exception e) {
        throw error(e);
      }

      if (!HttpJspPage.class.isAssignableFrom(cl))
        throw error(
            L.l(
                "'{0}' must implement HttpJspPage.  The class named by jsp:directive.page extends='...' must implement HttpJspPage.",
                value));

      Class oldExtends = _parseState.getExtends();

      if (oldExtends != null && !cl.equals(oldExtends))
        throw error(
            L.l(
                "extends '{0}' conflicts with previous value of extends '{1}'.  Check the .jsp and any included .jsp files for conflicts.",
                value, oldExtends.getName()));

      _parseState.setExtends(cl);
    } else if (TRIM_WS.equals(name)) {
      if (value.equals("true")) _parseState.setTrimWhitespace(true);
      else if (value.equals("false")) _parseState.setTrimWhitespace(false);
      else throw error(L.l("trimDirectiveWhitespaces expects 'true' or 'false' at '{0}'", value));
    } else if (DEFER.equals(name)) {
      if (value.equals("true")) _parseState.setDeferredSyntaxAllowedAsLiteral(true);
      else if (value.equals("false")) _parseState.setDeferredSyntaxAllowedAsLiteral(false);
      else
        throw error(
            L.l("deferredSyntaxAllowedAsLiteral expects 'true' or 'false' at '{0}'", value));
    } else {
      throw error(
          L.l(
              "'{0}' is an unknown JSP page directive attribute.  See the JSP documentation for a complete list of page directive attributes.",
              name.getName()));
    }
  }
  /**
   * Loads the compiled stylesheet .class file
   *
   * @param className the mangled classname for the stylesheet
   */
  protected StylesheetImpl loadStylesheet(String systemId, String className) throws Exception {
    LruCache<String, SoftReference<StylesheetImpl>> cache;

    ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();

    cache = _stylesheetCache.getLevel(parentLoader);

    if (cache == null) {
      cache = new LruCache<String, SoftReference<StylesheetImpl>>(256);
      _stylesheetCache.set(cache, parentLoader);
    }

    SoftReference<StylesheetImpl> stylesheetRef;

    stylesheetRef = cache.get(className);

    StylesheetImpl stylesheet = null;

    if (stylesheetRef != null) stylesheet = stylesheetRef.get();

    try {
      if (stylesheet != null && !stylesheet.isModified()) return stylesheet;
    } catch (Throwable e) {
      log.log(Level.FINER, e.toString(), e);
    }

    Path classPath = getWorkPath().lookup(className.replace('.', '/') + ".class");
    if (!classPath.canRead())
      throw new ClassNotFoundException("can't find compiled XSL `" + className + "'");

    DynamicClassLoader loader;
    loader = SimpleLoader.create(parentLoader, getWorkPath(), className);

    Class cl = null;

    // If the loading fails, remove the class because it may be corrupted
    try {
      cl = CauchoSystem.loadClass(className, false, loader);
    } catch (Error e) {
      try {
        classPath.remove();
      } catch (IOException e1) {
        log.log(Level.FINE, e1.toString(), e1);
      }

      throw e;
    }

    stylesheet = (StylesheetImpl) cl.newInstance();
    Path path;

    path = getSearchPath().lookup("").lookup(systemId);
    /*
    try {
    } catch (TransformerException e) {
      log.log(Level.FINE, e.toString(), e);

      path = Vfs.lookup(systemId);
    }
      */

    // stylesheet.init(path);
    stylesheet.init(getStylePath());
    stylesheet.setURIResolver(_uriResolver);

    stylesheetRef = new SoftReference<StylesheetImpl>(stylesheet);
    cache.put(className, stylesheetRef);

    return stylesheet;
  }