org.apache.tomcat.websocket.server.WsFilter.doFilter

huangapple go评论29阅读模式
英文:

org.apache.tomcat.websocket.server.WsFilter.doFilter

问题

I am getting a javax.servlet.ServletException: Servlet execution threw an exception when I am deploying my .war on Apache Tomcat.

Exception

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.lang.ExceptionInInitializerError
java.base/java.lang.Class.forName0(Native Method)
java.base/java.lang.Class.forName(Class.java:398)
org.zkoss.lang.Classes.forNameByThread(Classes.java:274)
org.zkoss.lang.ImportedClassResolver.resolveClass(ImportedClassResolver.java:129)
org.zkoss.zk.ui.impl.PageImpl.resolveClass(PageImpl.java:575)
org.zkoss.zk.ui.impl.AbstractUiFactory.newComposer(AbstractUiFactory.java:184)
org.zkoss.zk.ui.impl.Utils.newComposer(Utils.java:91)
org.zkoss.zk.ui.metainfo.ComponentInfo.toComposer(ComponentInfo.java:360)
org.zkoss.zk.ui.metainfo.ComponentInfo.toComposers(ComponentInfo.java:330)
org.zkoss.zk.ui.metainfo.ComponentInfo.resolveComposer(ComponentInfo.java:318)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:901)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:894)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:781)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:841)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:799)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:745)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:466)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:374)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:215)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.lang.NullPointerException
java.base/java.util.Objects.requireNonNull(Objects.java:221)
java.logging/java.util.logging.Logger.addHandler(Logger.java:2060)
xx.xx.xx.util.ApplicationLogger.log(ApplicationLogger.java:118)
xx.xx.xx.composers.LoginComposer.<clinit>(LoginComposer.java:115)
java.base/java.lang.Class.forName0(Native Method)
java.base/java.lang.Class.forName(Class.java:398)
org.zkoss.lang.Classes.forNameByThread(Classes.java:274)
org.zkoss.lang.ImportedClassResolver.resolveClass(ImportedClassResolver.java:129)
org.zkoss.zk.ui.impl.PageImpl.resolveClass(PageImpl.java:575)
org.zkoss.zk.ui.impl.AbstractUiFactory.newComposer(AbstractUiFactory.java:184)
org.zkoss.zk.ui.impl.Utils.newComposer(Utils.java:91)
org.zkoss.zk.ui.metainfo.ComponentInfo.toComposer(ComponentInfo.java:360)
org.zkoss.zk.ui.metainfo.ComponentInfo.toComposers(ComponentInfo.java:330)
org.zkoss.zk.ui.metainfo.ComponentInfo.resolveComposer(ComponentInfo.java:318)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:901)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:894)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:781)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:841)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:799)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:745)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:466)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:374)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:215)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

This is what I have at line 118 on ApplicationLogger.java

logger.addHandler(fileHandler);

And this is what I have at line 115 on LoginComposer.java

ApplicationLogger.log(Level.INFO, PROPERTIES.getProperty("LOG_HEADER"),className);

ApplicationLogger.java

public class ApplicationLogger {

  private static ApplicationLogger applicationLogger;
  private static Logger logger;
  private static final SimpleFormatter simpleFormatter = new SimpleFormatter();
  private static final Properties PROPERTIES = new Properties();
  private static final String PROPERTIES_FILE = "application-masterconfig.properties";
  private static FileHandler fileHandler;

  private ApplicationLogger() {

}

  static {
    try {

      // Get context ClassLoader for this thread.
      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

      // InputStream for reading the properties file.
      InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);

      // Read the property list
      PROPERTIES.load(inputStream);

      // Initialize the file handler. true ensures append mode.
      fileHandler = new FileHandler(PROPERTIES.getProperty("PATH_TO_APPLICATION_LOGS_DEVELOPMENT"), true);

      // This Formatter will be used to format LogRecords for this Handler.
      fileHandler.setFormatter(simpleFormatter);

    } catch(SecurityException securityException) {
      securityException.printStackTrace();
    } catch(IOException ioException) {
      ioException.printStackTrace();
    } finally {

}
  }

  public static ApplicationLogger getNewInstance() {
    if (Objects.isNull(applicationLogger)) {
      return new ApplicationLogger();
    }
    return applicationLogger;
  }

  public static void log(final Level level, final String msg, final String canonicalClassName) {
    logger = Logger.getLogger(canonicalClassName);
    logger.addHandler(fileHandler);
    logger.setUseParentHandlers(false); // No need to write at console.
    logger.log(level, msg); // Log the message
  }
}

LoginComposer.java

