[Registry-dev] svn commit r16677 - in trunk/registry/modules/core/src/main/java/org/wso2/registry: . app jdbc jdbc/dao session

svn at wso2.org svn at wso2.org
Thu May 8 01:11:58 PDT 2008


Author: deepal
Date: Thu May  8 01:11:43 2008
New Revision: 16677

Log:

pagination for logs

Added:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/LogEntryCollection.java
Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/NonTransactionalJDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/TransactionalJDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/LogsDAO.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/session/UserRegistry.java

Added: trunk/registry/modules/core/src/main/java/org/wso2/registry/LogEntryCollection.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/LogEntryCollection.java	Thu May  8 01:11:43 2008
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wso2.registry;
+
+import org.wso2.registry.jdbc.dao.LogsDAO;
+
+import javax.sql.DataSource;
+import java.util.Date;
+
+/**
+ * The main purpose of this class is to handle pagination for log entries. From the registry API it will returns
+ * LogEntryCollection , and from the LogEntryCollection user can get all the logs or he can ask for log for a
+ * given range
+ */
+public class LogEntryCollection {
+
+    private int logCount;
+    private DataSource dataSource;
+
+    private String resourcePath;
+    private int action;
+    private String userName;
+    private Date from;
+    private Date to;
+    private boolean recentFirst;
+
+
+    public int getLogCount() {
+        return logCount;
+    }
+
+    public void setLogCount(int logCount) {
+        this.logCount = logCount;
+    }
+
+    public LogEntry[] getLogEntries() throws RegistryException {
+        LogsDAO logsDAO = new LogsDAO();
+        return logsDAO.getLogs(resourcePath,
+                action,
+                userName,
+                from,
+                to,
+                recentFirst,
+                dataSource);
+    }
+
+    public LogEntry[] getLogEntries(int start, int pageLen) throws RegistryException {
+        LogsDAO logsDAO = new LogsDAO();
+        return logsDAO.getLogs(resourcePath,
+                action,
+                userName,
+                from,
+                to,
+                recentFirst,
+                start,
+                pageLen,
+                dataSource);
+    }
+
+    public DataSource getDataSource() {
+        return dataSource;
+    }
+
+    public void setDataSource(DataSource dataSource) {
+        this.dataSource = dataSource;
+    }
+
+
+    public void setResourcePath(String resourcePath) {
+        this.resourcePath = resourcePath;
+    }
+
+    public void setAction(int action) {
+        this.action = action;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public void setFrom(Date from) {
+        this.from = from;
+    }
+
+    public void setTo(Date to) {
+        this.to = to;
+    }
+
+    public void setRecentFirst(boolean recentFirst) {
+        this.recentFirst = recentFirst;
+    }
+}

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java	Thu May  8 01:11:43 2008
@@ -292,6 +292,33 @@
                        Date to,
                        boolean recentFirst) throws RegistryException;
 
+     /**
+     * Returns the logs of the activities occured in the registy.
+     *
+     * @see LogEntry Accepted values for action parameter
+     *
+     * @param resourcePath If given, only the logs related to the resource path will be returned. If
+     *                     null, logs for all resources will be returned.
+     * @param action       Only the logs pertaining to this action will be returned.  For
+     *                     acceptable values, see LogEntry.
+     * @param userName     If given, only the logs for activities done by the given user will be
+     *                     returned. If null, logs for all users will be returned.
+     * @param from         If given, logs for activities occured after the given date will be
+     *                     returned. If null, there will not be a bound for the starting date.
+     * @param to           If given, logs for activities occured before the given date will be
+     *                     returned. If null, there will not be a bound for the ending date.
+     * @param recentFirst  If true, returned activities will be most-recent first. If false,
+     *                     returned activities will be oldest first.
+     * @return LogEntryCollection representing collection of log entries
+     * @throws RegistryException if there is a problem
+     */
+    LogEntryCollection getLogCollection(String resourcePath,
+                                        int action,
+                                        String userName,
+                                        Date from,
+                                        Date to,
+                                        boolean recentFirst) throws RegistryException;
+
     void addHandler(Filter filter, Handler handler) throws RegistryException;
 
     void addAspect(String name, Aspect aspect) throws RegistryException;

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java	Thu May  8 01:11:43 2008
@@ -952,6 +952,16 @@
         return logs;
     }
 
