View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.config;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.io.Serializable;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  public class RenderersConfigImpl implements RenderersConfig, Serializable {
34  
35    private static final long serialVersionUID = 1L;
36  
37    private static final Logger LOG = LoggerFactory.getLogger(RenderersConfigImpl.class);
38  
39    private Map<String, RendererConfig> rendererMap = new HashMap<String, RendererConfig>();
40    private boolean merged = false;
41  
42    private boolean unmodifiable = false;
43  
44    private void checkLocked() throws IllegalStateException {
45      if (unmodifiable) {
46        throw new RuntimeException("The configuration must not be changed after initialization!");
47      }
48    }
49  
50    /**
51     * Lock the configuration, so it cannot be modified any more.
52     */
53    public void lock() {
54      unmodifiable = true;
55      rendererMap = Collections.unmodifiableMap(rendererMap);
56    }
57  
58    public boolean isMerged() {
59      return merged;
60    }
61  
62    public void setMerged(final boolean merged) {
63      checkLocked();
64      this.merged = merged;
65    }
66  
67    public List<RendererConfig> getRendererConfigs() {
68      final ArrayList<RendererConfig> result = new ArrayList<RendererConfig>();
69      result.addAll(rendererMap.values());
70      return result;
71    }
72  
73    public void addRenderer(final RendererConfig rendererConfig) {
74      checkLocked();
75      final String name = rendererConfig.getName();
76      if (rendererMap.containsKey(name)) {
77        rendererMap.get(name).merge(rendererConfig);
78      } else {
79        rendererMap.put(name, rendererConfig);
80      }
81    }
82  
83    public boolean isMarkupSupported(final String rendererName, final String markup) {
84      final RendererConfig rendererConfig = rendererMap.get(rendererName);
85      if (rendererConfig != null) {
86        return rendererConfig.contains(markup);
87      } else {
88        LOG.error("Calling isMarkupSupported('{}', '{}'), but no configuration found.", rendererName, markup);
89        return false;
90      }
91    }
92  
93    public void merge(final RenderersConfig renderersConfig, final boolean override) {
94      checkLocked();
95      final Collection<RendererConfig> renderers = renderersConfig.getRendererConfigs();
96      for (final RendererConfig rendererConfig : renderers) {
97        addRenderer(rendererConfig);
98      }
99    }
100 
101   public String toString() {
102     return rendererMap.toString();
103   }
104 }