View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.push.cdi;
21  
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import javax.enterprise.context.ApplicationScoped;
29  
30  /**
31   *
32   */
33  @ApplicationScoped
34  public class WebsocketApplicationBean
35  {
36      
37      /**
38       * This map has as key the channel and as values a list of websocket channels
39       */
40      private Map<String, List<WebsocketChannel> > channelTokenListMap = 
41          new HashMap<String, List<WebsocketChannel> >(2);
42  
43      public void registerWebsocketSession(String token, WebsocketChannelMetadata metadata)
44      {
45          if ("application".equals(metadata.getScope()))
46          {
47              channelTokenListMap.putIfAbsent(metadata.getChannel(), new ArrayList<WebsocketChannel>(1));
48              channelTokenListMap.get(metadata.getChannel()).add(new WebsocketChannel(
49                      token, metadata));
50          }
51      }
52      
53      /**
54       * Indicate if the channel mentioned is valid for view scope.
55       * 
56       * A channel is valid if there is at least one token that represents a valid connection to this channel.
57       * 
58       * @param channel
59       * @return 
60       */
61      public boolean isChannelAvailable(String channel)
62      {
63          return channelTokenListMap.containsKey(channel);
64      }
65      
66      public List<String> getChannelTokensFor(String channel)
67      {
68          List<WebsocketChannel> list = channelTokenListMap.get(channel);
69          if (list != null && !list.isEmpty())
70          {
71              List<String> value = new ArrayList<String>(list.size());
72              for (WebsocketChannel md : list)
73              {
74                  value.add(md.getChannelToken());
75              }
76              return value;
77          }
78          return Collections.emptyList();
79      }
80      
81      public <S extends Serializable> List<String> getChannelTokensFor(String channel, S user)
82      {
83          List<WebsocketChannel> list = channelTokenListMap.get(channel);
84          if (list != null && !list.isEmpty())
85          {
86              List<String> value = new ArrayList<String>(list.size());
87              for (WebsocketChannel md : list)
88              {
89                  if (user.equals(md.getUser()))
90                  {
91                      value.add(md.getChannelToken());
92                  }
93              }
94              return value;
95          }
96          return null;
97      }
98  }