KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > log > Log4jLogDevice


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.model.log;
15
16 import java.io.Serializable JavaDoc;
17 import java.security.cert.X509Certificate JavaDoc;
18 import java.text.DateFormat JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import org.apache.log4j.Level;
23 import org.apache.log4j.Logger;
24 import org.apache.log4j.Priority;
25 import org.ejbca.core.model.InternalResources;
26
27
28 /**
29  * Implements a log device using Log4j, implementes the Singleton pattern.
30  *
31  * @version $Id: Log4jLogDevice.java,v 1.3 2007/01/15 14:32:32 anatom Exp $
32  */

33 public class Log4jLogDevice implements ILogDevice, Serializable JavaDoc {
34
35     /** Log4j instance */
36     private static final Logger log = Logger.getLogger(Log4jLogDevice.class);
37
38     /** Internal localization of logs and errors */
39     private static final InternalResources intres = InternalResources.getInstance();
40
41     /**
42      * A handle to the unique Singleton instance.
43      */

44     private static Log4jLogDevice instance;
45
46
47     /**
48      * Initializes all internal data
49      *
50      * @param prop Arguments needed for the eventual creation of the object
51      */

52
53     protected Log4jLogDevice(Properties JavaDoc prop) throws Exception JavaDoc {
54         // Do nothing
55
}
56
57     /**
58      * Creates (if needed) the log device and returns the object.
59      *
60      * @param prop Arguments needed for the eventual creation of the object
61      * @return An instance of the log device.
62      */

63     public static synchronized ILogDevice instance(Properties JavaDoc prop) throws Exception JavaDoc {
64         if (instance == null) {
65             instance = new Log4jLogDevice(prop);
66         }
67         return instance;
68     }
69
70     public void log(Admin admininfo, int caid, int module, Date JavaDoc time, String JavaDoc username, X509Certificate JavaDoc certificate, int event, String JavaDoc comment) {
71         log(admininfo, caid, module, time, username, certificate, event, comment, null);
72     }
73
74     public void log(Admin admininfo, int caid, int module, Date JavaDoc time, String JavaDoc username, X509Certificate JavaDoc certificate, int event, String JavaDoc comment, Exception JavaDoc exception) {
75
76         String JavaDoc user = intres.getLocalizedMessage("log.nouserinvolved");
77         String JavaDoc cert = intres.getLocalizedMessage("log.nocertinvolved");
78         String JavaDoc admin = intres.getLocalizedMessage("log.adminnotknown");
79
80         if (username != null) {
81             user = username;
82         }
83
84         if (certificate != null) {
85             cert = certificate.getSerialNumber().toString(16) + ", issuer: " + certificate.getIssuerDN().toString();
86         }
87
88         if (admininfo.getAdminType() == Admin.TYPE_CLIENTCERT_USER) {
89             admin = Admin.ADMINTYPETEXTS[Admin.TYPE_CLIENTCERT_USER] + ", Certificate SNR : " + admininfo.getAdminData();
90         } else if (admininfo.getAdminType() == Admin.TYPE_PUBLIC_WEB_USER) {
91             if (admininfo.getAdminData() != null) {
92                 if (!admininfo.getAdminData().equals(""))
93                     admin = Admin.ADMINTYPETEXTS[Admin.TYPE_PUBLIC_WEB_USER] + ", IP Address : " + admininfo.getAdminData();
94             } else {
95                 admin = Admin.ADMINTYPETEXTS[Admin.TYPE_PUBLIC_WEB_USER];
96             }
97         } else {
98             admin = Admin.ADMINTYPETEXTS[admininfo.getAdminType()];
99         }
100
101         Priority priority = Level.INFO;
102         String JavaDoc eventText = "";
103         if (event >= LogEntry.EVENT_ERROR_BOUNDRARY) {
104             priority = Level.ERROR;
105             event -= LogEntry.EVENT_ERROR_BOUNDRARY;
106             eventText = LogEntry.EVENTNAMES_ERROR[event];
107         }else{
108             eventText = LogEntry.EVENTNAMES_INFO[event];
109         }
110
111         String JavaDoc logline = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(time) + ", CAId : " + caid + ", " + LogEntry.MODULETEXTS[module] + ", " + eventText + ", Administrator : " +
112                 admin + ", User : " + user + ", Certificate : " + cert + ", Comment : " + comment;
113         log.log(priority, logline, null);
114
115         if (exception != null) {
116             log.error("Exception : ", exception);
117         }
118     }
119 }
120
Popular Tags