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    package org.apache.geronimo.common.propertyeditor;
018    
019    import java.io.ByteArrayInputStream;
020    import java.io.ByteArrayOutputStream;
021    import java.io.IOException;
022    import java.util.Properties;
023    
024    /**
025     * A property editor for indirect property bundles.  This editor
026     * transforms the text value of the propery into a Property resource bundle.
027     *
028     * @version $Rev: 547737 $
029     */
030    public class PropertiesEditor extends TextPropertyEditorSupport {
031        /**
032         * Treats the text value of this property as an input stream that
033         * is converted into a Property bundle.
034         *
035         * @return a Properties object
036         * @throws PropertyEditorException An error occurred creating the Properties object.
037         */
038        public Object getValue() {
039            Object currentValue = super.getValue();
040            if (currentValue instanceof Properties) {
041                return (Properties) currentValue;
042            } else {
043                // convert the text value into an in-memory input stream we can used for
044                // property loading.
045                ByteArrayInputStream stream = new ByteArrayInputStream(currentValue.toString().getBytes());
046                // load this into a properties instance.
047                Properties bundle = new Properties();
048                try {
049                    bundle.load(stream);
050                } catch (IOException e) {
051                    // any errors here are just a property exception
052                    throw new PropertyEditorException(e.getMessage(), e);
053                }
054                return bundle;
055            }
056        }
057    
058        /**
059         * Provides a String version of a Properties object suitable
060         * for loading into a Properties table using the load method.
061         *
062         * @return The String value of the Properties object as created
063         *         by the store method.
064         * @throws PropertyEditorException An error occurred converting the Properties object
065         * @see Properties#store(java.io.OutputStream, String)
066         */
067        public String getAsText() {
068            Object value = getValue();
069            if (value instanceof Properties) {
070                ByteArrayOutputStream baos = new ByteArrayOutputStream();
071                try {
072                    ((Properties) value).store(baos, null);
073                } catch (IOException e) {
074                    // any errors here are just a property exception
075                    throw new PropertyEditorException(e.getMessage(), e);
076                }
077                return baos.toString();
078            }
079            return ("" + value);
080        }
081    
082    }