1 7 package com.inversoft.verge.repository.config; 8 9 10 import java.util.Iterator ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 14 import org.apache.log4j.Logger; 15 import org.jdom.Document; 16 import org.jdom.Element; 17 18 import com.inversoft.config.ConfigBuilder; 19 import com.inversoft.config.ConfigRegistry; 20 import com.inversoft.config.ConfigurationException; 21 import com.inversoft.config.component.ComponentConfigBuilderRegistry; 22 import com.inversoft.error.ErrorList; 23 24 25 41 public class RepositoryConfigBuilder implements ConfigBuilder { 42 43 46 private static final Logger logger = Logger.getLogger(RepositoryConfigBuilder.class); 47 48 52 public static final String BUNDLE_NAME = 53 "com.inversoft.verge.repository.config.ConfigBuilders"; 54 55 58 private ComponentConfigBuilderRegistry builderRegistry; 59 60 61 64 public RepositoryConfigBuilder() { 65 try { 66 builderRegistry = new ComponentConfigBuilderRegistry(BUNDLE_NAME); 67 } catch (ConfigurationException ce) { 68 assert (false) : ce.toString(); 69 } 70 } 71 72 73 95 public synchronized void build(Document document, ConfigRegistry registry) 96 throws ConfigurationException { 97 98 ErrorList errors = new ErrorList(); 100 101 Iterator iter = document.getRootElement().getChildren().iterator(); 103 Element currentElement; 104 Builder builder; 105 106 while (iter.hasNext()) { 107 currentElement = (Element) iter.next(); 108 builder = (Builder) builderRegistry.lookup(currentElement.getName()); 109 110 try { 111 builder.build(currentElement, (RepositoryConfigRegistry) registry); 112 } catch (ConfigurationException ce) { 113 logger.error("Error building item", ce); 114 errors.addErrorList(ce.getErrors()); 115 continue; 116 } 117 } 118 119 if (!errors.isEmpty()) { 120 throw new ConfigurationException(errors); 121 } 122 } 123 124 132 public void rebuild(Document document, ConfigRegistry registry) 133 throws ConfigurationException { 134 build(document, registry); 135 } 136 137 147 public void validate(ConfigRegistry registry, Map otherRegistries) 148 throws ConfigurationException { 149 150 RepositoryConfigRegistry repositoryRegistry = 151 (RepositoryConfigRegistry) registry; 152 153 ErrorList errors = new ErrorList(); 154 Iterator iter = repositoryRegistry.getConfigurations().iterator(); 155 Config config; 156 Properties refs; 157 Iterator refIter; 158 String refName; 159 160 while (iter.hasNext()) { 161 config = (Config) iter.next(); 162 refs = config.getReferences(); 163 164 refIter = refs.values().iterator(); 165 while (refIter.hasNext()) { 166 refName = (String ) refIter.next(); 167 if (repositoryRegistry.lookup(refName) == null) { 168 errors.addError("Invalid reference: " + refName); 169 } 170 } 171 } 172 173 if (!errors.isEmpty()) { 174 throw new ConfigurationException(errors); 175 } 176 } 177 178 185 public void commit(ConfigRegistry registry, Map otherRegistries) { 186 RepositoryConfigRegistry.setInstance((RepositoryConfigRegistry) registry); 187 } 188 } | Popular Tags |