public class LoginComposer extends GenericForwardComposer < Component > {
...
...
...
...
static {
    try {

      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);
      PROPERTIES.load(inputStream);

      /**
			 * &lt
英文:

I am getting a javax.servlet.ServletException: Servlet execution threw an exception when I am deploying my .war on Apache Tomcat.

Exception

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.lang.ExceptionInInitializerError
java.base/java.lang.Class.forName0(Native Method)
java.base/java.lang.Class.forName(Class.java:398)
org.zkoss.lang.Classes.forNameByThread(Classes.java:274)
org.zkoss.lang.ImportedClassResolver.resolveClass(ImportedClassResolver.java:129)
org.zkoss.zk.ui.impl.PageImpl.resolveClass(PageImpl.java:575)
org.zkoss.zk.ui.impl.AbstractUiFactory.newComposer(AbstractUiFactory.java:184)
org.zkoss.zk.ui.impl.Utils.newComposer(Utils.java:91)
org.zkoss.zk.ui.metainfo.ComponentInfo.toComposer(ComponentInfo.java:360)
org.zkoss.zk.ui.metainfo.ComponentInfo.toComposers(ComponentInfo.java:330)
org.zkoss.zk.ui.metainfo.ComponentInfo.resolveComposer(ComponentInfo.java:318)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:901)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:894)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:781)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:841)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:799)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:745)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:466)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:374)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:215)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.lang.NullPointerException
java.base/java.util.Objects.requireNonNull(Objects.java:221)
java.logging/java.util.logging.Logger.addHandler(Logger.java:2060)
xx.xx.xx.util.ApplicationLogger.log(ApplicationLogger.java:118)
xx.xx.xx.composers.LoginComposer.<clinit>(LoginComposer.java:115)
java.base/java.lang.Class.forName0(Native Method)
java.base/java.lang.Class.forName(Class.java:398)
org.zkoss.lang.Classes.forNameByThread(Classes.java:274)
org.zkoss.lang.ImportedClassResolver.resolveClass(ImportedClassResolver.java:129)
org.zkoss.zk.ui.impl.PageImpl.resolveClass(PageImpl.java:575)
org.zkoss.zk.ui.impl.AbstractUiFactory.newComposer(AbstractUiFactory.java:184)
org.zkoss.zk.ui.impl.Utils.newComposer(Utils.java:91)
org.zkoss.zk.ui.metainfo.ComponentInfo.toComposer(ComponentInfo.java:360)
org.zkoss.zk.ui.metainfo.ComponentInfo.toComposers(ComponentInfo.java:330)
org.zkoss.zk.ui.metainfo.ComponentInfo.resolveComposer(ComponentInfo.java:318)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:901)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:894)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:781)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:841)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:799)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:745)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:466)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:374)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:215)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

This is what I have at line 118 on ApplicationLogger.java

logger.addHandler(fileHandler);

And this is what I have at line 115 on LoginComposer.java

ApplicationLogger.log(Level.INFO, PROPERTIES.getProperty("LOG_HEADER"),className);

ApplicationLogger.java

public class ApplicationLogger {

  private static ApplicationLogger applicationLogger;
  private static Logger logger;
  private static final SimpleFormatter simpleFormatter = new SimpleFormatter();
  private static final Properties PROPERTIES = new Properties();
  private static final String PROPERTIES_FILE = "application-masterconfig.properties";
  private static FileHandler fileHandler;

  private ApplicationLogger() {

}

  static {
    try {

      // Get context ClassLoader for this thread.
      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

      // InputStream for reading the properties file.
      InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);

      // Read the property list
      PROPERTIES.load(inputStream);

      // Initialize the file handler. true ensures append mode.
      fileHandler = new FileHandler(PROPERTIES.getProperty("PATH_TO_APPLICATION_LOGS_DEVELOPMENT"), true);

      // This Formatter will be used to format LogRecords for this Handler.
      fileHandler.setFormatter(simpleFormatter);

    } catch(SecurityException securityException) {
      securityException.printStackTrace();
    } catch(IOException ioException) {
      ioException.printStackTrace();
    } finally {

}
  }

  public static ApplicationLogger getNewInstance() {
    if (Objects.isNull(applicationLogger)) {
      return new ApplicationLogger();
    }
    return applicationLogger;
  }

  public static void log(final Level level, final String msg, final String canonicalClassName) {
    logger = Logger.getLogger(canonicalClassName);
    logger.addHandler(fileHandler);
    logger.setUseParentHandlers(false); // No need to write at console.
    logger.log(level, msg); // Log the message
  }
}

LoginComposer.java

public class LoginComposer extends GenericForwardComposer < Component > {
...
...
...
...
static {
    try {

      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);
      PROPERTIES.load(inputStream);

      /**
			 * <p>
			 * Log the message indicating the appication has started and the login page has
			 * loaded.
			 */
      ApplicationLogger.log(Level.INFO, PROPERTIES.getProperty("LOG_HEADER"), className);
    } catch(SecurityException securityException) {
      ApplicationLogger.log(Level.INFO, PROPERTIES.getProperty("SECURITY_VIOLATION_EXCEPTION_HEADER"), className);
      PRINT_WRITER = new PrintWriter(STRING_WRITER);
      securityException.printStackTrace(PRINT_WRITER);
      ApplicationLogger.log(Level.SEVERE, STRING_WRITER.toString(), className);
    } catch(IOException ioException) {
      ApplicationLogger.log(Level.INFO, PROPERTIES.getProperty("IOEXCEPTION_HEADER"), className);
      PRINT_WRITER = new PrintWriter(STRING_WRITER);
      ioException.printStackTrace(PRINT_WRITER);
      ApplicationLogger.log(Level.SEVERE, STRING_WRITER.toString(), className);
    }
  }
  ......
}

How to fix this please? Any help appreciated.

答案1

得分: 2

我认为你的静态构造函数失败了,因此fileHandler保持为null。这通常发生在静态构造函数依赖尚不可用的东西或存在循环依赖时。

在静态构造函数中读取文件通常不会按预期工作。我多次遇到了相同的问题。最后,我在Java EE应用程序中完全停止使用静态构造函数。

而不是使用静态构造函数,你可以在首次使用时按需初始化类(例如,在getNewInstance()内部)。

英文:

I think your static constructor fails, so that fileHandler remains null. This does usually happen when the static constructor depends on things that are not yet available or when you have circular dependencies.

Reading files in static contructors does often not work as expected. I had the same problem multiple times. Finally I stopped using static constructor at all in Java EE applications.

Instead of using a static constructor, you could initialize the class on demand when it is used first time (e.g. inside getNewInstance()).

huangapple
  • 本文由 发表于 2020年8月1日 14:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63202574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定