KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > resource > ResourceBundleFactory


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.tools.jsfext.resource;
24
25 import com.sun.enterprise.tools.jsfext.layout.descriptor.Resource;
26 import com.sun.enterprise.tools.jsfext.util.Util;
27
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.faces.component.UIViewRoot;
32 import javax.faces.context.FacesContext;
33
34
35 /**
36  * <p> This factory class provides a means to instantiate a
37  * java.util.ResouceBundle. It implements the {@link ResourceFactory}
38  * which the
39  * {@link com.sun.enterprise.tools.jsfext.renderer.TemplateRenderer} knows
40  * how to use to create arbitrary {@link Resource} objects. This
41  * factory utilizes the ResourceBundleManager for efficiency.</p>
42  *
43  * @see ResourceFactory
44  * @see Resource
45  *
46  * @author Ken Paulsen (ken.paulsen@sun.com)
47  */

48 public class ResourceBundleFactory implements ResourceFactory {
49
50     /**
51      * <p> This is the factory method responsible for obtaining a
52      * ResourceBundle. This method uses the ResourceBundleManager to
53      * manage instances of ResourceBundles per key/locale.</p>
54      *
55      * <p> It should be noted that this method does not do anything if there
56      * is already a request attribute with the given id.</p>
57      *
58      * @param context The FacesContext
59      * @param descriptor The Resource descriptor that is associated
60      * with the requested Resource.
61      *
62      * @return The newly created Resource
63      */

64     public Object JavaDoc getResource(FacesContext context, Resource descriptor) {
65     // Get the id from the descriptor, this is the id that should be used
66
// to store it in the RequestScope
67
String JavaDoc id = descriptor.getId();
68     Map JavaDoc map = context.getExternalContext().getRequestMap();
69     if (map.containsKey(id)) {
70         // It is already set
71
return map.get(id);
72     }
73
74     // Obtain the ResourceBundle
75
Object JavaDoc resource = ResourceBundleManager.getInstance().getBundle(
76         descriptor.getExtraInfo(), getLocale(context));
77
78     // The id does not exist in the request scope yet.
79
map.put(id, resource);
80
81     return resource;
82     }
83
84     /**
85      * <p> Help obtain the current <code>Locale</code>.</p>
86      */

87     public static Locale JavaDoc getLocale(FacesContext context) {
88     Locale JavaDoc locale = null;
89     if (context != null) {
90         // Attempt to obtain the locale from the UIViewRoot
91
UIViewRoot root = context.getViewRoot();
92         if (root != null) {
93         locale = root.getLocale();
94         }
95     }
96
97     // Return the locale; if not found, return the system default Locale
98
return (locale == null) ? Locale.getDefault() : locale;
99     }
100 }
101
Popular Tags