Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--Context antiJARLocking="true" path="/PresenceRepository"/-->
  3. <Context path="/PresenceRepository">
  4. <Resource auth="Container"
  5. cachePrepStmts="true"
  6. defaultAutoCommit="true"
  7. driverClassName="com.mysql.jdbc.Driver"
  8. factory="com.zaxxer.hikari.HikariJNDIFactory"
  9. global="jdbc/presencedb"
  10. jdbcUrl="jdbc:mysql://192.168.253.128/opensips"
  11. maxWaitMillis="10000" maximumPoolSize="20"
  12. name="jdbc/presencedb"
  13. password="root"
  14. prepStmtCacheSize="30"
  15. prepStmtCacheSqlLimit="500"
  16. removeAbandoned="true"
  17. removeAbandonedTimeout="300"
  18. type="javax.sql.DataSource"
  19. username="root"/>
  20. </Context>
  21.  
  22. public class DAOConnectionFactory {
  23.  
  24. private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DAOConnectionFactory.class);
  25. private static final String databaseJNDI = "jdbc/presencedb";
  26. private static DataSource dataSource = null;
  27. private DAOConnectionFactory(){}
  28. static {
  29.  
  30. try {
  31. dataSource = (DataSource) new InitialContext().lookup("java:comp/env/" + databaseJNDI);
  32. } catch (NamingException e) {
  33. logger.error("Error while creating datasource.", e);
  34. }
  35.  
  36. }
  37.  
  38. protected static Connection getConnection() throws SQLException {
  39.  
  40. return dataSource.getConnection();
  41. }
  42.  
  43. protected static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) {
  44. if (resultSet != null) {
  45. try {
  46. resultSet.close();
  47. } catch (SQLException e) {
  48. logger.error("Error while closing resultset.", e);
  49. }
  50. }
  51. if (statement != null) {
  52. try {
  53. statement.close();
  54. } catch (SQLException e) {
  55. logger.error("Error while closing statement.", e);
  56. }
  57. }
  58. if (connection != null) {
  59. try {
  60. connection.close();
  61. } catch (SQLException e) {
  62. logger.error("Error while closing connection.", e);
  63. }
  64. }
  65. }
  66. }
  67.  
  68. @Path("/watcher")
  69. public class WatcherService {
  70.  
  71. private static final org.slf4j.Logger logger = LoggerFactory.getLogger(WatcherService.class);
  72.  
  73. @GET
  74. @Path("/presentity/{presentityURI}")
  75. @Produces("application/json")
  76. public Response getWatcherForPresentityByStatus(@Context UriInfo uriInfo) {
  77.  
  78. MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
  79. MultivaluedMap<String, String> pathParameters = uriInfo.getPathParameters();
  80.  
  81. List<Watchers> watchersList;
  82. GenericEntity< List< Watchers>> entity;
  83. try {
  84. if (uriInfo.getQueryParameters().containsKey("status") && uriInfo.getQueryParameters().containsKey("event")) {
  85. watchersList = WatcherDAO.findByPresentityAndStatus(queryParameters, pathParameters);
  86. } else {
  87. watchersList = WatcherDAO.findByPresentityAndEvent(queryParameters, pathParameters);
  88. }
  89. Response.ResponseBuilder response;
  90.  
  91. if (!watchersList.isEmpty()) {
  92. entity = new GenericEntity<List<Watchers>>(watchersList) {
  93. };
  94. response = Response.ok(entity);
  95. } else {
  96. response = Response.status(Response.Status.NOT_FOUND).entity("Requested resource could not be found.");
  97. }
  98. return response.build();
  99. } catch (Exception e) {
  100. logger.error("Error while fetching watcher details for presentity {}.", pathParameters.getFirst("presentityURI"), e);
  101. return Response.status(500).entity("Server was unable to process the request.").build();
  102. }
  103. }
  104.  
  105. }
  106.  
  107. public class WatcherDAO {
  108.  
  109. private static final Logger logger = LoggerFactory.getLogger(WatcherDAO.class);
  110.  
  111. private static final String SELECT_BY_PRES_AND_STATUS = "select watcher_username,watcher_domain from watchers where presentity_uri=? AND event=? AND status=?";
  112.  
  113. //Watcher Table: Primary key = id.
  114. //Unique index: presentity_uri,watcher_username,event,watcher_domain
  115.  
  116.  
  117. public static List<Watchers> findByPresentityAndStatus(MultivaluedMap<String, String> queryParameters, MultivaluedMap<String, String> pathParameters) throws SQLException {
  118. Connection connection = null;
  119. PreparedStatement preparedStatement = null;
  120. int index = 0;
  121. ResultSet rs = null;
  122.  
  123. try {
  124. connection = DAOConnectionFactory.getConnection();
  125. preparedStatement = connection.prepareStatement(SELECT_BY_PRES_AND_STATUS);
  126.  
  127. preparedStatement.setObject(++index, pathParameters.getFirst("presentityURI"));
  128. preparedStatement.setObject(++index, queryParameters.getFirst("event"));
  129. preparedStatement.setObject(++index, queryParameters.getFirst("status"));
  130.  
  131. rs = preparedStatement.executeQuery();
  132. Watchers watchers;
  133. List<Watchers> watchersList = new ArrayList<>();
  134. while (rs.next()) {
  135. watchers = new Watchers();
  136. watchers.setWatcherUsername(rs.getString(1));
  137. watchers.setWatcherDomain(rs.getString(2));
  138. watchersList.add(watchers);
  139. }
  140. logger.debug("Query returned {} results.", watchersList.size());
  141. return watchersList;
  142. } catch (SQLException ex) {
  143. logger.error("Error while fetching watcher for presentity {}.", pathParameters.getFirst("presentityURI"));
  144. throw ex;
  145. } catch (Exception ex) {
  146. logger.error("Error while fetching watcher for presentity {}.", pathParameters.getFirst("presentityURI"));
  147. throw ex;
  148. } finally {
  149. DAOConnectionFactory.closeConnection(connection, preparedStatement, rs);
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement