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 public IoSessionFinder(String query) { 047 if (query == null) { 048 throw new IllegalArgumentException("query"); 049 } 050 051 query = query.trim(); 052 if (query.length() == 0) { 053 throw new IllegalArgumentException("query is empty."); 054 } 055 056 this.query = query; 057 try { 058 expression = Ognl.parseExpression(query); 059 } catch (OgnlException e) { 060 throw new IllegalArgumentException("query: " + query); 061 } 062 } 063 064 /** 065 * Finds a {@link Set} of {@link IoSession}s that matches the query 066 * from the specified sessions and returns the matches. 067 * @throws OgnlException if failed to evaluate the OGNL expression 068 */ 069 public Set<IoSession> find(Iterable<IoSession> sessions) throws OgnlException { 070 if (sessions == null) { 071 throw new IllegalArgumentException("sessions"); 072 } 073 074 Set<IoSession> answer = new LinkedHashSet<IoSession>(); 075 for (IoSession s : sessions) { 076 OgnlContext context = (OgnlContext) Ognl.createDefaultContext(s); 077 context.setTypeConverter(typeConverter); 078 context.put(AbstractPropertyAccessor.READ_ONLY_MODE, true); 079 context.put(AbstractPropertyAccessor.QUERY, query); 080 Object result = Ognl.getValue(expression, context, s); 081 if (result instanceof Boolean) { 082 if (((Boolean) result).booleanValue()) { 083 answer.add(s); 084 } 085 } else { 086 throw new OgnlException("Query didn't return a boolean value: " + query); 087 } 088 } 089 090 return answer; 091 } 092}