All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.mina.integration.ognl.IoSessionFinder Maven / Gradle / Ivy

There is a newer version: 2.2.4
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.mina.integration.ognl;

import java.util.LinkedHashSet;
import java.util.Set;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.TypeConverter;

import org.apache.mina.core.session.IoSession;

/**
 * Finds {@link IoSession}s that match a boolean OGNL expression.
 * 
 * @author Apache MINA Project
 */
public class IoSessionFinder {

    private final String query;

    private final TypeConverter typeConverter = new PropertyTypeConverter();

    private final Object expression;

    /**
     * Creates a new instance with the specified OGNL expression that returns
     * a boolean value (e.g. {@code "id == 0x12345678"}).
     * 
     * @param query The OGNL expression 
     */
    public IoSessionFinder(String query) {
        if (query == null) {
            throw new IllegalArgumentException("query");
        }

        query = query.trim();
        
        if (query.length() == 0) {
            throw new IllegalArgumentException("query is empty.");
        }

        // Only accept queries like [a-zA-Z_$ ]+ (== | < | > | <= | >=) [a-zA-Z\-$\.0-9 ]+
        int comp = -1;
        
        for (int i=0; i') || (c == '!')) {
                comp = i;
            } else if ( !Character.isJavaIdentifierPart(c) && (c != ' ')) {
                throw new IllegalArgumentException("Invalid query.");
            } else {
                if ( comp > 0) {
                    break;
                }
            }
        }
        
        if (comp<=0) {
            throw new IllegalArgumentException("Invalid query.");
        }
        
        for (int i=comp+1; i find(Iterable sessions) throws OgnlException {
        if (sessions == null) {
            throw new IllegalArgumentException("sessions");
        }

        Set answer = new LinkedHashSet<>();
        
        for (IoSession s : sessions) {
            OgnlContext context = (OgnlContext) Ognl.createDefaultContext(s);
            context.setTypeConverter(typeConverter);
            context.put(AbstractPropertyAccessor.READ_ONLY_MODE, true);
            context.put(AbstractPropertyAccessor.QUERY, query);
            Object result = Ognl.getValue(expression, context, s);
            
            if (result instanceof Boolean) {
                if (((Boolean) result).booleanValue()) {
                    answer.add(s);
                }
            } else {
                throw new OgnlException("Query didn't return a boolean value: " + query);
            }
        }

        return answer;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy