1 18 package org.apache.beehive.netui.util.config; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 26 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument; 27 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument.NetuiConfig; 28 import org.apache.beehive.netui.util.logging.Logger; 29 30 import org.apache.xmlbeans.XmlException; 31 import org.apache.xmlbeans.XmlOptions; 32 import org.apache.xmlbeans.XmlError; 33 34 49 public class ConfigUtil { 50 51 54 private static final Logger LOGGER = Logger.getInstance(ConfigUtil.class); 55 56 private static final String DEFAULT_CONFIG = "org/apache/beehive/netui/util/config/beehive-netui-config-default.xml"; 57 58 private static NetuiConfigDocument _config = null; 59 60 61 protected ConfigUtil() { 62 } 63 64 79 public static final void init(InputStream is) 80 throws ConfigInitializationException { 81 if(_config != null) 82 throw new ConfigInitializationException("Config initialization already completed; unable to reload the NetUI config file."); 83 84 internalInit(is); 85 } 86 87 public static final boolean isInit() { 88 return (_config != null); 89 } 90 91 101 protected static final void internalInit(InputStream is) 102 throws ConfigInitializationException { 103 104 if(is == null) { 106 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 107 is = cl.getResourceAsStream(DEFAULT_CONFIG); 108 109 if(is == null) 110 throw new ConfigInitializationException("The NetUI runtime could not find the default config file. " + 111 "The webapp may not function properly."); 112 113 if(LOGGER.isInfoEnabled()) 114 LOGGER.info("Loading the default NetUI config file. The runtime will be configured " + 115 "with a set of minimum parameters."); 116 } 117 118 if(_config == null) { 119 try { 120 XmlOptions loadOptions = new XmlOptions(); 121 loadOptions.setLoadLineNumbers(); 122 _config = NetuiConfigDocument.Factory.parse(is, loadOptions); 123 } 124 catch(Exception ex) { 126 assert ex instanceof XmlException || ex instanceof IOException ; 127 128 throw new ConfigInitializationException("Unable load the NetUI config file. Cause: " + ex, ex); 129 } 130 } 131 132 assert _config != null; 133 134 XmlOptions validateOptions = new XmlOptions(); 136 ArrayList errorList = new ArrayList (); 137 validateOptions.setErrorListener(errorList); 138 boolean isValid = _config.validate(validateOptions); 139 140 if(!isValid) { 142 InternalStringBuilder msg = new InternalStringBuilder("Invalid NetUI configuration file."); 143 144 for(int i = 0; i < errorList.size(); i++) { 145 XmlError error = (XmlError)errorList.get(i); 146 msg.append("\n line "); 147 msg.append(error.getLine()); 148 msg.append(": "); 149 msg.append(error.getMessage()); 150 msg.append(" ("); 151 msg.append(error.getCursorLocation().toString()); 152 msg.append(")"); 153 } 154 155 throw new ConfigInitializationException(msg.toString()); 156 } 157 } 158 159 165 public static NetuiConfig getConfig() { 166 if(_config != null) { 167 return _config.getNetuiConfig(); 168 } 169 173 else { 174 177 if(LOGGER.isErrorEnabled()) 178 LOGGER.error("An error occurred parsing the default config file. " + 179 "The NetUI runtime is not properly configured."); 180 181 throw new IllegalStateException ("The NetUI runtime could not find the default config file. The webapp may not function properly."); 182 } 183 } 184 } 185 | Popular Tags |