1 16 package org.apache.cocoon.components.validation.impl; 17 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.avalon.framework.activity.Startable; 26 import org.apache.avalon.framework.configuration.Configurable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 import org.apache.avalon.framework.context.Context; 30 import org.apache.avalon.framework.context.ContextException; 31 import org.apache.avalon.framework.context.Contextualizable; 32 import org.apache.avalon.framework.logger.LogEnabled; 33 import org.apache.avalon.framework.logger.Logger; 34 import org.apache.avalon.framework.parameters.Parameterizable; 35 import org.apache.avalon.framework.parameters.Parameters; 36 import org.apache.avalon.framework.service.ServiceException; 37 import org.apache.avalon.framework.service.ServiceManager; 38 import org.apache.avalon.framework.service.ServiceSelector; 39 import org.apache.avalon.framework.service.Serviceable; 40 import org.apache.avalon.framework.thread.ThreadSafe; 41 import org.apache.cocoon.components.validation.SchemaParser; 42 import org.apache.cocoon.components.validation.Validator; 43 44 58 public class DefaultValidator extends AbstractValidator implements ServiceSelector, 59 ThreadSafe, Contextualizable, Initializable, Disposable, Configurable { 60 61 62 private final Map components = Collections.synchronizedMap(new HashMap ()); 63 64 private final Map grammars = Collections.synchronizedMap(new HashMap ()); 65 66 67 private Context context = null; 68 69 private Configuration conf = null; 70 71 74 public DefaultValidator() { 75 super(); 76 } 77 78 81 public void contextualize(Context context) 82 throws ContextException { 83 this.context = context; 84 } 85 86 89 public void configure(Configuration conf) 90 throws ConfigurationException { 91 this.conf = conf; 92 } 93 94 97 public void initialize() 98 throws Exception { 99 this.logger.debug("Initializing " + this.getClass().getName()); 100 101 if (this.logger == null) throw new IllegalStateException ("Null logger"); 102 if (this.context == null) throw new IllegalStateException ("Null context"); 103 if (this.manager == null) throw new IllegalStateException ("Null manager"); 104 if (this.conf == null) throw new IllegalStateException ("Null configuration"); 105 106 Configuration configurations[] = this.conf.getChildren("schema-parser"); 107 this.logger.debug("Configuring " + configurations.length + " schema parsers" 108 + " from " + this.conf.getLocation()); 109 110 111 for (int x = 0; x < configurations.length; x++) try { 112 final Configuration configuration = configurations[x]; 113 final String className = configuration.getAttribute("class"); 114 final String selectionKey = configuration.getAttribute("name"); 115 116 117 if (this.components.containsKey(selectionKey)) { 118 String message = "Duplicate schema parser \"" + selectionKey + "\""; 119 throw new ConfigurationException(message, configuration); 120 } 121 122 123 this.logger.debug("Configuring schema parser " + selectionKey + " as " 124 + className + " from " + configuration.getLocation()); 125 126 127 final SchemaParser schemaParser; 128 try { 129 130 final Class clazz = Class.forName(className); 131 132 133 if (! SchemaParser.class.isAssignableFrom(clazz)) { 134 String message = "Class " + className + " doesn't implement the " 135 + SchemaParser.class.getName() + " interface"; 136 throw new ConfigurationException(message, configuration); 137 } 138 139 140 if (! ThreadSafe.class.isAssignableFrom(clazz)) { 141 String message = "Class " + className + " doesn't implement the " 142 + ThreadSafe.class.getName() + " interface"; 143 throw new ConfigurationException(message, configuration); 144 } 145 146 147 schemaParser = (SchemaParser) clazz.newInstance(); 148 this.setupComponent(selectionKey, schemaParser, configuration); 149 150 } catch (ConfigurationException exception) { 151 throw exception; 152 } catch (Exception exception) { 153 String message = "Unable to instantiate SchemaParser " + className; 154 throw new ConfigurationException(message, configuration, exception); 155 } 156 157 158 this.components.put(selectionKey, schemaParser); 159 this.logger.debug("SchemaParser \"" + selectionKey + "\" instantiated" + 160 " from class " + className); 161 162 163 String grammars[] = schemaParser.getSupportedGrammars(); 164 if (grammars == null) continue; 165 166 167 for (int k = 0; k < grammars.length; k++) { 168 if (this.grammars.containsKey(grammars[k])) { 169 if (this.logger.isDebugEnabled()) { 170 this.logger.debug("SchemaParser \"" + selectionKey + "\" " + 171 "supports grammar \"" + grammars[k] + 172 "\" but is not the default provider"); 173 } 174 continue; 175 } 176 177 178 this.grammars.put(grammars[k], selectionKey); 179 if (this.logger.isDebugEnabled()) { 180 this.logger.debug("SchemaParser \"" + selectionKey + "\" is the " 181 + "default grammar provider for "+grammars[k]); 182 } 183 } 184 185 } catch (Exception exception) { 186 187 exception.printStackTrace(); 188 this.logger.fatalError("Exception creating schema parsers", exception); 189 190 191 Iterator iterator = this.components.values().iterator(); 192 while (iterator.hasNext()) try { 193 this.decommissionComponent(iterator.next()); 194 } catch (Exception nested) { 195 this.logger.fatalError("Error decommissioning component", nested); 196 } 197 198 199 if (exception instanceof ConfigurationException) { 200 throw exception; 201 } else { 202 Configuration configuration = configurations[x]; 203 String message = "Unable to setup SchemaParser declared at "; 204 message += configuration.getLocation(); 205 throw new ConfigurationException(message, configuration, exception); 206 } 207 } 208 } 209 210 216 public void dispose() { 217 Iterator iterator = this.components.values().iterator(); 218 while (iterator.hasNext()) try { 219 this.decommissionComponent(iterator.next()); 220 } catch (Exception exception) { 221 this.logger.fatalError("Error decommissioning component", exception); 222 } 223 } 224 225 226 227 228 229 238 protected SchemaParser lookupParserByGrammar(String grammar) { 239 if (this.grammars.containsKey(grammar)) { 240 return this.lookupParserByName((String ) this.grammars.get(grammar)); 241 } 242 return null; 243 } 244 245 252 protected SchemaParser lookupParserByName(String name) { 253 if (this.isSelectable(name)) try { 254 return (SchemaParser) this.select(name); 255 } catch (ServiceException exception) { 256 return null; 257 } 258 return null; 259 } 260 261 270 protected void releaseParser(SchemaParser parser) { 271 this.release(parser); 272 } 273 274 275 276 277 278 282 public Object select(Object selectionKey) 283 throws ServiceException { 284 285 if ( this.components.containsKey(selectionKey)) { 286 return this.components.get(selectionKey); 287 } 288 289 290 String message = "No component associated with " + selectionKey; 291 throw new ServiceException((String ) selectionKey, message); 292 } 293 294 299 public boolean isSelectable(Object selectionKey) { 300 return this.components.containsKey(selectionKey); 301 } 302 303 307 public void release(Object component) { 308 } 310 311 312 313 314 315 318 private Object setupComponent(String name, Object component, Configuration conf) 319 throws Exception { 320 boolean initialized = false; 321 boolean started = false; 322 323 try { 324 if (component instanceof LogEnabled) { 325 Logger logger = this.logger.getChildLogger(name); 326 ((LogEnabled) component).enableLogging(logger); 327 } 328 329 if (component instanceof Contextualizable) { 330 ((Contextualizable) component).contextualize(this.context); 331 } 332 333 if (component instanceof Serviceable) { 334 ((Serviceable) component).service(this.manager); 335 } 336 337 if (component instanceof Configurable) { 338 ((Configurable) component).configure(conf); 339 } 340 341 if (component instanceof Parameterizable) { 342 Parameters parameters = Parameters.fromConfiguration(conf); 343 ((Parameterizable) component).parameterize(parameters); 344 } 345 346 if (component instanceof Initializable) { 347 ((Initializable) component).initialize(); 348 initialized = true; 349 } 350 351 if (component instanceof Startable) { 352 ((Startable) component).start(); 353 started = true; 354 } 355 356 return component; 357 358 } catch (Exception exception) { 359 if ((started) && (component instanceof Startable)) try { 360 ((Startable) component).stop(); 361 } catch (Exception nested) { 362 this.logger.fatalError("Error stopping component", nested); 363 } 364 if ((initialized) && (component instanceof Disposable)) try { 365 ((Disposable) component).dispose(); 366 } catch (Exception nested) { 367 this.logger.fatalError("Error disposing component", nested); 368 } 369 throw exception; 370 } 371 } 372 373 376 private void decommissionComponent(Object component) 377 throws Exception { 378 try { 379 if (component instanceof Startable) ((Startable) component).stop(); 380 } finally { 381 if (component instanceof Disposable) ((Disposable) component).dispose(); 382 } 383 } 384 } | Popular Tags |