KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > PackageStringSources


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.appserv.management.util.misc;
24
25 import java.lang.reflect.Constructor JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31
32 import com.sun.appserv.management.util.misc.ClassUtil;
33
34 /**
35     A StringSource for a package which first looks for a class in the the package
36     named "PackageStringSource", and then, if not found, attempts to locate
37     a ResourceBundle with the name "Strings".
38     <p>
39     Also maintains a cache of such sources so that subsequent uses reuse prior ones.
40  */

41 public class PackageStringSources
42 {
43     static final Map JavaDoc<Class JavaDoc,StringSource> mSources = new HashMap JavaDoc<Class JavaDoc,StringSource>();
44     
45     /**
46         Get a string source for the specified class, using the specified
47         StringSource as its delegate.
48      */

49         public static StringSource
50     get( final Class JavaDoc theClass, final StringSource delegate )
51     {
52         StringSource source = mSources.get( theClass );
53         if ( source == null )
54         {
55             source = init( theClass, delegate );
56             mSources.put( theClass, source );
57         }
58
59         return( source );
60     }
61     
62     private PackageStringSources() {}
63     
64         private static StringSource
65     init( final Class JavaDoc theClass, final StringSource delegate )
66     {
67         StringSource source = null;
68         final String JavaDoc packageName = theClass.getPackage().getName();
69         
70         try
71         {
72             final String JavaDoc classname = packageName + ".PackageStrings";
73             
74             final Class JavaDoc packageStringSourceClass = ClassUtil.getClassFromName( classname );
75             
76             final Constructor JavaDoc c = packageStringSourceClass.getConstructor( new Class JavaDoc[] { StringSource.class } );
77             
78             source = (StringSource)c.newInstance( new Object JavaDoc[] { delegate } );
79         }
80         catch( Exception JavaDoc e )
81         {
82             // no such class exists
83
e.printStackTrace();
84         }
85         
86         if ( source == null )
87         {
88             final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle( packageName + ".Strings",
89                                         Locale.getDefault(), theClass.getClassLoader());
90             
91             source = new ResourceBundleStringSource( bundle, delegate );
92         }
93         
94         return( source );
95     }
96 };
97
98
99
100
Popular Tags