01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.console.i18n;
17:
18: import java.util.ArrayList;
19: import java.util.List;
20: import java.util.Locale;
21: import java.util.MissingResourceException;
22: import java.util.ResourceBundle;
23:
24: import org.apache.geronimo.gbean.GBeanInfo;
25: import org.apache.geronimo.gbean.GBeanInfoBuilder;
26:
27: /**
28: * Each console extension will register its classloader to this GBean, then
29: * console-portal-driver can load the resource bundle for the above classloader.
30: */
31: public class ConsoleResourceRegistry {
32:
33: private List<ClassLoader> classloaders = new ArrayList<ClassLoader>();
34:
35: /**
36: * Register the classloader of the console extension
37: *
38: * @param classloader console plugin classloader to register
39: */
40: public void registerConsoleResource(ClassLoader classloader) {
41: classloaders.add(classloader);
42: }
43:
44: /**
45: * Remove the classloader of the console extension
46: * @param classloader console plugin classloader to unregister
47: */
48: public void removeConsoleResource(ClassLoader classloader) {
49: classloaders.remove(classloader);
50: }
51:
52: /**
53: * Iterate the classloaders of the console extensions to find the value
54: * related to the key.
55: * @param basename name of the bundle
56: * @param locale locale desired
57: * @param key key for desired string
58: */
59: public String handleGetObject(String basename, Locale locale,
60: String key) {
61: for (ClassLoader classloader : classloaders) {
62: ResourceBundle rb = ResourceBundle.getBundle(basename,
63: locale, classloader);
64: try {
65: if (rb != null) {
66: String value = rb.getString(key);
67: if (value != null) {
68: return value;
69: }
70: }
71: } catch (MissingResourceException e) {
72: //nothing to do
73: }
74: }
75:
76: return null;
77: }
78:
79: /*
80: * Standard GBean information
81: */
82: public static final GBeanInfo GBEAN_INFO;
83:
84: static {
85: GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(
86: "ConsoleResourceRegistry",
87: ConsoleResourceRegistry.class);
88: GBEAN_INFO = infoFactory.getBeanInfo();
89: }
90:
91: public static GBeanInfo getGBeanInfo() {
92: return GBEAN_INFO;
93: }
94: }
|