KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > i18n > MessageFinderFactory


1 /**
2  * Nov 22, 2004
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.tagutil.i18n;
19
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
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 /**
31  * Returns a message finder instance of the appropriate type.
32  *
33  * @author jonni
34  * @version $Id$
35  */

36 public final class MessageFinderFactory {
37   /**
38    * Logger.
39    */

40   private static final Log log = LogFactory.getLog(MessageFinderFactory.class);
41   /**
42    * The key of the property which specifies the type of the message finder
43    * to use
44    */

45   private static final String JavaDoc MESSAGE_FINDER = "common.message.finder";
46   /**
47    * Shared message finder class
48    */

49   private static Class JavaDoc finderClass;
50
51   /**
52    * Non-instantiable by client.
53    */

54   private MessageFinderFactory() {
55     super();
56   }
57
58   /**
59    * Returns a message finder whose actual type is specified in the
60    * configuration properties file. This factory reuses and stores a message
61    * finder in session. If session doesn't exist, it reads and writes to
62    * request instead.
63    *
64    * @param pageContext to initialize the <code>MessageFinder</code> to be
65    * returned
66    * @return a message finder
67    * @throws ClassCastException if the message finder specified in the
68    * configuration file does not implement <code>MessageFinder</code>.
69    * @throws IllegalPropertyValueException if the message finder specified
70    * in the configuration file is not instantiable for any reason
71    */

72   public static MessageFinder getInstance(PageContext JavaDoc pageContext) {
73     // Return a message finder stored in the session/request scope if one exists
74
MessageFinder finder = findFromScope(pageContext);
75
76     if (finder == null) {
77       // No message finder found in scope, instantiate one and store
78
// in appropriate scope.
79
try {
80         if (finderClass == null) {
81           String JavaDoc 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 JavaDoc e) {
93         new IllegalPropertyValueException(e);
94       }
95       catch (IllegalAccessException JavaDoc e) {
96         new IllegalPropertyValueException(e);
97       }
98       catch (InstantiationException JavaDoc e) {
99         new IllegalPropertyValueException(e);
100       }
101     }
102
103     finder.setPageContext(pageContext);
104     return finder;
105   }
106
107   /**
108    * Attemps to find a message finder from the session scope. If session
109    * doesn't exist, request scope is attempted. If that also fails, returns
110    * <code>null</code>.
111    *
112    * @param pageContext allows access to scopes
113    * @return message finder from session/request, or <code>null</code> if
114    * not found
115    */

116   private static MessageFinder findFromScope(PageContext JavaDoc pageContext) {
117     HttpSession JavaDoc 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   /**
132    * Attempts to save a message finder to session scope. If session doesn't
133    * exist, saves to the request scope.
134    *
135    * @param pageContext allows access to scopes
136    * @param msgFinder message finder to save
137    */

138   private static void saveToScope(PageContext JavaDoc pageContext,
139       MessageFinder msgFinder) {
140     HttpSession JavaDoc session = pageContext.getSession();
141     if (session != null) {
142       session.setAttribute(MessageFinder.REUSED_INSTANCE, msgFinder);
143     }
144     else {
145       ServletRequest JavaDoc request = pageContext.getRequest();
146       request.setAttribute(MessageFinder.REUSED_INSTANCE, msgFinder);
147     }
148   }
149 }
150
Popular Tags