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.xbean.server.propertyeditor;
018    
019    import java.beans.PropertyEditorSupport;
020    import javax.management.MalformedObjectNameException;
021    import javax.management.ObjectName;
022    
023    /**
024     * ObjectNameEditor is a java beans property editor that can convert an ObjectName to and from a String.
025     *
026     * @author Dain Sundstrom
027     * @version $Id$
028     * @since 2.0
029     */
030    public class ObjectNameEditor extends PropertyEditorSupport {
031        /**
032         * Converts the specified string value into an ObjectName and stores the value in this instance.
033         * @param value the string to convert into an ObjectName
034         * @throws IllegalArgumentException if the specified string value is not a valid ObjectName
035         */
036        public void setAsText(String value) throws IllegalArgumentException {
037            try {
038                setValue(new ObjectName(value));
039            } catch (MalformedObjectNameException e) {
040                throw (IllegalArgumentException) new IllegalArgumentException().initCause(e);
041            }
042        }
043    
044        /**
045         * Converts the stored ObjectName value into a String.
046         * @return the canonical string form of the current ObjectName value
047         * @throws NullPointerException if the current ObjectName is null
048         */
049        public String getAsText() {
050            ObjectName objectName = (ObjectName) getValue();
051            if (objectName == null) {
052                throw new NullPointerException("Current ObjectName value is null");
053            }
054            String text = objectName.getCanonicalName();
055            return text;
056        }
057    }