All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.ldap.odm.typeconversion.impl.ConverterManagerFactoryBean Maven / Gradle / Ivy

There is a newer version: 3.2.4
Show newest version
/*
 * Copyright 2005-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.ldap.odm.typeconversion.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.FactoryBeanNotInitializedException;

import java.util.HashSet;
import java.util.Set;

/**
 * A utility class to allow {@link ConverterManagerImpl} instances to be easily configured via spring.xml.
 * 

* The following shows a typical simple example which creates two {@link Converter} instances: *

    *
  • fromStringConverter
  • *
  • toStringConverter
  • *
* Configured in an {@link ConverterManagerImpl} to: *
    *
  • Use fromStringConverter to convert from String to Byte, Short, * Integer, Long, Float, Double, Boolean
  • *
  • Use toStringConverter to convert from Byte, Short, * Integer, Long, Float, Double, Boolean to String
  • *
*
 * <bean id="converterManager" class="org.springframework.ldap.odm.typeconversion.impl.ConverterManagerFactoryBean">
 *   <property name="converterConfig">
 *     <set>
 *       <bean class="org.springframework.ldap.odm.typeconversion.impl.ConverterManagerFactoryBean$ConverterConfig">
 *         <property name="fromClasses">
 *           <set>
 *             <value>java.lang.String</value>
 *           </set>
 *         </property>
 *         <property name="toClasses">
 *           <set>
 *             <value>java.lang.Byte</value>
 *             <value>java.lang.Short</value>
 *             <value>java.lang.Integer</value>
 *             <value>java.lang.Long</value>
 *             <value>java.lang.Float</value>
 *             <value>java.lang.Double</value>
 *             <value>java.lang.Boolean</value>
 *           </set>
 *         </property>
 *         <property name="converter" ref="fromStringConverter"/>
 *       </bean>
 *       <bean class="org.springframework.ldap.odm.typeconversion.impl.ConverterManagerFactoryBean$ConverterConfig">
 *         <property name="fromClasses">
 *           <set>
 *             <value>java.lang.Byte</value>
 *             <value>java.lang.Short</value>
 *             <value>java.lang.Integer</value>
 *             <value>java.lang.Long</value>
 *             <value>java.lang.Float</value>
 *             <value>java.lang.Double</value>
 *             <value>java.lang.Boolean</value>
 *           </set>
 *         </property>
 *         <property name="toClasses">
 *           <set>
 *             <value>java.lang.String</value>
 *           </set>
 *         </property>
 *         <property name="converter" ref="toStringConverter"/>
 *       </bean>
 *     </set>
 *   </property>
 * </bean>
 * 
* {@link ConverterConfig} has a second constructor which takes an additional parameter to allow * an LDAP syntax to be defined. * * @author Paul Harvey <paul.at.pauls-place.me.uk> */ public final class ConverterManagerFactoryBean implements FactoryBean { private static final Logger LOG = LoggerFactory.getLogger(ConverterManagerFactoryBean.class); /** * Configuration information for a single Converter instance. */ public static final class ConverterConfig { // The set of classes the Converter will convert from. private Set> fromClasses = new HashSet>(); // The (optional) LDAP syntax. private String syntax=null; // The set of classes the Converter will convert to. private Set> toClasses = new HashSet>(); // The Converter to use. private Converter converter=null; public ConverterConfig() { } /** * @param fromClasses Comma separated list of classes the {@link Converter} should can convert from. */ public void setFromClasses(Set> fromClasses) { this.fromClasses=fromClasses; } /** * @param toClasses Comma separated list of classes the {@link Converter} can convert to. */ public void setToClasses(Set> toClasses) { this.toClasses=toClasses; } /** * @param syntax An LDAP syntax supported by the {@link Converter}. */ public void setSyntax(String syntax) { this.syntax=syntax; } /** * @param converter The {@link Converter} to use. */ public void setConverter(Converter converter) { this.converter=converter; } @Override public String toString() { return String.format("fromClasses=%1$s, syntax=%2$s, toClasses=%3$s, converter=%4$s", fromClasses, syntax, toClasses, converter); } } private Set converterConfigList=null; /** * @param converterConfigList */ public void setConverterConfig(Set converterConfigList) { this.converterConfigList=converterConfigList; } /** * Creates a ConverterManagerImpl populating it with Converter instances from the converterConfigList property. * * @return The newly created {@link org.springframework.ldap.odm.typeconversion.ConverterManager}. * @throws ClassNotFoundException Thrown if any of the classes to be converted to or from cannot be found. * * @see org.springframework.beans.factory.FactoryBean#getObject() */ public Object getObject() throws Exception { if (converterConfigList==null) { throw new FactoryBeanNotInitializedException("converterConfigList has not been set"); } ConverterManagerImpl result = new ConverterManagerImpl(); for (ConverterConfig converterConfig : converterConfigList) { if (converterConfig.fromClasses==null || converterConfig.toClasses==null || converterConfig.converter==null) { throw new FactoryBeanNotInitializedException( String.format("All of fromClasses, toClasses and converter must be specified in bean %1$s", converterConfig.toString())); } for (Class fromClass : converterConfig.fromClasses) { for (Class toClass : converterConfig.toClasses) { if (LOG.isDebugEnabled()) { LOG.debug(String.format("Adding converter from %1$s to %2$s", fromClass, toClass)); } result.addConverter(fromClass, converterConfig.syntax, toClass, converterConfig.converter); } } } return result; } /* (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#getObjectType() */ public Class getObjectType() { return ConverterManagerImpl.class; } /* (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#isSingleton() */ public boolean isSingleton() { return true; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy