View Javadoc

1   /*
2    * $Id: ApplicationParametersMap.java,v 1.2 2009/03/04 10:09:07 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.util;
5   
6   import java.util.Collection;
7   import java.util.Map;
8   import java.util.Set;
9   
10  import javax.faces.context.ExternalContext;
11  import javax.faces.context.FacesContext;
12  import javax.servlet.ServletContext;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  /**
18   * 
19   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
20   * @version $Revision: 1.2 $ $Date: 2009/03/04 10:09:07 $
21   */
22  public final class ApplicationParametersMap implements Map {
23      private static final String REVISION = "$Revision: 1.2 $";
24  
25      private static final Log LOG = LogFactory
26              .getLog(ApplicationParametersMap.class);
27  
28      private static final Object REMOVED = new Object();
29  
30      private ServletContext servletContext;
31  
32      private ExternalContext externalContext;
33  
34      public ApplicationParametersMap(ServletContext servletContext) {
35          this.servletContext = servletContext;
36      }
37  
38      public ApplicationParametersMap(FacesContext facesContext) {
39          this.externalContext = facesContext.getExternalContext();
40      }
41  
42      public void clear() {
43          throw new UnsupportedOperationException();
44      }
45  
46      public boolean containsKey(Object key) {
47          return get(key) != null;
48      }
49  
50      public boolean containsValue(Object value) {
51          throw new UnsupportedOperationException();
52      }
53  
54      public Set entrySet() {
55          throw new UnsupportedOperationException();
56      }
57  
58      public Object get(Object key) {
59          Object ret = getAttribute(key);
60          if (ret == REMOVED) {
61              return null;
62          }
63          if (ret != null) {
64              return ret;
65          }
66  
67          return getInitParameter(key);
68      }
69  
70      public boolean isEmpty() {
71          return false;
72      }
73  
74      public Set keySet() {
75          throw new UnsupportedOperationException();
76      }
77  
78      public Object put(Object key, Object value) {
79  
80          Object old = getInitParameter(key);
81          Object old2 = getAttribute(key);
82  
83          if (value == null) {
84              if (old != null) {
85                  if (old2 == REMOVED) {
86                      old = null;
87  
88                  } else if (old2 != null) {
89                      old = old2;
90                  }
91  
92                  setAttribute(key, REMOVED);
93                  return old;
94              }
95  
96              removeAttribute(key);
97              return old2;
98          }
99  
100         if (old2 == REMOVED) {
101             old2 = null;
102         }
103 
104         setAttribute(key, value);
105 
106         return old2;
107     }
108 
109     public void putAll(Map arg0) {
110         throw new UnsupportedOperationException();
111     }
112 
113     public Object remove(Object key) {
114         return put(key, REMOVED);
115     }
116 
117     public int size() {
118         throw new UnsupportedOperationException();
119     }
120 
121     public Collection values() {
122         throw new UnsupportedOperationException();
123     }
124 
125     private Object getInitParameter(Object key) {
126         if (servletContext != null) {
127             return servletContext.getInitParameter((String) key);
128         }
129 
130         return externalContext.getInitParameter((String) key);
131     }
132 
133     private Object getAttribute(Object key) {
134         if (servletContext != null) {
135             return servletContext.getAttribute((String) key);
136         }
137 
138         return externalContext.getApplicationMap().get(key);
139     }
140 
141     private void removeAttribute(Object key) {
142         if (servletContext != null) {
143             servletContext.removeAttribute((String) key);
144             return;
145         }
146 
147         externalContext.getApplicationMap().remove(key);
148     }
149 
150     private void setAttribute(Object key, Object value) {
151         if (servletContext != null) {
152             servletContext.setAttribute((String) key, value);
153             return;
154         }
155 
156         // Ne compile pas sans le cast !!!
157         externalContext.getApplicationMap().put((String)key, value);
158     }
159 
160 }