1 64 65 package com.jcorporate.expresso.ext.xml.dbobj; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.dbobj.DBObject; 69 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 70 import com.jcorporate.expresso.core.dbobj.ValidValue; 71 import org.apache.commons.collections.LRUMap; 72 import org.apache.oro.text.regex.MalformedPatternException; 73 import org.apache.oro.text.regex.Pattern; 74 import org.apache.oro.text.regex.PatternCompiler; 75 import org.apache.oro.text.regex.PatternMatcher; 76 import org.apache.oro.text.regex.Perl5Compiler; 77 import org.apache.oro.text.regex.Perl5Matcher; 78 79 import java.util.Iterator ; 80 import java.util.Map ; 81 import java.util.Vector ; 82 83 84 87 95 public class UserAgent 96 extends SecuredDBObject { 97 private static final String thisClass = UserAgent.class.getName() + "."; 98 99 103 protected static Map regExpCache = null; 104 105 108 protected static PatternCompiler compiler = new Perl5Compiler(); 109 110 113 protected static PatternMatcher matcher = new Perl5Matcher(); 114 115 118 public UserAgent() 119 throws DBException { 120 super(); 121 } 122 123 129 public UserAgent(int uid) 130 throws DBException { 131 super(uid); 132 } 133 134 139 private synchronized void createObjectCache(UserAgent agentList) { 140 try { 141 if (regExpCache == null) { 142 int numMatches = agentList.count(); 143 144 if (numMatches > 60) { 147 numMatches = 60; 148 } 149 150 regExpCache = new LRUMap(numMatches); 151 } 152 } catch (DBException dbe) { 153 regExpCache = new LRUMap(60); 154 } 155 } 156 157 164 private synchronized Pattern getPattern(String userAgentPattern) 165 throws DBException { 166 Pattern p = (Pattern) regExpCache.get(userAgentPattern); 167 168 if (p == null) { 169 try { 170 p = compiler.compile(userAgentPattern, Perl5Compiler.READ_ONLY_MASK); 171 regExpCache.put(userAgentPattern, p); 172 } catch (MalformedPatternException mpe) { 173 throw new DBException(thisClass + "getPattern(String)", mpe); 174 } 175 } 176 177 return p; 178 } 179 180 public Vector getValues() 181 throws DBException { 182 return getValuesDefault("UserAgent", "Descrip"); 183 } 184 185 193 public boolean getMatch(String userAgentString) 194 throws DBException { 195 UserAgent oneAgent = null; 196 UserAgent agentList = new UserAgent(); 197 agentList.setDataContext(getDataContext()); 198 createObjectCache(agentList); 199 200 for (Iterator i = agentList.searchAndRetrieveList("MatchSeq").iterator(); 201 i.hasNext();) { 202 oneAgent = (UserAgent) i.next(); 203 204 String uaPattern = oneAgent.getField("UAPattern"); 205 Pattern compiledRegExp = getPattern(uaPattern); 206 207 boolean result; 208 synchronized (matcher) { 209 result = matcher.matches(userAgentString, compiledRegExp); 210 } 211 if (result) { setField("UserAgent", oneAgent.getField("UserAgent")); 213 retrieve(); 214 215 return true; 216 } 217 } 218 219 return false; 220 } 221 222 223 229 public DBObject getThisDBObj() 230 throws DBException { 231 return (DBObject) new UserAgent(); 232 } 233 234 235 243 public synchronized Vector getValidValues(String fieldName) 244 throws DBException { 245 if (fieldName.equals("Protocol")) { 246 Vector myValues = new Vector (4); 247 myValues.addElement(new ValidValue("http", "HTTP")); 248 myValues.addElement(new ValidValue("jobqueue", "Via Job Queue")); 249 250 return myValues; 251 } 252 253 return super.getValidValues(fieldName); 254 } 255 256 257 262 public void setupFields() 263 throws DBException { 264 setTargetTable("USERAGENT"); 265 setDescription("User-Agent Matching"); 266 setCharset("ISO-8859-1"); 267 addField("UserAgent", "auto-inc", 0, false, "User Agent Id"); 268 addField("MatchSeq", "int", 0, false, "Match Sequence"); 269 addField("Descrip", "varchar", 80, false, "Description"); 270 addField("UAPattern", "text", 0, false, "Pattern to Match"); 271 addKey("UserAgent"); 272 setReadOnly("UserAgent"); 273 } 274 275 276 public synchronized void populateDefaultValues() 277 throws DBException { 278 UserAgent defAgent = new UserAgent(); 279 defAgent.setDataContext(getDataContext()); 280 defAgent.setField("UAPattern", ".*"); 281 282 if (!defAgent.find()) { 283 defAgent.setField("Descrip", "Default Agent"); 284 defAgent.setField("MatchSeq", "9999"); 285 defAgent.add(); 286 } 287 } 288 289 290 } 291 292 | Popular Tags |