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        this.query = query;
060        
061        try {
062            expression = Ognl.parseExpression(query);
063        } catch (OgnlException e) {
064            throw new IllegalArgumentException("query: " + query);
065        }
066    }
067
068    /**
069     * Finds a {@link Set} of {@link IoSession}s that matches the query
070     * from the specified sessions and returns the matches.
071     * @throws OgnlException if failed to evaluate the OGNL expression
072     * 
073     * @param sessions The list of sessions to check
074     * @return A set of the session that matches the query
075     * @throws OgnlException If we can't find a boolean value in a session's context
076     */
077    public Set<IoSession> find(Iterable<IoSession> sessions) throws OgnlException {
078        if (sessions == null) {
079            throw new IllegalArgumentException("sessions");
080        }
081
082        Set<IoSession> answer = new LinkedHashSet<IoSession>();
083        
084        for (IoSession s : sessions) {
085            OgnlContext context = (OgnlContext) Ognl.createDefaultContext(s);
086            context.setTypeConverter(typeConverter);
087            context.put(AbstractPropertyAccessor.READ_ONLY_MODE, true);
088            context.put(AbstractPropertyAccessor.QUERY, query);
089            Object result = Ognl.getValue(expression, context, s);
090            
091            if (result instanceof Boolean) {
092                if (((Boolean) result).booleanValue()) {
093                    answer.add(s);
094                }
095            } else {
096                throw new OgnlException("Query didn't return a boolean value: " + query);
097            }
098        }
099
100        return answer;
101    }
102}