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.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.XmlBeanJndiUtils;
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;

/**
* 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(  ) );
   private static final Messages MSG                         = MessagesImpl.getInstance(  );
   private static String         COTEXT_NAME_QUERY_EVALUATOR = JndiConstants.CONTEXT_NAME_WSRF + "/query/eval";
   private Map                   m_evaluators                = Collections.synchronizedMap( new HashMap(  ) );

   /**
    * Creates a new {@link QueryEngineImpl} object.
    */
   public QueryEngineImpl(  )
   {
      refresh(  );
   }

   /**
    * @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 );
   }

   /**
    * 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(  );
         try
         {
            list = initialContext.list( COTEXT_NAME_QUERY_EVALUATOR );
         }
         catch ( NamingException ne )
         {
            // ignore
         }

         if ( ( list != null ) && list.hasMore(  ) )
         {
            while ( list.hasMore(  ) )
            {
               NameClassPair       pair      = (NameClassPair) list.next(  );
               ExpressionEvaluator evaluator =
                  (ExpressionEvaluator) XmlBeanJndiUtils.lookup( initialContext,
                                                                 COTEXT_NAME_QUERY_EVALUATOR + "/"
                                                                 + pair.getName(  ), ExpressionEvaluator.class );
               registerEvaluator( evaluator );
            }
         }
         else
         {
            // nothing in JNDI - register a default evaluator for XPath 1.0...
            registerEvaluator( QueryConstants.DIALECT_URI__XPATH1_0,
                               new XalanXPathExpressionEvaluator(  ) );
         }
      }
      catch ( Exception e )
      {
         LOG.error( MSG.getMessage( Keys.QUERY_ENG_INIT_ERROR, e ) );
      }
      finally
      {
         if ( list != null )
         {
            try
            {
               list.close(  );
            }
            catch ( NamingException ne )
            {
               // intentionally ignored
            }
         }
      }
   }

   /**
    * 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 UnknownQueryExpressionDialectException
    *
    */
   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 );
      }
   }

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

      return evaluator;
   }
}
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.