Source Code Cross Referenced for StringManager.java in  » Sevlet-Container » apache-tomcat-6.0.14 » org » apache » catalina » util » 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 » Sevlet Container » apache tomcat 6.0.14 » org.apache.catalina.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.catalina.util;
019:
020:        import java.text.MessageFormat;
021:        import java.util.Hashtable;
022:        import java.util.Locale;
023:        import java.util.MissingResourceException;
024:        import java.util.ResourceBundle;
025:        import java.net.URLClassLoader;
026:
027:        /**
028:         * An internationalization / localization helper class which reduces
029:         * the bother of handling ResourceBundles and takes care of the
030:         * common cases of message formating which otherwise require the
031:         * creation of Object arrays and such.
032:         *
033:         * <p>The StringManager operates on a package basis. One StringManager
034:         * per package can be created and accessed via the getManager method
035:         * call.
036:         *
037:         * <p>The StringManager will look for a ResourceBundle named by
038:         * the package name given plus the suffix of "LocalStrings". In
039:         * practice, this means that the localized information will be contained
040:         * in a LocalStrings.properties file located in the package
041:         * directory of the classpath.
042:         *
043:         * <p>Please see the documentation for java.util.ResourceBundle for
044:         * more information.
045:         *
046:         * @author James Duncan Davidson [duncan@eng.sun.com]
047:         * @author James Todd [gonzo@eng.sun.com]
048:         */
049:
050:        public class StringManager {
051:
052:            /**
053:             * The ResourceBundle for this StringManager.
054:             */
055:
056:            private ResourceBundle bundle;
057:
058:            private static org.apache.juli.logging.Log log = org.apache.juli.logging.LogFactory
059:                    .getLog(StringManager.class);
060:
061:            /**
062:             * Creates a new StringManager for a given package. This is a
063:             * private method and all access to it is arbitrated by the
064:             * static getManager method call so that only one StringManager
065:             * per package will be created.
066:             *
067:             * @param packageName Name of package to create StringManager for.
068:             */
069:
070:            private StringManager(String packageName) {
071:                String bundleName = packageName + ".LocalStrings";
072:                try {
073:                    bundle = ResourceBundle.getBundle(bundleName);
074:                    return;
075:                } catch (MissingResourceException ex) {
076:                    // Try from the current loader ( that's the case for trusted apps )
077:                    ClassLoader cl = Thread.currentThread()
078:                            .getContextClassLoader();
079:                    if (cl != null) {
080:                        try {
081:                            bundle = ResourceBundle.getBundle(bundleName,
082:                                    Locale.getDefault(), cl);
083:                            return;
084:                        } catch (MissingResourceException ex2) {
085:                        }
086:                    }
087:                    if (cl == null)
088:                        cl = this .getClass().getClassLoader();
089:
090:                    if (log.isDebugEnabled())
091:                        log.debug("Can't find resource " + bundleName + " "
092:                                + cl);
093:                    if (cl instanceof  URLClassLoader) {
094:                        if (log.isDebugEnabled())
095:                            log.debug(((URLClassLoader) cl).getURLs());
096:                    }
097:                }
098:            }
099:
100:            /**
101:             * Get a string from the underlying resource bundle.
102:             *
103:             * @param key The resource name
104:             */
105:            public String getString(String key) {
106:                return MessageFormat.format(getStringInternal(key),
107:                        (Object[]) null);
108:            }
109:
110:            protected String getStringInternal(String key) {
111:                if (key == null) {
112:                    String msg = "key is null";
113:
114:                    throw new NullPointerException(msg);
115:                }
116:
117:                String str = null;
118:
119:                if (bundle == null)
120:                    return key;
121:                try {
122:                    str = bundle.getString(key);
123:                } catch (MissingResourceException mre) {
124:                    str = "Cannot find message associated with key '" + key
125:                            + "'";
126:                }
127:
128:                return str;
129:            }
130:
131:            /**
132:             * Get a string from the underlying resource bundle and format
133:             * it with the given set of arguments.
134:             *
135:             * @param key The resource name
136:             * @param args Formatting directives
137:             */
138:
139:            public String getString(String key, Object[] args) {
140:                String iString = null;
141:                String value = getStringInternal(key);
142:
143:                // this check for the runtime exception is some pre 1.1.6
144:                // VM's don't do an automatic toString() on the passed in
145:                // objects and barf out
146:
147:                try {
148:                    // ensure the arguments are not null so pre 1.2 VM's don't barf
149:                    Object nonNullArgs[] = args;
150:                    for (int i = 0; i < args.length; i++) {
151:                        if (args[i] == null) {
152:                            if (nonNullArgs == args)
153:                                nonNullArgs = (Object[]) args.clone();
154:                            nonNullArgs[i] = "null";
155:                        }
156:                    }
157:
158:                    iString = MessageFormat.format(value, nonNullArgs);
159:                } catch (IllegalArgumentException iae) {
160:                    StringBuffer buf = new StringBuffer();
161:                    buf.append(value);
162:                    for (int i = 0; i < args.length; i++) {
163:                        buf.append(" arg[" + i + "]=" + args[i]);
164:                    }
165:                    iString = buf.toString();
166:                }
167:                return iString;
168:            }
169:
170:            /**
171:             * Get a string from the underlying resource bundle and format it
172:             * with the given object argument. This argument can of course be
173:             * a String object.
174:             *
175:             * @param key The resource name
176:             * @param arg Formatting directive
177:             */
178:
179:            public String getString(String key, Object arg) {
180:                Object[] args = new Object[] { arg };
181:                return getString(key, args);
182:            }
183:
184:            /**
185:             * Get a string from the underlying resource bundle and format it
186:             * with the given object arguments. These arguments can of course
187:             * be String objects.
188:             *
189:             * @param key The resource name
190:             * @param arg1 Formatting directive
191:             * @param arg2 Formatting directive
192:             */
193:
194:            public String getString(String key, Object arg1, Object arg2) {
195:                Object[] args = new Object[] { arg1, arg2 };
196:                return getString(key, args);
197:            }
198:
199:            /**
200:             * Get a string from the underlying resource bundle and format it
201:             * with the given object arguments. These arguments can of course
202:             * be String objects.
203:             *
204:             * @param key The resource name
205:             * @param arg1 Formatting directive
206:             * @param arg2 Formatting directive
207:             * @param arg3 Formatting directive
208:             */
209:
210:            public String getString(String key, Object arg1, Object arg2,
211:                    Object arg3) {
212:                Object[] args = new Object[] { arg1, arg2, arg3 };
213:                return getString(key, args);
214:            }
215:
216:            /**
217:             * Get a string from the underlying resource bundle and format it
218:             * with the given object arguments. These arguments can of course
219:             * be String objects.
220:             *
221:             * @param key The resource name
222:             * @param arg1 Formatting directive
223:             * @param arg2 Formatting directive
224:             * @param arg3 Formatting directive
225:             * @param arg4 Formatting directive
226:             */
227:
228:            public String getString(String key, Object arg1, Object arg2,
229:                    Object arg3, Object arg4) {
230:                Object[] args = new Object[] { arg1, arg2, arg3, arg4 };
231:                return getString(key, args);
232:            }
233:
234:            // --------------------------------------------------------------
235:            // STATIC SUPPORT METHODS
236:            // --------------------------------------------------------------
237:
238:            private static Hashtable managers = new Hashtable();
239:
240:            /**
241:             * Get the StringManager for a particular package. If a manager for
242:             * a package already exists, it will be reused, else a new
243:             * StringManager will be created and returned.
244:             *
245:             * @param packageName The package name
246:             */
247:
248:            public synchronized static StringManager getManager(
249:                    String packageName) {
250:                StringManager mgr = (StringManager) managers.get(packageName);
251:
252:                if (mgr == null) {
253:                    mgr = new StringManager(packageName);
254:                    managers.put(packageName, mgr);
255:                }
256:                return mgr;
257:            }
258:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.