KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > logging > diagnostics > ResourceBundleLocator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.server.logging.diagnostics;
24
25 import java.util.Hashtable JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 /**
30  * A Class to locate the Resource Bundle based on Module Id.
31  *
32  * @author Hemanth Puttaswamy
33  *
34  * Issues:
35  * 1. We need to be able to handle a case where the diagnostic information
36  * may come from multiple resource bundles. Ex: JDO's message ids comes from
37  * 4 different resource bundles.
38  */

39 public class ResourceBundleLocator {
40     private static Hashtable JavaDoc moduleIdToResourceBundleTable;
41
42     private static String JavaDoc[] jdoResourceBundles = {
43         "com.sun.jdo.spi.persistence.support.ejb.ejbc.Bundle",
44         "com.sun.jdo.spi.persistence.generator.database.Bundle",
45         "com.sun.jdo.spi.persistence.support.ejb.ejbqlc.Bundle",
46         "com.sun.jdo.spi.persistence.support.sqlstore.Bundle",
47         "com.sun.jdo.spi.persistence.utility.logging.Bundle" };
48
49     static {
50         moduleIdToResourceBundleTable = new Hashtable JavaDoc( );
51         moduleIdToResourceBundleTable.put( "ADM",
52             "com.sun.logging.enterprise.system.tools.admin.LogStrings" );
53         moduleIdToResourceBundleTable.put( "CONF",
54             "com.sun.logging.enterprise.system.core.config.LogStrings" );
55         moduleIdToResourceBundleTable.put( "DPL",
56             "com.sun.logging.enterprise.system.tools.deployment.LogStrings" );
57         moduleIdToResourceBundleTable.put( "EJB",
58             "com.sun.logging.enterprise.system.container.ejb.LogStrings" );
59         moduleIdToResourceBundleTable.put( "IOP",
60             "com.sun.corba.ee.impl.logging.LogStrings" );
61         moduleIdToResourceBundleTable.put( "JAXR",
62             "com.sun.logging.enterprise.system.webservices.registry.LogDomains" );
63         moduleIdToResourceBundleTable.put( "NAM",
64             "com.sun.logging.enterprise.system.core.naming.LogStrings" );
65         moduleIdToResourceBundleTable.put( "DTX",
66             "com.sun.logging.enterprise.resource.jta.LogStrings" );
67         moduleIdToResourceBundleTable.put( "SYNC",
68             "com.sun.logging.ee.enterprise.system.tools.synchronization.LogStrings" );
69         moduleIdToResourceBundleTable.put( "HADBMG",
70             "com.sun.enterprise.ee.admin.hadbmgmt.LocalStrings" );
71         moduleIdToResourceBundleTable.put( "JAXRPC",
72             "com.sun.logging.enterprise.system.webservices.rpc.LogDomains" );
73         moduleIdToResourceBundleTable.put( "JML",
74             "com.sun.logging.enterprise.resource.javamail.LogStrings");
75         moduleIdToResourceBundleTable.put( "JMS",
76             "com.sun.logging.enterprise.resource.jms.LogStrings");
77         moduleIdToResourceBundleTable.put( "JTS",
78             "com.sun.logging.enterprise.system.core.transaction.LogStrings");
79         moduleIdToResourceBundleTable.put( "LDR",
80             "com.sun.logging.enterprise.system.core.classloading.LogStrings");
81         moduleIdToResourceBundleTable.put( "MDB",
82             "com.sun.logging.enterprise.system.container.ejb.mdb.LogStrings" );
83         moduleIdToResourceBundleTable.put( "JNDI",
84             "com.sun.logging.enterprise.system.core.naming.LogStrings" );
85         moduleIdToResourceBundleTable.put( "RAR",
86             "com.sun.logging.enterprise.resource.resourceadapter.LogStrings" );
87         moduleIdToResourceBundleTable.put( "SAAJ",
88             "com.sun.logging.enterprise.system.webservices.saaj.LogDomains" );
89         moduleIdToResourceBundleTable.put( "SEC",
90             "com.sun.logging.enterprise.system.core.security.LogStrings" );
91         moduleIdToResourceBundleTable.put( "SERVER",
92             "com.sun.logging.enterprise.system.LogStrings");
93         moduleIdToResourceBundleTable.put( "TLS",
94             "com.sun.logging.enterprise.system.tools.LogStrings");
95         moduleIdToResourceBundleTable.put( "UTIL",
96             "com.sun.logging.enterprise.system.util.LogStrings" );
97         moduleIdToResourceBundleTable.put( "VRFY",
98             "com.sun.logging.enterprise.system.tools.verifier.LogStrings");
99         moduleIdToResourceBundleTable.put( "WEB",
100             "com.sun.logging.enterprise.system.container.web.LogStrings");
101         moduleIdToResourceBundleTable.put( "PWC",
102             "com.sun.enterprise.web.logging.pwc.LogStrings");
103         moduleIdToResourceBundleTable.put( "CMNUTL",
104             "com.sun.common.util.logging.LogStrings");
105         moduleIdToResourceBundleTable.put( "TEST",
106             "com.sun.enterprise.server.logging.diagnostics.LogStrings" );
107     }
108
109     /**
110       * Utility method to get the ResourceBundle.
111       */

112      public static ResourceBundle JavaDoc getResourceBundleForMessageId( String JavaDoc messageId ) {
113          String JavaDoc moduleId = getModuleId( messageId );
114          if( moduleId == null ) { return null; }
115          ResourceBundle JavaDoc rb = null;
116          if( moduleId.equals( Constants.JDO_MESSAGE_PREFIX ) ) {
117              // Specialized search for JDO.
118
rb = getResourceBundleForJDOMessageId( messageId );
119          } else {
120              rb = getResourceBundleForModuleId( moduleId );
121          }
122          return rb;
123      }
124
125
126     /**
127      * Locates the Resource Bundle using moduleId as the key.
128      * ModuleId's will be the 3-6 character prefix in the message id's
129      * examples: JAXRPC1234, JDO5678. Here JAXRPC and JDO are the module
130      * id's used to locate resource bundles.
131      */

132     public static ResourceBundle JavaDoc getResourceBundleForModuleId( String JavaDoc moduleId ) {
133         if( moduleId == null ) { return null; }
134         String JavaDoc bundleName = null;
135         try {
136             bundleName = (String JavaDoc)moduleIdToResourceBundleTable.get( moduleId );
137             if( bundleName == null ) return null;
138             return ResourceBundle.getBundle(bundleName,
139                 Locale.getDefault(), getClassLoader( ) );
140         } catch( Exception JavaDoc e) {
141             System.err.println( e );
142             e.printStackTrace( );
143         }
144         return null;
145     }
146
147
148     /**
149      * A Specialized method to look up JDO ResourceBundle with a
150      * particular message id. JDO Module has more than 1 resource bundle,
151      * hence this specialization is required.
152      */

153     public static ResourceBundle JavaDoc getResourceBundleForJDOMessageId(
154         String JavaDoc messageId )
155     {
156         for( int i = 0; i < jdoResourceBundles.length; i++ ) {
157             ResourceBundle JavaDoc rb = ResourceBundle.getBundle(jdoResourceBundles[i],
158                 Locale.getDefault(), getClassLoader( ) );
159             if( rb != null ) {
160                 try {
161                     if( rb.getString(
162                         messageId + Constants.CAUSE_PREFIX + 1 ) != null )
163                     {
164                         // We found the diagnostics for the JDO MessageId.
165
// Return the resource bundle.
166
return rb;
167                     }
168                 } catch( java.util.MissingResourceException JavaDoc e ) {
169                     // We will just drop this exception becuse it's normal
170
// for java to throw an exception if a key look up in
171
// resource bundle fails
172
}
173             }
174         }
175         return null;
176     }
177
178     private static ClassLoader JavaDoc getClassLoader( ) {
179         // Use the thread's context ClassLoader. If there isn't one,
180
// use the SystemClassloader.
181
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
182         if (cl == null) {
183             cl = ClassLoader.getSystemClassLoader();
184         }
185         return cl;
186     }
187
188      /**
189       * An utility method to separate module id prefix in the message id.
190       */

191      public static String JavaDoc getModuleId( String JavaDoc messageId ) {
192         if( (messageId == null )
193           ||(messageId.length() == 0 ) )
194         {
195             return null;
196         }
197         int lastIndex = 6;
198         if( messageId.length() < lastIndex ) { lastIndex = messageId.length();}
199                                                                                  
200         char[] moduleIdCharacters =
201             messageId.substring(0,lastIndex).toCharArray( );
202         lastIndex = moduleIdCharacters.length;
203         // If there are no numbers and the moduleId like JAXRPC doesn't need
204
// further processing
205
if( Character.isDigit(moduleIdCharacters[moduleIdCharacters.length-1]) )
206         {
207              for( int index = (moduleIdCharacters.length-1); index > 0;
208                  index-- )
209              {
210                  if( !Character.isDigit(moduleIdCharacters[index]) ) {
211                     lastIndex = index + 1;
212                     break;
213                  }
214             }
215         }
216         return messageId.substring( 0, lastIndex );
217     }
218 }
219
Popular Tags