1 package com.protomatter.syslog.xml; 2 3 52 53 import java.net.*; 54 import java.util.*; 55 import java.text.*; 56 import org.jdom.*; 57 import com.protomatter.xml.*; 58 import com.protomatter.syslog.*; 59 60 63 64 public abstract class BasicLogger_Helper 65 implements XMLConfigHelper 66 { 67 136 public void configure(Object o, Element e) 137 throws SyslogInitException 138 { 139 configure(o, e, true, true); 140 } 141 142 public void configure(Object o, Element e, boolean includeFormat, boolean includePolicy) 143 throws SyslogInitException 144 { 145 BasicLogger log = (BasicLogger)o; 146 147 String tmp = e.getAttributeValue("name"); 149 150 if (tmp != null && !tmp.equals("")) 151 log.setName(tmp); 152 153 if (includeFormat) 155 { 156 Element formatElement = null; 157 String formatClass = null; 158 formatElement = e.getChild("Format", e.getNamespace()); 159 160 if (formatElement != null) 161 { 162 formatClass = formatElement.getAttributeValue("class"); 163 164 if (formatClass == null) 165 { 166 throw new IllegalArgumentException (MessageFormat.format( 167 Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_ATTRIBUTE_MESSAGE), 168 new Object [] { "class", "Format" } )); 169 } 170 171 try 172 { 173 SyslogTextFormatter formatter 174 = (SyslogTextFormatter)Class.forName(formatClass).newInstance(); 175 176 try 177 { 178 XMLConfigHelper helper = XMLConfigUtil.getConfigHelper(formatter); 180 helper.configure(formatter, formatElement); 181 log.setTextFormatter(formatter); 182 } 183 catch (Exception x) 184 { 185 x.printStackTrace(); 186 throw new IllegalArgumentException (MessageFormat.format( 187 Syslog.getResourceString(MessageConstants.BASICLOG_CANNOT_CONFIGURE_FORMATTER_MESSAGE), 188 new Object [] { formatClass } )); 189 } 190 191 } 192 catch (Exception x) 193 { 194 x.printStackTrace(); 195 throw new IllegalArgumentException (MessageFormat.format( 196 Syslog.getResourceString(MessageConstants.BASICLOG_CANNOT_LOAD_FORMATTER_MESSAGE), 197 new Object [] { formatClass } )); 198 } 199 200 } 201 else 202 { 203 log.setTextFormatter(new SimpleSyslogTextFormatter()); 204 } 205 206 } 207 208 if (includePolicy) 210 { 211 Element policyElement = null; 212 String policyClass = null; 213 policyElement = e.getChild("Policy", e.getNamespace()); 214 215 if (policyElement != null) 216 { 217 policyClass = policyElement.getAttributeValue("class"); 218 219 if (policyClass == null) 220 { 221 throw new IllegalArgumentException (MessageFormat.format( 222 Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_ATTRIBUTE_MESSAGE), 223 new Object [] { "class", "Policy" } )); 224 } 225 226 try 227 { 228 LogPolicy policy = (LogPolicy)Class.forName(policyClass).newInstance(); 229 230 try 231 { 232 XMLConfigHelper helper = XMLConfigUtil.getConfigHelper(policy); 234 helper.configure(policy, policyElement); 235 log.setPolicy(policy); 236 } 237 catch (Exception x) 238 { 239 x.printStackTrace(); 240 throw new IllegalArgumentException (MessageFormat.format( 241 Syslog.getResourceString(MessageConstants.BASICLOG_CANNOT_CONFIGURE_POLICY_MESSAGE), 242 new Object [] { policyClass } )); 243 } 244 245 } 246 catch (Exception x) 247 { 248 x.printStackTrace(); 249 throw new IllegalArgumentException (MessageFormat.format( 250 Syslog.getResourceString(MessageConstants.BASICLOG_CANNOT_LOAD_POLICY_MESSAGE), 251 new Object [] { policyClass } )); 252 } 253 254 } 255 else 256 { 257 log.setPolicy(new SimpleLogPolicy()); 258 } 259 260 } 261 } 262 263 public Element getConfiguration(Object o, Element element) 264 { 265 return getConfiguration(o, element, true, true); 266 } 267 268 public Element getConfiguration(Object o, Element element, boolean includeFormat, boolean includePolicy) 269 { 270 BasicLogger log = (BasicLogger)o; 271 272 if (element == null) 273 { 274 element = new Element("Logger"); 275 } 276 277 if (log.getName() != null) 279 { 280 element.setAttribute("name", log.getName()); 281 } 282 283 element.setAttribute("class", log.getClass().getName()); 284 285 if (includeFormat) 287 { 288 Object format = log.getTextFormatter(); 289 Element formatterElement = new Element("Format"); 290 formatterElement.setAttribute("class", format.getClass().getName()); 291 292 XMLConfigHelper helper = null; 293 294 try 295 { 296 helper = XMLConfigUtil.getConfigHelper(format); 297 } 298 catch (Exception x) 299 { 300 x.printStackTrace(); 301 } 302 303 formatterElement = helper.getConfiguration(format, formatterElement); 304 305 element.getChildren().add(formatterElement); 306 } 307 308 if (includePolicy) 310 { 311 Object policy = log.getPolicy(); 312 Element policyElement = new Element("Policy"); 313 policyElement.setAttribute("class", policy.getClass().getName()); 314 315 XMLConfigHelper helper = null; 316 317 try 318 { 319 helper = XMLConfigUtil.getConfigHelper(policy); 320 } 321 catch (Exception x) 322 { 323 x.printStackTrace(); 324 } 325 326 policyElement = helper.getConfiguration(policy, policyElement); 327 328 element.getChildren().add(policyElement); 329 } 330 331 return element; 332 } 333 } 334 | Popular Tags |