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 import nl.justobjects.pushlet.util.PushletException;
8 
9 
10/**
11 * Represents single subject subscription
12 *
13 * @author Just van den Broecke - Just Objects &copy;
14 * @version $Id: Subscription.java,v 1.5 2007/11/23 14:33:07 justb Exp $
15 */
16public class Subscription implements ConfigDefs {
17    public static final int ID_SIZE = 5;
18    public static final String SUBJECT_SEPARATOR = ",";
19    private String id = Rand.randomName(ID_SIZE);
20    private String subject;
21    private String[] subjects;
22
23    /**
24     * Optional label, a user supplied token.
25     */
26    private String label;
27
28
29    /**
30     * Protected constructor as we create through factory method.
31     */
32    protected Subscription() {
33    }
34
35    /**
36     * Create instance through factory method.
37     *
38     * @param aSubject the subject (topic).
39     * @return a Subscription object (or derived)
40     * @throws nl.justobjects.pushlet.util.PushletException
41     *          exception, usually misconfiguration
42     */
43    public static Subscription create(String aSubject) throws PushletException {
44        return create(aSubject, null);
45    }
46
47    /**
48     * Create instance through factory method.
49     *
50     * @param aSubject the subject (topic).
51     * @param aLabel   the subject label (optional).
52     * @return a Subscription object (or derived)
53     * @throws nl.justobjects.pushlet.util.PushletException
54     *          exception, usually misconfiguration
55     */
56    public static Subscription create(String aSubject, String aLabel) throws PushletException {
57        if (aSubject == null || aSubject.length() == 0) {
58            throw new IllegalArgumentException("Null or emtpy subject");
59        }
60
61        Subscription subscription;
62        try {
63            subscription = (Subscription) Config.getClass(SUBSCRIPTION_CLASS, "nl.justobjects.pushlet.core.Subscription").newInstance();
64        } catch (Throwable t) {
65            throw new PushletException("Cannot instantiate Subscriber from config", t);
66        }
67
68        // Init
69        subscription.subject = aSubject;
70
71        // We may subscribe to multiple subjects by separating
72        // them with SUBJECT_SEPARATOR, e.g. "/stocks/aex,/system/memory,..").
73        subscription.subjects = aSubject.split(SUBJECT_SEPARATOR);
74
75        subscription.label = aLabel;
76        return subscription;
77    }
78
79
80    public String getId() {
81        return id;
82    }
83
84    public String getLabel() {
85        return label;
86    }
87
88    public String getSubject() {
89        return subject;
90    }
91
92    /**
93     * Determine if Event matches subscription.
94     */
95    public boolean match(Event event) {
96        String eventSubject = event.getSubject();
97
98        // Silly case but check anyway
99        if (eventSubject == null || eventSubject.length() == 0) {
00            return false;
01        }
02
03        // Test if one of the subjects matches
04        for (int i = 0; i < subjects.length; i++) {
05            if (eventSubject.startsWith(subjects[i])) {
06                return true;
07            }
08        }
09
10        // No match
11        return false;
12    }
13}
14
15/*
16  * $Log: Subscription.java,v $
17  * Revision 1.5  2007/11/23 14:33:07  justb
18  * core classes now configurable through factory
19  *
20  * Revision 1.4  2006/05/06 00:10:11  justb
21  * various chgs but not too serious...
22  *
23  * Revision 1.3  2005/02/16 15:23:10  justb
24  * multiple subject (, separated) support
25  *
26  * Revision 1.2  2005/01/18 16:47:10  justb
27  * protocol changes for v2 and publishing from pushlet client
28  *
29  * Revision 1.1  2004/09/26 21:39:43  justb
30  * allow multiple subscriptions and out-of-band requests
31  *
32  *
33  */
34