View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.mina.integration.ognl;
18  
19  import java.util.LinkedHashSet;
20  import java.util.Set;
21  
22  import ognl.Ognl;
23  import ognl.OgnlContext;
24  import ognl.OgnlException;
25  import ognl.TypeConverter;
26  
27  import org.apache.mina.core.session.IoSession;
28  
29  /**
30   * Finds {@link IoSession}s that match a boolean OGNL expression.
31   * 
32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
33   */
34  public class IoSessionFinder {
35  
36      private final String query;
37  
38      private final TypeConverter typeConverter = new PropertyTypeConverter();
39  
40      private final Object expression;
41  
42      /**
43       * Creates a new instance with the specified OGNL expression that returns
44       * a boolean value (e.g. <tt>"id == 0x12345678"</tt>).
45       * 
46       * @param query The OGNL expression 
47       */
48      public IoSessionFinder(String query) {
49          if (query == null) {
50              throw new IllegalArgumentException("query");
51          }
52  
53          query = query.trim();
54          
55          if (query.length() == 0) {
56              throw new IllegalArgumentException("query is empty.");
57          }
58  
59          // Only accept queries like [a-zA-Z_$ ]+ (== | < | > | <= | >=) [a-zA-Z\-$\.0-9 ]+
60          int comp = -1;
61          
62          for (int i=0; i<query.length();i++) {
63          	char c = query.charAt(i);
64          	
65          	if ((c == '=') || (c == '<') || (c == '>') || (c == '!')) {
66          		comp = i;
67          	} else if ( !Character.isJavaIdentifierPart(c) && (c != ' ')) {
68                  throw new IllegalArgumentException("Invalid query.");
69          	} else {
70          		if ( comp > 0) {
71          			break;
72          		}
73          	}
74          }
75          
76          if (comp<=0) {
77              throw new IllegalArgumentException("Invalid query.");
78          }
79          
80          for (int i=comp+1; i<query.length();i++) {
81          	char c = query.charAt(i);
82  
83          	if (!Character.isJavaIdentifierPart(c) && (c != ' ') && (c != '"') && (c != '\'')) {
84                  throw new IllegalArgumentException("Invalid query.");
85          	}
86          }
87          
88          this.query = query;
89          
90          try {
91              expression = Ognl.parseExpression(query);
92          } catch (OgnlException e) {
93              throw new IllegalArgumentException("query: " + query);
94          }
95      }
96  
97      /**
98       * Finds a {@link Set} of {@link IoSession}s that matches the query
99       * from the specified sessions and returns the matches.
100      * @throws OgnlException if failed to evaluate the OGNL expression
101      * 
102      * @param sessions The list of sessions to check
103      * @return A set of the session that matches the query
104      * @throws OgnlException If we can't find a boolean value in a session's context
105      */
106     public Set<IoSession> find(Iterable<IoSession> sessions) throws OgnlException {
107         if (sessions == null) {
108             throw new IllegalArgumentException("sessions");
109         }
110 
111         Set<IoSession> answer = new LinkedHashSet<IoSession>();
112         
113         for (IoSession s : sessions) {
114             OgnlContext context = (OgnlContext) Ognl.createDefaultContext(s);
115             context.setTypeConverter(typeConverter);
116             context.put(AbstractPropertyAccessor.READ_ONLY_MODE, true);
117             context.put(AbstractPropertyAccessor.QUERY, query);
118             Object result = Ognl.getValue(expression, context, s);
119             
120             if (result instanceof Boolean) {
121                 if (((Boolean) result).booleanValue()) {
122                     answer.add(s);
123                 }
124             } else {
125                 throw new OgnlException("Query didn't return a boolean value: " + query);
126             }
127         }
128 
129         return answer;
130     }
131 }