1 package com.protomatter.syslog; 2 3 52 53 import java.util.*; 54 import java.text.MessageFormat ; 55 import org.apache.oro.text.regex.*; 56 57 88 public class PerChannelPolicy 89 extends SimpleLogPolicy 90 { 91 private List groupList = new ArrayList(); 92 private static Perl5Compiler patternCompiler = new Perl5Compiler(); 93 94 97 public PerChannelPolicy() 98 { 99 super(); 100 removeAllChannels(); 101 setLogMask(0); 102 } 103 104 107 public List getGroupList() 108 { 109 return this.groupList; 110 } 111 114 public void setGroupList(List list) 115 { 116 this.groupList = list; 117 } 118 119 132 public boolean shouldLog(SyslogMessage message) 133 { 134 if (message.channel != null) 136 { 137 int size = groupList.size(); 138 PolicyGroup group = null; 139 for (int i=0; i<size; i++) 140 { 141 group = (PolicyGroup)groupList.get(i); 142 if (group.shouldLog(message)) 143 return true; 144 } 145 } 146 147 return false; 148 } 149 150 153 public Iterator getPolicyGroups() 154 { 155 return this.groupList.iterator(); 156 } 157 158 161 public void addPolicyGroup(PolicyGroup group) 162 { 163 this.groupList.add(group); 164 } 165 166 169 public void removePolicyGroup(PolicyGroup group) 170 { 171 this.groupList.remove(group); 172 } 173 174 182 public static class PolicyGroup 183 extends SimpleLogPolicy 184 { 185 private Set patternSet = new HashSet(); 186 private Set channelNameSet = new HashSet(); 187 private Pattern pattern = null; 188 private Object lock = new Object (); 189 private Perl5Matcher patternMatcher = new Perl5Matcher(); 190 191 194 public PolicyGroup() 195 { 196 super(); 197 removeAllChannels(); 198 setLogMask(Syslog.DEBUG); 199 } 200 201 204 public Iterator getChannelSet() 205 { 206 return this.channelNameSet.iterator(); 207 } 208 209 213 public void clearChannelSet() 214 { 215 this.channelNameSet = new HashSet(); 216 } 217 218 222 public void addChannelName(String channelname) 223 { 224 this.channelNameSet.add(channelname); 225 } 226 227 231 public void removeChannelName(String channelname) 232 { 233 this.channelNameSet.remove(channelname); 234 } 235 236 239 public Iterator getPatternSet() 240 { 241 return this.patternSet.iterator(); 242 } 243 244 248 public void clearPatternSet() 249 { 250 this.patternSet = new HashSet(); 251 this.pattern = null; 252 } 253 254 258 public void addChannelPattern(String channelname) 259 { 260 this.patternSet.add(channelname); 261 generatePattern(); 262 } 263 264 268 public void removeChannelPattern(String channelname) 269 { 270 this.patternSet.remove(channelname); 271 generatePattern(); 272 } 273 274 278 private void generatePattern() 279 throws IllegalArgumentException 280 { 281 StringBuffer thePattern = new StringBuffer (); 282 try 283 { 284 Iterator patterns = getPatternSet(); 285 while (patterns.hasNext()) 286 { 287 thePattern.append(patterns.next()); 288 if (patterns.hasNext()) 289 thePattern.append("|"); 290 } 291 pattern = patternCompiler.compile(thePattern.toString()); 292 } 293 catch (MalformedPatternException x) 294 { 295 throw new IllegalArgumentException ( 296 MessageFormat.format(Syslog.getResources().getString(MessageConstants.INVALID_PATTERN_MESSAGE), 297 new Object [] { thePattern })); 298 } 299 } 300 301 308 public boolean shouldLog(SyslogMessage m) 309 { 310 if (inMask(m.level, getLogMask())) 312 { 313 if (channelNameSet.contains(m.channel)) 315 { 316 return true; 317 } 318 else if (pattern != null) { 320 synchronized (lock) 321 { 322 if (patternMatcher.contains(m.channel, pattern)) 323 { 324 return true; 325 } 326 } 327 } 328 } 329 330 return false; 332 } 333 } 334 } 335 | Popular Tags |