KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > shared > selectors > Selector


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s): ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.shared.selectors;
25
26 import org.objectweb.joram.shared.excepts.SelectorException;
27 import org.objectweb.joram.shared.messages.Message;
28
29 /**
30  * The <code>Selector</code> class is used for filtering messages according
31  * to their header fields and properties.
32  */

33 public class Selector
34 {
35   /**
36    * Clients call this method to check a selector syntax.
37    *
38    * @return <code>true</code> when the syntax is ok.
39    * @exception SelectorException When the selector syntax is incorrect.
40    */

41   public static boolean checks(String JavaDoc selector) throws SelectorException
42   {
43     if (selector == null || selector.equals(""))
44       return true;
45
46     try {
47       Checker checker = new Checker(new Lexer(selector));
48       return ((Boolean JavaDoc) checker.parse().value).booleanValue();
49     }
50     catch (SelectorException sE) {
51       throw sE;
52     }
53     catch (Throwable JavaDoc t) {
54       throw new SelectorException("Invalid selector: " + t.getMessage());
55     }
56   }
57
58   /**
59    * Destinations call this method to filter a message according to a selector.
60    *
61    * @return <code>true</code> when the selection matches, <code>false</code>
62    * otherwise.
63    */

64   public static boolean matches(Message message, String JavaDoc selector)
65   {
66     if (selector == null || selector.equals(""))
67       return true;
68
69     try {
70       Filter filter = new Filter(new Lexer(selector), message, "JMS");
71       Boolean JavaDoc result = (Boolean JavaDoc) filter.parse().value;
72
73       if (result == null)
74         return false;
75     
76       return result.booleanValue();
77     }
78     catch (Throwable JavaDoc t) {
79       return false;
80     }
81   }
82 }
83   
84
Popular Tags