View Javadoc

1   package com.explosion.expf.menusandtools;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.awt.Component;
23  import java.util.Map;
24  
25  /***
26   * This class is a little util for checking cookies.
27   * @author Stephen Cowx
28   */
29  public class CookieChecker
30  {
31  
32    private String cookieName;
33    private int enabledThreshold = 1;
34  
35    public CookieChecker(String cookieName, int enabledThreshold)
36    {
37      this.cookieName = cookieName;
38      this.enabledThreshold = enabledThreshold;
39    }
40  
41    /***
42     * This method checks for an object of type Integer
43     */
44    private boolean checkEnabled(Map cookieDatabase) throws Exception
45    {
46      if (cookieDatabase == null)
47        return false;
48      
49      try
50      {
51        Object cookie = cookieDatabase.get(cookieName);
52        if (cookie != null && ((Integer) cookie).intValue() >= enabledThreshold)
53          return true;
54        else
55          return false;
56      }
57      catch (ClassCastException e)
58      {
59        throw new CookieException("Expected object of type Integer in cookie database but found object of another type.", e);
60      }
61    }
62    
63    /***
64     * This method checks for an object of type Integer
65     */
66    public void checkEnabled(Map componentCookies, Map localCookies,Map globalCookies, Component component) throws Exception
67    {
68      boolean componenttrue = checkEnabled(componentCookies);
69      boolean localtrue = checkEnabled(localCookies);
70      boolean globaltrue = checkEnabled(globalCookies);
71      
72      if(componenttrue || localtrue || globaltrue )
73       component.setEnabled(true);
74      else
75       component.setEnabled(false);  
76    }
77  }