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 com.jcorporate.expresso.core.security.User; 72 import org.apache.commons.collections.LRUMap; 73 import org.apache.oro.text.regex.MalformedPatternException; 74 import org.apache.oro.text.regex.Pattern; 75 import org.apache.oro.text.regex.PatternCompiler; 76 import org.apache.oro.text.regex.PatternMatcher; 77 import org.apache.oro.text.regex.Perl5Compiler; 78 import org.apache.oro.text.regex.Perl5Matcher; 79 80 import java.util.Enumeration ; 81 import java.util.Iterator ; 82 import java.util.Map ; 83 import java.util.Vector ; 84 85 87 88 100 public class ControllerXSLMap 101 extends SecuredDBObject { 102 private static final String thisClass = ControllerXSLMap.class.getName() + "."; 103 104 108 protected static Map regExpCache = null; 109 110 113 protected static PatternCompiler compiler = new Perl5Compiler(); 114 115 118 protected static PatternMatcher matcher = new Perl5Matcher(); 119 120 123 public ControllerXSLMap() 124 throws DBException { 125 super(); 126 } 127 128 133 public ControllerXSLMap(int uid) 134 throws DBException { 135 super(uid); 136 } 137 138 143 private synchronized void createObjectCache(ControllerXSLMap mapList) { 144 try { 145 if (regExpCache == null) { 146 int numMatches = mapList.count(); 147 148 if (numMatches > 60) { 151 numMatches = 60; 152 } 153 154 regExpCache = new LRUMap(numMatches); 155 } 156 } catch (DBException dbe) { 157 regExpCache = new LRUMap(60); 158 } 159 } 160 161 169 private synchronized Pattern getPattern(String controllerClass) 170 throws DBException { 171 Pattern p = (Pattern) regExpCache.get(controllerClass); 172 173 if (p == null) { 174 try { 175 p = compiler.compile(controllerClass, Perl5Compiler.READ_ONLY_MASK); 176 regExpCache.put(controllerClass, p); 177 } catch (MalformedPatternException mpe) { 178 throw new DBException(thisClass + "getPattern(String)", mpe); 179 } 180 } 181 182 return p; 183 } 184 185 200 public synchronized boolean getMatch(String userName, 201 String controllerClassParam, 202 int userAgentId) 203 throws DBException { 204 205 206 209 214 215 User oneUser = new User(); 216 oneUser.setDataContext(getDataContext()); 217 218 oneUser.setLoginName(userName); 220 221 oneUser.find(); 223 224 Vector groupList = oneUser.getGroups(); 225 ControllerXSLMap oneMap = null; 226 ControllerXSLMap mapList = new ControllerXSLMap(); 227 229 mapList.setField("UserAgent", userAgentId); 231 mapList.setDataContext(getDataContext()); 232 createObjectCache(mapList); 233 234 for (Iterator i = mapList.searchAndRetrieveList("MatchSeq").iterator(); 235 i.hasNext();) { 236 oneMap = (ControllerXSLMap) i.next(); 237 238 String oneGroup = null; 239 240 for (Enumeration eg = groupList.elements(); eg.hasMoreElements();) { 241 oneGroup = (String ) eg.nextElement(); 242 243 if (oneGroup.equals(oneMap.getField("GroupName"))) { 244 String controllerClass = oneMap.getField("ControllerClass"); 245 Pattern compiledRegExp = getPattern(controllerClass); 246 247 boolean result; 248 synchronized (matcher) { 249 result = matcher.matches(controllerClassParam, 250 compiledRegExp); 251 252 } 253 if (result) { 254 257 setField("MatchNumber", 258 oneMap.getField("MatchNumber")); 259 retrieve(); 260 261 return true; 262 } 263 264 265 } 266 267 } 268 269 } 270 271 return false; 272 } 273 274 275 286 public synchronized boolean getMatch(String userName, String controllerClassName, 287 String userAgentString) 288 throws DBException { 289 290 UserAgent ua = new UserAgent(); 291 ua.setDataContext(getDataContext()); 292 ua.getMatch(userAgentString); 293 294 295 User oneUser = new User(); 296 oneUser.setDataContext(getDataContext()); 297 oneUser.setLoginName(userName); 298 if (!oneUser.find()) { 300 throw new DBException("User not found: '" + userName + "'"); 301 } 302 303 Vector groupList = oneUser.getGroups(); 304 ControllerXSLMap oneMap = null; 305 ControllerXSLMap mapList = new ControllerXSLMap(); 306 mapList.setField("UserAgent", ua.getField("UserAgent")); 307 mapList.setDataContext(getDataContext()); 308 createObjectCache(mapList); 309 310 for (Iterator i = mapList.searchAndRetrieveList("MatchSeq").iterator(); 311 i.hasNext();) { 312 oneMap = (ControllerXSLMap) i.next(); 313 314 String oneGroup = null; 315 316 for (Enumeration eg = groupList.elements(); eg.hasMoreElements();) { 317 oneGroup = (String ) eg.nextElement(); 318 319 if (oneGroup.equals(oneMap.getField("GroupName"))) { 320 String controllerClass = oneMap.getField("ControllerClass"); 321 Pattern compiledRegExp = getPattern(controllerClass); 322 323 boolean result; 324 synchronized (matcher) { 325 result = matcher.matches(controllerClassName, compiledRegExp); 326 } 327 if (result) { 328 setField("MatchNumber", oneMap.getField("MapNumber")); 329 retrieve(); 330 331 return true; 332 } 333 334 } 335 336 } 337 338 } 339 340 return false; 341 } 342 343 344 350 public DBObject getThisDBObj() 351 throws DBException { 352 return (DBObject) new ControllerXSLMap(); 353 } 354 355 356 364 public synchronized Vector getValidValues(String fieldName) 365 throws DBException { 366 if (fieldName.equals("Protocol")) { 367 Vector myValues = new Vector (4); 368 myValues.addElement(new ValidValue("http", "HTTP")); 369 myValues.addElement(new ValidValue("jobqueue", "Via Job Queue")); 370 371 return myValues; 372 } 373 374 return super.getValidValues(fieldName); 375 } 376 377 378 383 public void setupFields() 384 throws DBException { 385 setTargetTable("CTLXSLMAP"); 386 setDescription("Controller/XSL Mapping Entries"); 387 setCharset("ISO-8859-1"); 388 addField("MatchNumber", "int", 0, false, "Match Number"); 389 addField("ControllerClass", "text", 0, false, 390 "Controller Class Pattern"); 391 addField("MatchSeq", "int", 0, false, "Match Sequence"); 392 addField("GroupName", "varchar", 10, false, "User Group"); 393 addField("XSLFileName", "text", 0, false, "XSL File Name"); 394 addField("UserAgent", "int", 0, false, "User-Agent/Browser"); 395 addField("Param", "text", 0, false, "Additional Parameters"); 396 addKey("MatchNumber"); 397 } 398 399 400 } 401 402 | Popular Tags |