KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > core > Subscription


1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2
// Distributable under LGPL license. See terms of license at gnu.org.
3

4 package nl.justobjects.pushlet.core;
5
6 import nl.justobjects.pushlet.util.Rand;
7
8
9 /**
10  * Represents single subject subscription
11  *
12  * @version $Id: Subscription.java,v 1.3 2005/02/16 15:23:10 justb Exp $
13  * @author Just van den Broecke - Just Objects &copy;
14  **/

15 public class Subscription {
16     public static final int ID_SIZE = 5;
17     public static final String JavaDoc SUBJECT_SEPARATOR = ",";
18     private String JavaDoc id = Rand.randomName(ID_SIZE);
19     private String JavaDoc subject;
20     private String JavaDoc[] subjects;
21
22     /** Optional label, a user supplied token. */
23     private String JavaDoc label;
24
25     public Subscription(String JavaDoc aSubject) {
26         this(aSubject, null);
27     }
28
29     public Subscription(String JavaDoc aSubject, String JavaDoc aLabel) {
30         if (aSubject == null || aSubject.length() == 0) {
31             throw new IllegalArgumentException JavaDoc("Null or emtpy subject");
32         }
33
34         subject = aSubject;
35
36         // We may subscribe to multiple subjects by separating
37
// them with SUBJECT_SEPARATOR, e.g. "/stocks/aex,/system/memory,..").
38
subjects = aSubject.split(SUBJECT_SEPARATOR);
39
40         label = aLabel;
41     }
42
43     public String JavaDoc getId() {
44         return id;
45     }
46
47     public String JavaDoc getLabel() {
48         return label;
49     }
50
51     public String JavaDoc getSubject() {
52         return subject;
53     }
54
55     /** Determine if Event matches subscription. */
56     public boolean match(Event event) {
57         String JavaDoc eventSubject = event.getSubject();
58
59         // Silly case but check anyway
60
if (eventSubject == null || eventSubject.length() == 0) {
61             return false;
62         }
63
64         // Test if one of the subjects matches
65
for (int i = 0; i < subjects.length; i++) {
66             if (eventSubject.startsWith(subjects[i])) {
67                 return true;
68             }
69         }
70
71         // No match
72
return false;
73     }
74 }
75
76 /*
77   * $Log: Subscription.java,v $
78   * Revision 1.3 2005/02/16 15:23:10 justb
79   * multiple subject (, separated) support
80   *
81   * Revision 1.2 2005/01/18 16:47:10 justb
82   * protocol changes for v2 and publishing from pushlet client
83   *
84   * Revision 1.1 2004/09/26 21:39:43 justb
85   * allow multiple subscriptions and out-of-band requests
86   *
87   *
88   */

89
Popular Tags