📄 carbean.java
字号:
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */package carstore;import javax.faces.application.Application;import javax.faces.application.FacesMessage;import javax.faces.component.UIComponent;import javax.faces.component.UIInput;import javax.faces.component.UISelectItems;import javax.faces.component.ValueHolder;import javax.faces.context.FacesContext;import javax.faces.convert.Converter;import javax.faces.model.SelectItem;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;import java.util.MissingResourceException;import java.util.ResourceBundle;import java.util.List;import java.util.Collection;import java.util.logging.Level;import java.util.logging.Logger;/** * <p>This bean encapsulates a car model, including pricing and package * choices. The system allows the user to customize the properties of * this bean with the help of the {@link CarCustomizer}.</p> * <p/> * <h3>Data Access</h3> * <p/> * <p>This is the only bean in the system that has complicated access to * the persistent store of data. In the present implementation, this * persistent store is in <code>ResourceBundle</code> instances.</p> * <p/> * <p>There are three data source <code>ResourceBundle</code> files * used:</p> * <p/> * <ol> * <p/> * <li><p><code><ModelName></code></p> * <p/> * <p>This contains the localized content for this model. There * is a variant of this file for each supported locale, for * example, <code>Jalopy_de.properties</code></p> * <p/> * </li> * <p/> * <li><p><code><Common_properties></code></p> * <p/> * <p>This contains the localized content common to all * models.</p> * <p/> * </li> * <p/> * <li><p><code><ModelName_options></code></p> * <p/> * <p>This contains the non-localized content for this model, * including the non-localized options. There is only one * variant of this file for all locales for example, * <code>Jalopy_options.properties</code></p> * <p/> * </li> * <p/> * </ol> * <p/> * <p>All files conform to the following convention:</p> * <p/> * <code><pre> * key * key_componentType * key_valueType * </pre></code> * <p/> * <p>Where <code>key</code> is the name of an attribute of this car. * For example, <code>basePrice</code>, or <code>description</code>. * <code>key_componentType</code> is the component type of the * <code>UIComponent</code> subclass to be used to represent this * attribute in the page, for example <code>SelectManyMenu</code>. * <code>key_valueType</code> is the data type of the value of the * <code>UIComponent</code>, for example <code>java.lang.Integer</code>. * For all non-String valueTypes.</p> * <p/> * <p>When the bean is instantiated, we load both of the above * properties files and iterate over the keys in each one. For each * key, we look at the <code>componentType</code> and ask the * <code>Application</code> to create a component of that type. We * store that <code>UIComponent</code> instance in our * <code>components</code> <code>Map</code> under the name * <code>key</code>. We look at the <code>valueType</code> for the * <code>key</code>. For non <code>java.lang.String</code> types, we * ask the <code>Application</code> for a <code>Converter</code> * instance for that class. If found, we use it to convert the value * for the <code>key</code> to the appropriate type and store that as * the <code>value</code> of the <code>UIComponent</code> instance.</p> */public class CarBean { private static final Logger LOGGER = Logger.getLogger("carstore"); /** * <p>The message identifier of the Message to be created if * the conversion fails. The message format string for this * message may optionally include a <code>{0}</code> * placeholder, which will be replaced by the object and value.</p> */ public static final String CONVERTER_ERROR_MESSAGE_ID = "carstore.Converter_Error"; // // Relationship Instance Variables // /** Localized labels */ private ResourceBundle resources = null; /** Price data */ private ResourceBundle priceData = null; /** * Keys: String attribute name, such as engine. Values: UIComponent * for the attribute */ private Map<String, UIComponent> components = null; /** * Keys: String attribute name, such as engine. Values: String value * of the component named by key in our components Map. */ private Map<String,Object> attributes = null; // // Constructors // public CarBean() { this.init(CarStore.DEFAULT_MODEL_PROPERTIES); } public CarBean(String bundleName) { this.init(bundleName); } /** * <p>Initialize our components <code>Map</code> as described in the * class documentation.</p> * <p/> * <p>Create a wrapper <code>Map</code> around the components * <code>Map</code> that exposes the String converted value of each * component.</p> * * @param bundleName the resource bundle name */ private void init(String bundleName) { FacesContext context = FacesContext.getCurrentInstance(); components = new HashMap<String, UIComponent>(); // load the labels resources = ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + ".bundles.Resources", context.getViewRoot().getLocale()); // load the prices priceData = ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + ".bundles.OptionPrices"); // populate the locale-specific information if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Loading bundle: " + bundleName + "."); } ResourceBundle data = ResourceBundle.getBundle(bundleName, context.getViewRoot().getLocale()); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Bundle " + bundleName + " loaded. Reading properties..."); } initComponentsFromProperties(context, data); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("done."); } // populate the non-locale-specific information common to all cars if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Loading bundle: Common_options."); } data = ResourceBundle.getBundle(CarStore.CARSTORE_PREFIX + ".bundles.Common_options"); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Bundle Common_options loaded. Reading properties..."); } initComponentsFromProperties(context, data); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("done."); } // populate the non-locale-specific information specific to each car if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Loading bundle: " + bundleName + "_options."); } data = ResourceBundle.getBundle(bundleName + "_options"); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Bundle " + bundleName + "_options loaded. Reading properties..."); } initComponentsFromProperties(context, data); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("done."); } // create a read-only Map exposing the values of all of our // components. attributes = new Map() { public void clear() { CarBean.this.components.clear(); } public boolean containsKey(Object key) { return CarBean.this.components.containsKey(key); } public boolean containsValue(Object value) { throw new UnsupportedOperationException(); } public java.util.Set<Map.Entry<String,Object>> entrySet() { throw new UnsupportedOperationException(); } public boolean equals(Object o) { throw new UnsupportedOperationException(); } public Object get(Object key) { UIComponent component; Converter converter = null; Object result = null; if (null == key) { return null; } if (null != (component = CarBean.this.components.get(key))) { // if this component can have a Converter if (component instanceof ValueHolder) { // try to get it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -