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.tobago.example.demo.info;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.enterprise.context.ApplicationScoped;
26  import javax.inject.Named;
27  import java.io.Serializable;
28  import java.lang.invoke.MethodHandles;
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.concurrent.ConcurrentHashMap;
33  
34  @Named
35  @ApplicationScoped
36  public class ActivityList implements Serializable {
37  
38    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
39  
40    // XXX using the session id as key is not good for applications with login, because the container should change
41    // XXX the session id while the login process.
42    private Map<String, Activity> data = new ConcurrentHashMap<>();
43  
44    public void add(final Activity activity) {
45      LOG.info("Adding session id: '{}'", activity.getSessionId());
46      data.put(activity.getSessionId(), activity);
47    }
48  
49    public void remove(final String sessionId) {
50      LOG.info("Removing session id: '{}'", sessionId);
51      data.remove(sessionId);
52    }
53  
54    public List<Activity> getValues() {
55      return new ArrayList<>(data.values());
56    }
57  
58    public void executeJsfRequest(final String sessionId) {
59      final Activity activity = data.get(sessionId);
60      if (activity != null) {
61        activity.executeJsfRequest();
62      } else {
63        LOG.error("Ignoring sessionId='{}'", sessionId);
64      }
65    }
66  
67    public void executeAjaxRequest(final String sessionId) {
68      final Activity activity = data.get(sessionId);
69      if (activity != null) {
70        activity.executeAjaxRequest();
71      } else {
72        LOG.error("Ignoring sessionId='{}'", sessionId);
73      }
74    }
75  }