1 7 package com.inversoft.verge.util.url.config; 8 9 10 import java.util.Iterator ; 11 import java.util.Map ; 12 13 import org.jdom.Document; 14 import org.jdom.Element; 15 16 import com.inversoft.config.ConfigBuilder; 17 import com.inversoft.config.ConfigRegistry; 18 import com.inversoft.config.ConfigurationException; 19 import com.inversoft.error.ErrorList; 20 21 22 32 public class URLConfigBuilder implements ConfigBuilder { 33 34 35 38 public URLConfigBuilder() { 39 } 41 42 43 63 public synchronized void build(Document document, ConfigRegistry registry) 64 throws ConfigurationException { 65 ErrorList errors = new ErrorList(); 66 Element root = document.getRootElement(); 67 Iterator iter = root.getChildren(Constants.CATEGORY_ELEMENT).iterator(); 68 URLConfigRegistry reg = (URLConfigRegistry) registry; 69 Element category; 70 String name; 71 String protocol; 72 String host; 73 String port; 74 String context; 75 int portInt; 76 CategoryConfig cc; 77 boolean error; 78 79 while (iter.hasNext()) { 80 category = (Element) iter.next(); 81 error = false; 82 portInt = -1; 83 84 name = category.getAttributeValue(Constants.NAME_ATTRIBUTE); 85 if (name == null) { 86 errors.addError("Name required for URL category"); 87 error = true; 88 } else if (reg.lookupCategory(name) != null) { 89 errors.addError("Category named: " + name + " defined twice"); 90 error = true; 91 } 92 93 protocol = category.getChildText(Constants.PROTOCOL_ELEMENT); 94 if (protocol != null && !protocol.equalsIgnoreCase("http") && 95 !protocol.equalsIgnoreCase("https")) { 96 errors.addError("Invalid protocol: " + protocol); 97 error = true; 98 } 99 100 host = category.getChildText(Constants.HOST_ELEMENT); 101 context = category.getChildText(Constants.CONTEXT_ELEMENT); 102 port = category.getChildText(Constants.PORT_ELEMENT); 103 104 if (port != null) { 105 try { 106 portInt = Integer.parseInt(port); 107 108 if (portInt <= 0) { 109 errors.addError("Invalid port number: " + port); 110 error = true; 111 } 112 } catch (NumberFormatException nfe) { 113 errors.addError("Invalid port number: " + port); 114 error = true; 115 } 116 } 117 118 119 if (error) { 120 continue; 121 } 122 123 cc = new CategoryConfig(name, protocol, host, portInt, context); 124 reg.register(cc); 125 } 126 127 if (!errors.isEmpty()) { 128 throw new ConfigurationException(errors); 129 } 130 } 131 132 141 public void rebuild(Document document, ConfigRegistry registry) 142 throws ConfigurationException { 143 build(document, registry); 144 } 145 146 151 public void validate(ConfigRegistry registry, Map otherRegistries) 152 throws ConfigurationException { 153 } 155 156 163 public void commit(ConfigRegistry registry, Map otherRegistries) { 164 URLConfigRegistry reg = (URLConfigRegistry) registry; 165 URLConfigRegistry.setInstance(reg); 166 } 167 } | Popular Tags |