001: package de.schlund.pfixxml.testrecording;
002:
003: import java.io.IOException;
004: import java.lang.management.ManagementFactory;
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Properties;
009:
010: import javax.management.MBeanServer;
011: import javax.management.ObjectName;
012: import javax.servlet.http.HttpSession;
013:
014: import org.apache.log4j.Logger;
015:
016: import de.schlund.pfixxml.resources.FileResource;
017: import de.schlund.pfixxml.resources.ResourceUtil;
018: import de.schlund.pfixxml.serverutil.SessionAdmin;
019: import de.schlund.pfixxml.serverutil.SessionData;
020: import de.schlund.pfixxml.serverutil.SessionInfoStruct;
021: import de.schlund.pfixxml.util.Xml;
022:
023: public class TestRecording implements TestRecordingMBean {
024:
025: private final static Logger LOG = Logger
026: .getLogger(TestRecording.class);
027:
028: private static TestRecording instance = new TestRecording();
029:
030: private final List<String> knownClients = new ArrayList<String>();
031:
032: public static TestRecording getInstance() {
033: return instance;
034: }
035:
036: public void init(Properties props) {
037: LOG.info("TestRecording init");
038: try {
039: MBeanServer mbeanServer = ManagementFactory
040: .getPlatformMBeanServer();
041: ObjectName objectName = new ObjectName(
042: "Pustefix:type=TestRecording");
043: mbeanServer.registerMBean(this , objectName);
044: // otherwise, clients cannot instaniate TrailLogger objects:
045: ObjectName createName = new ObjectName(
046: "Pustefix:type=TestRecording,name=loader");
047: mbeanServer.registerMBean(this .getClass().getClassLoader(),
048: createName);
049: } catch (Exception x) {
050: throw new RuntimeException(
051: "Can't register TestRecording MBean.", x);
052: }
053: }
054:
055: public TestRecording() {
056:
057: }
058:
059: // TODO: who's responsible for unregister?
060: public ObjectName startRecording(String sessionId)
061: throws IOException {
062: ObjectName name;
063: TrailLogger logger;
064:
065: logger = new TrailLogger(TrailLogger
066: .getVisit(getSession(sessionId)));
067:
068: try {
069: name = new ObjectName(
070: "Pustefix:type=TestRecording,name=TrailLogger,session="
071: + sessionId);
072: MBeanServer mbeanServer = ManagementFactory
073: .getPlatformMBeanServer();
074: mbeanServer.registerMBean(logger, name);
075: return name;
076: } catch (Exception x) {
077: LOG.error("Can't register TrailLogger MBean!", x);
078: throw new RuntimeException(
079: "Can't register TrailLogger MBean!", x);
080: }
081:
082: }
083:
084: public ApplicationList getApplicationList(boolean tomcat,
085: String sessionSuffix) {
086: FileResource file;
087:
088: file = ResourceUtil
089: .getFileResourceFromDocroot("servletconf/projects.xml");
090: try {
091: return ApplicationList.load(Xml.parseMutable(file), tomcat,
092: sessionSuffix);
093: } catch (Exception e) {
094: throw new RuntimeException(e);
095: }
096: }
097:
098: public List<SessionData> getSessions(String serverName,
099: String remoteAddr) {
100: SessionAdmin admin;
101: Iterator<String> iter;
102: String id;
103: List<SessionData> lst;
104: SessionInfoStruct info;
105:
106: lst = new ArrayList<SessionData>();
107: admin = SessionAdmin.getInstance();
108: iter = admin.getAllSessionIds().iterator();
109: while (iter.hasNext()) {
110: id = (String) iter.next();
111: info = admin.getInfo(id);
112: if (serverName.equals(info.getData().getServerName())
113: && remoteAddr
114: .equals(info.getData().getRemoteAddr())) {
115: lst.add(info.getData());
116: }
117: }
118: return lst;
119: }
120:
121: public void invalidateSession(String id) throws IOException {
122: getSession(id).invalidate();
123: }
124:
125: public boolean isKnownClient(String remoteAddr) {
126: return knownClients.contains(remoteAddr);
127: }
128:
129: public void addKnownClient(String remoteAddr) {
130: knownClients.add(remoteAddr);
131: }
132:
133: public void removeKnownClient(String remoteAddr) {
134: knownClients.remove(remoteAddr);
135: }
136:
137: public static HttpSession getSession(String id) throws IOException {
138: SessionInfoStruct info;
139:
140: info = SessionAdmin.getInstance().getInfo(id);
141: if (info == null) {
142: throw new IOException("session not found: " + id);
143: }
144: return info.getSession();
145: }
146:
147: }
|