+
+    public LogEntryCollection getLogCollection(String resourcePath,
+                                               int action,
+                                               String userName,
+                                               Date from,
+                                               Date to,
+                                               boolean recentFirst) throws RegistryException {
+        throw new UnsupportedOperationException("Sorry we need to implement this method");
+    }
+
     /**
      * This method will create a RequestOptions object adding  Authorization headers.
      *

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java	Thu May  8 01:11:43 2008
@@ -590,7 +590,6 @@
     }
 
     public LogEntry[] getLogs(String resourcePath, int action, String userName, Date from, Date to, boolean recentFirst) throws RegistryException {
-
         if (Transaction.isStarted()) {
             return transactionalRegistry.
                     getLogs(resourcePath, action, userName, from, to, recentFirst);
@@ -600,6 +599,32 @@
         }
     }
 
+
+    public LogEntryCollection getLogCollection(String resourcePath,
+                                               int action,
+                                               String userName,
+                                               Date from,
+                                               Date to,
+                                               boolean recentFirst) throws RegistryException {
+        LogEntryCollection logEntryCollection ;
+        if (Transaction.isStarted()) {
+            logEntryCollection = transactionalRegistry.
+                    getLogCollection(resourcePath, action, userName, from, to, recentFirst);
+        } else {
+            logEntryCollection = nonTransactionalRegistry.
+                    getLogCollection(resourcePath, action, userName, from, to, recentFirst);
+        }
+        logEntryCollection.setDataSource(dataSource);
+        logEntryCollection.setResourcePath(resourcePath);
+        logEntryCollection.setAction(action);
+        logEntryCollection.setUserName(userName);
+        logEntryCollection.setFrom(from);
+        logEntryCollection.setTo(to);
+        logEntryCollection.setRecentFirst(recentFirst);
+        return logEntryCollection;
+
+    }
+
     public void addAspect(String name, Aspect aspect) throws RegistryException {
         aspects.put(name, aspect);
     }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/NonTransactionalJDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/NonTransactionalJDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/NonTransactionalJDBCRegistry.java	Thu May  8 01:11:43 2008
@@ -1229,6 +1229,28 @@
         }
     }
 
+
+    public LogEntryCollection getLogCollection(String resourcePath,
+                                               int action,
+                                               String userName,
+                                               Date from,
+                                               Date to,
+                                               boolean recentFirst) throws RegistryException {
+         try {
+            beginTransaction();
+            LogEntryCollection logEntryCollection = transactionalJDBCRegistry.
+                    getLogCollection(resourcePath, action, userName, from, to, recentFirst);
+            commitTransaction();
+            return logEntryCollection;
+        } catch (Exception e) {
+            String msg = "Failed to get log. . All database operations will be rolled back. " +
+                    e.getMessage();
+            log.error(msg, e);
+            rollbackTransaction();
+            throw new RegistryException(msg, e);
+        }
+    }
+
     public void addAspect(String name, Aspect aspect) throws RegistryException {
         String msg = "Atomic registry does not support adding aspects.";
         log.error(msg);

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/TransactionalJDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/TransactionalJDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/TransactionalJDBCRegistry.java	Thu May  8 01:11:43 2008
@@ -824,6 +824,18 @@
         return logEntries;
     }
 
+
+    public LogEntryCollection getLogCollection(String resourcePath,
+                                               int action,
+                                               String userName,
+                                               Date from,
+                                               Date to,
+                                               boolean recentFirst) throws RegistryException {
+        LogEntryCollection logEntryCollection = new LogEntryCollection();
+        logEntryCollection.setLogCount(logsDAO.getLogsCount(resourcePath,action,userName,from,to,recentFirst));
+        return logEntryCollection;
+    }
+
     public void addAspect(String name, Aspect aspect) throws RegistryException {
         String msg = "Transactional registry does not support adding aspects.";
         log.error(msg);

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/LogsDAO.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/LogsDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/LogsDAO.java	Thu May  8 01:11:43 2008
@@ -75,6 +75,74 @@
 
         boolean queryStarted = false;
 
+        sql = addWherePart(resourcePath, queryStarted, sql, userName, from, to, action);
+
+        if (descending) {
+            sql = sql + " ORDER BY LOGGED_TIME DESC";
+        }
+
+        try {
+            PreparedStatement s = conn.prepareStatement(sql);
+
+            int paramNumber = 1;
+
+            if (resourcePath != null) {
+                s.setString(paramNumber, resourcePath);
+                paramNumber++;
+            }
+
+            if (userName != null) {
+                s.setString(paramNumber, userName);
+                paramNumber++;
+            }
+
+            if (from != null) {
+                s.setTimestamp(paramNumber, new Timestamp(from.getTime()));
+                paramNumber++;
+            }
+
+            if (to != null) {
+                s.setTimestamp(paramNumber, new Timestamp(to.getTime()));
+                paramNumber++;
+            }
+
+            if (action != -1) {
+                s.setInt(paramNumber, action);
+            }
+
+            ResultSet results = s.executeQuery();
+
+            List resultList = new ArrayList();
+            while (results.next()) {
+                LogEntry logEntry = new LogEntry();
+                logEntry.setResourcePath(results.getString(DatabaseConstants.PATH_FIELD));
+                logEntry.setUserName(results.getString(DatabaseConstants.USER_ID_FIELD));
+                logEntry.setDate(
+                        new Date(results.getTimestamp(DatabaseConstants.LOGGED_TIME_FIELD).getTime()));
+                logEntry.setAction(results.getInt(DatabaseConstants.ACTION_FIELD));
+                logEntry.setActionData(results.getString(DatabaseConstants.ACTION_DATA_FIELD));
+
+                resultList.add(logEntry);
+            }
+            s.close();
+
+            return resultList;
+
+        } catch (SQLException e) {
+
+            String msg = "Failed to get logs. " + e.getMessage();
+            log.error(msg, e);
+            throw new RegistryException(msg, e);
+        }
+    }
+
+    private String addWherePart(String resourcePath,
+                                boolean queryStarted,
+                                String sql,
+                                String userName,
+                                Date from,
+                                Date to,
+                                int action) {
         if (resourcePath != null) {
             if (queryStarted) {
                 sql = sql + " AND PATH=?";
@@ -119,12 +187,31 @@
                 queryStarted = true;
             }
         }
+        return sql;
+    }
+
+    public LogEntry[] getLogs(String resourcePath,
+                        int action,
+                        String userName,
+                        Date from,
+                        Date to,
+                        boolean descending ,
+                        int start,
+                        int pageLen ,
+                        DataSource dataSource)
+            throws RegistryException {
+        String sql = "SELECT PATH, USER_ID, LOGGED_TIME, ACTION, ACTION_DATA FROM " +
+                     "LOG";
+        boolean queryStarted = false;
+
+        sql = addWherePart(resourcePath, queryStarted, sql, userName, from, to, action);
 
         if (descending) {
             sql = sql + " ORDER BY LOGGED_TIME DESC";
         }
 
         try {
+            Connection conn = dataSource.getConnection();
             PreparedStatement s = conn.prepareStatement(sql);
 
             int paramNumber = 1;
@@ -155,21 +242,163 @@
 
             ResultSet results = s.executeQuery();
 
-            List resultList = new ArrayList();
+            List<LogEntry> resultList = new ArrayList();
+            int current = 0;
             while (results.next()) {
-                LogEntry logEntry = new LogEntry();
-                logEntry.setResourcePath(results.getString(DatabaseConstants.PATH_FIELD));
-                logEntry.setUserName(results.getString(DatabaseConstants.USER_ID_FIELD));
-                logEntry.setDate(
-                        new Date(results.getTimestamp(DatabaseConstants.LOGGED_TIME_FIELD).getTime()));
-                logEntry.setAction(results.getInt(DatabaseConstants.ACTION_FIELD));
-                logEntry.setActionData(results.getString(DatabaseConstants.ACTION_DATA_FIELD));
+                if (current >= start && (pageLen == -1 || current < start + pageLen)) {
+                    LogEntry logEntry = new LogEntry();
+                    logEntry.setResourcePath(results.getString(DatabaseConstants.PATH_FIELD));
+                    logEntry.setUserName(results.getString(DatabaseConstants.USER_ID_FIELD));
+                    logEntry.setDate(
+                            new Date(results.getTimestamp(DatabaseConstants.LOGGED_TIME_FIELD).getTime()));
+                    logEntry.setAction(results.getInt(DatabaseConstants.ACTION_FIELD));
+                    logEntry.setActionData(results.getString(DatabaseConstants.ACTION_DATA_FIELD));
+
+                    resultList.add(logEntry);
+                }
+                current++;
+            }
+            s.close();
+            return resultList.toArray(new LogEntry[resultList.size()]);
 
-                resultList.add(logEntry);
+        } catch (SQLException e) {
+
+            String msg = "Failed to get logs. " + e.getMessage();
+            log.error(msg, e);
+            throw new RegistryException(msg, e);
+        }
+    }
+
+     public LogEntry[] getLogs(String resourcePath,
+                        int action,
+                        String userName,
+                        Date from,
+                        Date to,
+                        boolean descending ,
+                        DataSource dataSource)
+            throws RegistryException {
+
+        String sql = "SELECT PATH, USER_ID, LOGGED_TIME, ACTION, ACTION_DATA FROM " +
+                     "LOG";
+
+        boolean queryStarted = false;
+
+        sql = addWherePart(resourcePath, queryStarted, sql, userName, from, to, action);
+
+        if (descending) {
+            sql = sql + " ORDER BY LOGGED_TIME DESC";
+        }
+
+        try {
+            Connection conn = dataSource.getConnection();
+            PreparedStatement s = conn.prepareStatement(sql);
+
+            int paramNumber = 1;
+
+            if (resourcePath != null) {
+                s.setString(paramNumber, resourcePath);
+                paramNumber++;
+            }
+
+            if (userName != null) {
+                s.setString(paramNumber, userName);
+                paramNumber++;
+            }
+
+            if (from != null) {
+                s.setTimestamp(paramNumber, new Timestamp(from.getTime()));
+                paramNumber++;
+            }
+
+            if (to != null) {
+                s.setTimestamp(paramNumber, new Timestamp(to.getTime()));
+                paramNumber++;
+            }
+
+            if (action != -1) {
+                s.setInt(paramNumber, action);
+            }
+
+            ResultSet results = s.executeQuery();
+
+            List<LogEntry> resultList = new ArrayList();
+            int current = 0;
+            while (results.next()) {
+                    LogEntry logEntry = new LogEntry();
+                    logEntry.setResourcePath(results.getString(DatabaseConstants.PATH_FIELD));
+                    logEntry.setUserName(results.getString(DatabaseConstants.USER_ID_FIELD));
+                    logEntry.setDate(
+                            new Date(results.getTimestamp(DatabaseConstants.LOGGED_TIME_FIELD).getTime()));
+                    logEntry.setAction(results.getInt(DatabaseConstants.ACTION_FIELD));
+                    logEntry.setActionData(results.getString(DatabaseConstants.ACTION_DATA_FIELD));
+
+                    resultList.add(logEntry);
+                current++;
+            }
+            s.close();
+            return resultList.toArray(new LogEntry[resultList.size()]);
+
+        } catch (SQLException e) {
+
+            String msg = "Failed to get logs. " + e.getMessage();
+            log.error(msg, e);
+            throw new RegistryException(msg, e);
+        }
+    }
+
+    public int getLogsCount(String resourcePath,
+                            int action,
+                            String userName,
+                            Date from,
+                            Date to,
+                            boolean descending)
+            throws RegistryException {
+        int count =0 ;
+
+        Connection conn = Transaction.getConnection();
+
+        String sql = "SELECT COUNT(*) AS LOG_COUNT FROM LOG";
+
+        boolean queryStarted = false;
+
+        sql = addWherePart(resourcePath, queryStarted, sql, userName, from, to, action);
+
+        try {
+            PreparedStatement s = conn.prepareStatement(sql);
+
+            int paramNumber = 1;
+
+            if (resourcePath != null) {
+                s.setString(paramNumber, resourcePath);
+                paramNumber++;
+            }
+
+            if (userName != null) {
+                s.setString(paramNumber, userName);
+                paramNumber++;
+            }
+
+            if (from != null) {
+                s.setTimestamp(paramNumber, new Timestamp(from.getTime()));
+                paramNumber++;
+            }
+
+            if (to != null) {
+                s.setTimestamp(paramNumber, new Timestamp(to.getTime()));
+                paramNumber++;
+            }
+
+            if (action != -1) {
+                s.setInt(paramNumber, action);
+            }
+
+            ResultSet results = s.executeQuery();
+
+            if (results.next()) {
+                count =  results.getInt("LOG_COUNT");
             }
             s.close();
 
-            return resultList;
 
         } catch (SQLException e) {
 
@@ -177,5 +406,6 @@
             log.error(msg, e);
             throw new RegistryException(msg, e);
         }
+        return count;
     }
 }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/session/UserRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/session/UserRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/session/UserRegistry.java	Thu May  8 01:11:43 2008
@@ -449,6 +449,18 @@
         return coreRegistry.getLogs(resourcePath, action, userName, from, to, recentFirst);
     }
 
+
+    public LogEntryCollection getLogCollection(String resourcePath,
+                                               int action,
+                                               String userName,
+                                               Date from,
+                                               Date to,
+                                               boolean recentFirst) throws RegistryException {
+        CurrentSession.setUser(userName);
+        CurrentSession.setRealm(userRealm);
+        return coreRegistry.getLogCollection(resourcePath, action, userName, from, to, recentFirst);
+    }
+
     public void addAspect(String name, Aspect aspect) throws RegistryException {
 
         CurrentSession.setUser(userName);



More information about the Registry-dev mailing list