KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > RoutingCriteria


1 package org.jahia.services.usermanager;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Properties JavaDoc;
5 import java.util.StringTokenizer JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 /**
9  * <p>Title: a routing criteria is a set of pattern matching rules that
10  * define a criteria to route method calls to a specific object</p>
11  * <p>Description: </p>
12  * <p>Copyright: Copyright (c) 2002</p>
13  * <p>Company: Jahia Inc.</p>
14  *
15  * @author Serge Huber
16  * @version 3.0
17  */

18
19 class RoutingCriteria {
20
21     private String JavaDoc name;
22     private String JavaDoc description;
23     private Properties JavaDoc conditions;
24     private String JavaDoc destination;
25     private JahiaUserManagerProvider providerInstance = null;
26
27     public RoutingCriteria (String JavaDoc name,
28                             String JavaDoc description,
29                             Properties JavaDoc conditions,
30                             String JavaDoc destination) {
31         this.name = name;
32         this.description = description;
33         this.conditions = conditions;
34         this.destination = destination;
35     }
36
37     public String JavaDoc getName () {
38         return name;
39     }
40
41     public String JavaDoc getDescription () {
42         return description;
43     }
44
45     public Properties JavaDoc getConditions () {
46         return conditions;
47     }
48
49     public String JavaDoc getDestination () {
50         return destination;
51     }
52
53
54     public boolean matchesValues (Properties JavaDoc values) {
55
56         // let's first test all the stupid stuff...
57
if (conditions == null) {
58             return false;
59         }
60         if (conditions.size () == 0) {
61             return false;
62         }
63         if (values == null) {
64             return false;
65         }
66         if (values.size () == 0) {
67             return false;
68         }
69
70         // now let's do some real work guys...
71
Enumeration JavaDoc valueKeys = values.keys ();
72         while (valueKeys.hasMoreElements ()) {
73             Object JavaDoc curKeyObj = valueKeys.nextElement ();
74             if (curKeyObj instanceof String JavaDoc) {
75                 String JavaDoc curKey = (String JavaDoc) curKeyObj;
76                 String JavaDoc curValue = values.getProperty (curKey);
77                 String JavaDoc curConditionPattern = conditions.getProperty (curKey);
78                 if (curConditionPattern != null) {
79                     // we found a matching condition for this property key
80
if (!starMatching (curConditionPattern, curValue)) {
81                         return false;
82                     }
83                 }
84             }
85         }
86         return true;
87     }
88
89     /**
90      * Case sensitive star pattern matching. eg t*t matches "test", but not
91      * "true"
92      *
93      * @param starPattern
94      * @param inputToTest
95      *
96      * @return true if the inputToTest string matches the starPattern pattern
97      */

98     private static boolean starMatching (String JavaDoc starPattern, String JavaDoc inputToTest) {
99         // we try to make every effort to determine a match as quickly as possible,
100
// but we must at least parse the pattern :(
101
StringTokenizer JavaDoc patternTokens = new StringTokenizer JavaDoc (starPattern, "*", false);
102         Vector JavaDoc patternMatchers = new Vector JavaDoc ();
103
104         while (patternTokens.hasMoreTokens ()) {
105             String JavaDoc curToken = patternTokens.nextToken ();
106             patternMatchers.add (curToken);
107         }
108
109         if (patternMatchers.size () == 0) {
110             return false;
111         }
112
113         if (!starPattern.startsWith ("*")) {
114             if (!inputToTest.startsWith ((String JavaDoc) patternMatchers.elementAt (0))) {
115                 // beginning doesn't match...
116
return false;
117             }
118         }
119         if (!starPattern.endsWith ("*")) {
120             if (!inputToTest.endsWith (
121                     (String JavaDoc) patternMatchers.elementAt (patternMatchers.size () - 1))) {
122                 // beginning doesn't match...
123
return false;
124             }
125         }
126
127         Enumeration JavaDoc patternMatchersEnum = patternMatchers.elements ();
128         int offsetInInput = 0;
129         int matchPos = 0;
130         String JavaDoc curMatcher = null;
131         while (patternMatchersEnum.hasMoreElements ()) {
132             curMatcher = (String JavaDoc) patternMatchersEnum.nextElement ();
133             matchPos = inputToTest.indexOf (curMatcher, offsetInInput);
134             if (matchPos == -1) {
135                 return false;
136             }
137             offsetInInput = matchPos + curMatcher.length ();
138             if (offsetInInput >= inputToTest.length ()) {
139                 // we still have pattern to match but we got to the end of
140
// the string too early.
141
return false;
142             }
143         }
144         // if we got here it means we've matched all the pattern matchers in
145
// the input string.
146
return true;
147     }
148
149 }
Popular Tags