From dacdd8282d96e6367b77670b094dbc55f620a23b Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Wed, 19 Sep 2012 10:36:11 -0500
Subject: [PATCH] Added log messages using Log4j.

Some REST services (user, group, and group member) have been modified
to log messages using Log4j.

Ticket #195
---
 base/common/shared/conf/log4j.properties           | 50 ++++++++++++----------
 base/common/src/CMakeLists.txt                     |  9 +++-
 .../cms/servlet/admin/GroupMemberService.java      | 31 +++++++++++---
 .../netscape/cms/servlet/admin/GroupService.java   | 21 +++++++++
 .../netscape/cms/servlet/admin/UserService.java    | 23 ++++++++++
 5 files changed, 105 insertions(+), 29 deletions(-)

diff --git a/base/common/shared/conf/log4j.properties b/base/common/shared/conf/log4j.properties
index dd4bd9318b4bd4f416e05a4d830d992a7a86f4b6..42c52513bced2b0a5b995344a85a251e00a039dd 100644
--- a/base/common/shared/conf/log4j.properties
+++ b/base/common/shared/conf/log4j.properties
@@ -4,27 +4,31 @@
 # Modifications: configuration parameters
 # --- END COPYRIGHT BLOCK ---
 
-log4j.rootLogger=debug, R
-log4j.appender.R=org.apache.log4j.RollingFileAppender
-log4j.appender.R.File=${catalina.base}/logs/catalina.out
-log4j.appender.R.MaxFileSize=10MB
-log4j.appender.R.MaxBackupIndex=10
-log4j.appender.R.layout=org.apache.log4j.PatternLayout
-log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
-log4j.logger.org.apache.catalina=DEBUG, R
-log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
-log4j.logger.org.apache.catalina.core=DEBUG, R
-log4j.logger.org.apache.catalina.session=DEBUG, R
+log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
+log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
+log4j.appender.CONSOLE.layout.ConversionPattern=%p %t %c - %m%n
 
-#resteasy
-log4j.appender.stdout=org.apache.log4j.ConsoleAppender
-log4j.appender.stdout.Target=System.out
-log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
-log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p (%c:%L) - %m%n
-log4j.rootLogger=warn, stdout
-log4j.rootCategory=debug, stdout
-log4j.category.org.jboss.resteasy.core=debug
-log4j.category.org.jboss.resteasy.plugins.providers=debug
-log4j.category.org.jboss.resteasy.specimpl=debug
-log4j.category.org.jboss.resteasy.plugins.server=debug
-log4j.logger.org.jboss.resteasy.mock=debug
+log4j.appender.CATALINA=org.apache.log4j.RollingFileAppender
+log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.out
+log4j.appender.CATALINA.MaxFileSize=10MB
+log4j.appender.CATALINA.MaxBackupIndex=10
+log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
+log4j.appender.CATALINA.layout.ConversionPattern=%d{ABSOLUTE} %5p (%c:%L) - %m%n
+
+log4j.rootLogger=INFO, CONSOLE
+
+# Catalina
+log4j.logger.org.apache.catalina=INFO
+log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO
+log4j.logger.org.apache.catalina.core=INFO
+log4j.logger.org.apache.catalina.session=INFO
+
+# RESTEasy
+log4j.logger.org.jboss.resteasy.core=INFO
+log4j.logger.org.jboss.resteasy.plugins.providers=INFO
+log4j.logger.org.jboss.resteasy.specimpl=INFO
+log4j.logger.org.jboss.resteasy.plugins.server=INFO
+log4j.logger.org.jboss.resteasy.mock=INFO
+
+# PKI
+log4j.logger.com.netscape.cms=INFO
diff --git a/base/common/src/CMakeLists.txt b/base/common/src/CMakeLists.txt
index fb4f5675ea74de52ea122946d390cb807045071a..195d5d00eb1ec6e30dfe4b19c3a781c2546e7493 100644
--- a/base/common/src/CMakeLists.txt
+++ b/base/common/src/CMakeLists.txt
@@ -37,6 +37,13 @@ find_file(APACHE_COMMONS_LANG_JAR
         /usr/share/java
 )
 
