Commit 73365e45 authored by Administrator's avatar Administrator

java.util.logging.Logger as logger

The java.util.logging.Logger was introduced as the logger for the
application.
parent 29daab8f
...@@ -2,6 +2,8 @@ package es.uvigo.esei.daa.dao; ...@@ -2,6 +2,8 @@ package es.uvigo.esei.daa.dao;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.InitialContext; import javax.naming.InitialContext;
...@@ -9,6 +11,7 @@ import javax.naming.NamingException; ...@@ -9,6 +11,7 @@ import javax.naming.NamingException;
import javax.sql.DataSource; import javax.sql.DataSource;
public abstract class DAO { 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 static String JNDI_NAME = "java:/comp/env/jdbc/daaexample";
private final DataSource dataSource; private final DataSource dataSource;
...@@ -21,7 +24,7 @@ public abstract class DAO { ...@@ -21,7 +24,7 @@ public abstract class DAO {
System.getProperty("db.jndi", JNDI_NAME) System.getProperty("db.jndi", JNDI_NAME)
); );
} catch (NamingException e) { } catch (NamingException e) {
e.printStackTrace(); LOG.log(Level.SEVERE, "Error initializing DAO", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
......
...@@ -7,10 +7,14 @@ import java.sql.SQLException; ...@@ -7,10 +7,14 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import es.uvigo.esei.daa.entities.Person; import es.uvigo.esei.daa.entities.Person;
public class PeopleDAO extends DAO { public class PeopleDAO extends DAO {
private final static Logger LOG = Logger.getLogger("PeopleDAO");
public Person get(int id) public Person get(int id)
throws DAOException, IllegalArgumentException { throws DAOException, IllegalArgumentException {
try (final Connection conn = this.getConnection()) { try (final Connection conn = this.getConnection()) {
...@@ -32,6 +36,7 @@ public class PeopleDAO extends DAO { ...@@ -32,6 +36,7 @@ public class PeopleDAO extends DAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
LOG.log(Level.SEVERE, "Error getting a person", e);
throw new DAOException(e); throw new DAOException(e);
} }
} }
...@@ -54,6 +59,7 @@ public class PeopleDAO extends DAO { ...@@ -54,6 +59,7 @@ public class PeopleDAO extends DAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
LOG.log(Level.SEVERE, "Error listing people", e);
throw new DAOException(e); throw new DAOException(e);
} }
} }
...@@ -71,6 +77,7 @@ public class PeopleDAO extends DAO { ...@@ -71,6 +77,7 @@ public class PeopleDAO extends DAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
LOG.log(Level.SEVERE, "Error deleting a person", e);
throw new DAOException(e); throw new DAOException(e);
} }
} }
...@@ -96,6 +103,7 @@ public class PeopleDAO extends DAO { ...@@ -96,6 +103,7 @@ public class PeopleDAO extends DAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
LOG.log(Level.SEVERE, "Error modifying a person", e);
throw new DAOException(); throw new DAOException();
} }
} }
...@@ -118,14 +126,17 @@ public class PeopleDAO extends DAO { ...@@ -118,14 +126,17 @@ public class PeopleDAO extends DAO {
if (resultKeys.next()) { if (resultKeys.next()) {
return new Person(resultKeys.getInt(1), name, surname); return new Person(resultKeys.getInt(1), name, surname);
} else { } else {
LOG.log(Level.SEVERE, "Error retrieving inserted id");
throw new SQLException("Error retrieving inserted id"); throw new SQLException("Error retrieving inserted id");
} }
} }
} else { } else {
LOG.log(Level.SEVERE, "Error inserting value");
throw new SQLException("Error inserting value"); throw new SQLException("Error inserting value");
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
LOG.log(Level.SEVERE, "Error adding a person", e);
throw new DAOException(e); throw new DAOException(e);
} }
} }
......
...@@ -4,11 +4,15 @@ import java.sql.Connection; ...@@ -4,11 +4,15 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; 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.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
public class UsersDAO extends DAO { public class UsersDAO extends DAO {
private final static Logger LOG = Logger.getLogger("UsersDAO");
public String checkLogin(String login, String password) throws DAOException { public String checkLogin(String login, String password) throws DAOException {
try (final Connection conn = this.getConnection()) { try (final Connection conn = this.getConnection()) {
final String query = "SELECT password FROM users WHERE login=?"; final String query = "SELECT password FROM users WHERE login=?";
...@@ -32,6 +36,7 @@ public class UsersDAO extends DAO { ...@@ -32,6 +36,7 @@ public class UsersDAO extends DAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
LOG.log(Level.SEVERE, "Error checking login", e);
throw new DAOException(e); throw new DAOException(e);
} }
} }
...@@ -67,6 +72,7 @@ public class UsersDAO extends DAO { ...@@ -67,6 +72,7 @@ public class UsersDAO extends DAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
LOG.log(Level.SEVERE, "Error checking token", e);
throw new DAOException(e); throw new DAOException(e);
} }
} }
......
package es.uvigo.esei.daa.rest; 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.DELETE;
import javax.ws.rs.FormParam; import javax.ws.rs.FormParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
...@@ -17,6 +20,8 @@ import es.uvigo.esei.daa.dao.PeopleDAO; ...@@ -17,6 +20,8 @@ import es.uvigo.esei.daa.dao.PeopleDAO;
@Path("/people") @Path("/people")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public class PeopleResource { public class PeopleResource {
private final static Logger LOG = Logger.getLogger("PeopleResource");
private final PeopleDAO dao; private final PeopleDAO dao;
public PeopleResource() { public PeopleResource() {
...@@ -28,7 +33,7 @@ public class PeopleResource { ...@@ -28,7 +33,7 @@ public class PeopleResource {
try { try {
return Response.ok(this.dao.list(), MediaType.APPLICATION_JSON).build(); return Response.ok(this.dao.list(), MediaType.APPLICATION_JSON).build();
} catch (DAOException e) { } catch (DAOException e) {
e.printStackTrace(); LOG.log(Level.SEVERE, "Error listing people", e);
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
} }
} }
...@@ -45,7 +50,7 @@ public class PeopleResource { ...@@ -45,7 +50,7 @@ public class PeopleResource {
return Response.status(Response.Status.BAD_REQUEST) return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage()).build(); .entity(iae.getMessage()).build();
} catch (DAOException e) { } catch (DAOException e) {
e.printStackTrace(); LOG.log(Level.SEVERE, "Error getting a person", e);
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
} }
} }
...@@ -64,7 +69,7 @@ public class PeopleResource { ...@@ -64,7 +69,7 @@ public class PeopleResource {
return Response.status(Response.Status.BAD_REQUEST) return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage()).build(); .entity(iae.getMessage()).build();
} catch (DAOException e) { } catch (DAOException e) {
e.printStackTrace(); LOG.log(Level.SEVERE, "Error deleting a person", e);
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
} }
} }
...@@ -83,7 +88,7 @@ public class PeopleResource { ...@@ -83,7 +88,7 @@ public class PeopleResource {
return Response.status(Response.Status.BAD_REQUEST) return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage()).build(); .entity(iae.getMessage()).build();
} catch (DAOException e) { } catch (DAOException e) {
e.printStackTrace(); LOG.log(Level.SEVERE, "Error modifying a person", e);
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
} }
} }
...@@ -100,7 +105,7 @@ public class PeopleResource { ...@@ -100,7 +105,7 @@ public class PeopleResource {
return Response.status(Response.Status.BAD_REQUEST) return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage()).build(); .entity(iae.getMessage()).build();
} catch (DAOException e) { } catch (DAOException e) {
e.printStackTrace(); LOG.log(Level.SEVERE, "Error adding a person", e);
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment