001/*
002 * Copyright 2002-2019 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;
023import org.springframework.lang.Nullable;
024
025/**
026 * Properties editor for JndiTemplate objects. Allows properties of type
027 * JndiTemplate to be populated with a properties-format string.
028 *
029 * @author Rod Johnson
030 * @since 09.05.2003
031 */
032public class JndiTemplateEditor extends PropertyEditorSupport {
033
034        private final PropertiesEditor propertiesEditor = new PropertiesEditor();
035
036        @Override
037        public void setAsText(@Nullable String text) throws IllegalArgumentException {
038                if (text == null) {
039                        throw new IllegalArgumentException("JndiTemplate cannot be created from null string");
040                }
041                if (text.isEmpty()) {
042                        // empty environment
043                        setValue(new JndiTemplate());
044                }
045                else {
046                        // we have a non-empty properties string
047                        this.propertiesEditor.setAsText(text);
048                        Properties props = (Properties) this.propertiesEditor.getValue();
049                        setValue(new JndiTemplate(props));
050                }
051        }
052
053}