1 7 package com.inversoft.verge.repository; 8 9 10 import java.util.Enumeration ; 11 import java.util.Properties ; 12 13 import javax.servlet.ServletContext ; 14 import javax.servlet.ServletRequest ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpSession ; 17 18 import org.apache.log4j.Logger; 19 20 import com.inversoft.beans.BeanException; 21 import com.inversoft.beans.JavaBean; 22 import com.inversoft.util.typeconverter.TypeConversionException; 23 import com.inversoft.verge.repository.config.Config; 24 import com.inversoft.verge.repository.config.RepositoryConfigRegistry; 25 import com.inversoft.verge.util.ScopeConstants; 26 27 28 38 public class Repository { 39 40 43 private static final Logger logger = Logger.getLogger(Repository.class); 44 45 50 private Repository() { 51 } 52 53 54 58 private static Repository instance = new Repository(); 59 60 64 public static Repository getInstance() { 65 return instance; 66 } 67 68 69 73 74 86 public Object lookupItem(HttpServletRequest request, String id) { 87 return lookupItem(request, id, true); 88 } 89 90 111 public Object lookupItem(HttpServletRequest request, String id, 112 boolean createRequest) { 113 114 Config config = RepositoryConfigRegistry.getInstance(request).lookup(id); 115 if (config == null) { 116 throw new RepositoryException("Item with id of: " + id + 117 " was not defined in the config file"); 118 } 119 120 return lookupItem(request, config, createRequest); 121 } 122 123 135 public Object lookupItem(HttpServletRequest request, Config config) { 136 return lookupItem(request, config, true); 137 } 138 139 160 public Object lookupItem(HttpServletRequest request, Config config, 161 boolean createRequest) { 162 163 if (logger.isDebugEnabled()) { 164 logger.debug("Looking up item with an ID of: " + config.getID()); 165 logger.debug("Looking up item with an scope of: " + config.getScopeStr()); 166 } 167 168 int scope = config.getScope(); 169 boolean requestScope = false; 170 boolean sessionScope = false; 171 boolean applicationScope = false; 172 173 switch (scope) { 175 case ScopeConstants.REQUEST_INT: 176 requestScope = true; 177 logger.debug("Item is request scoped"); 178 break; 179 case ScopeConstants.SESSION_INT: 180 sessionScope = true; 181 logger.debug("Item is session scoped"); 182 break; 183 case ScopeConstants.APPLICATION_INT: 184 applicationScope = true; 185 logger.debug("Item is application scoped"); 186 break; 187 default: 188 logger.error("Item config contains an invalid scope"); 189 throw new RepositoryException("Item config contains an invalid scope"); 190 } 191 192 if (applicationScope) { 193 return lookupItemSync(request, config); 194 } 195 196 Object item = null; 198 String id = config.getID(); 199 HttpSession session = request.getSession(); 200 if (requestScope) { 201 item = request.getAttribute(id); 202 } else if (sessionScope) { 203 item = session.getAttribute(id); 204 } else { 205 assert (false) : "Should never get into this block. Application " + 206 "look ups should be synchronized"; 207 } 208 209 if (item == null) { 210 211 logger.debug("Creating item"); 212 213 if (!createRequest && requestScope) { 215 return null; 216 } 217 218 try { 219 Class klass = config.getBeanClass(); 220 item = klass.newInstance(); 221 logger.debug("Instantiating item"); 222 } catch (Exception e) { 223 logger.error("Error instantiating item", e); 224 throw new RepositoryException("Error instantiating item", e); 225 } 226 227 logger.debug("Storing item"); 229 if (requestScope) { 230 request.setAttribute(id, item); 231 } else if (sessionScope) { 232 session.setAttribute(id, item); 233 } else { 234 throw new AssertionError ("Should never get into this block. " + 235 "Application look ups should be synchronized"); 236 } 237 238 populateProperties(request, config, item); 240 populateReferences(request, config, item); 241 242 logger.debug("Finished creating item"); 243 } 244 245 logger.debug("Finished looking up item"); 246 247 return item; 248 } 249 250 263 Object lookupItemSync(HttpServletRequest request, Config config) { 264 logger.debug("Looking up item"); 265 266 Object item = null; 268 String id = config.getID(); 269 ServletContext context = request.getSession().getServletContext(); 270 271 synchronized (context) { 275 item = context.getAttribute(id); 276 277 if (item == null) { 278 279 logger.debug("Creating item"); 280 281 try { 282 Class klass = config.getBeanClass(); 283 item = klass.newInstance(); 284 logger.debug("Instantiating item"); 285 } catch (Exception e) { 286 logger.error("Error instantiating item", e); 287 throw new RepositoryException("Error instantiating item", e); 288 } 289 290 context.setAttribute(id, item); 291 292 populateProperties(request, config, item); 294 populateReferences(request, config, item); 295 296 logger.debug("Finished creating item"); 297 } 298 } 299 300 logger.debug("Finished looking up item"); 301 302 return item; 303 } 304 305 318 void populateProperties(HttpServletRequest request, Config config, 319 Object item) throws RepositoryException { 320 logger.debug("Populating item properties"); 321 322 Properties props = config.getProperties(); 323 JavaBean javaBean = config.getJavaBean(); 324 Enumeration names; 325 String name; 326 String value; 327 328 if (logger.isDebugEnabled()) { 330 logger.debug("Item has " + props.size() + " properties"); 331 } 332 333 if (props.size() > 0) { 334 335 names = props.propertyNames(); 337 while (names.hasMoreElements()) { 338 name = (String ) names.nextElement(); 339 value = props.getProperty(name); 340 341 try { 342 if (logger.isDebugEnabled()) { 343 logger.debug("Setting property named: " + name + " with value: " 344 + value + " for item with id of: " + config.getID()); 345 } 346 javaBean.setPropertyValue(name, item, value, true); 348 } catch (BeanException be) { 349 throw new RepositoryException(be); 350 } catch (TypeConversionException tce) { 351 throw new RepositoryException(tce); 352 } 353 } 354 } 355 356 logger.debug("Finished populating item properties"); 357 } 358 359 371 void populateReferences(HttpServletRequest request, Config config, 372 Object item) throws RepositoryException { 373 JavaBean javaBean = config.getJavaBean(); 374 Properties refs = config.getReferences(); 375 Enumeration names; 376 Object reference; 377 String name, value; 378 379 if (refs != null && refs.size() > 0) { 380 381 names = refs.propertyNames(); 382 383 while (names.hasMoreElements()) { 384 name = (String ) names.nextElement(); 385 value = refs.getProperty(name); 386 387 try { 388 if (logger.isDebugEnabled()) { 389 logger.debug("Setting reference property: " + name + 390 " with value: " + value + " for item with id of: " + 391 config.getID()); 392 } 393 394 reference = lookupItem(request, value); 396 logger.debug("Found reference object"); 397 398 javaBean.setPropertyValue(name, item, reference, false); 400 } catch (BeanException be) { 401 throw new RepositoryException(be); 402 } catch (RepositoryException re) { 403 throw new RepositoryException("Error resolving reference named: " 404 + value, re); 405 } catch (TypeConversionException tce) { 406 throw new RepositoryException(tce); 407 } 408 } 409 } 410 } 411 412 419 public boolean isValidItem(ServletRequest request, String id) { 420 return (RepositoryConfigRegistry.getInstance(request).lookup(id) != null); 421 } 422 423 432 public boolean isItemLoaded(HttpServletRequest request, String id) { 433 434 Config config = RepositoryConfigRegistry.getInstance(request).lookup(id); 435 if (config == null) { 436 return false; 437 } 438 439 int scope = config.getScope(); 441 switch (scope) { 442 case ScopeConstants.REQUEST_INT: 443 logger.debug("Item is request scoped"); 444 return (request.getAttribute(id) != null); 445 case ScopeConstants.SESSION_INT: 446 logger.debug("Item is session scoped"); 447 return (request.getSession().getAttribute(id) != null); 448 case ScopeConstants.APPLICATION_INT: 449 return isItemLoadedSync(request, id); 450 } 451 452 logger.error("Item config contains an invalid scope"); 453 throw new RepositoryException("Item config contains an invalid scope"); 454 } 455 456 465 boolean isItemLoadedSync(HttpServletRequest request, String id) { 466 467 logger.debug("Item is application scoped"); 468 ServletContext context = request.getSession().getServletContext(); 469 synchronized (context) { 470 return (context.getAttribute(id) != null); 471 } 472 } 473 474 475 481 public Config lookupConfig(HttpServletRequest request, String id) { 482 return RepositoryConfigRegistry.getInstance(request).lookup(id); 483 } 484 } 485 | Popular Tags |