Home » activemq-parent-5.3.1-source-release » org.apache.activemq.console.command.store.amq.reader » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.console.command.store.amq.reader;
   18   
   19   import java.util.Iterator;
   20   import javax.jms.InvalidSelectorException;
   21   import javax.jms.Message;
   22   import org.apache.activemq.broker.region.MessageReference;
   23   import org.apache.activemq.filter.BooleanExpression;
   24   import org.apache.activemq.filter.MessageEvaluationContext;
   25   import org.apache.activemq.selector.SelectorParser;
   26   
   27   /**
   28    * An Iterator for the AMQReader
   29    *
   30    */
   31   class AMQIterator  implements Iterator<Message>{
   32       private AMQReader reader;
   33       private BooleanExpression expression;
   34       private MessageLocation currentLocation;
   35       private MessageLocation nextLocation;
   36       private boolean valid=true;
   37       
   38           
   39       AMQIterator(AMQReader reader, BooleanExpression expression){
   40           this.reader=reader;
   41           this.expression=expression;
   42       } 
   43       
   44       public boolean hasNext() {
   45           try {
   46               this.nextLocation = reader.getNextMessage(currentLocation);
   47               Message next = nextLocation != null ? nextLocation.getMessage()
   48                       : null;
   49               if (expression == null) {
   50                   return next != null;
   51               } else {
   52                   while (next != null) {
   53                       MessageEvaluationContext context = new MessageEvaluationContext();
   54                       context.setMessageReference((MessageReference) next);
   55                       if (expression.matches(context)) {
   56                           return true;
   57                       }
   58                       this.nextLocation = reader.getNextMessage(currentLocation);
   59                       next = nextLocation != null ? nextLocation.getMessage()
   60                               : null;
   61                   }
   62                   valid=false;
   63                   return false;
   64               }
   65           } catch (Exception e) {
   66               throw new RuntimeException(
   67                       "Failed to get next message from reader ", e);
   68           }
   69       }
   70   
   71      
   72       public Message next() {
   73           if (valid && (nextLocation != null || hasNext())) {
   74               this.currentLocation=nextLocation;
   75               return nextLocation.getMessage();
   76           }
   77           return null;
   78       }
   79   
   80      
   81       public void remove() {
   82           throw new IllegalStateException("Not supported");
   83           
   84       }
   85   
   86   }

Home » activemq-parent-5.3.1-source-release » org.apache.activemq.console.command.store.amq.reader » [javadoc | source]