+find_file(COMMONS_LOGGING_JAR
+    NAMES
+        commons-logging.jar
+    PATHS
+        /usr/share/java
+)
+
 find_file(TOMCAT_CATALINA_JAR
     NAMES
         catalina.jar
@@ -177,7 +184,7 @@ javac(pki-cms-classes
         ${PKI_NSUTIL_JAR} ${PKI_CMSUTIL_JAR}
         ${LDAPJDK_JAR} ${SERVLET_JAR} ${VELOCITY_JAR} ${XALAN_JAR} ${XERCES_JAR}
         ${JSS_JAR} ${COMMONS_CODEC_JAR} ${COMMONS_HTTPCLIENT_JAR}
-        ${APACHE_COMMONS_LANG_JAR}
+        ${APACHE_COMMONS_LANG_JAR} ${COMMONS_LOGGING_JAR}
         ${TOMCAT_CATALINA_JAR} ${TOMCAT_UTIL_JAR} ${SYMKEY_JAR}
         ${JAXRS_API_JAR} ${RESTEASY_JAXRS_JAR} ${RESTEASY_ATOM_PROVIDER_JAR}
         ${HTTPCLIENT_JAR} ${HTTPCORE_JAR}
diff --git a/base/common/src/com/netscape/cms/servlet/admin/GroupMemberService.java b/base/common/src/com/netscape/cms/servlet/admin/GroupMemberService.java
index 0854be3aae1e4591aa1b078a35094498ef7177b7..c4ba2c6ca595e7945f1a9e659c5bac42e6995ce3 100644
--- a/base/common/src/com/netscape/cms/servlet/admin/GroupMemberService.java
+++ b/base/common/src/com/netscape/cms/servlet/admin/GroupMemberService.java
@@ -26,6 +26,8 @@ import java.util.Map;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
 import com.netscape.certsrv.apps.CMS;
@@ -51,6 +53,8 @@ import com.netscape.cms.servlet.base.PKIService;
  */
 public class GroupMemberService extends PKIService implements GroupMemberResource {
 
+    public static Log log = LogFactory.getLog(GroupMemberService.class);
+
     public final static int DEFAULT_SIZE = 20;
 
     public final static String MULTI_ROLE_ENABLE = "multiroles.enable";
@@ -75,6 +79,9 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
 
     @Override
     public GroupMemberCollection findGroupMembers(String groupID, Integer start, Integer size) {
+
+        log.info("Finding members in group "+groupID+".");
+
         try {
             start = start == null ? 0 : start;
             size = size == null ? DEFAULT_SIZE : size;
@@ -118,6 +125,8 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
                 response.addLink(new Link("next", uri));
             }
 
+            log.info("Found "+i+" member(s) in group "+groupID+".");
+
             return response;
 
         } catch (PKIException e) {
@@ -138,6 +147,9 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
 
     public Response addGroupMember(String groupID, GroupMemberData groupMemberData) {
 
+        String memberID = groupMemberData.getID();
+        log.info("Adding member "+memberID+" into group "+groupID+".");
+
         try {
             if (groupID == null) {
                 log(ILogger.LL_FAILURE, CMS.getLogMessage("ADMIN_SRVLT_NULL_RS_ID"));
@@ -150,7 +162,6 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
                 throw new PKIException(getUserMessage("CMS_USRGRP_SRVLT_GROUP_NOT_EXIST"));
             }
 
-            String memberID = groupMemberData.getID();
             boolean multiRole = true;
 
             try {
@@ -189,6 +200,7 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
                     new Object[] { adminId, memberID, groupID });
 
             auditAddGroupMember(groupID, groupMemberData, ILogger.SUCCESS);
+            log.warn("Added member "+memberID+" into group "+groupID+".");
 
             // read the data back
             groupMemberData = getGroupMember(groupID, memberID);
@@ -286,6 +298,8 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
     @Override
     public GroupMemberData getGroupMember(String groupID, String memberID) {
 
+        log.info("Finding member "+memberID+" in group "+groupID+".");
+
         try {
             if (groupID == null) {
                 log(ILogger.LL_FAILURE, CMS.getLogMessage("ADMIN_SRVLT_NULL_RS_ID"));
@@ -298,16 +312,19 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
                 throw new PKIException(getUserMessage("CMS_USRGRP_SRVLT_GROUP_NOT_EXIST"));
             }
 
+            log.debug("Members:");
             Enumeration<String> e = group.getMemberNames();
             while (e.hasMoreElements()) {
                 String memberName = e.nextElement();
+                log.debug(" - "+memberName);
                 if (!memberName.equals(memberID)) continue;
 
+                log.info("Found member "+memberID+" in group "+groupID+".");
                 GroupMemberData groupMemberData = createGroupMemberData(memberID);
                 return groupMemberData;
             }
 
-            throw new PKIException("Group member not found");
+            throw new PKIException("Member "+memberID+" not found in group "+groupID+".");
 
         } catch (PKIException e) {
             throw e;
@@ -326,6 +343,10 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
     }
 
     public void removeGroupMember(String groupID, GroupMemberData groupMemberData) {
+
+        String memberID = groupMemberData.getID();
+        log.info("Deleting member "+memberID+" from group "+groupID+".");
+
         try {
             if (groupID == null) {
                 log(ILogger.LL_FAILURE, CMS.getLogMessage("ADMIN_SRVLT_NULL_RS_ID"));
@@ -338,8 +359,7 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
                 throw new PKIException(getUserMessage("CMS_USRGRP_SRVLT_GROUP_NOT_EXIST"));
             }
 
-            String member = groupMemberData.getID();
-            userGroupManager.removeUserFromGroup(group, member);
+            userGroupManager.removeUserFromGroup(group, memberID);
 
             // for audit log
             SessionContext sContext = SessionContext.getContext();
@@ -347,9 +367,10 @@ public class GroupMemberService extends PKIService implements GroupMemberResourc
 
             logger.log(ILogger.EV_AUDIT, ILogger.S_USRGRP,
                     AuditFormat.LEVEL, AuditFormat.REMOVEUSERGROUPFORMAT,
-                    new Object[] { adminId, member, groupID });
+                    new Object[] { adminId, memberID, groupID });
 
             auditDeleteGroupMember(groupID, groupMemberData, ILogger.SUCCESS);
+            log.warn("Deleted member "+memberID+" from group "+groupID+".");
 
         } catch (PKIException e) {
             auditDeleteGroupMember(groupID, groupMemberData, ILogger.FAILURE);
diff --git a/base/common/src/com/netscape/cms/servlet/admin/GroupService.java b/base/common/src/com/netscape/cms/servlet/admin/GroupService.java
index b82df9a2fc8d293138ff1920eb4a7c34f7a145d7..659a0bd8c36899c2fc84a4f02a79d7d9db4b60ff 100644
--- a/base/common/src/com/netscape/cms/servlet/admin/GroupService.java
+++ b/base/common/src/com/netscape/cms/servlet/admin/GroupService.java
@@ -27,6 +27,8 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
 import com.netscape.certsrv.apps.CMS;
@@ -49,6 +51,8 @@ import com.netscape.cmsutil.ldap.LDAPUtil;
  */
 public class GroupService extends PKIService implements GroupResource {
 
+    public static Log log = LogFactory.getLog(GroupService.class);
+
     public final static int DEFAULT_SIZE = 20;
 
     public IUGSubsystem userGroupManager = (IUGSubsystem) CMS.getSubsystem(CMS.SUBSYSTEM_UG);
@@ -79,6 +83,9 @@ public class GroupService extends PKIService implements GroupResource {
      */
     @Override
     public GroupCollection findGroups(String filter, Integer start, Integer size) {
+
+        log.info("Finding groups.");
+
         try {
             filter = StringUtils.isEmpty(filter) ? "*" : "*"+LDAPUtil.escapeFilter(filter)+"*";
             start = start == null ? 0 : start;
@@ -112,6 +119,8 @@ public class GroupService extends PKIService implements GroupResource {
                 response.addLink(new Link("next", uri));
             }
 
+            log.info("Found "+i+" group(s).");
+
             return response;
 
         } catch (Exception e) {
@@ -128,6 +137,8 @@ public class GroupService extends PKIService implements GroupResource {
     @Override
     public GroupData getGroup(String groupID) {
 
+        log.info("Finding group "+groupID+".");
+
         try {
             if (groupID == null) {
                 log(ILogger.LL_FAILURE, CMS.getLogMessage("ADMIN_SRVLT_NULL_RS_ID"));
@@ -140,6 +151,8 @@ public class GroupService extends PKIService implements GroupResource {
                 throw new PKIException(getUserMessage("CMS_USRGRP_SRVLT_GROUP_NOT_EXIST"));
             }
 
+            log.info("Found group "+groupID+".");
+
             return createGroupData(group);
 
         } catch (PKIException e) {
@@ -167,6 +180,7 @@ public class GroupService extends PKIService implements GroupResource {
     public Response addGroup(GroupData groupData) {
 
         String groupID = groupData.getID();
+        log.info("Adding group "+groupID+".");
 
         // ensure that any low-level exceptions are reported
         // to the signed audit log and stored as failures
@@ -190,6 +204,7 @@ public class GroupService extends PKIService implements GroupResource {
                 userGroupManager.addGroup(group);
 
                 auditAddGroup(groupID, groupData, ILogger.SUCCESS);
+                log.warn("Added group "+groupID+".");
 
                 // read the data back
                 groupData = getGroup(groupID);
@@ -232,6 +247,8 @@ public class GroupService extends PKIService implements GroupResource {
     @Override
     public Response modifyGroup(String groupID, GroupData groupData) {
 
+        log.info("Modifying group "+groupID+".");
+
         // ensure that any low-level exceptions are reported
         // to the signed audit log and stored as failures
         try {
@@ -250,6 +267,7 @@ public class GroupService extends PKIService implements GroupResource {
                 userGroupManager.modifyGroup(group);
 
                 auditModifyGroup(groupID, groupData, ILogger.SUCCESS);
+                log.warn("Modified group "+groupID+".");
 
                 // read the data back
                 groupData = getGroup(groupID);
@@ -290,6 +308,8 @@ public class GroupService extends PKIService implements GroupResource {
     @Override
     public void removeGroup(String groupID) {
 
+        log.info("Deleting group "+groupID+".");
+
         // ensure that any low-level exceptions are reported
         // to the signed audit log and stored as failures
         try {
@@ -302,6 +322,7 @@ public class GroupService extends PKIService implements GroupResource {
             userGroupManager.removeGroup(groupID);
 
             auditDeleteGroup(groupID, ILogger.SUCCESS);
+            log.warn("Deleted group "+groupID+".");
 
         } catch (PKIException e) {
             auditDeleteGroup(groupID, ILogger.FAILURE);
diff --git a/base/common/src/com/netscape/cms/servlet/admin/UserService.java b/base/common/src/com/netscape/cms/servlet/admin/UserService.java
index f28a8151f43386df22db0839aafd4ba7f90f8af5..8cb3936c428819cade50a702d3356409e364fc00 100644
--- a/base/common/src/com/netscape/cms/servlet/admin/UserService.java
+++ b/base/common/src/com/netscape/cms/servlet/admin/UserService.java
@@ -29,6 +29,8 @@ import javax.ws.rs.core.Response;
 import netscape.ldap.LDAPException;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
 import com.netscape.certsrv.apps.CMS;
@@ -54,6 +56,8 @@ import com.netscape.cmsutil.ldap.LDAPUtil;
  */
 public class UserService extends PKIService implements UserResource {
 
+    public static Log log = LogFactory.getLog(UserService.class);
+
     public final static int DEFAULT_SIZE = 20;
 
     public final static String BACK_SLASH = "\\";
@@ -87,8 +91,12 @@ public class UserService extends PKIService implements UserResource {
      */
     @Override
     public UserCollection findUsers(String filter, Integer start, Integer size) {
+
+        log.info("Finding users.");
+
         try {
             filter = StringUtils.isEmpty(filter) ? "*" : "*"+LDAPUtil.escapeFilter(filter)+"*";
+
             start = start == null ? 0 : start;
             size = size == null ? DEFAULT_SIZE : size;
 
@@ -120,6 +128,8 @@ public class UserService extends PKIService implements UserResource {
                 response.addLink(new Link("next", uri));
             }
 
+            log.info("Found "+i+" user(s).");
+
             return response;
 
         } catch (Exception e) {
@@ -138,6 +148,9 @@ public class UserService extends PKIService implements UserResource {
      */
     @Override
     public UserData getUser(String userID) {
+
+        log.info("Finding user "+userID+".");
+
         try {
             if (userID == null) {
                 log(ILogger.LL_FAILURE, CMS.getLogMessage("ADMIN_SRVLT_NULL_RS_ID"));
@@ -173,6 +186,8 @@ public class UserService extends PKIService implements UserResource {
             String type = user.getUserType();
             if (!StringUtils.isEmpty(type)) userData.setType(type);
 
+            log.info("Found user "+userID+".");
+
             return userData;
 
         } catch (PKIException e) {
@@ -201,6 +216,7 @@ public class UserService extends PKIService implements UserResource {
     public Response addUser(UserData userData) {
 
         String userID = userData.getID();
+        log.info("Adding user "+userID+".");
 
         // ensure that any low-level exceptions are reported
         // to the signed audit log and stored as failures
@@ -278,6 +294,7 @@ public class UserService extends PKIService implements UserResource {
                 userGroupManager.addUser(user);
 
                 auditAddUser(userID, userData, ILogger.SUCCESS);
+                log.warn("Added user "+userID+".");
 
                 // read the data back
                 userData = getUser(userID);
@@ -332,6 +349,8 @@ public class UserService extends PKIService implements UserResource {
     @Override
     public Response modifyUser(String userID, UserData userData) {
 
+        log.info("Modifying user "+userID+".");
+
         // ensure that any low-level exceptions are reported
         // to the signed audit log and stored as failures
         try {
@@ -377,6 +396,7 @@ public class UserService extends PKIService implements UserResource {
                 userGroupManager.modifyUser(user);
 
                 auditModifyUser(userID, userData, ILogger.SUCCESS);
+                log.warn("Modified user "+userID+".");
 
                 // read the data back
                 userData = getUser(userID);
@@ -420,6 +440,8 @@ public class UserService extends PKIService implements UserResource {
     @Override
     public void removeUser(String userID) {
 
+        log.info("Deleting user "+userID+".");
+
         // ensure that any low-level exceptions are reported
         // to the signed audit log and stored as failures
         try {
@@ -450,6 +472,7 @@ public class UserService extends PKIService implements UserResource {
                 userGroupManager.removeUser(userID);
 
                 auditDeleteUser(userID, ILogger.SUCCESS);
+                log.warn("Deleted user "+userID+".");
 
             } catch (Exception e) {
                 throw new PKIException(getUserMessage("CMS_USRGRP_SRVLT_FAIL_USER_RMV"));
-- 
1.7.11.2

