1 18 package net.sf.uitags.tagutil.i18n; 19 20 import javax.servlet.ServletRequest ; 21 import javax.servlet.http.HttpSession ; 22 import javax.servlet.jsp.PageContext ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import net.sf.uitags.tagutil.TaglibProperties; 28 import net.sf.uitags.util.IllegalPropertyValueException; 29 30 36 public final class MessageFinderFactory { 37 40 private static final Log log = LogFactory.getLog(MessageFinderFactory.class); 41 45 private static final String MESSAGE_FINDER = "common.message.finder"; 46 49 private static Class finderClass; 50 51 54 private MessageFinderFactory() { 55 super(); 56 } 57 58 72 public static MessageFinder getInstance(PageContext pageContext) { 73 MessageFinder finder = findFromScope(pageContext); 75 76 if (finder == null) { 77 try { 80 if (finderClass == null) { 81 String finderName = TaglibProperties.MERGED.get(MESSAGE_FINDER); 82 finderClass = Class.forName(finderName); 83 84 if (log.isDebugEnabled()) { 85 log.debug("Message finder found: '" + finderName + "'."); 86 } 87 } 88 89 finder = (MessageFinder) finderClass.newInstance(); 90 saveToScope(pageContext, finder); 91 } 92 catch (ClassNotFoundException e) { 93 new IllegalPropertyValueException(e); 94 } 95 catch (IllegalAccessException e) { 96 new IllegalPropertyValueException(e); 97 } 98 catch (InstantiationException e) { 99 new IllegalPropertyValueException(e); 100 } 101 } 102 103 finder.setPageContext(pageContext); 104 return finder; 105 } 106 107 116 private static MessageFinder findFromScope(PageContext pageContext) { 117 HttpSession session = pageContext.getSession(); 118 MessageFinder ret = null; 119 if (session != null) { 120 ret = (MessageFinder) session.getAttribute(MessageFinder.REUSED_INSTANCE); 121 } 122 123 if (ret == null) { 124 ret = (MessageFinder) pageContext.getRequest(). 125 getAttribute(MessageFinder.REUSED_INSTANCE); 126 } 127 128 return ret; 129 } 130 131 138 private static void saveToScope(PageContext pageContext, 139 MessageFinder msgFinder) { 140 HttpSession session = pageContext.getSession(); 141 if (session != null) { 142 session.setAttribute(MessageFinder.REUSED_INSTANCE, msgFinder); 143 } 144 else { 145 ServletRequest request = pageContext.getRequest(); 146 request.setAttribute(MessageFinder.REUSED_INSTANCE, msgFinder); 147 } 148 } 149 } 150 | Popular Tags |