Source Code Cross Referenced for JavaTemplateContextMapImpl.java in  » UML » MetaBoss » com » metaboss » javatemplate » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » UML » MetaBoss » com.metaboss.javatemplate 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.