001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jndi;
018
019import java.beans.PropertyEditorSupport;
020import java.util.Properties;
021
022import org.springframework.beans.propertyeditors.PropertiesEditor;
023
024/**
025 * Properties editor for JndiTemplate objects. Allows properties of type
026 * JndiTemplate to be populated with a properties-format string.
027 *
028 * @author Rod Johnson
029 * @since 09.05.2003
030 */
031public class JndiTemplateEditor extends PropertyEditorSupport {
032
033        private final PropertiesEditor propertiesEditor = new PropertiesEditor();
034
035        @Override
036        public void setAsText(String text) throws IllegalArgumentException {
037                if (text == null) {
038                        throw new IllegalArgumentException("JndiTemplate cannot be created from null string");
039                }
040                if ("".equals(text)) {
041                        // empty environment
042                        setValue(new JndiTemplate());
043                }
044                else {
045                        // we have a non-empty properties string
046                        this.propertiesEditor.setAsText(text);
047                        Properties props = (Properties) this.propertiesEditor.getValue();
048                        setValue(new JndiTemplate(props));
049                }
050        }
051
052}