01: package org.drools.audit;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.io.ObjectOutputStream;
20: import java.io.StringWriter;
21: import java.util.ArrayList;
22: import java.util.List;
23:
24: import org.drools.WorkingMemory;
25: import org.drools.audit.event.LogEvent;
26:
27: import com.thoughtworks.xstream.XStream;
28:
29: /**
30: * A logger of events generated by a working memory.
31: * It stores its information in memory, so it can be retrieved later.
32: *
33: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen </a>
34: */
35: public class WorkingMemoryInMemoryLogger extends WorkingMemoryLogger {
36:
37: private final List events = new ArrayList();
38:
39: /**
40: * Creates a new WorkingMemoryInMemoryLogger for the given working memory.
41: * @param workingMemory
42: */
43: public WorkingMemoryInMemoryLogger(final WorkingMemory workingMemory) {
44: super (workingMemory);
45: }
46:
47: public String getEvents() {
48: final XStream xstream = new XStream();
49: StringWriter writer = new StringWriter();
50: try {
51: final ObjectOutputStream out = xstream
52: .createObjectOutputStream(writer);
53: out.writeObject(this .events);
54: out.close();
55: } catch (Throwable t) {
56: throw new RuntimeException(
57: "Unable to create event output: " + t.getMessage());
58: }
59: return writer.toString();
60: }
61:
62: /**
63: * Clears all the events in the log.
64: */
65: public void clear() {
66: this .events.clear();
67: }
68:
69: /**
70: * @see org.drools.audit.WorkingMemoryLogger
71: */
72: public void logEventCreated(final LogEvent logEvent) {
73: this.events.add(logEvent);
74: }
75: }
|