View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
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  package org.apache.jetspeed.util;
18  
19  //standard java stuff
20  import java.util.Hashtable;
21  import java.util.Map;
22  import java.util.Iterator;
23  
24  /***
25  Defines a standard object configuration
26  A Config provides the parameters passed in the current request as well
27  as init parameters.
28  
29  @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
30  @version $Id: BaseConfig.java,v 1.3 2004/02/23 03:23:42 jford Exp $
31  */
32  
33  public class BaseConfig extends Hashtable implements Config
34  {
35  
36      private String name = null;
37  
38      /***
39      Returns the name for this configuration
40      */
41      public String getName()
42      {
43          return this.name;
44      }
45  
46      /***
47      Sets the name of this configuration
48      */
49      public void setName(String name)
50      {
51          this.name = name;
52      }
53  
54      /***
55      Used to define a Portlet's parameters.
56      */
57      public void setInitParameters( Map init_params )
58      {
59          clear();
60          putAll( init_params );
61      }
62  
63      /***
64      Used to override Portlet's parameters.
65      */
66      public void addInitParameters( Map init_params )
67      {
68          Iterator keys = init_params.keySet().iterator();
69          
70          while (keys.hasNext() )
71          {
72              String key = (String)keys.next();
73              
74              if( ! containsKey( key ) )
75              {
76                  put( key , (String)init_params.get( key ) );
77              }
78          }
79      }
80  
81  
82      /***
83      Retrieves the PortletController parameters
84      */
85      public Map getInitParameters()
86      {
87          return this;
88      }
89  
90  
91      /***
92      Used to define a PortletController's parameter.if value is null, removes
93      the key from the stored properties
94      */
95      public void setInitParameter(String name, Object value)
96      {
97          if (name!=null)
98          {
99              if (value==null)
100             {
101                 remove(name);
102             }
103             else
104             {
105                 put(name,value);
106             }
107         }
108     }
109 
110     /***
111     Returns a parameter (or null) that was given the controller.
112     */
113     public String getInitParameter(String name)
114     {
115         return getInitParameter( name, null );
116     }
117 
118     /***
119     Returns a parameter (or defaultValue) that was given the controller.
120     */
121     public String getInitParameter(String name, String defaultValue)
122     {
123         String value = null;
124 
125         try
126         {
127             value=(String)get(name);
128             if (value==null) value=defaultValue;
129         }
130         catch (RuntimeException e)
131         {
132             value=defaultValue;
133         }
134 
135         return value;
136     }
137 
138     /***
139     Returns the parameter names of this Config.
140     */
141     public Iterator getInitParameterNames()
142     {
143         return keySet().iterator();
144     }
145 
146 }