View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 Internet2
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  /**
17   * @author mchyzer
18   * $Id: ContextContainer.java,v 1.2 2009-10-11 22:04:18 mchyzer Exp $
19   */
20  package edu.internet2.middleware.grouper.grouperUi.beans;
21  
22  import java.util.ArrayList;
23  import java.util.Comparator;
24  import java.util.List;
25  
26  import javax.servlet.ServletContext;
27  
28  import edu.emory.mathcs.backport.java.util.Collections;
29  import edu.internet2.middleware.grouper.cache.GrouperCache;
30  import edu.internet2.middleware.grouper.ui.GrouperUiFilter;
31  import edu.internet2.middleware.subject.Source;
32  import edu.internet2.middleware.subject.provider.SourceManager;
33  
34  
35  /**
36   * container for global things in the context attribute
37   */
38  public class ContextContainer {
39    
40    /** singleton */
41    private static ContextContainerrouperUi/beans/ContextContainer.html#ContextContainer">ContextContainer instance = new ContextContainer();
42    
43    /**
44     * return the isntance
45     * @return the instance
46     */
47    public static ContextContainer instance() {
48      return instance;
49    }
50  
51    /**
52     * store to session scope
53     */
54    public void storeToContext() {
55      ServletContext servletContext = GrouperUiFilter.retrieveHttpServlet().getServletContext();
56      if (servletContext.getAttribute("contextContainer") != instance) {
57        servletContext.setAttribute("contextContainer", instance);
58      }
59    }
60    
61    /** cache of sources */
62    private static GrouperCache<Boolean, List<Source>> sourcesCache = new GrouperCache<Boolean, List<Source>>(
63        ContextContainer.class.getName() + ".sources", 100, false, 120, 120, false); 
64    
65    /**
66     * available sourceIds for the upload form
67     * @return the source ids
68     */
69    public List<Source> getSources() {
70      try {
71        List<Source> sources = sourcesCache.get(Boolean.TRUE);
72        if (sources == null) {
73          sources = new ArrayList<Source>(SourceManager.getInstance().getSources());
74          
75          //lets sort them by id
76          Collections.sort(sources, new Comparator() {
77    
78            public int compare(Object o1, Object o2) {
79              return ((Source)o1).getId().compareTo(((Source)o2).getId());
80            }
81          });
82          
83          sourcesCache.put(Boolean.TRUE, sources);
84        }
85        
86        return sources;
87      } catch (Exception e) {
88        //TODO take out this try/catch when upgrading to grouper 1.5 and the new source api
89        throw new RuntimeException(e);
90      }
91    }
92  
93  
94  }