View Javadoc

1   package org.apache.velocity.tools.config;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional debugrmation
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Iterator;
23  
24  /**
25   * 
26   *
27   * @author Nathan Bubna
28   * @version $Id: Configuration.java 511959 2007-02-26 19:24:39Z nbubna $
29   */
30  public class ConfigurationCleaner extends LogSupport
31  {
32      private static final String LOG_PREFIX = "ConfigurationCleaner : ";
33  
34      @Override
35      protected String logPrefix()
36      {
37          return LOG_PREFIX;
38      }
39  
40      public void clean(FactoryConfiguration factory)
41      {
42          if (isTraceEnabled())
43          {
44              trace("Cleaning factory: "+factory);
45          }
46  
47          cleanProperties(factory);
48  
49          // go thru data to log and remove debug ones
50          Iterator<Data> i = factory.getData().iterator();
51          while (i.hasNext())
52          {
53              Data datum = i.next();
54              try
55              {
56                  datum.validate();
57              }
58              catch (ConfigurationException ce)
59              {
60                  if (isDebugEnabled())
61                  {
62                      debug(ce.getMessage());
63                  }
64                  if (isWarnEnabled())
65                  {
66                      warn("Removing "+datum);
67                  }
68                  i.remove();
69              }
70          }
71  
72          // clean all toolboxes
73          for (ToolboxConfiguration toolbox : factory.getToolboxes())
74          {
75              clean(toolbox);
76          }
77      }
78  
79      public void clean(ToolboxConfiguration toolbox)
80      {
81          cleanProperties(toolbox);
82  
83          // go thru tools to log and remove debug ones
84          Iterator<ToolConfiguration> i = toolbox.getTools().iterator();
85          while (i.hasNext())
86          {
87              ToolConfiguration tool = i.next();
88              cleanProperties(tool);
89              try
90              {
91                  tool.validate();
92              }
93              catch (ConfigurationException ce)
94              {
95                  if (isDebugEnabled())
96                  {
97                      debug(ce.getMessage());
98                  }
99                  if (isWarnEnabled())
100                 {
101                     warn("Removing "+tool);
102                 }
103                 i.remove();
104             }
105         }
106 
107         //TODO: loop on validate() until all debug scoped tools are removed
108     }
109 
110     public void clean(Configuration config)
111     {
112         // delegate to the appropriate method...
113         if (config instanceof FactoryConfiguration)
114         {
115             clean((FactoryConfiguration)config);
116         }
117         else if (config instanceof ToolboxConfiguration)
118         {
119             clean((ToolboxConfiguration)config);
120         }
121         else
122         {
123             cleanProperties(config);
124         }
125     }
126 
127     public void cleanProperties(Configuration config)
128     {
129         // go thru properties to log and remove debug ones
130         Iterator<Property> i = config.getProperties().iterator();
131         while (i.hasNext())
132         {
133             Property prop = i.next();
134             try
135             {
136                 prop.validate();
137             }
138             catch (ConfigurationException ce)
139             {
140                 if (isDebugEnabled())
141                 {
142                     debug(ce.getMessage());
143                 }
144                 if (isWarnEnabled())
145                 {
146                     warn("Removing "+prop);
147                 }
148                 i.remove();
149             }
150         }
151     }
152 
153 }