From 73365e4550881f3aa100b2ea4079e0a9a6e3c844 Mon Sep 17 00:00:00 2001 From: michi Date: Thu, 5 Feb 2015 00:16:30 +0100 Subject: [PATCH] java.util.logging.Logger as logger The java.util.logging.Logger was introduced as the logger for the application. --- src/main/java/es/uvigo/esei/daa/dao/DAO.java | 5 ++++- .../java/es/uvigo/esei/daa/dao/PeopleDAO.java | 11 +++++++++++ src/main/java/es/uvigo/esei/daa/dao/UsersDAO.java | 6 ++++++ .../es/uvigo/esei/daa/rest/PeopleResource.java | 15 ++++++++++----- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/java/es/uvigo/esei/daa/dao/DAO.java b/src/main/java/es/uvigo/esei/daa/dao/DAO.java index e3822ee..bd5d925 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/DAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/DAO.java @@ -2,6 +2,8 @@ package es.uvigo.esei.daa.dao; import java.sql.Connection; import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.naming.Context; import javax.naming.InitialContext; @@ -9,6 +11,7 @@ import javax.naming.NamingException; import javax.sql.DataSource; public abstract class DAO { + private final static Logger LOG = Logger.getLogger("DAO"); private final static String JNDI_NAME = "java:/comp/env/jdbc/daaexample"; private final DataSource dataSource; @@ -21,7 +24,7 @@ public abstract class DAO { System.getProperty("db.jndi", JNDI_NAME) ); } catch (NamingException e) { - e.printStackTrace(); + LOG.log(Level.SEVERE, "Error initializing DAO", e); throw new RuntimeException(e); } } diff --git a/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java index d0d932f..4c5a568 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java @@ -7,10 +7,14 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.LinkedList; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import es.uvigo.esei.daa.entities.Person; public class PeopleDAO extends DAO { + private final static Logger LOG = Logger.getLogger("PeopleDAO"); + public Person get(int id) throws DAOException, IllegalArgumentException { try (final Connection conn = this.getConnection()) { @@ -32,6 +36,7 @@ public class PeopleDAO extends DAO { } } } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error getting a person", e); throw new DAOException(e); } } @@ -54,6 +59,7 @@ public class PeopleDAO extends DAO { } } } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error listing people", e); throw new DAOException(e); } } @@ -71,6 +77,7 @@ public class PeopleDAO extends DAO { } } } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error deleting a person", e); throw new DAOException(e); } } @@ -96,6 +103,7 @@ public class PeopleDAO extends DAO { } } } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error modifying a person", e); throw new DAOException(); } } @@ -118,14 +126,17 @@ public class PeopleDAO extends DAO { if (resultKeys.next()) { return new Person(resultKeys.getInt(1), name, surname); } else { + LOG.log(Level.SEVERE, "Error retrieving inserted id"); throw new SQLException("Error retrieving inserted id"); } } } else { + LOG.log(Level.SEVERE, "Error inserting value"); throw new SQLException("Error inserting value"); } } } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error adding a person", e); throw new DAOException(e); } } diff --git a/src/main/java/es/uvigo/esei/daa/dao/UsersDAO.java b/src/main/java/es/uvigo/esei/daa/dao/UsersDAO.java index 6d4cfe2..b05fdd4 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/UsersDAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/UsersDAO.java @@ -4,11 +4,15 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; public class UsersDAO extends DAO { + private final static Logger LOG = Logger.getLogger("UsersDAO"); + public String checkLogin(String login, String password) throws DAOException { try (final Connection conn = this.getConnection()) { final String query = "SELECT password FROM users WHERE login=?"; @@ -32,6 +36,7 @@ public class UsersDAO extends DAO { } } } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error checking login", e); throw new DAOException(e); } } @@ -67,6 +72,7 @@ public class UsersDAO extends DAO { } } } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error checking token", e); throw new DAOException(e); } } diff --git a/src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java b/src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java index 5518e56..e9f856e 100644 --- a/src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java +++ b/src/main/java/es/uvigo/esei/daa/rest/PeopleResource.java @@ -1,5 +1,8 @@ package es.uvigo.esei.daa.rest; +import java.util.logging.Level; +import java.util.logging.Logger; + import javax.ws.rs.DELETE; import javax.ws.rs.FormParam; import javax.ws.rs.GET; @@ -17,6 +20,8 @@ import es.uvigo.esei.daa.dao.PeopleDAO; @Path("/people") @Produces(MediaType.APPLICATION_JSON) public class PeopleResource { + private final static Logger LOG = Logger.getLogger("PeopleResource"); + private final PeopleDAO dao; public PeopleResource() { @@ -28,7 +33,7 @@ public class PeopleResource { try { return Response.ok(this.dao.list(), MediaType.APPLICATION_JSON).build(); } catch (DAOException e) { - e.printStackTrace(); + LOG.log(Level.SEVERE, "Error listing people", e); return Response.serverError().entity(e.getMessage()).build(); } } @@ -45,7 +50,7 @@ public class PeopleResource { return Response.status(Response.Status.BAD_REQUEST) .entity(iae.getMessage()).build(); } catch (DAOException e) { - e.printStackTrace(); + LOG.log(Level.SEVERE, "Error getting a person", e); return Response.serverError().entity(e.getMessage()).build(); } } @@ -64,7 +69,7 @@ public class PeopleResource { return Response.status(Response.Status.BAD_REQUEST) .entity(iae.getMessage()).build(); } catch (DAOException e) { - e.printStackTrace(); + LOG.log(Level.SEVERE, "Error deleting a person", e); return Response.serverError().entity(e.getMessage()).build(); } } @@ -83,7 +88,7 @@ public class PeopleResource { return Response.status(Response.Status.BAD_REQUEST) .entity(iae.getMessage()).build(); } catch (DAOException e) { - e.printStackTrace(); + LOG.log(Level.SEVERE, "Error modifying a person", e); return Response.serverError().entity(e.getMessage()).build(); } } @@ -100,7 +105,7 @@ public class PeopleResource { return Response.status(Response.Status.BAD_REQUEST) .entity(iae.getMessage()).build(); } catch (DAOException e) { - e.printStackTrace(); + LOG.log(Level.SEVERE, "Error adding a person", e); return Response.serverError().entity(e.getMessage()).build(); } } -- 2.18.1