From: Adam R. <ad...@ex...> - 2010-07-28 17:43:10
|
This functionality should be integrated with Triggers. It also make no sense to configure this separately via the command line! This should be done in conf.xml! Thanks Adam. On 28 July 2010 18:06, <ix...@us...> wrote: > Revision: 12086 > http://exist.svn.sourceforge.net/exist/?rev=12086&view=rev > Author: ixitar > Date: 2010-07-28 17:06:45 +0000 (Wed, 28 Jul 2010) > > Log Message: > ----------- > [feature] Adding the ability to execute an XQuery script when an HTTP session is created and another XQuery script when an HTTP session is destroyed. > > Two listeners are being added to the server: > - org.exist.http.AuditTrailSessionListener > - org.exist.http.SessionCountListener > > NOTE: This feature is disabled by default, so it will have no impact on a system until enabled. > > These listeners can be enabled by uncommenting the following in tools/jetty/etc/webdefaults.xml: > > <listener> > <listener-class>org.exist.http.AuditTrailSessionListener</listener-class> > </listener> > <listener> > <listener-class>org.exist.http.SessionCountListener</listener-class> > </listener> > > To change the session timeout from the default of 30 minutes, change the following value in webdefaults.xml: > > <session-config> > <session-timeout>30</session-timeout> > </session-config> > > > When the AuditTrailSessionListener is enabled, then there are two Java properties that need to be set: > > - org.exist.http.session_create_listener > - org.exist.http.session_destroy_listener > > If a property is not set, then the listener will do nothing for the create or destroy. > > If the property is set, but the XQuery script does not exist, then a Log4J error message "Resource [resource path] does not exist." is processed and then no other action is taken. > > If the resource exists and there is an execution failure, then a Log4J error message "Exception while executing [resource path] script for admin" is processed and then no other action is taken. > > If the resource exists and the execution succeeds, then a Log4J info message "XQuery execution results: results" is processed and then the method is completed. > > To set these properties, you need to edit bin/functions.d/eXist-settings.sh if starting from the command line in Unix/Linux: > > set_java_options() { > if [ -z "${JAVA_OPTIONS}" ]; then > JAVA_OPTIONS="-Xms128m -Xmx512m -Dfile.encoding=UTF-8 -Dorg.exist.http.session_destroy_listener=/db/session/session-destroy.xq"; > fi > JAVA_OPTIONS="${JAVA_OPTIONS} -Djava.endorsed.dirs=${JAVA_ENDORSED_DIRS}"; > } > > To set these properties for The command line execution in Windows, you need to edit bin/startup.bat: > > :gotExistHome > set JAVA_ENDORSED_DIRS="%EXIST_HOME%"\lib\endorsed > set JAVA_OPTS="-Xms128m -Xmx512m -Dfile.encoding=UTF-8 -Dorg.exist.http.session_destroy_listener=/db/session/session-destroy.xq -Djava.endorsed.dirs=%JAVA_ENDORSED_DIRS%" > > To set these properties in wrapper, you need to edit tools/wrapper/conf/wrapper.conf: > > # Java Additional Parameters > wrapper.java.additional.1=-Djava.endorsed.dirs=../../../lib/endorsed > wrapper.java.additional.2=-Dfile.encoding=UTF-8 > wrapper.java.additional.3=-Dexist.home=../../.. > wrapper.java.additional.4=-Djetty.home=../../../tools/jetty > wrapper.java.additional.5=-Dorg.exist.http.session_create_listener=/db/session/session-create.xq > wrapper.java.additional.6=-Dorg.exist.http.session_destroy_listener=/db/session/session-destroy.xq > > > - - - - - - - - - - - - - - - > The SessionCountListener will return the number of open sessions are on the server, the way to access the value is through SessionCountListener.getActiveSessions() which returns a long. An XQuery function will be implemented to return this value. > > Modified Paths: > -------------- > trunk/eXist/tools/jetty/etc/webdefault.xml > > Added Paths: > ----------- > trunk/eXist/src/org/exist/http/AuditTrailSessionListener.java > trunk/eXist/src/org/exist/http/SessionCountListener.java > > Added: trunk/eXist/src/org/exist/http/AuditTrailSessionListener.java > =================================================================== > --- trunk/eXist/src/org/exist/http/AuditTrailSessionListener.java (rev 0) > +++ trunk/eXist/src/org/exist/http/AuditTrailSessionListener.java 2010-07-28 17:06:45 UTC (rev 12086) > @@ -0,0 +1,151 @@ > +/* > + * eXist Open Source Native XML Database > + * Copyright (C) 2010 The eXist Project > + * http://exist-db.org > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. > + * > + * $Id$ > + */ > +package org.exist.http; > + > +import org.apache.log4j.Logger; > +import org.exist.dom.BinaryDocument; > +import org.exist.dom.DocumentImpl; > +import org.exist.security.User; > +import org.exist.security.xacml.AccessContext; > +import org.exist.source.DBSource; > +import org.exist.source.Source; > +import org.exist.storage.BrokerPool; > +import org.exist.storage.DBBroker; > +import org.exist.storage.lock.Lock; > +import org.exist.xmldb.XmldbURI; > +import org.exist.xquery.CompiledXQuery; > +import org.exist.xquery.XQuery; > +import org.exist.xquery.XQueryContext; > +import org.exist.xquery.value.Sequence; > + > +import javax.servlet.http.HttpSessionListener; > +import javax.servlet.http.HttpSessionEvent; > +import javax.servlet.http.HttpSession; > +import java.util.Properties; > + > +/** > + * Executes an XQuery script whose filename is retrieved from the > + * java option 'org.exist.http.session_create_listener' when an > + * HTTP session is created and 'org.exist.http.session_destroy_listener' > + * when an HTTP session is destroyed. > + * > + * If the java option is not set, then do nothing. > + * > + * If the java option is set, then retrieve the script from the file > + * or resource designated by the value of the property. Execute the > + * XQuery script to record the creation or destruction of a HTTP session. > + */ > +public class AuditTrailSessionListener implements HttpSessionListener { > + > + private final static Logger LOG = Logger.getLogger(AuditTrailSessionListener.class); > + private static final String REGISTER_CREATE_XQUERY_SCRIPT_PROPERTY = "org.exist.http.session_create_listener"; > + private static final String REGISTER_DESTROY_XQUERY_SCRIPT_PROPERTY = "org.exist.http.session_destroy_listener"; > + > + > + > + /** > + * > + * @param sessionEvent > + */ > + public void sessionCreated(HttpSessionEvent sessionEvent) { > + HttpSession session = sessionEvent.getSession(); > + > + LOG.info("session created " + session.getId()); > + String xqueryResourcePath = System.getProperty(REGISTER_CREATE_XQUERY_SCRIPT_PROPERTY); > + executeXQuery(xqueryResourcePath); > + } > + > + /** > + * > + * @param sessionEvent > + */ > + public void sessionDestroyed(HttpSessionEvent sessionEvent) { > + HttpSession session = (sessionEvent != null) ? sessionEvent.getSession() : null; > + if (session != null) > + LOG.info("destroy session " + session.getId()); > + else > + LOG.info("destroy session"); > + > + String xqueryResourcePath = System.getProperty(REGISTER_DESTROY_XQUERY_SCRIPT_PROPERTY); > + executeXQuery(xqueryResourcePath); > + } > + > + private void executeXQuery(String xqueryResourcePath) { > + if (xqueryResourcePath != null && xqueryResourcePath.length() > 0) { > + xqueryResourcePath = xqueryResourcePath.trim(); > + BrokerPool pool = null; > + DBBroker broker = null; > + User principal = null; > + > + try { > + DocumentImpl resource = null; > + Source source = null; > + > + pool = BrokerPool.getInstance(); > + principal = pool.getSecurityManager().getSystemAccount(); > + > + broker = pool.get(principal); > + if (broker == null) { > + LOG.error("Unable to retrieve DBBroker for " + principal.getName()); > + return; > + } > + > + XmldbURI pathUri = XmldbURI.create(xqueryResourcePath); > + > + > + resource = broker.getXMLResource(pathUri, Lock.READ_LOCK); > + > + if(resource != null) { > + LOG.info("Resource [" + xqueryResourcePath + "] exists."); > + source = new DBSource(broker, (BinaryDocument)resource, true); > + } else { > + LOG.error("Resource [" + xqueryResourcePath + "] does not exist."); > + return; > + } > + > + > + XQuery xquery = broker.getXQueryService(); > + > + if (xquery == null) { > + LOG.error("broker unable to retrieve XQueryService"); > + return; > + } > + > + XQueryContext context = xquery.newContext(AccessContext.REST); > + > + CompiledXQuery compiled = xquery.compile(context, source); > + > + Properties outputProperties = new Properties(); > + > + Sequence result = xquery.execute(compiled, null, outputProperties); > + LOG.info("XQuery execution results: " + result.toString()); > + > + } catch (Exception e) { > + LOG.error("Exception while executing [" + xqueryResourcePath + "] script for " + principal.getName(), e); > + } > + finally { > + if (pool != null) > + pool.release(broker); > + } > + } > + } > +} > \ No newline at end of file > > > Property changes on: trunk/eXist/src/org/exist/http/AuditTrailSessionListener.java > ___________________________________________________________________ > Added: svn:keywords > + Id > > Added: trunk/eXist/src/org/exist/http/SessionCountListener.java > =================================================================== > --- trunk/eXist/src/org/exist/http/SessionCountListener.java (rev 0) > +++ trunk/eXist/src/org/exist/http/SessionCountListener.java 2010-07-28 17:06:45 UTC (rev 12086) > @@ -0,0 +1,50 @@ > +/* > + * eXist Open Source Native XML Database > + * Copyright (C) 2010 The eXist Project > + * http://exist-db.org > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. > + * > + * $Id$ > + */ > +package org.exist.http; > + > +import javax.servlet.http.HttpSessionEvent; > +import javax.servlet.http.HttpSessionListener; > + > +/** > + * Created by IntelliJ IDEA. > + * User: lcahlander > + * Date: Jul 27, 2010 > + * Time: 10:31:47 PM > + * To change this template use File | Settings | File Templates. > + */ > +public class SessionCountListener implements HttpSessionListener { > + > + private static long activeSessions = 0; > + > + public void sessionCreated(HttpSessionEvent httpSessionEvent) { > + activeSessions++; > + } > + > + public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { > + if (activeSessions > 0) > + activeSessions--; > + } > + > + public static long getActiveSessions() { > + return activeSessions; > + } > +} > > > Property changes on: trunk/eXist/src/org/exist/http/SessionCountListener.java > ___________________________________________________________________ > Added: svn:keywords > + Id > > Modified: trunk/eXist/tools/jetty/etc/webdefault.xml > =================================================================== > --- trunk/eXist/tools/jetty/etc/webdefault.xml 2010-07-28 16:49:11 UTC (rev 12085) > +++ trunk/eXist/tools/jetty/etc/webdefault.xml 2010-07-28 17:06:45 UTC (rev 12086) > @@ -29,6 +29,22 @@ > This file is applied to a Web application before it's own WEB_INF/web.xml file > </description> > > + <!-- ======================================================================== --> > + <!-- The AuditTrailSessionListener allows for the execution of XQuery scripts --> > + <!-- when an HTTP session is created or destroyed. Set the following Java --> > + <!-- properties to specify the scripts to run. --> > + <!-- --> > + <!-- org.exist.http.session_create_listener --> > + <!-- org.exist.http.session_destroy_listener --> > + <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> > + <!-- UNCOMMENT TO ACTIVATE > + <listener> > + <listener-class>org.exist.http.AuditTrailSessionListener</listener-class> > + </listener> > + <listener> > + <listener-class>org.exist.http.SessionCountListener</listener-class> > + </listener> > + --> > > <!-- ==================================================================== --> > <!-- Context params to control Session Cookies --> > > > This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. > > ------------------------------------------------------------------------------ > The Palm PDK Hot Apps Program offers developers who use the > Plug-In Development Kit to bring their C/C++ apps to Palm for a share > of $1 Million in cash or HP Products. Visit us here for more details: > http://p.sf.net/sfu/dev2dev-palm > _______________________________________________ > Exist-commits mailing list > Exi...@li... > https://lists.sourceforge.net/lists/listinfo/exist-commits > -- Adam Retter eXist Developer { United Kingdom } ad...@ex... irc://irc.freenode.net/existdb |