Package org.apache.ws.resource.properties.query.impl

Source Code of org.apache.ws.resource.properties.query.impl.QueryEngineImpl

/*=============================================================================*
*  Copyright 2004 The Apache Software Foundation
*
*  Licensed 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.ws.resource.properties.query.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.resource.JndiConstants;
import org.apache.ws.resource.i18n.Keys;
import org.apache.ws.resource.i18n.MessagesImpl;
import org.apache.ws.resource.properties.ResourcePropertySet;
import org.apache.ws.resource.properties.faults.UnknownQueryExpressionDialectFaultException;
import org.apache.ws.resource.properties.query.ExpressionEvaluator;
import org.apache.ws.resource.properties.query.InvalidQueryExpressionException;
import org.apache.ws.resource.properties.query.QueryConstants;
import org.apache.ws.resource.properties.query.QueryEngine;
import org.apache.ws.resource.properties.query.QueryEvaluationErrorException;
import org.apache.ws.resource.properties.query.QueryExpression;
import org.apache.ws.resource.properties.query.UnknownQueryExpressionDialectException;
import org.apache.ws.resource.properties.query.xpath.impl.XalanXPathExpressionEvaluator;
import org.apache.ws.util.i18n.Messages;
import org.apache.ws.util.jndi.JNDIUtils;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* LOG-DONE Executes queries on resource property sets. The engine looks for evaluators under the "wsrf/query/eval" JNDI
* context.
*
* @see ResourcePropertySet
*/
public class QueryEngineImpl
        implements QueryEngine
{
    /**
     * DOCUMENT_ME
     */
    private static final Log LOG = LogFactory.getLog( QueryEngineImpl.class.getName() );
    public static final Messages MSG = MessagesImpl.getInstance();
    private static String QUERY_EVALUATOR_CONTEXT = JndiConstants.CONTEXT_NAME_BASE + "/query/eval";
    private Map m_evaluators = Collections.synchronizedMap( new HashMap() );

    /**
     * Creates a new {@link QueryEngineImpl} object.
     */
    public QueryEngineImpl()
    {
        // TODO: refactor this
        //refresh();
        //registerEvaluator( QueryConstants.DIALECT_URI__XPATH1_0, new XmlBeansXPathExpressionEvaluator() );
        try
        {
            /*registerEvaluator( QueryConstants.DIALECT_URI__XPATH1_0,
                               new JaxenXPathExpressionEvaluator() );*/
            registerEvaluator( QueryConstants.DIALECT_URI__XPATH1_0,
                    new XalanXPathExpressionEvaluator() );
        }
        catch ( UnknownQueryExpressionDialectException uqede )
        {
            throw new RuntimeException( "Error while registering expression evaluators with query engine.", uqede );
        }
    }

    /**
     * @param dialect
     *
     * @return
     */
    public ExpressionEvaluator getEvaluator( URI dialect )
    {
        return (ExpressionEvaluator) m_evaluators.get( dialect );
    }

    /**
     * @see QueryEngine#executeQuery(org.apache.ws.resource.properties.query.QueryExpression,
     *      org.apache.ws.resource.properties.ResourcePropertySet)
     */
    public Object executeQuery( QueryExpression queryExpr,
                                ResourcePropertySet resourcePropertySet )
            throws UnknownQueryExpressionDialectException,
            QueryEvaluationErrorException,
            InvalidQueryExpressionException
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( MSG.getMessage( Keys.EXEC_QUERY ) + ": " + queryExpr +
                    MSG.getMessage( Keys.ON_RP_SET ) + resourcePropertySet.getMetaData().getName().toString() );
        }
        return getEvaluator( queryExpr ).evaluate( queryExpr, resourcePropertySet );
    }

    /**
     * @see QueryEngine#executeQuery(org.apache.ws.resource.properties.query.QueryExpression, Object)}
     */
    public Object executeQuery( QueryExpression queryExpr,
                                Object evalContext )
            throws UnknownQueryExpressionDialectException,
            QueryEvaluationErrorException,
            InvalidQueryExpressionException
    {
        return getEvaluator( queryExpr ).evaluate( queryExpr, evalContext );
    }

    private ExpressionEvaluator getEvaluator( QueryExpression queryExpr )
            throws UnknownQueryExpressionDialectException
    {
        ExpressionEvaluator evaluator = getEvaluator( queryExpr.getDialect() );
        if ( evaluator == null )
        {
            throw new UnknownQueryExpressionDialectException( queryExpr.getDialect() );
        }
        return evaluator;
    }

    /**
     * Reinitializes the evaluators list from JNDI context. If any evaluators were added using {@link
     * #registerEvaluator(ExpressionEvaluator) registerEvaluator()} function they will be lost.
     */
    public synchronized void refresh()
    {
        m_evaluators.clear();
        NamingEnumeration list = null;
        try
        {
            Context initialContext = new InitialContext();
            list = initialContext.list( QUERY_EVALUATOR_CONTEXT );
            NameClassPair pair = null;
            ExpressionEvaluator evaluator = null;
            while ( list.hasMore() )
            {
                pair = (NameClassPair) list.next();
                evaluator =
                        (ExpressionEvaluator) JNDIUtils.lookup( initialContext,
                                QUERY_EVALUATOR_CONTEXT + "/" + pair.getName(),
                                ExpressionEvaluator.class );
                registerEvaluator( evaluator );
            }
        }
        catch ( NamingException ne )
        {
            LOG.error( MSG.getMessage( Keys.QUERY_ENG_INIT_ERROR, ne ) );
        }
        finally
        {
            if ( list != null )
            {
                try
                {
                    list.close();
                }
                catch ( NamingException ee )
                {
                }
            }
        }
    }

    /**
     * DOCUMENT_ME
     *
     * @param evaluator DOCUMENT_ME
     */
    public synchronized void registerEvaluator( ExpressionEvaluator evaluator )
    {
        URI[] dialects = evaluator.getSupportedDialects();
        for ( int i = 0; i < dialects.length; i++ )
        {
            m_evaluators.put( dialects[i], evaluator );
        }
    }

    /**
     * @param dialect
     * @param evaluator
     *
     * @throws UnknownQueryExpressionDialectFaultException
     *
     */
    public void registerEvaluator( URI dialect,
                                   ExpressionEvaluator evaluator )
            throws UnknownQueryExpressionDialectException
    {
        URI[] supportedDialects = evaluator.getSupportedDialects();
        boolean isSupportedDialect = false;
        for ( int i = 0; i < supportedDialects.length; i++ )
        {
            if ( supportedDialects[i].equals( dialect ) )
            {
                isSupportedDialect = true;
            }
        }
        if ( isSupportedDialect )
        {
            m_evaluators.put( dialect, evaluator );
        }
        else
        {
            throw new UnknownQueryExpressionDialectException( dialect );
        }
    }
}
TOP

Related Classes of org.apache.ws.resource.properties.query.impl.QueryEngineImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.