Skip to content

Commit a5432e5

Browse files
committed
added servlet lister to process configurations in we application scope
1 parent 692b47d commit a5432e5

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@
8383
<version>1.2</version>
8484
<scope>provided</scope>
8585
</dependency>
86+
<dependency>
87+
<groupId>javax.servlet</groupId>
88+
<artifactId>servlet-api</artifactId>
89+
<version>2.5</version>
90+
<scope>provided</scope>
91+
</dependency>
8692
</dependencies>
8793

8894

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.audit4j.core.web;
2+
3+
import javax.servlet.ServletContext;
4+
import javax.servlet.ServletContextEvent;
5+
import javax.servlet.ServletContextListener;
6+
7+
import org.audit4j.core.AuditManager;
8+
9+
/**
10+
* The listener interface for receiving auditContext events. The class that is
11+
* interested in processing a auditContext event implements this interface, and
12+
* the object created with that class is registered with a component using the
13+
* component's <code>addAuditContextListener<code> method. When
14+
* the auditContext event occurs, that object's appropriate
15+
* method is invoked.
16+
*
17+
* @see AuditContextEvent
18+
*/
19+
public class AuditContextListener implements ServletContextListener {
20+
21+
/** The config support. */
22+
private ServletContexConfigSupport configSupport = null;
23+
24+
/*
25+
* (non-Javadoc)
26+
*
27+
* @see
28+
* javax.servlet.ServletContextListener#contextInitialized(javax.servlet
29+
* .ServletContextEvent)
30+
*/
31+
@Override
32+
public void contextInitialized(ServletContextEvent contextEvent) {
33+
configSupport = new ServletContexConfigSupport();
34+
if (configSupport.hasHandlers(contextEvent.getServletContext())) {
35+
AuditManager.startWithConfiguration(configSupport.loadConfig(contextEvent.getServletContext()));
36+
} else {
37+
AuditManager.startWithConfiguration(getConfFilePath(contextEvent.getServletContext()));
38+
}
39+
}
40+
41+
/*
42+
* (non-Javadoc)
43+
*
44+
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
45+
* ServletContextEvent)
46+
*/
47+
@Override
48+
public void contextDestroyed(ServletContextEvent contextEvent) {
49+
AuditManager.getInstance().shutdown();
50+
}
51+
52+
/**
53+
* Gets the conf file path.
54+
*
55+
* @param context the context
56+
* @return the conf file path
57+
*/
58+
private String getConfFilePath(ServletContext context) {
59+
return context.getRealPath("/WEB-INF/classes");
60+
}
61+
62+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.audit4j.core.web;
2+
3+
/**
4+
* The Class ContextConfigParams.
5+
*
6+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
7+
*/
8+
class ContextConfigParams {
9+
10+
/** The Constant PARAM_HANDLERS. */
11+
static final String PARAM_HANDLERS = "handlers";
12+
13+
/** The Constant PARAM_LAYOUT. */
14+
static final String PARAM_LAYOUT = "layout";
15+
16+
/** The Constant PARAM_FILTERS. */
17+
static final String PARAM_FILTERS = "filters";
18+
19+
/** The Constant PARAM_OPTIONS. */
20+
static final String PARAM_OPTIONS = "options";
21+
22+
/** The Constant PARAM_META_DATA. */
23+
static final String PARAM_META_DATA = "metaData";
24+
25+
/** The Constant PARAM_PROPERTIES. */
26+
static final String PARAM_PROPERTIES = "properties";
27+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.audit4j.core.web;
2+
3+
import javax.servlet.ServletContext;
4+
5+
import org.audit4j.core.Configuration;
6+
import org.audit4j.core.MetaData;
7+
import org.audit4j.core.filter.AuditEventFilter;
8+
import org.audit4j.core.handler.Handler;
9+
import org.audit4j.core.layout.Layout;
10+
import org.audit4j.core.util.ReflectUtil;
11+
12+
/**
13+
* The Class ServletContexConfigSupport.
14+
*
15+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
16+
*/
17+
class ServletContexConfigSupport {
18+
19+
/**
20+
* Load config.
21+
*
22+
* @param servletContext
23+
* the servlet context
24+
* @return the configuration
25+
*/
26+
Configuration loadConfig(ServletContext servletContext) {
27+
String handlers = servletContext.getInitParameter(ContextConfigParams.PARAM_HANDLERS);
28+
String layout = servletContext.getInitParameter(ContextConfigParams.PARAM_LAYOUT);
29+
String filters = servletContext.getInitParameter(ContextConfigParams.PARAM_FILTERS);
30+
String options = servletContext.getInitParameter(ContextConfigParams.PARAM_OPTIONS);
31+
String metaData = servletContext.getInitParameter(ContextConfigParams.PARAM_META_DATA);
32+
String properties = servletContext.getInitParameter(ContextConfigParams.PARAM_PROPERTIES);
33+
34+
Configuration config = Configuration.INSTANCE;
35+
config.setHandlers(new ReflectUtil<Handler>().getNewInstanceList(handlers.split(";")));
36+
config.setLayout(new ReflectUtil<Layout>().getNewInstance(layout));
37+
config.setFilters(new ReflectUtil<AuditEventFilter>().getNewInstanceList(filters.split(";")));
38+
config.setOptions(options);
39+
config.setMetaData(new ReflectUtil<MetaData>().getNewInstance(metaData));
40+
String[] propertiesList = properties.split(";");
41+
for (String property : propertiesList) {
42+
String[] keyValue = property.split(":");
43+
config.addProperty(keyValue[0], keyValue[1]);
44+
}
45+
return config;
46+
}
47+
48+
/**
49+
* Checks for handlers.
50+
*
51+
* @param servletContext
52+
* the servlet context
53+
* @return true, if successful
54+
*/
55+
boolean hasHandlers(ServletContext servletContext) {
56+
String handlers = servletContext.getInitParameter("handlers");
57+
if (handlers == null || handlers.equals("")) {
58+
return false;
59+
} else {
60+
return true;
61+
}
62+
}
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.audit4j.core.web;
2+
3+
import static org.mockito.Mockito.mock;
4+
import static org.mockito.Mockito.when;
5+
6+
import javax.servlet.ServletContext;
7+
8+
import org.audit4j.core.Configuration;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
public class ServletContextConfigSupportTest {
13+
14+
@Test
15+
public void testLoadConfig() {
16+
ServletContext context = mock(ServletContext.class);
17+
when(context.getInitParameter(ContextConfigParams.PARAM_HANDLERS)).thenReturn("org.audit4j.core.handler.ConsoleAuditHandler;org.audit4j.core.handler.file.FileAuditHandler;");
18+
//when(context.getInitParameter(ContextConfigParams.PARAM_FILTERS)).thenReturn("");
19+
when(context.getInitParameter(ContextConfigParams.PARAM_LAYOUT)).thenReturn("org.audit4j.core.layout.SimpleLayout");
20+
//when(context.getInitParameter(ContextConfigParams.PARAM_OPTIONS)).thenReturn("");
21+
when(context.getInitParameter(ContextConfigParams.PARAM_META_DATA)).thenReturn("org.audit4j.core.DummyMetaData");
22+
when(context.getInitParameter(ContextConfigParams.PARAM_PROPERTIES)).thenReturn(" log.file.location:user.dir");
23+
24+
ServletContexConfigSupport support = new ServletContexConfigSupport();
25+
Assert.assertTrue(support.hasHandlers(context));
26+
27+
Configuration config = support.loadConfig(context);
28+
Assert.assertNotNull(config.getHandlers());
29+
}
30+
}

0 commit comments

Comments
 (0)