Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.tomcat.bayeux;
 
 import static org.jboss.web.CoyoteMessages.MESSAGES;
 
 import java.util.List;
 

Author(s):
Filip Hanik
Version:
1.0
 
 public class ChannelImpl implements Channel {
    
    
The unique id of this channel
 
     protected String id = null;
    
    
A list of the current subscribers
 
     protected LinkedList<Clientsubscribers = new LinkedList<Client>();
    
    
A list of the current filters
 
     protected LinkedList<DataFilterfilters = new LinkedList<DataFilter>();
    
    
Is this channel persistent, default value is true
 
     protected boolean persistent = true
    
    
Creates a new channel

Parameters:
id String - the id of the channel, can not be null
 
     protected ChannelImpl(String id) {
         assert id != null;
         this. = id;
     }

    
returns the id of this channel

Returns:
String
 
     public String getId() {
         return ;
     }
    
    
Returns true if this channel matches the pattern to its id. The channel pattern can be a complete name like /service/mychannel or it can be a wild card pattern like /service/app2/**

Parameters:
pattern String according to the Bayeux specification section 2.2.1 Channel Globbing, can not be null.
Returns:
boolean true if the id of this channel matches the pattern
 
     public boolean matches(String pattern) {
         if (pattern == null)
             throw .invalidNullChannelPattern();
         if (getId().equals(pattern))
             return true;
         int wildcardPos = pattern.indexOf("/*");
         if (wildcardPos == -1)
             return false;
         boolean multiSegment = pattern.indexOf("**") != -1;
         String leadSubstring = pattern.substring(0, wildcardPos);
         if (leadSubstring == null)
             return false;
         if (multiSegment
             return getId().startsWith(leadSubstring);
         else {
             if (getId().length() <= wildcardPos + 2)
                 return false;
             return !(getId().substring(wildcardPos + 2).contains("/"));
        }
    }



    

Returns:
returns a non modifiable list of the subscribers for this channel.
    public List<ClientgetSubscribers() {
        return Collections.unmodifiableList();
    }

    

Returns:
true if the Channel will persist without any subscription.
    public boolean isPersistent() {
        return ;
    }
    
    public void publish(Message msg) {
        publish(new Message[] {msg});
    }
    public void publish(Message[] msgs) {
        if (msgs==nullreturn;
        MessageImpl[] imsgs = new MessageImpl[msgs.length];
        for (int i=0; msgs!=null && i<msgs.lengthi++) {
            Message data = msgs[i];
            if (!(data instanceof MessageImpl)) 
                throw .invalidMessagePublish();
            /*if (log.isDebugEnabled()) {
                log.debug("Publishing message:"+data+" to channel:"+this);
            }*/
            //clone it so that we can set this channel as a reference
            MessageImpl msg = (MessageImpl)((MessageImpl)data).clone();
            //this is the channel it was delivered through
            msg.setChannel(this);
            //pass through filters
            for (Iterator<DataFilterit = .iterator(); it.hasNext(); ) {
                it.next().filter(msg);
            }
            imsgs[i] = msg;
        }
        //deliver it to the clients
        for (Iterator<Clientit = .iterator(); it.hasNext(); ) {
            ClientImpl c = (ClientImpl)it.next();
            c.deliverInternal(this,imsgs);
        }
        
    }
    public void setPersistent(boolean persistent) {
        this. = persistent;
    }
    public void subscribe(Client subscriber) {
        if (!.contains((subscriber))) { 
            .addLast(subscriber);
            ((ClientImpl)subscriber).subscribed(this);
        }
    }
    public Client unsubscribe(Client subscriber) {
        if (.remove(subscriber)) {
            ((ClientImpl)subscriber).unsubscribed(this);
            return subscriber;
        } else
            return null;
    }
    
    public void addFilter(DataFilter filter) {
        if (!.contains(filter)) 
            .addLast(filter);
    }
    public DataFilter removeFilter(DataFilter filter) {
        if ( .remove(filter) ) return filter;
        else return null;
    }
    
    public String toString() {
        StringBuilder buf = new StringBuilder(super.toString());
        buf.append("; channelId=").append(getId());
        return buf.toString();
    }
New to GrepCode? Check out our FAQ X