001: package org.obe.server.j2ee.repository;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.apache.slide.event.ContentEvent;
006: import org.apache.slide.event.ContentListener;
007: import org.apache.slide.event.EventDispatcher;
008: import org.apache.slide.event.VetoException;
009: import org.obe.client.api.repository.RepositoryException;
010: import org.obe.engine.repository.BasicResourceRepository;
011: import org.obe.spi.event.ApplicationEvent;
012: import org.obe.spi.service.ApplicationEventBroker;
013: import org.obe.spi.service.ServiceManager;
014:
015: import javax.servlet.*;
016: import javax.servlet.http.HttpServletRequest;
017: import java.io.IOException;
018: import java.util.EventListener;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: /**
023: * A resource repository that is implemented upon a Jakarta Slide WebDAV
024: * document store. At least, that's the longer term intention. For now, we'll
025: * just register a listener for WebDAV events and forward them to the
026: * application event broker so that we can trigger workflows from WebDAV
027: * actions.
028: * <br/>
029: * N.B. When we finally do implement all operations by delegating to the WebDAV
030: * repository, we must make sure that the calls enlist the repository in the
031: * transaction.
032: *
033: * @author Adrian Price
034: */
035: public class WebDAVResourceRepository extends BasicResourceRepository {
036: private static final Log _logger = LogFactory
037: .getLog(EventForwarder.class);
038: private static final ThreadLocal _requestURL = new ThreadLocal();
039: private final EventListener _listener = new EventForwarder();
040: private final ApplicationEventBroker _eventBroker;
041:
042: public static class WebDAVServletContextListener implements
043: ServletContextListener {
044: public WebDAVServletContextListener() {
045: }
046:
047: public void contextInitialized(ServletContextEvent event) {
048: _logger.debug(event);
049: }
050:
051: public void contextDestroyed(ServletContextEvent event) {
052: _logger.debug(event);
053: }
054: }
055:
056: public static class EventFilter implements Filter {
057: public void init(FilterConfig filterConfig)
058: throws ServletException {
059: }
060:
061: public void doFilter(ServletRequest request,
062: ServletResponse response, FilterChain chain)
063: throws IOException, ServletException {
064:
065: HttpServletRequest httpRequest = (HttpServletRequest) request;
066: StringBuffer requestURL = httpRequest.getRequestURL();
067: _requestURL.set(requestURL == null ? null : requestURL
068: .toString());
069:
070: chain.doFilter(request, response);
071: }
072:
073: public void destroy() {
074: }
075: }
076:
077: public class EventForwarder implements ContentListener {
078: public void create(ContentEvent event) throws VetoException {
079: _logger.info("WebDAV.create: " + event);
080: fireApplicationEvent(event, "create");
081: }
082:
083: public void fork(ContentEvent event) throws VetoException {
084: _logger.info("WebDAV.fork: " + event);
085: fireApplicationEvent(event, "fork");
086: }
087:
088: public void merge(ContentEvent event) throws VetoException {
089: _logger.info("WebDAV.merge: " + event);
090: fireApplicationEvent(event, "merge");
091: }
092:
093: public void remove(ContentEvent event) throws VetoException {
094: _logger.info("WebDAV.remove: " + event);
095: fireApplicationEvent(event, "remove");
096: }
097:
098: public void retrieve(ContentEvent event) throws VetoException {
099: _logger.info("WebDAV.retrieve: " + event);
100: fireApplicationEvent(event, "retrieve");
101: }
102:
103: public void store(ContentEvent event) throws VetoException {
104: _logger.info("WebDAV.store: " + event);
105: fireApplicationEvent(event, "store");
106: }
107:
108: }
109:
110: private static String requestURL() {
111: return (String) _requestURL.get();
112: }
113:
114: public WebDAVResourceRepository(ServiceManager svcMgr) {
115: super (svcMgr);
116: _eventBroker = getServiceManager().getApplicationEventBroker();
117: }
118:
119: public synchronized void init() throws IOException,
120: RepositoryException {
121: super .init();
122: EventDispatcher.getInstance().addEventListener(_listener);
123: }
124:
125: public synchronized void exit() {
126: super .exit();
127: }
128:
129: public String getServiceName() {
130: return "ResourceRepository";
131: }
132:
133: private void fireApplicationEvent(ContentEvent event, String action) {
134: try {
135: Map props = new HashMap();
136: props.put(ApplicationEvent.ACTION, action);
137: props.put("requestURL", requestURL());
138: _eventBroker.publish(event, props);
139: } catch (Exception e) {
140: _logger.error(e);
141: }
142: }
143: }
|