01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.common.IoSession;
28:
29: /**
30: * Finds {@link IoSession}s that match a boolean OGNL expression.
31: *
32: * @author The Apache MINA Project (dev@mina.apache.org)
33: * @version $Rev: 601229 $, $Date: 2007-12-05 00:13:18 -0700 (Wed, 05 Dec 2007) $
34: */
35: public class IoSessionFinder {
36:
37: private final String query;
38: private final TypeConverter typeConverter = new PropertyTypeConverter();
39: private final Object expression;
40:
41: /**
42: * Creates a new instance with the specified OGNL expression that returns
43: * a boolean value (e.g. <tt>"id == 0x12345678"</tt>).
44: */
45: public IoSessionFinder(String query) {
46: if (query == null) {
47: throw new NullPointerException("query");
48: }
49:
50: query = query.trim();
51: if (query.length() == 0) {
52: throw new IllegalArgumentException("query is empty.");
53: }
54:
55: this .query = query;
56: try {
57: expression = Ognl.parseExpression(query);
58: } catch (OgnlException e) {
59: throw new IllegalArgumentException("query: " + query);
60: }
61: }
62:
63: /**
64: * Finds a {@link Set} of {@link IoSession}s that matches the query
65: * from the specified sessions and returns the matches.
66: * @throws OgnlException if failed to evaluate the OGNL expression
67: */
68: public Set<IoSession> find(Iterable<IoSession> sessions)
69: throws OgnlException {
70: if (sessions == null) {
71: throw new NullPointerException("sessions");
72: }
73:
74: Set<IoSession> answer = new LinkedHashSet<IoSession>();
75: for (IoSession s : sessions) {
76: OgnlContext context = (OgnlContext) Ognl
77: .createDefaultContext(s);
78: context.setTypeConverter(typeConverter);
79: context.put(AbstractPropertyAccessor.READ_ONLY_MODE, true);
80: context.put(AbstractPropertyAccessor.QUERY, query);
81: Object result = Ognl.getValue(expression, context, s);
82: if (result instanceof Boolean) {
83: if (((Boolean) result).booleanValue()) {
84: answer.add(s);
85: }
86: } else {
87: throw new OgnlException(
88: "Query didn't return a boolean value: " + query);
89: }
90: }
91:
92: return answer;
93: }
94: }
|