KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > event > Subscriber


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/event/Subscriber.java,v 1.11 2004/08/05 14:43:30 dflorey Exp $
3  * $Revision: 1.11 $
4  * $Date: 2004/08/05 14:43:30 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.event;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.TimerTask JavaDoc;
30
31 import org.apache.slide.event.ResourceEvent;
32 import org.apache.slide.webdav.method.AbstractWebdavMethod;
33
34 /**
35  * @version $Revision: 1.11 $
36  */

37 public class Subscriber {
38     protected static final String JavaDoc LOG_CHANNEL = Subscriber.class.getName();
39     final static String JavaDoc UPDATE = "Update";
40     final static String JavaDoc DELETE = "Delete";
41     final static String JavaDoc MOVE = "Move";
42     final static String JavaDoc NEW_MEMBER = "Update/newmember";
43     final static String JavaDoc NEW_MAIL = "pragma/<http://schemas.microsoft.com/exchange/newmail>";
44     
45     private String JavaDoc callback;
46     private String JavaDoc notificationType, uri;
47     private int depth, notificationDelay, subscriptionLifetime, id;
48     private long subscriptionEnd;
49     private List JavaDoc events = new ArrayList JavaDoc();
50     private TimerTask JavaDoc lifetime, notify;
51
52     public Subscriber(String JavaDoc uri, String JavaDoc callback, String JavaDoc notificationType, int notificationDelay, int subscriptionLifetime, int depth) {
53         this.callback = callback;
54         this.notificationType = notificationType;
55         this.notificationDelay = notificationDelay;
56         this.subscriptionLifetime = subscriptionLifetime;
57         this.subscriptionEnd = System.currentTimeMillis() + subscriptionLifetime*1000;
58         this.uri = uri;
59         this.depth = depth;
60     }
61
62     public void addEvent(ResourceEvent event) {
63         events.add(event);
64     }
65
66     public List JavaDoc getEvents() {
67         return events;
68     }
69
70     public void clearEvents() {
71         events = new ArrayList JavaDoc();
72     }
73
74     public TimerTask JavaDoc getLifetime() {
75         return lifetime;
76     }
77
78     public long getSubscriptionEnd() {
79         return subscriptionEnd;
80     }
81     
82     public void setLifetime(TimerTask JavaDoc lifetime) {
83         this.lifetime = lifetime;
84     }
85
86     public TimerTask JavaDoc getNotify() {
87         return notify;
88     }
89
90     public void setNotify(TimerTask JavaDoc notify) {
91         this.notify = notify;
92     }
93
94     public int getDepth() {
95         return depth;
96     }
97
98     public String JavaDoc getUri() {
99         return uri;
100     }
101     
102     public boolean matches(String JavaDoc type, ResourceEvent event) {
103         // check if event matches notification-type
104
// see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wss/wss/_webdav_notification_type_header.asp
105
// for details
106
if ( type.equalsIgnoreCase(notificationType) ||
107             ( type.equalsIgnoreCase(NEW_MEMBER) && notificationType.equalsIgnoreCase(UPDATE) && depth > 0 ) ||
108             ( type.equalsIgnoreCase(DELETE) && notificationType.equalsIgnoreCase(UPDATE) && depth > 0 ) ){
109             String JavaDoc eventUri = event.getUri();
110             if ( eventUri != null && uri != null ) {
111                 if ( depth == 0 && eventUri.equals(uri.toString()) ) return true;
112                 if ( depth == AbstractWebdavMethod.INFINITY && eventUri.startsWith(uri.toString()) ) return true;
113                 if ( eventUri.startsWith(uri.toString() )) {
114                     String JavaDoc subpath = eventUri.substring(uri.toString().length());
115                     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(subpath, "/");
116                     if ( tokenizer.countTokens() <= depth ) return true;
117                 }
118             }
119         }
120         return false;
121     }
122
123     public boolean hasCallback() {
124        return this.callback != null && this.callback.length() > 0;
125     }
126     
127     public String JavaDoc getCallback() {
128         return callback;
129     }
130
131     public String JavaDoc getNotificationType() {
132         return notificationType;
133     }
134
135     public int getNotificationDelay() {
136         return notificationDelay;
137     }
138
139     public void setSubscriptionLifetime(int subscriptionLifetime) {
140         this.subscriptionLifetime = subscriptionLifetime;
141     }
142
143     public int getSubscriptionLifetime() {
144         return subscriptionLifetime;
145     }
146
147     public int getId() {
148         return id;
149     }
150
151     public void setId(int id) {
152         this.id = id;
153     }
154 }
Popular Tags