JmsLogger.java
package
information.sh.lp.jms;
import
org.apache.log4j.LogManager;
import
org.apache.log4j.Logger;
public
class JmsLogger extends
Logger {
private static JMSQueueAppender JMS_APPENDER
;
private static final String FQCN = Logger.class
.getName();
protected
JmsLogger(String name) {
super
(name);
}
protected
JmsLogger(String name,JMSQueueAppender JMS_APPENDER ) {
super
(name);
this.JMS_APPENDER
=JMS_APPENDER;
}
static public
Logger getLogger(String name,JMSQueueAppender JMS_APPENDER) {
Logger logger = getLogger(name);
if(JMS_APPENDER!=null
){
logger.addAppender(JMS_APPENDER);
}
return
LogManager.getLogger(name);
}
static public
Logger getLogger(Class clazz,JMSQueueAppender JMS_APPENDER) {
Logger logger = LogManager.getLogger(clazz.getName());
if(JMS_APPENDER!=null
){
logger.addAppender(JMS_APPENDER);
}
return
LogManager.getLogger(clazz.getName());
}
public void
setJMSQueueAppender(JMSQueueAppender JMS_APPENDER) {
this.JMS_APPENDER
= JMS_APPENDER;
}
public
JMSQueueAppender getJMSQueueAppender() {
return this.JMS_APPENDER
;
}}
JMSLoggingEvent.java
package
information.sh.lp.jms;
import
java.util.Map;
import
org.apache.log4j.Category;
import
org.apache.log4j.Level;
import
org.apache.log4j.spi.LocationInfo;
import
org.apache.log4j.spi.LoggingEvent;
import
org.apache.log4j.spi.ThrowableInformation;
public
class JMSLoggingEvent extends LoggingEvent {
private static final long serialVersionUID = 5692208486663581076L;
private String projectName;
public JMSLoggingEvent(String fqnOfCategoryClass, Category logger,
long timeStamp, Level level, Object message, String threadName,
ThrowableInformation throwable, String ndc, LocationInfo info,
Map properties,String projectName) {
super(fqnOfCategoryClass, logger, timeStamp, level, message,
threadName, throwable, ndc, info, properties);
this.projectName=projectName;
}
public String getProjectName(){
return this.projectName;
}
public void setProjectName(String projectName){
this.projectName=projectName;
}
}
JMSQueueAppender.java
package
information.sh.lp.jms;
import
java.util.Hashtable;
import
java.util.Map;
import
javax.jms.ObjectMessage;
import
javax.jms.Queue;
import
javax.jms.QueueConnection;
import
javax.jms.QueueConnectionFactory;
import
javax.jms.QueueSender;
import
javax.jms.QueueSession;
import
javax.jms.Session;
import
javax.naming.Context;
import
javax.naming.InitialContext;
import
javax.naming.NamingException;
import
org.apache.log4j.AppenderSkeleton;
import
org.apache.log4j.Category;
import
org.apache.log4j.Layout;
import
org.apache.log4j.Level;
import
org.apache.log4j.helpers.LogLog;
import
org.apache.log4j.spi.ErrorCode;
import
org.apache.log4j.spi.LocationInfo;
import
org.apache.log4j.spi.LoggingEvent;
import
org.apache.log4j.spi.ThrowableInformation;
/**
* A Simple JMS (P2P) Queue Appender.
*
*
@author
Ceki G
ülcü
*
@author
Jamie
Tsao
*/
public
class JMSQueueAppender extends
AppenderSkeleton {
protected QueueConnection queueConnection
;
protected QueueSession queueSession
;
protected QueueSender queueSender
;
protected Queue queue
;
String
initialContextFactory
;
String
providerUrl
;
String
queueBindingName
;
String
queueConnectionFactoryBindingName
;
Layout
layout;
//add by Li, Chengri
String
projectName;
//add by Li, Chengri
public
JMSQueueAppender(){
}
public
JMSQueueAppender(Layout layout,String projectName) {
this.layout
= layout;
this.projectName
=projectName;
}
/**
* The
<b>InitialContextFactory</b>
option takes a string value.
* Its value, along with the
<b>ProviderUrl</b>
option will be used
* to get the InitialContext.
*/
public void
setInitialContextFactory(String initialContextFactory) {
this.initialContextFactory
= initialContextFactory;
}
/**
* Returns the value of the
<b>InitialContextFactory</b>
option.
*/
public
String getInitialContextFactory() {
return initialContextFactory
;
}
/**
* The
<b>ProviderUrl</b>
option takes a string value.
* Its value, along with the
<b>InitialContextFactory</b>
option will be used
* to get the InitialContext.
*/
public void
setProviderUrl(String providerUrl) {
this.providerUrl
= providerUrl;
}
/**
* Returns the value of the
<b>ProviderUrl</b>
option.
*/
public
String getProviderUrl() {
return providerUrl
;
}
/**
* The
<b>QueueConnectionFactoryBindingName</b>
option takes a
* string value. Its value will be used to lookup the appropriate
*
<code>QueueConnectionFactory</code>
from the JNDI context.
*/
public void
setQueueConnectionFactoryBindingName(String queueConnectionFactoryBindingName) {
this.queueConnectionFactoryBindingName
= queueConnectionFactoryBindingName;
}
/**
* Returns the value of the
<b>QueueConnectionFactoryBindingName</b>
option.
*/
public
String getQueueConnectionFactoryBindingName() {
return queueConnectionFactoryBindingName
;
}
/**
* The
<b>QueueBindingName</b>
option takes a
* string value. Its value will be used to lookup the appropriate
* destination
<code>Queue</code>
from the JNDI context.
*/
public void
setQueueBindingName(String queueBindingName) {
this.queueBindingName
= queueBindingName;
}
/**
Returns the value of the
<b>QueueBindingName</b>
option.
*/
public
String getQueueBindingName() {
return queueBindingName
;
}
public
Layout getLayout(Layout layout){
return this.layout
;
}
public void
setLayout(Layout layout){
this.layout
=layout;
}
public
String getProjectName(){
return this.projectName
;
}
public void
setProejctName(String projectName){
this.projectName
=projectName;
}
/**
* Overriding this method to activate the options for this class
* i.e. Looking up the Connection factory ...
*/
public void
activateOptions() {
QueueConnectionFactory queueConnectionFactory;
try
{
Context ctx = getInitialContext();
queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(
queueConnectionFactoryBindingName
);
queueConnection
= queueConnectionFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE
);
Queue queue = (Queue) ctx.lookup(
queueBindingName
);
queueSender = queueSession
.createSender(queue);
queueConnection
.start();
ctx.close();
}
catch
(Exception e) {
errorHandler
.error(
"Error while activating options for appender named ["
+
name + "].", e, ErrorCode.GENERIC_FAILURE
);
}
}
protected InitialContext getInitialContext() throws
NamingException {
try
{
Hashtable ht =
new
Hashtable();
// Populate property hashtable with data to retrieve the context.
ht.put(Context.
INITIAL_CONTEXT_FACTORY, initialContextFactory
);
ht.put(Context.
PROVIDER_URL, providerUrl
);
return (new
InitialContext(ht));
}
catch
(NamingException ne) {
LogLog.error(
"Could not get initial context with ["
+
initialContextFactory + "] and [" + providerUrl + "]."
);
throw
ne;
}
}
protected boolean
checkEntryConditions() {
String fail =
null
;
if (this.queueConnection == null
) {
fail =
"No QueueConnection"
;
}
else if (this.queueSession == null
) {
fail =
"No QueueSession"
;
}
else if (this.queueSender == null
) {
fail =
"No QueueSender"
;
}
if (fail != null
) {
errorHandler.error(fail + " for JMSQueueAppender named [" +
name
+
"]."
);
return false
;
}
else
{
return true
;
}
}
/**
* Close this JMSQueueAppender. Closing releases all resources used by the
* appender. A closed appender cannot be re
-
opened.
*/
public synchronized
// avoid concurrent append and close operations
void
close() {
if (this.closed
)
return
;
LogLog.debug(
"Closing appender [" + name + "]."
);
this.closed = true
;
try
{
if (queueSession != null
)
queueSession
.close();
if (queueConnection != null
)
queueConnection
.close();
}
catch
(Exception e) {
LogLog.error(
"Error while closing JMSQueueAppender [" + name + "]."
, e);
}
// Help garbage collection
queueSender = null
;
queueSession = null
;
queueConnection = null
;
}
/**
* This method called by
{@link AppenderSkeleton#doAppend}
method to
* do most of the real appending work. The LoggingEvent will be
* be wrapped in an ObjectMessage to be put on the JMS queue.
*/
public void
append(LoggingEvent event) {
information.sh.lp.jms.JMSLoggingEvent jevent;
if
(!checkEntryConditions()) {
return
;
}
try
{
ObjectMessage msg =
queueSession
.createObjectMessage();
if (layout != null
) {
layout
.format(event);
}
String fqnOfCategoryClass = event.getFQNOfLoggerClass();
Category logger = event.getLogger();
long
timeStamp = event.getTimeStamp();
Level level = event.getLevel();
Object message = event.getMessage();
String threadName = event.getThreadName();
ThrowableInformation throwable = event.getThrowableInformation();
String ndc = event.getNDC();
LocationInfo info = event.getLocationInformation();
Map properties = event.getProperties();
jevent =
new
information.sh.lp.jms.JMSLoggingEvent(fqnOfCategoryClass,
logger, timeStamp, level, message, threadName, throwable,
ndc, info, properties,
projectName
);
msg.setObject(jevent);
queueSender
.send(msg);
}
catch
(Exception e) {
errorHandler.error(
"Could not send message in JMSQueueAppender ["
+
name + "].", e, ErrorCode.GENERIC_FAILURE
);
}
}
public boolean
requiresLayout() {
return false
;
}}
JspFunction.java
package
information.sh.lp.jspFun;
import
information.sh.lp.infoBean.ContextInfo;
import
information.sh.lp.infoBean.Log;
import
information.sh.lp.model.IndexDAO;
import
information.sh.lp.util.FileUtilTool;
import
information.sh.lp.util.ParseString;
import
java.io.File;
import
java.util.ArrayList;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
*
@author
Li,
Chengri
* Describe: This class provides static methods to be used in JSP
*/
public
class
JspFunction {
private static int pageSize
= 30;
/**
*
*
@param
page Current page number
*
@param
logs Store the result of query
*
@return
The string which will show user the query result.
*/
public static
String curPgData(String page, ArrayList logs) {
StringBuffer sb =
new
StringBuffer();
int
pno = 1;
if (page != null && !(""
).equals(page)) {
try
{
pno = Integer.parseInt(page);
}
catch
(Exception e) {
pno = 1;
}
}
int startRecord = (pno - 1) * pageSize
;
int endRecord = startRecord + pageSize
;
String message =
null
;
while
(startRecord < endRecord && startRecord < logs.size()) {
Log temp = (Log)logs.get(startRecord++);
sb.append(
"<tr>"
);
sb.append(
"<td>" + startRecord + "</td>"
);
sb.append(
"<td>" + temp.getTime() + "</td>"
);
sb.append(
"<td>" + temp.getLevel() + "</td>"
);
message = ParseString.inputToHtml(temp.getMessage());
if
(message.length() > 45) {
message = message.substring(0, 35) +
"......"
;
}
sb.append(
"<td>" + message + "</td>"
);
sb.append(
"<td algin=/"center/">"
);
sb.append(
"<a style='color: blue' href=query.do?action=detail&&recordIndex="+ startRecord + ">detail</a>" + "</td>"
);
sb.append(
"</tr>"
);
}
return
sb.toString();
}
/**
*
*
@param
page Current page number
*
@param
logs Store the result of query
*
@return
The string which shows user the links
*/
public static
String printFoot(String page, ArrayList logs) {
StringBuffer sb =
new
StringBuffer();
int
pno = 1,start_page = 1;
String head =
"", tail = ""
;
if (page != null && !(""
).equals(page)) {
try
{
pno = Integer.parseInt(page);
}
catch
(Exception e) {
pno = 1;
}
}
double ps=pageSize
;
int pre = pno - 1, pageCount = (int
)Math.ceil(logs.size() / ps);
if
(pre > 0) {
sb.append(
"<a style='color: blue'href=query.do?action=list&&page=" + pre + "><<"+ "</a>"
);
}
else
{
sb.append(
"<a style='color: blue'href=#><<</a>"
);
}
if
(pno - 9 <= 0) {
start_page = 1;
head =
""
;
if
(pageCount <= 20) {
tail =
""
;
}
else
{
tail =
"......."
;
}
}
else if
(pno + 10 > pageCount) {
start_page = pageCount - 19;
if
(start_page > 0) {
head =
"......."
;
tail =
""
;
}
else
{
start_page = 1;
head =
""
;
tail =
""
;
}
}
else
{
start_page = pno - 9;
head =
"......."
;
tail =
"......."
;
}
sb.append(head);
for (int
i = 0; i < 20 && start_page <= pageCount; i++) {
if
(start_page==pno){
sb.append(
"<a style='color: red' href=query.do?action=list&&page="
+ start_page+
">"+ start_page+ "</a>  "
);
}
else
{
sb.append(
"<a style='color: blue' href=query.do?action=list&&page="
+ start_page+
">"+ start_page+ "</a>  "
);
}
start_page++;
}
sb.append(tail);
if
(pno + 1 <= pageCount) {
sb.append(
"<a style='color: blue' href=query.do?action=list&&page=" + (pno + 1) + ">>>"+ "</a>"
);
}
else
{
sb.append(
"<a style='color: blue' href=#>>></a>"
);
}
sb.append(
"   <select οnchange=/"MM_jumpMenu('parent',this,0)/" >"
);
for (int
i = 1; i <= pageCount; i++) {
if
(i == pno) {
sb.append(
"<option selected value='query.do?action=list&&page="
+ i
+
"'>" + i + "</option>"
);
}
else
{
sb.append(
"<option value='query.do?action=list&&page="
+ i
+
"'>" + i + "</option>"
);
}
}
sb.append(
"</select>"
);
return
sb.toString();
}
public static
String listProject(String asd){
ApplicationContext actx =
new ClassPathXmlApplicationContext("citi/sh/lp/configFiles/springBean-config.xml"
);
IndexDAO indexDao=(IndexDAO)actx.getBean(
"indexDAO"
);
ArrayList<File> projects=FileUtilTool.subDirs(
new File(ContextInfo.DATA_DIR
));
StringBuffer sb=
new
StringBuffer();
sb.append(
"<select name='project'>"
);
for (int
i = 0; i < projects.size(); i++) {
String project = ((File) projects.get(i)).getName();
if
(i == 0) {
sb.append(
"<option selected value='"+project+"'>" + project + "</option>"
);
}
else
{
sb.append(
"<option value='"+project+"'>" + project + "</option>"
);
}
}
sb.append(
"</select>"
);
return
sb.toString();
}}
IndexDAO.java
package
information.sh.lp.model;
import
information.sh.lp.infoBean.ContextInfo;
import
information.sh.lp.infoBean.Index;
import
information.sh.lp.util.FileUtilTool;
import
information.sh.lp.util.IOUtil;
import
information.sh.lp.util.ParseString;
import
java.io.File;
import
java.io.IOException;
import
java.util.ArrayList;
import
java.util.Iterator;
/**
*
*
*/
public
class IndexDAO {
/**
* Describe: Reads all log files and creates indexes, then write the indexes
* into a index file.
*/
public boolean buildIndex() {
File logFileDir =
new File(ContextInfo.DATA_DIR);
if (!logFileDir.exists()) {
System.
out.println("Log Files` Directory Was Not Found!");
return false;
}
ArrayList<File> dirs = FileUtilTool.subDirs(logFileDir);
if (dirs.isEmpty()) {
System.
out.println("There is no project directory!");
return false;
}
for (int j = 0; j < dirs.size(); j++) {
if (!FileUtilTool.subFiles(dirs.get(j)).isEmpty()) {
this.buildIndex(dirs.get(j));
}
}
return true;
}
public boolean buildIndex(File logDir) {
File[] files = logDir.listFiles();
ArrayList<Index> indexes =
new ArrayList<Index>();
ArrayList<Index> indexesTemp =
new ArrayList<Index>();
// Create index file in the relative log file directory
File indexFile =
new File(logDir.getAbsolutePath() + "/" + "indexFile");
if (!indexFile.exists()) {
indexFile.mkdir();
}
indexFile =
new File(indexFile.getAbsolutePath() + "/" + "index.txt");
if (!indexFile.exists()) {
try {
indexFile.createNewFile();
}
catch (IOException e) {
System.
out.println("Create index.txt file failed!");
}
}
if (files== null || files.length == 0) {
return false;
}
for (int i = 0; i < files.length; i++) {
if (i == 0) {
indexes = IOUtil.parseIndexs(files[i],
ContextInfo.
INDEX_INTERVAL);
}
else {
indexesTemp = IOUtil.parseIndexs(files[i],
ContextInfo.
INDEX_INTERVAL);
mergeIndexes(indexes, indexesTemp);
}
}
IOUtil.writeIndexes(indexes, indexFile);
return true;
}
/**
*
*
@param log_file
* Did not implement
*/
public void addIndex(File log_file) {
}
/**
*
*
@param indexesA
*
@param indexesB
* Describe: Put indexesB into indexesA, and ensure the merged
* indexesA is ordered
*/
public void mergeIndexes(ArrayList<Index> indexesA,
ArrayList<Index> indexesB) {
if (indexesB == null) {
return;
}
Iterator itB = indexesB.iterator();
if (indexesA.isEmpty()) {
indexesA.addAll(indexesB);
}
while (itB.hasNext()) {
Index indexB = (Index) itB.next();
int i = 0;
Index indexA = (Index) indexesA.get(i);
int aBeforeB = ParseString.compareTime(indexA.getStart_time(),
indexB.getStart_time());
while (i < (indexesA.size() - 1) && aBeforeB == 1) {
i++;
indexA = (Index) indexesA.get(i);
aBeforeB = ParseString.compareTime(indexA.getStart_time(),
indexB.getStart_time());
}
if (i == (indexesA.size() - 1) && aBeforeB == 1) {
indexesA.add(i + 1, indexB);
}
else if (aBeforeB == -1) {
indexesA.add(i, indexB);
}
else {
indexA.addPosition(indexB.getPosition(0));
}
}
}
}