KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > MergedBundle


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
24 /*
25  * MappingContext.java
26  *
27  * Created on January 28, 2002, 6:30 PM
28  */

29
30 package com.sun.jdo.spi.persistence.utility;
31
32 import java.util.*;
33
34 /** Special resource bundle which delegates to two others.
35  * Ideally could just set the parent on the first, but this is protected,
36  * so it might not work. It's still unclear whether that approach would work
37  * in this subclass because it may break the localization fall through
38  * mechanism if used.
39  * Note: This code is copied from NbBundle in the openide sources with
40  * the following modifications:
41  * - reformatting
42  * - making variables final
43  * - renaming variables and some params
44  * - removing locale code
45  * - creating the merged set of keys using jdk classes and not nb utils
46  *
47  * @author Rochelle Raccah
48  * @version %I%
49  */

50 public class MergedBundle extends ResourceBundle
51 {
52     private final ResourceBundle _mainBundle, _parentBundle;
53
54     public MergedBundle (ResourceBundle mainBundle,
55         ResourceBundle parentBundle)
56     {
57         _mainBundle = mainBundle;
58         _parentBundle = parentBundle;
59     }
60
61     public Enumeration getKeys () { return mergeKeys(); }
62
63     private Enumeration mergeKeys ()
64     {
65         Set noDuplicatesMerge =
66             new HashSet(getCollection(_mainBundle.getKeys()));
67
68         noDuplicatesMerge.addAll(getCollection(_parentBundle.getKeys()));
69
70         return Collections.enumeration(noDuplicatesMerge);
71     }
72
73     private Collection getCollection (Enumeration enumeration)
74     {
75         List returnList = new ArrayList();
76
77         if (enumeration != null)
78         {
79             while (enumeration.hasMoreElements())
80                 returnList.add(enumeration.nextElement());
81         }
82
83         return returnList;
84     }
85
86     protected Object JavaDoc handleGetObject (String JavaDoc key)
87         throws MissingResourceException
88     {
89         try
90         {
91             return _mainBundle.getObject(key);
92         }
93         catch (MissingResourceException mre) // try the other bundle
94
{
95             return _parentBundle.getObject(key);
96         }
97     }
98 }
99
Popular Tags