001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.console.i18n;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    import java.util.Locale;
023    import java.util.MissingResourceException;
024    import java.util.ResourceBundle;
025    
026    import org.apache.geronimo.gbean.GBeanInfo;
027    import org.apache.geronimo.gbean.GBeanInfoBuilder;
028    
029    /**
030     * Each console extension will register its classloader to this GBean, then
031     * console-portal-driver can load the resource bundle for the above classloader.
032     */
033    public class ConsoleResourceRegistry {
034    
035        private List<ClassLoader> classloaders = new ArrayList<ClassLoader>();
036    
037        /**
038         * Register the classloader of the console extension
039         *
040         * @param classloader console plugin classloader to register
041         */
042        public void registerConsoleResource(ClassLoader classloader) {
043            classloaders.add(classloader);
044        }
045    
046        /**
047         * Remove the classloader of the console extension
048         * @param classloader console plugin classloader to unregister
049         */
050        public void removeConsoleResource(ClassLoader classloader) {
051            classloaders.remove(classloader);
052        }
053    
054        /**
055         * Iterate the classloaders of the console extensions to find the value
056         * related to the key.
057         * @param basename name of the bundle
058         * @param locale locale desired
059         * @param key key for desired string
060         */
061        public String handleGetObject(String basename, Locale locale, String key) {
062            for (ClassLoader classloader : classloaders) {
063                ResourceBundle rb = ResourceBundle.getBundle(basename, locale, classloader);
064                try {
065                    if (rb != null) {
066                        String value = rb.getString(key);
067                        if (value != null) {
068                            return value;
069                        }
070                    }
071                } catch (MissingResourceException e) {
072                    //nothing to do
073                }
074            }
075    
076            return null;
077        }
078    
079        /*
080        * Standard GBean information
081        */
082        public static final GBeanInfo GBEAN_INFO;
083    
084        static {
085            GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ConsoleResourceRegistry", ConsoleResourceRegistry.class);
086            GBEAN_INFO = infoFactory.getBeanInfo();
087        }
088    
089        public static GBeanInfo getGBeanInfo() {
090            return GBEAN_INFO;
091        }
092    }