001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.mina.integration.ognl;
018
019import java.util.LinkedHashSet;
020import java.util.Set;
021
022import ognl.Ognl;
023import ognl.OgnlContext;
024import ognl.OgnlException;
025import ognl.TypeConverter;
026
027import org.apache.mina.core.session.IoSession;
028
029/**
030 * Finds {@link IoSession}s that match a boolean OGNL expression.
031 * 
032 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
033 */
034public class IoSessionFinder {
035
036    private final String query;
037
038    private final TypeConverter typeConverter = new PropertyTypeConverter();
039
040    private final Object expression;
041
042    /**
043     * Creates a new instance with the specified OGNL expression that returns
044     * a boolean value (e.g. <tt>"id == 0x12345678"</tt>).
045     * 
046     * @param query The OGNL expression 
047     */
048    public IoSessionFinder(String query) {
049        if (query == null) {
050            throw new IllegalArgumentException("query");
051        }
052
053        query = query.trim();
054        
055        if (query.length() == 0) {
056            throw new IllegalArgumentException("query is empty.");
057        }
058
059        // Only accept queries like [a-zA-Z_$ ]+ (== | < | > | <= | >=) [a-zA-Z\-$\.0-9 ]+
060        int comp = -1;
061        
062        for (int i=0; i<query.length();i++) {
063                char c = query.charAt(i);
064                
065                if ((c == '=') || (c == '<') || (c == '>') || (c == '!')) {
066                        comp = i;
067                } else if ( !Character.isJavaIdentifierPart(c) && (c != ' ')) {
068                throw new IllegalArgumentException("Invalid query.");
069                } else {
070                        if ( comp > 0) {
071                                break;
072                        }
073                }
074        }
075        
076        if (comp<=0) {
077            throw new IllegalArgumentException("Invalid query.");
078        }
079        
080        for (int i=comp+1; i<query.length();i++) {
081                char c = query.charAt(i);
082
083                if (!Character.isJavaIdentifierPart(c) && (c != ' ') && (c != '"') && (c != '\'')) {
084                throw new IllegalArgumentException("Invalid query.");
085                }
086        }
087        
088        this.query = query;
089        
090        try {
091            expression = Ognl.parseExpression(query);
092        } catch (OgnlException e) {
093            throw new IllegalArgumentException("query: " + query);
094        }
095    }
096
097    /**
098     * Finds a {@link Set} of {@link IoSession}s that matches the query
099     * 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}