[Registry-dev] svn commit r9898 - in
trunk/registry/modules/core/src: main/java/org/wso2/registry
main/java/org/wso2/registry/jdbc
main/java/org/wso2/registry/jdbc/dao
main/java/org/wso2/registry/jdbc/hsql
test/java/org/wso2/registry/jdbc
svn at wso2.org
svn at wso2.org
Mon Nov 19 01:52:42 PST 2007
Author: chathura
Date: Mon Nov 19 01:52:30 2007
New Revision: 9898
Added:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/LogsDAO.java
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/LogEntry.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/DatabaseConstants.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/hsql/DBUtils.java
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
Log:
Implemented the audit log support for major actions in the registry.
Implemented the search on audit logs based on resource path, user and time period.
Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/LogEntry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/LogEntry.java (original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/LogEntry.java Mon Nov 19 01:52:30 2007
@@ -28,6 +28,7 @@
public static final int COMMENT = 2;
public static final int TAG = 3;
public static final int RATING = 4;
+ public static final int DELETE_RESOURCE = 5;
/**
* Path of the resource on which the action is performed.
Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/DatabaseConstants.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/DatabaseConstants.java (original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/DatabaseConstants.java Mon Nov 19 01:52:30 2007
@@ -63,6 +63,11 @@
public static final String COMMENTED_USER_ID_FIELD = "USER_ID";
public static final String COMMENTED_TIME_FIELD = "COMMENTED_TIME";
+ // Field names of logs table
+ public static final String LOGGED_TIME_FIELD = "LOGGED_TIME";
+ public static final String ACTION_FIELD = "ACTION";
+ public static final String ACTION_DATA_FIELD = "ACTION_DATA";
+
// Field names of Ratings table
public static final String RATING_ID_FIELD = "R_ID";
public static final String RATED_TIME_FIELD = "RATED_TIME";
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 Mon Nov 19 01:52:30 2007
@@ -23,10 +23,7 @@
import org.apache.commons.logging.LogFactory;
import org.wso2.registry.*;
import org.wso2.registry.i18n.Messages;
-import org.wso2.registry.jdbc.dao.CommentsDAO;
-import org.wso2.registry.jdbc.dao.RatingsDAO;
-import org.wso2.registry.jdbc.dao.TagsDAO;
-import org.wso2.registry.jdbc.dao.VersionedResourceDAO;
+import org.wso2.registry.jdbc.dao.*;
import org.wso2.registry.jdbc.dataobjects.TagDO;
import org.wso2.registry.jdbc.mediatypes.MediaTypeManager;
import org.wso2.registry.jdbc.queries.QueryProcessor;
@@ -58,6 +55,7 @@
private TagsDAO tagsDAO = null;
private CommentsDAO commentsDAO = null;
private RatingsDAO ratingsDAO = null;
+ private LogsDAO logsDAO = null;
public JDBCRegistry(Realm realm) throws RegistryException {
connectionFactory = new ConnectionFactory();
@@ -68,6 +66,7 @@
this.tagsDAO = new TagsDAO();
this.commentsDAO = new CommentsDAO();
this.ratingsDAO = new RatingsDAO();
+ logsDAO = new LogsDAO();
// check if root is added. if not add it.
Connection conn = connectionFactory.getConnection();
@@ -179,6 +178,33 @@
public synchronized void put(String path, Resource resource) throws RegistryException {
path = preparePath(path);
mediaTypeManager.put(path, resource);
+
+ Connection conn = connectionFactory.getConnection();
+
+ try {
+ conn.setAutoCommit(false);
+
+ long resourceID = resourceDAO.getResourceID(path, conn);
+ logsDAO.addLog(resourceID, User.getCurrentUser(), LogEntry.UPDATE, null, conn);
+
+ conn.commit();
+
+ } catch (SQLException e) {
+ try {
+ conn.rollback();
+ } catch (SQLException e1) {
+ e1.printStackTrace();
+ }
+
+ String msg = e.getMessage();
+ log.error(msg, e);
+ throw new RegistryException(msg);
+
+ } finally {
+ try {
+ conn.close();
+ } catch (SQLException ignore) {}
+ }
}
/**
@@ -193,7 +219,34 @@
path = preparePath(path);
- mediaTypeManager.delete(path);
+ mediaTypeManager.delete(path);
+
+ Connection conn = connectionFactory.getConnection();
+
+ try {
+ conn.setAutoCommit(false);
+
+ long resourceID = resourceDAO.getResourceID(path, conn);
+ logsDAO.addLog(resourceID, User.getCurrentUser(), LogEntry.DELETE_RESOURCE, null, conn);
+
+ conn.commit();
+
+ } catch (SQLException e) {
+ try {
+ conn.rollback();
+ } catch (SQLException e1) {
+ e1.printStackTrace();
+ }
+
+ String msg = e.getMessage();
+ log.error(msg, e);
+ throw new RegistryException(msg);
+
+ } finally {
+ try {
+ conn.close();
+ } catch (SQLException ignore) {}
+ }
}
public String[] getVersions(String path) throws RegistryException {
@@ -335,6 +388,7 @@
}
tagsDAO.addTagging(tagDO.getId(), resource.getId(), userID, conn);
+ logsDAO.addLog(resource.getId(), userID, LogEntry.TAG, tags[i], conn);
}
conn.commit();
@@ -464,6 +518,8 @@
Resource resource = resourceDAO.getLatestVersion(resourcePath, conn);
if (resource != null) {
commentsDAO.addComment(resource.getId(), userID, comment, conn);
+ logsDAO.addLog(
+ resource.getId(), userID, LogEntry.COMMENT, comment.getCommentText(), conn);
} else {
String msg = Messages.getMessage("comment.on.null.artfact", resourcePath);
@@ -563,6 +619,10 @@
} else {
ratingsDAO.addRating(resource.getId(), userID, rating, conn);
}
+
+ logsDAO.addLog(resource.getId(),
+ userID, LogEntry.RATING, new Integer(rating).toString(), conn);
+
} else {
String msg = Messages.getMessage("rate.on.null.artfact", resourcePath);
log.error(msg);
@@ -757,23 +817,39 @@
Connection conn = connectionFactory.getConnection();
- List logEntryList = new ArrayList();
- try {
- List artifactLogs = new VersionedResourceDAO().getLogs(resourcePath,userName,from,to,
- conn);
- //logEntryList.addAll(artifactLogs);
-
- List commentLogs = commentsDAO.getLogs(resourcePath, userName, from, to, conn);
- logEntryList.addAll(commentLogs);
+ List logEntryList;
- List ratingLogs = ratingsDAO.getLogs(resourcePath, userName, from, to, conn);
- logEntryList.addAll(ratingLogs);
+ try {
+ logEntryList = logsDAO.getLogs(resourcePath, userName, from, to, conn);
+
+ } catch (SQLException e) {
+ String msg = "Could not get logs. Caused by: " + e.getMessage();
+ log.error(msg, e);
+ throw new RegistryException(msg);
- } catch (Exception e) {
- log.info("error when getting logs from version table");
- throw new RegistryException(e.getMessage());
+ } finally {
+ try {
+ conn.close();
+ } catch (SQLException ignore) {}
}
+ //List logEntryList = new ArrayList();
+ //try {
+ // List artifactLogs = new VersionedResourceDAO().getLogs(resourcePath,userName,from,to,
+ // conn);
+ // //logEntryList.addAll(artifactLogs);
+ //
+ // List commentLogs = commentsDAO.getLogs(resourcePath, userName, from, to, conn);
+ // logEntryList.addAll(commentLogs);
+ //
+ // List ratingLogs = ratingsDAO.getLogs(resourcePath, userName, from, to, conn);
+ // logEntryList.addAll(ratingLogs);
+ //
+ //} catch (Exception e) {
+ // log.info("error when getting logs from version table");
+ // throw new RegistryException(e.getMessage());
+ //}
+
LogEntry[] logEntries = new LogEntry[logEntryList.size()];
for (int i = 0; i < logEntryList.size(); i++) {
logEntries[i] = (LogEntry) logEntryList.get(i);
Added: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/LogsDAO.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/LogsDAO.java Mon Nov 19 01:52:30 2007
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2006, 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.jdbc.dao;
+
+import org.wso2.registry.LogEntry;
+import org.wso2.registry.jdbc.DatabaseConstants;
+
+import java.sql.*;
+import java.util.Date;
+import java.util.List;
+import java.util.ArrayList;
+
+public class LogsDAO {
+
+ public void addLog(long resourceID, String userName, int action, String actionData, Connection conn)
+ throws SQLException {
+
+ String sql = "INSERT INTO LOGS (RID, USER_ID, LOGGED_TIME, ACTION, ACTION_DATA) " +
+ "VALUES (?, ?, ?, ?, ?)";
+
+ PreparedStatement s = conn.prepareStatement(sql);
+ s.setLong(1, resourceID);
+ s.setString(2, userName);
+ s.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
+ s.setInt(4, action);
+ s.setString(5, actionData);
+
+ s.executeUpdate();
+ }
+
+ public List getLogs(String resourcePath,
+ String userName,
+ Date from, Date to, Connection conn) throws SQLException {
+
+ ResourceDAO resourceDAO = new ResourceDAO();
+
+ long aid = -1;
+ if (resourcePath != null) {
+ aid = resourceDAO.getArtifactID(resourcePath, conn);
+ }
+
+ String sql = "SELECT A.PATH, L.USER_ID, L.LOGGED_TIME, L.ACTION, L.ACTION_DATA FROM ARTIFACTS A, LOGS L WHERE A.AID=L.RID";
+
+ if (aid != -1) {
+ sql = sql + " AND L.RID=?";
+ }
+
+ if (userName != null) {
+ sql = sql + " AND L.USER_ID=?";
+ }
+
+ if (from != null) {
+ sql = sql + " AND L.LOGGED_TIME>?";
+ }
+
+ if (to != null) {
+ sql = sql + " AND L.LOGGED_TIME<?";
+ }
+
+ PreparedStatement s = conn.prepareStatement(sql);
+
+ int paramNumber = 1;
+
+ if (aid != -1) {
+ s.setLong(paramNumber, aid);
+ 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++;
+ }
+
+ 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 java.sql.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);
+ }
+
+ return resultList;
+ }
+}
Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/hsql/DBUtils.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/hsql/DBUtils.java (original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/hsql/DBUtils.java Mon Nov 19 01:52:30 2007
@@ -78,6 +78,14 @@
"PRIMARY KEY (CM_ID)," +
"FOREIGN KEY (AID) REFERENCES ARTIFACTS (AID))";
+ private static final String logsTable = "CREATE TABLE LOGS (LOG_ID INTEGER GENERATED BY DEFAULT AS IDENTITY," +
+ "RID INTEGER," +
+ "USER_ID VARCHAR (20) NOT NULL," +
+ "LOGGED_TIME TIMESTAMP NOT NULL," +
+ "ACTION INTEGER NOT NULL," +
+ "ACTION_DATA VARCHAR (500)," +
+ "PRIMARY KEY (LOG_ID))";
+
private static final String ratingsTable = "CREATE TABLE RATINGS (R_ID INTEGER GENERATED BY DEFAULT AS IDENTITY," +
"AID INTEGER NOT NULL," +
"USER_ID VARCHAR (20) NOT NULL," +
@@ -114,6 +122,7 @@
s.executeUpdate(taggingsTable);
s.executeUpdate(commentsTable);
s.executeUpdate(ratingsTable);
+ s.executeUpdate(logsTable);
//creating version related tables
s.executeUpdate(versionTable);
Modified: trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
==============================================================================
--- trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java (original)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java Mon Nov 19 01:52:30 2007
@@ -756,6 +756,31 @@
//int a = 1;
}
+ public void testLogs() throws RegistryException {
+
+ String r1Content = "this is the r200 content.";
+ Resource r1 = new Resource();
+ r1.setContent(r1Content.getBytes());
+
+ try {
+ registry.put("/r200", r1);
+ } catch (RegistryException e) {
+ fail("Couldn't put a content resource in to path /r200");
+ }
+
+ registry.rateResource("/r200", 5);
+
+ LogEntry[] logs = registry.getLogs("/r200", null, null, null);
+
+ LogEntry l1 = logs[0];
+ assertEquals("Log for adding /r200 is not added properly.", LogEntry.UPDATE, l1.getAction());
+ assertEquals("Log for adding /r200 is not added properly.", "/r200", l1.getResourcePath());
+
+ LogEntry l2 = logs[1];
+ assertEquals("Log for rating /r200 is not added properly.", LogEntry.RATING, l2.getAction());
+ assertEquals("Log for rating /r200 is not added properly.", "5", l2.getActionData());
+ }
+
public void testCombinedScenario() throws RegistryException {
// put a content resource in to the root
More information about the Registry-dev
mailing list