View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.springframework.extensions.config.Config;
28  import org.springframework.extensions.config.ConfigElement;
29  import org.springframework.extensions.config.ConfigService;
30  
31  /**
32   * Abstract base class used for objects that represent configuration 
33   * as a root object in a script or template model.
34   * 
35   * @author gavinc
36   */
37  public abstract class ConfigModel
38  {
39     protected ConfigService configService;
40     protected Map<String, ConfigElement> globalConfig;
41     protected String scriptConfig;
42     
43     private static Log logger = LogFactory.getLog(ConfigModel.class);
44     
45     public ConfigModel(ConfigService configService, String scriptConfig)
46     {
47        this.configService = configService;
48        this.scriptConfig = scriptConfig;
49        
50        if (this.configService != null)
51        {
52           // get the global config
53           this.globalConfig = this.configService.getGlobalConfig().getConfigElements();
54        }
55        
56        // if no global config was found create an empty map
57        if (this.globalConfig == null)
58        {
59           this.globalConfig = Collections.emptyMap();
60        }
61     }
62  
63     /**
64      * Retrieves the global configuration as a Map.
65      * 
66      * @return Map of the global config
67      */
68     public Map<String, ConfigElement> getGlobal()
69     {
70        return this.globalConfig;
71     }
72     
73     /**
74      * Retrieves scoped configuration as a Map.
75      * 
76      * @return Map of the scoped config
77      */
78     @SuppressWarnings("unchecked")
79     public Map<String, ConfigElement> getScoped()
80     {
81        return new ScopedConfigMap();
82     }
83     
84     /**
85      * Retrieves the script configuration.<br/>
86      * It's up to the subclass what is returned to represent script config.
87      * 
88      * @return script configuration
89      */
90     public abstract Object getScript();
91     
92     /**
93      * Map to allow access to scoped config in a unified way 
94      * for scripts and templates.
95      * 
96      * @author gavinc
97      */
98     @SuppressWarnings({ "unchecked", "serial" })
99     public class ScopedConfigMap extends HashMap
100    {
101       @Override
102       public Object get(Object identifier)
103       {
104          if (logger.isDebugEnabled())
105             logger.debug("Getting scoped config for '" + identifier + "'");
106          
107          Map<String, ConfigElement> map = null;
108          
109          if (configService != null)
110          {
111             Config result = configService.getConfig(identifier);
112             map = result.getConfigElements();
113          }
114          else
115          {
116             map = Collections.emptyMap();
117          }
118          
119          if (logger.isDebugEnabled())
120             logger.debug("Returning config for '" + identifier + "': " + map);
121          
122          return map;
123       }
124    }
125 }
126 
127 
128 
129