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.XmlBeansXPathExpressionEvaluator;
import org.apache.ws.util.JaxpUtils;
import org.apache.ws.util.i18n.Messages;
import org.apache.ws.util.jndi.JNDIUtils;
import org.w3c.dom.Element;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.xml.rpc.JAXRPCException;
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 "java:comp/env/query/eval"
* 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 XmlBeansXPathExpressionEvaluator() );
      }
      catch ( UnknownQueryExpressionDialectException uqede )
      {
         throw new JAXRPCException( uqede );
      }
   }

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

   /**
    * DOCUMENT_ME
    *
    * @param queryExpr           DOCUMENT_ME
    * @param resourcePropertySet DOCUMENT_ME
    * @param nsContext           DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    *
    * @throws UnknownQueryExpressionDialectException
    *                                       DOCUMENT_ME
    * @throws QueryEvaluationErrorException DOCUMENT_ME
    * @throws InvalidQueryExpressionException
    *                                       DOCUMENT_ME
    * @throws UnknownQueryExpressionDialectFaultException
    *                                       DOCUMENT_ME
    */
   public Object executeQuery( QueryExpression     queryExpr,
                               ResourcePropertySet resourcePropertySet,
                               Element             nsContext )
   throws UnknownQueryExpressionDialectException,
          QueryEvaluationErrorException,
          InvalidQueryExpressionException
   {
      ExpressionEvaluator evaluator = getEvaluator( queryExpr.getDialect(  ) );
      if ( evaluator == null )
      {
         throw new UnknownQueryExpressionDialectFaultException( queryExpr.getDialect(  ) );
      }
      if(LOG.isDebugEnabled())
      {
          DebugLogQueryExpression(queryExpr, resourcePropertySet);
      }
      return evaluator.evaluate( queryExpr, resourcePropertySet, nsContext );
   }

    private void DebugLogQueryExpression(QueryExpression queryExpr, ResourcePropertySet resourcePropertySet)
    {
        if(queryExpr instanceof XmlBeansQueryExpression)
        {
           LOG.debug(MSG.getMessage( Keys.EXEC_QUERY) + ": " + ((XmlBeansQueryExpression)queryExpr).toString() +
                     MSG.getMessage( Keys.ON_RP_SET) + resourcePropertySet.getMetaData().getName().toString());
        }
        else
        {
            try
            {
                LOG.debug(MSG.getMessage( Keys.EXEC_QUERY)+ ": " + JaxpUtils.toString(queryExpr.getContent()) + MSG.getMessage( Keys.FOR_DIALECT) +
                          queryExpr.getDialect().toString() +
                          MSG.getMessage( Keys.ON_RP_SET) + resourcePropertySet.getMetaData().getName().toString());
            }
            catch (Exception e)
            {
                LOG.debug(MSG.getMessage( Keys.EXEC_QUERY)+MSG.getMessage( Keys.ON_RP_SET) + resourcePropertySet.getMetaData().getName().toString());
            }
        }
    }

    /**
    * 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 e )
      {
         LOG.error( MSG.getMessage( Keys.QUERY_ENG_INIT_ERROR,e ));
      }
      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.