1 package com.protomatter.syslog; 2 3 52 53 import java.util.*; 54 import java.util.regex.*; 55 import java.text.MessageFormat ; 56 57 86 public class JDK14PerChannelPolicy 87 extends SimpleLogPolicy 88 { 89 private List groupList = new ArrayList(); 90 91 94 public JDK14PerChannelPolicy() 95 { 96 super(); 97 removeAllChannels(); 98 setLogMask(0); 99 } 100 101 104 public List getGroupList() 105 { 106 return this.groupList; 107 } 108 111 public void setGroupList(List list) 112 { 113 this.groupList = list; 114 } 115 116 129 public boolean shouldLog(SyslogMessage message) 130 { 131 if (message.channel != null) 133 { 134 int size = groupList.size(); 135 PolicyGroup group = null; 136 for (int i=0; i<size; i++) 137 { 138 group = (PolicyGroup)groupList.get(i); 139 if (group.shouldLog(message)) 140 return true; 141 } 142 } 143 144 return false; 145 } 146 147 150 public Iterator getPolicyGroups() 151 { 152 return this.groupList.iterator(); 153 } 154 155 158 public void addPolicyGroup(PolicyGroup group) 159 { 160 this.groupList.add(group); 161 } 162 163 166 public void removePolicyGroup(PolicyGroup group) 167 { 168 this.groupList.remove(group); 169 } 170 171 179 public static class PolicyGroup 180 extends SimpleLogPolicy 181 { 182 private Set patternSet = new HashSet(); 183 private Set channelNameSet = new HashSet(); 184 private Pattern pattern = null; 185 186 189 public PolicyGroup() 190 { 191 super(); 192 removeAllChannels(); 193 } 194 195 198 public Iterator getChannelSet() 199 { 200 return this.channelNameSet.iterator(); 201 } 202 203 207 public void clearChannelSet() 208 { 209 this.channelNameSet = new HashSet(); 210 } 211 212 216 public void addChannelName(String channelname) 217 { 218 this.channelNameSet.add(channelname); 219 } 220 221 225 public void removeChannelName(String channelname) 226 { 227 this.channelNameSet.remove(channelname); 228 } 229 230 233 public Iterator getPatternSet() 234 { 235 return this.patternSet.iterator(); 236 } 237 238 242 public void clearPatternSet() 243 { 244 this.patternSet = new HashSet(); 245 this.pattern = null; 246 } 247 248 252 public void addChannelPattern(String channelname) 253 { 254 this.patternSet.add(channelname); 255 generatePattern(); 256 } 257 258 262 public void removeChannelPattern(String channelname) 263 { 264 this.patternSet.remove(channelname); 265 generatePattern(); 266 } 267 268 272 private void generatePattern() 273 throws IllegalArgumentException 274 { 275 StringBuffer thePattern = new StringBuffer (); 276 try 277 { 278 Iterator patterns = getPatternSet(); 279 while (patterns.hasNext()) 280 { 281 thePattern.append(patterns.next()); 282 if (patterns.hasNext()) 283 thePattern.append("|"); 284 } 285 pattern = Pattern.compile(thePattern.toString()); 286 } 287 catch (PatternSyntaxException x) 288 { 289 throw new IllegalArgumentException ( 290 MessageFormat.format(Syslog.getResources().getString(MessageConstants.INVALID_PATTERN_MESSAGE), 291 new Object [] { thePattern })); 292 } 293 } 294 295 302 public boolean shouldLog(SyslogMessage m) 303 { 304 if (super.shouldLog(m)) 306 { 307 if (channelNameSet.contains(m.channel)) 309 { 310 return true; 311 } 312 else if (pattern != null) { 314 return pattern.matcher(m.channel).matches(); 315 } 316 } 317 318 return false; 320 } 321 } 322 } 323 | Popular Tags |