fixes several warnings

Sat, 09 May 2020 15:19:21 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 May 2020 15:19:21 +0200
changeset 33
fd8c40ff78c3
parent 32
63a31871189e
child 34
824d4042c857

fixes several warnings

src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/DatabaseFacade.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/Functions.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/ModuleManager.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/dao/CoreDAOFactory.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/dao/ModuleDao.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/dao/UserDao.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/entities/User.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sat May 09 14:58:41 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sat May 09 15:19:21 2020 +0200
     1.3 @@ -28,22 +28,18 @@
     1.4   */
     1.5  package de.uapcore.lightpit;
     1.6  
     1.7 -import java.io.IOException;
     1.8 -import java.lang.reflect.Method;
     1.9 -import java.lang.reflect.Modifier;
    1.10 -import java.util.Arrays;
    1.11 -import java.util.HashMap;
    1.12 -import java.util.List;
    1.13 -import java.util.Locale;
    1.14 -import java.util.Map;
    1.15 -import java.util.Optional;
    1.16 +import org.slf4j.Logger;
    1.17 +import org.slf4j.LoggerFactory;
    1.18 +
    1.19  import javax.servlet.ServletException;
    1.20  import javax.servlet.http.HttpServlet;
    1.21  import javax.servlet.http.HttpServletRequest;
    1.22  import javax.servlet.http.HttpServletResponse;
    1.23  import javax.servlet.http.HttpSession;
    1.24 -import org.slf4j.Logger;
    1.25 -import org.slf4j.LoggerFactory;
    1.26 +import java.io.IOException;
    1.27 +import java.lang.reflect.Method;
    1.28 +import java.lang.reflect.Modifier;
    1.29 +import java.util.*;
    1.30  
    1.31  /**
    1.32   * A special implementation of a HTTPServlet which is focused on implementing
    1.33 @@ -54,21 +50,21 @@
    1.34      private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class);
    1.35      
    1.36      private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full");
    1.37 -    
    1.38 +
    1.39      /**
    1.40       * Store a reference to the annotation for quicker access.
    1.41       */
    1.42 -    private Optional<LightPITModule> moduleInfo = Optional.empty();
    1.43 +    private LightPITModule moduleInfo = null;
    1.44  
    1.45      /**
    1.46       * The EL proxy is necessary, because the EL resolver cannot handle annotation properties.
    1.47       */
    1.48 -    private Optional<LightPITModule.ELProxy> moduleInfoELProxy = Optional.empty();
    1.49 -    
    1.50 -    
    1.51 +    private LightPITModule.ELProxy moduleInfoELProxy = null;
    1.52 +
    1.53 +
    1.54      @FunctionalInterface
    1.55 -    private static interface HandlerMethod {
    1.56 -        ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException, ServletException;
    1.57 +    private interface HandlerMethod {
    1.58 +        ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException;
    1.59      }
    1.60      
    1.61      /**
    1.62 @@ -84,22 +80,27 @@
    1.63  
    1.64      /**
    1.65       * Gives implementing modules access to the {@link ModuleManager}.
    1.66 +     *
    1.67       * @return the module manager
    1.68       */
    1.69      protected final ModuleManager getModuleManager() {
    1.70          return (ModuleManager) getServletContext().getAttribute(ModuleManager.SC_ATTR_NAME);
    1.71      }
    1.72 -    
    1.73 +
    1.74 +    public final LightPITModule getModuleInfo() {
    1.75 +        return moduleInfo;
    1.76 +    }
    1.77 +
    1.78      /**
    1.79       * Gives implementing modules access to the {@link DatabaseFacade}.
    1.80 +     *
    1.81       * @return the database facade
    1.82       */
    1.83      protected final DatabaseFacade getDatabaseFacade() {
    1.84          return (DatabaseFacade) getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);
    1.85      }
    1.86 -    
    1.87 -    private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp)
    1.88 -            throws IOException, ServletException {
    1.89 +
    1.90 +    private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    1.91          try {
    1.92              LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName());
    1.93              return (ResponseType) method.invoke(this, req, resp);
    1.94 @@ -112,13 +113,13 @@
    1.95  
    1.96      @Override
    1.97      public void init() throws ServletException {
    1.98 -        moduleInfo = Optional.ofNullable(this.getClass().getAnnotation(LightPITModule.class));
    1.99 -        moduleInfoELProxy = moduleInfo.map(LightPITModule.ELProxy::convert);
   1.100 -        
   1.101 -        if (moduleInfo.isPresent()) {
   1.102 +        moduleInfo = this.getClass().getAnnotation(LightPITModule.class);
   1.103 +        moduleInfoELProxy = moduleInfo == null ? null : LightPITModule.ELProxy.convert(moduleInfo);
   1.104 +
   1.105 +        if (moduleInfo != null) {
   1.106              scanForRequestMappings();
   1.107          }
   1.108 -        
   1.109 +
   1.110          LOG.trace("{} initialized", getServletName());
   1.111      }
   1.112  
   1.113 @@ -270,7 +271,7 @@
   1.114          // set some internal request attributes
   1.115          req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
   1.116          req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName());
   1.117 -        moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
   1.118 +        Optional.ofNullable(moduleInfoELProxy).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
   1.119          
   1.120          
   1.121          // call the handler, if available, or send an HTTP 404 error
     2.1 --- a/src/main/java/de/uapcore/lightpit/DatabaseFacade.java	Sat May 09 14:58:41 2020 +0200
     2.2 +++ b/src/main/java/de/uapcore/lightpit/DatabaseFacade.java	Sat May 09 15:19:21 2020 +0200
     2.3 @@ -28,10 +28,9 @@
     2.4   */
     2.5  package de.uapcore.lightpit;
     2.6  
     2.7 -import java.sql.Connection;
     2.8 -import java.sql.DatabaseMetaData;
     2.9 -import java.sql.SQLException;
    2.10 -import java.util.Optional;
    2.11 +import org.slf4j.Logger;
    2.12 +import org.slf4j.LoggerFactory;
    2.13 +
    2.14  import javax.naming.Context;
    2.15  import javax.naming.InitialContext;
    2.16  import javax.naming.NamingException;
    2.17 @@ -40,31 +39,33 @@
    2.18  import javax.servlet.ServletContextListener;
    2.19  import javax.servlet.annotation.WebListener;
    2.20  import javax.sql.DataSource;
    2.21 -import org.slf4j.Logger;
    2.22 -import org.slf4j.LoggerFactory;
    2.23 +import java.sql.Connection;
    2.24 +import java.sql.DatabaseMetaData;
    2.25 +import java.sql.SQLException;
    2.26 +import java.util.Optional;
    2.27  
    2.28  /**
    2.29   * Provides access to different privilege layers within the database.
    2.30   */
    2.31  @WebListener
    2.32  public final class DatabaseFacade implements ServletContextListener {
    2.33 -    
    2.34 +
    2.35      private static final Logger LOG = LoggerFactory.getLogger(DatabaseFacade.class);
    2.36 -    
    2.37 +
    2.38      /**
    2.39       * Timeout in seconds for the validation test.
    2.40       */
    2.41      private static final int DB_TEST_TIMEOUT = 10;
    2.42 -    
    2.43 -    public static enum Dialect {
    2.44 -        Postgres;
    2.45 +
    2.46 +    public enum Dialect {
    2.47 +        Postgres
    2.48      }
    2.49 -    
    2.50 +
    2.51      /**
    2.52       * The database dialect to use.
    2.53 -     * 
    2.54 +     * <p>
    2.55       * May be override by context parameter.
    2.56 -     * 
    2.57 +     *
    2.58       * @see Constants#CTX_ATTR_DB_DIALECT
    2.59       */
    2.60      private Dialect dialect = Dialect.Postgres;
    2.61 @@ -82,10 +83,9 @@
    2.62       * The attribute name in the Servlet context under which an instance of this class can be found.
    2.63       */
    2.64      public static final String SC_ATTR_NAME = DatabaseFacade.class.getName();
    2.65 -    private ServletContext sc;
    2.66 -    
    2.67 +
    2.68      private static final String DS_JNDI_NAME = "jdbc/lightpit/app";
    2.69 -    private Optional<DataSource> dataSource;
    2.70 +    private DataSource dataSource;
    2.71      
    2.72      /**
    2.73       * Returns the data source.
    2.74 @@ -97,14 +97,15 @@
    2.75       * @return a data source
    2.76       */
    2.77      public Optional<DataSource> getDataSource() {
    2.78 -        return dataSource;
    2.79 +        // TODO: this should not be an optional, if an empty optional is actually an exception
    2.80 +        return Optional.ofNullable(dataSource);
    2.81      }
    2.82      
    2.83      public Dialect getSQLDialect() {
    2.84          return dialect;
    2.85      }
    2.86 -    
    2.87 -    private static void checkConnection(DataSource ds, String testSchema, String errMsg) {
    2.88 +
    2.89 +    private static void checkConnection(DataSource ds, String testSchema) {
    2.90          try (Connection conn = ds.getConnection()) {
    2.91              if (!conn.isValid(DB_TEST_TIMEOUT)) {
    2.92                  throw new SQLException("Validation check failed.");
    2.93 @@ -118,25 +119,25 @@
    2.94              DatabaseMetaData metaData = conn.getMetaData();
    2.95              LOG.info("Connections as {} to {}/{} ready to go.", metaData.getUserName(), metaData.getURL(), conn.getSchema());
    2.96          } catch (SQLException ex) {
    2.97 -            LOG.error(errMsg, ex);
    2.98 +            LOG.error("Checking database connection failed", ex);
    2.99          }
   2.100      }
   2.101 -    
   2.102 -    private static Optional<DataSource> retrieveDataSource(Context ctx) {
   2.103 +
   2.104 +    private static DataSource retrieveDataSource(Context ctx) {
   2.105          DataSource ret = null;
   2.106          try {
   2.107 -            ret = (DataSource)ctx.lookup(DS_JNDI_NAME);
   2.108 +            ret = (DataSource) ctx.lookup(DS_JNDI_NAME);
   2.109              LOG.info("Data source retrieved.");
   2.110          } catch (NamingException ex) {
   2.111              LOG.error("Data source {} not available.", DS_JNDI_NAME);
   2.112              LOG.error("Reason for the missing data source: ", ex);
   2.113          }
   2.114 -        return Optional.ofNullable(ret);
   2.115 +        return ret;
   2.116      }
   2.117  
   2.118      @Override
   2.119      public void contextInitialized(ServletContextEvent sce) {
   2.120 -        sc = sce.getServletContext();
   2.121 +        ServletContext sc = sce.getServletContext();
   2.122          
   2.123          dataSource = null;
   2.124          
   2.125 @@ -159,10 +160,12 @@
   2.126              LOG.debug("Trying to access JNDI context {}...", contextName);
   2.127              Context initialCtx = new InitialContext();
   2.128              Context ctx = (Context) initialCtx.lookup(contextName);
   2.129 -            
   2.130 +
   2.131              dataSource = retrieveDataSource(ctx);
   2.132 -            
   2.133 -            dataSource.ifPresent((ds) -> checkConnection(ds, dbSchema, "Checking database connection failed"));
   2.134 +
   2.135 +            if (dataSource != null) {
   2.136 +                checkConnection(dataSource, dbSchema);
   2.137 +            }
   2.138          } catch (NamingException | ClassCastException ex) {
   2.139              LOG.error("Cannot access JNDI resources.", ex);
   2.140          }
     3.1 --- a/src/main/java/de/uapcore/lightpit/Functions.java	Sat May 09 14:58:41 2020 +0200
     3.2 +++ b/src/main/java/de/uapcore/lightpit/Functions.java	Sat May 09 15:19:21 2020 +0200
     3.3 @@ -28,11 +28,12 @@
     3.4   */
     3.5  package de.uapcore.lightpit;
     3.6  
     3.7 -import java.util.Optional;
     3.8 +import org.slf4j.Logger;
     3.9 +import org.slf4j.LoggerFactory;
    3.10 +
    3.11  import javax.servlet.ServletContext;
    3.12  import javax.servlet.http.HttpServletRequest;
    3.13 -import org.slf4j.Logger;
    3.14 -import org.slf4j.LoggerFactory;
    3.15 +import java.util.Optional;
    3.16  
    3.17  /**
    3.18   * Contains common static functions.
    3.19 @@ -64,8 +65,8 @@
    3.20      public static String fqn(String base, String name) {
    3.21          return base+"."+name;
    3.22      }
    3.23 -    
    3.24 -    public static String fqn(Class clazz, String name) {
    3.25 +
    3.26 +    public static String fqn(Class<?> clazz, String name) {
    3.27          return fqn(clazz.getName(), name);
    3.28      }
    3.29      
     4.1 --- a/src/main/java/de/uapcore/lightpit/ModuleManager.java	Sat May 09 14:58:41 2020 +0200
     4.2 +++ b/src/main/java/de/uapcore/lightpit/ModuleManager.java	Sat May 09 15:19:21 2020 +0200
     4.3 @@ -84,7 +84,7 @@
     4.4      
     4.5      private Optional<LightPITModule> getModuleInfo(Registration reg) {
     4.6          try {
     4.7 -            final Class scclass = Class.forName(reg.getClassName());
     4.8 +            final Class<?> scclass = Class.forName(reg.getClassName());
     4.9              
    4.10              final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass);
    4.11              final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class);
    4.12 @@ -103,8 +103,7 @@
    4.13              }
    4.14              
    4.15              if (lpservlet && lpmodule) {
    4.16 -                final Class<? extends AbstractLightPITServlet> clazz = scclass;
    4.17 -                final LightPITModule moduleInfo = clazz.getAnnotation(LightPITModule.class);
    4.18 +                final LightPITModule moduleInfo = scclass.getAnnotation(LightPITModule.class);
    4.19                  return Optional.of(moduleInfo);
    4.20              } else {
    4.21                  return Optional.empty();
     5.1 --- a/src/main/java/de/uapcore/lightpit/dao/CoreDAOFactory.java	Sat May 09 14:58:41 2020 +0200
     5.2 +++ b/src/main/java/de/uapcore/lightpit/dao/CoreDAOFactory.java	Sat May 09 15:19:21 2020 +0200
     5.3 @@ -43,8 +43,7 @@
     5.4              case Postgres:
     5.5                  return moduleDao;
     5.6              default:
     5.7 -                assert (false);
     5.8 -                return null;
     5.9 +                throw new AssertionError("Switch was not exhaustive.");
    5.10          }
    5.11      }
    5.12  }
     6.1 --- a/src/main/java/de/uapcore/lightpit/dao/ModuleDao.java	Sat May 09 14:58:41 2020 +0200
     6.2 +++ b/src/main/java/de/uapcore/lightpit/dao/ModuleDao.java	Sat May 09 15:19:21 2020 +0200
     6.3 @@ -44,7 +44,7 @@
     6.4       *
     6.5       * @param result the database result set
     6.6       * @param mod    the POJO
     6.7 -     * @throws SQLException
     6.8 +     * @throws SQLException on any kind of SQL errors
     6.9       */
    6.10      protected void mapColumns(ResultSet result, Module mod) throws SQLException {
    6.11          mod.setModID(result.getInt("modid"));
    6.12 @@ -52,19 +52,19 @@
    6.13          mod.setVisible(result.getBoolean("visible"));
    6.14          mod.setPriority(result.getInt("priority"));
    6.15      }
    6.16 -            
    6.17 -    
    6.18 +
    6.19 +
    6.20      /**
    6.21       * Must return a prepared statement for a single object query with the specified properties.
    6.22 -     * 
    6.23 +     *
    6.24       * <ul>
    6.25       * <li>Parameter 1: classname</li>
    6.26       * <li>Result field 1: visible</li>
    6.27       * </ul>
    6.28 -     * 
    6.29 +     *
    6.30       * @param conn the connection to use
    6.31       * @return the prepared statement
    6.32 -     * @throws SQLException 
    6.33 +     * @throws SQLException on any kind of SQL errors
    6.34       */
    6.35      protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException {
    6.36          return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?");
    6.37 @@ -81,7 +81,7 @@
    6.38       *
    6.39       * @param conn the connection to use
    6.40       * @return the prepared statement
    6.41 -     * @throws SQLException
    6.42 +     * @throws SQLException on any kind of SQL errors
    6.43       */
    6.44      protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException {
    6.45          return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible, priority) VALUES (?, ?, ?)");
    6.46 @@ -89,13 +89,13 @@
    6.47      
    6.48      /**
    6.49       * Synchronizes a set of registered module classes with the database.
    6.50 -     * 
    6.51 +     *
    6.52       * Inserts module classes which are not known to the database and sets them to be visible by default.
    6.53       * Module classes known to the database, which are not in the given set, are ignored.
    6.54 -     * 
    6.55 +     *
    6.56       * @param conn the connection to use
    6.57       * @param moduleSet the module set to synchronize
    6.58 -     * @throws SQLException
    6.59 +     * @throws SQLException on any kind of SQL errors
    6.60       */
    6.61      public final void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException {
    6.62                  
    6.63 @@ -121,12 +121,12 @@
    6.64  
    6.65      /**
    6.66       * Returns a list of all modules known by the database.
    6.67 -     * 
    6.68 +     *
    6.69       * Keep in mind, that system modules are never known to the database.
    6.70 -     * 
    6.71 +     *
    6.72       * @param conn the connection to use
    6.73       * @return a list of all modules known by the database
    6.74 -     * @throws SQLException 
    6.75 +     * @throws SQLException on any kind of SQL errors
    6.76       */
    6.77      public List<Module> listAll(Connection conn) throws SQLException {
    6.78          List<Module> list = new ArrayList<>();
     7.1 --- a/src/main/java/de/uapcore/lightpit/dao/UserDao.java	Sat May 09 14:58:41 2020 +0200
     7.2 +++ b/src/main/java/de/uapcore/lightpit/dao/UserDao.java	Sat May 09 15:19:21 2020 +0200
     7.3 @@ -36,7 +36,6 @@
     7.4  import java.sql.Statement;
     7.5  import java.util.ArrayList;
     7.6  import java.util.List;
     7.7 -import java.util.Optional;
     7.8  
     7.9  public class UserDao {
    7.10  
    7.11 @@ -45,23 +44,23 @@
    7.12       *
    7.13       * @param result the database result set
    7.14       * @param user   the POJO
    7.15 -     * @throws SQLException
    7.16 +     * @throws SQLException on any kind of SQL errors
    7.17       */
    7.18      protected void mapColumns(ResultSet result, User user) throws SQLException {
    7.19          user.setUserID(result.getInt("userid"));
    7.20          user.setUsername(result.getString("username"));
    7.21 -        user.setGivenname(Optional.ofNullable(result.getString("givenname")));
    7.22 -        user.setLastname(Optional.ofNullable(result.getString("lastname"))); 
    7.23 +        user.setGivenname(result.getString("givenname"));
    7.24 +        user.setLastname(result.getString("lastname"));
    7.25      }
    7.26  
    7.27      /**
    7.28       * Returns a list of all users ordered by their username.
    7.29 -     * 
    7.30 +     * <p>
    7.31       * Does not return reserved system users with negative user IDs.
    7.32 -     * 
    7.33 +     *
    7.34       * @param conn the connection to use
    7.35       * @return a list of all users
    7.36 -     * @throws SQLException 
    7.37 +     * @throws SQLException on any kind of SQL errors
    7.38       */
    7.39      public List<User> listAll(Connection conn) throws SQLException {
    7.40          List<User> list = new ArrayList<>();
     8.1 --- a/src/main/java/de/uapcore/lightpit/entities/User.java	Sat May 09 14:58:41 2020 +0200
     8.2 +++ b/src/main/java/de/uapcore/lightpit/entities/User.java	Sat May 09 15:19:21 2020 +0200
     8.3 @@ -1,8 +1,8 @@
     8.4  /*
     8.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     8.6 - * 
     8.7 + *
     8.8   * Copyright 2018 Mike Becker. All rights reserved.
     8.9 - * 
    8.10 + *
    8.11   * Redistribution and use in source and binary forms, with or without
    8.12   * modification, are permitted provided that the following conditions are met:
    8.13   *
    8.14 @@ -24,20 +24,18 @@
    8.15   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    8.16   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    8.17   * POSSIBILITY OF SUCH DAMAGE.
    8.18 - * 
    8.19 + *
    8.20   */
    8.21  package de.uapcore.lightpit.entities;
    8.22  
    8.23 -import java.util.Optional;
    8.24 +public final class User {
    8.25  
    8.26 -public final class User {
    8.27 -    
    8.28      public static final int ANONYMOUS_USERID = -1;
    8.29 -    
    8.30 +
    8.31      private int userID;
    8.32      private String username;
    8.33 -    private Optional<String> givenname;
    8.34 -    private Optional<String> lastname;
    8.35 +    private String givenname;
    8.36 +    private String lastname;
    8.37  
    8.38      public int getUserID() {
    8.39          return userID;
    8.40 @@ -55,19 +53,19 @@
    8.41          this.username = username;
    8.42      }
    8.43  
    8.44 -    public Optional<String> getGivenname() {
    8.45 +    public String getGivenname() {
    8.46          return givenname;
    8.47      }
    8.48  
    8.49 -    public void setGivenname(Optional<String> givenname) {
    8.50 +    public void setGivenname(String givenname) {
    8.51          this.givenname = givenname;
    8.52      }
    8.53  
    8.54 -    public Optional<String> getLastname() {
    8.55 +    public String getLastname() {
    8.56          return lastname;
    8.57      }
    8.58  
    8.59 -    public void setLastname(Optional<String> lastname) {
    8.60 +    public void setLastname(String lastname) {
    8.61          this.lastname = lastname;
    8.62      }
    8.63  
    8.64 @@ -88,5 +86,5 @@
    8.65          } else {
    8.66              return this.userID == ((User) obj).userID;
    8.67          }
    8.68 -    }    
    8.69 +    }
    8.70  }

mercurial