001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.javatemplate;
016:
017: import java.util.Map;
018: import java.util.StringTokenizer;
019:
020: import javax.naming.Context;
021: import javax.naming.InitialContext;
022: import javax.naming.NameNotFoundException;
023: import javax.naming.NamingException;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: /** This class is the simplest possible implementation of the JavaTemplateContext.
029: * It implements the context container backed by a java.util.Map */
030: public class JavaTemplateContextMapImpl implements JavaTemplateContext {
031: // Commons Logging instance.
032: private static final Log sLogger = LogFactory
033: .getLog(JavaTemplateContextMapImpl.class);
034: private static final Object sMandatoryLookupMarker = new Object();
035: private Map mContextMap = null;
036: private Context mNamingContext = null;
037:
038: /** Constructs the context implementation based on context map */
039: public JavaTemplateContextMapImpl(Map pContextMap)
040: throws JavaTemplateException {
041: mContextMap = pContextMap;
042: try {
043: mNamingContext = new InitialContext();
044: } catch (NamingException e) {
045: throw new JavaTemplateException(
046: "Unable to establish initial naming context for Url lookups. Original exception is "
047: + e.getMessage());
048: }
049: }
050:
051: /** Returns object with specified path name or supplied default object, if
052: * object with this name is not found in context. */
053: public Object getObject(String pContextPath, Object pDefaultObject)
054: throws JavaTemplateException {
055: if (pContextPath.indexOf(":/") > 0) {
056: sLogger.debug("Looking for object with url: "
057: + pContextPath);
058: try {
059: Object lValue = mNamingContext.lookup(pContextPath);
060: if (lValue != null) {
061: sLogger.debug("Url '" + pContextPath
062: + "' is bound. Returning " + lValue);
063: return lValue;
064: }
065: } catch (NameNotFoundException e) {
066: // Ignore this one - will fall through and will return default
067: } catch (NamingException e) {
068: throw new JavaTemplateNamingAndDirectoryServiceInvocationException(
069: "Error occurred while looking up url: "
070: + pContextPath, e);
071: }
072: // Nothing has been found. See if we need to throw exception or return default
073: if (pDefaultObject == sMandatoryLookupMarker)
074: throw new JavaTemplateException("Mandatory url '"
075: + pContextPath + "' is not bound.");
076: if (pDefaultObject == null)
077: sLogger
078: .debug("Url '"
079: + pContextPath
080: + "' is not bound. Returning default object: <null>");
081: else
082: sLogger.debug("Url '" + pContextPath
083: + "' is not bound. Returning default object: "
084: + pDefaultObject.toString());
085: return pDefaultObject; // Did not get the full path
086: }
087: sLogger.debug("Looking for object with local path: "
088: + pContextPath);
089: // Disassemble the name going down the map tree
090: Map lCurrentLevelMap = mContextMap;
091: StringTokenizer lTokenizer = new StringTokenizer(pContextPath,
092: ".", false);
093: if (!lTokenizer.hasMoreTokens())
094: throw new java.lang.IllegalArgumentException(
095: "pContextPath parameter does not contain correctly formed value name");
096: while (true) {
097: String lToken = lTokenizer.nextToken();
098: Object lValue = lCurrentLevelMap.get(lToken);
099: if (lValue != null) {
100: if (lTokenizer.hasMoreTokens()) {
101: if (lValue instanceof Map) {
102: // This is just a name of the sub map - get it
103: lCurrentLevelMap = (Map) lValue;
104: } else
105: // We have a not null element (most definitely string)
106: // but we really need a map because there are still some tokens left
107: // There is only one possibility is that the whole remaining word is stored with one key
108: {
109: // Collect the remainder of the key as token
110: while (lTokenizer.hasMoreTokens()) {
111: lToken += "." + lTokenizer.nextToken();
112: }
113: // Only if we found not null object we will return it
114: if ((lValue = lCurrentLevelMap.get(lToken)) != null) {
115: sLogger
116: .debug("Path '" + pContextPath
117: + "' is bound. Returning "
118: + lValue);
119: return lValue;
120: }
121: // Nothing has been found. See if we need to throw exception or return default
122: if (pDefaultObject == sMandatoryLookupMarker)
123: throw new JavaTemplateException(
124: "Mandatory path '" + pContextPath
125: + "' is not bound.");
126: if (pDefaultObject == null)
127: sLogger
128: .debug("Path '"
129: + pContextPath
130: + "' is not bound. Returning default object: <null>");
131: else
132: sLogger
133: .debug("Path '"
134: + pContextPath
135: + "' is not bound. Returning default object: "
136: + pDefaultObject.toString());
137: return pDefaultObject; // Did not get the full path
138: }
139: } else {
140: sLogger.debug("Path '" + pContextPath
141: + "' is bound. Returning " + lValue);
142: return lValue;
143: }
144: } else {
145: // Could not find anything
146: if (lTokenizer.hasMoreTokens()) {
147: // There is a chance that current map stores whole key
148: // Collect the remainder of the key as token
149: while (lTokenizer.hasMoreTokens()) {
150: lToken += "." + lTokenizer.nextToken();
151: }
152: // Only if we found not null object we will return it
153: if ((lValue = lCurrentLevelMap.get(lToken)) != null) {
154: sLogger.debug("Path '" + pContextPath
155: + "' is bound. Returning " + lValue);
156: return lValue;
157: }
158: }
159: // Nothing has been found. See if we need to throw exception or return default
160: if (pDefaultObject == sMandatoryLookupMarker)
161: throw new JavaTemplateException("Mandatory path '"
162: + pContextPath + "' is not bound.");
163: if (pDefaultObject == null)
164: sLogger
165: .debug("Path '"
166: + pContextPath
167: + "' is not bound. Returning default object: <null>");
168: else
169: sLogger
170: .debug("Path '"
171: + pContextPath
172: + "' is not bound. Returning default object: "
173: + pDefaultObject.toString());
174: return pDefaultObject; // Did not get the full path
175: }
176: }
177: }
178:
179: /** Returns object with specified path name. If object not found
180: * it throws JavaTemplateException with an appropriate message. */
181: public Object getMandatoryObject(String pContextPath)
182: throws JavaTemplateException {
183: return getObject(pContextPath, sMandatoryLookupMarker);
184: }
185:
186: /** Returns string with specified path name or supplied default string, if
187: * string with this name is not found in context. This method will basically
188: * find an object in exact same way as getObject() does and than return toString() from it. */
189: public String getString(String pContextPath, String pDefaultString)
190: throws JavaTemplateException {
191: Object lReturn = getObject(pContextPath, pDefaultString);
192: if (lReturn == null)
193: return null;
194: return lReturn.toString();
195: }
196:
197: /** Returns string with specified path name. If string not found
198: * it throws JavaTemplateException with an appropriate message.
199: * This method will basically find an object in exact same way as getMandatoryObject() does and
200: * than return toString() from it. */
201: public String getMandatoryString(String pContextPath)
202: throws JavaTemplateException {
203: return getObject(pContextPath, sMandatoryLookupMarker)
204: .toString();
205: }
206: }
|