Author: adrianc
Date: Wed Nov 11 19:35:34 2009 New Revision: 835032 URL: http://svn.apache.org/viewvc?rev=835032&view=rev Log: More improvements to the conversion framework, plus bug fixes. Added: ofbiz/trunk/framework/base/src/META-INF/ ofbiz/trunk/framework/base/src/META-INF/services/ ofbiz/trunk/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader (with props) Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java Added: ofbiz/trunk/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader?rev=835032&view=auto ============================================================================== --- ofbiz/trunk/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader (added) +++ ofbiz/trunk/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader Wed Nov 11 19:35:34 2009 @@ -0,0 +1,22 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +org.ofbiz.base.conversion.BooleanConverters +org.ofbiz.base.conversion.DateTimeConverters +org.ofbiz.base.conversion.MiscConverters +org.ofbiz.base.conversion.NumberConverters + Propchange: ofbiz/trunk/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java?rev=835032&r1=835031&r2=835032&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java Wed Nov 11 19:35:34 2009 @@ -19,7 +19,7 @@ package org.ofbiz.base.conversion; /** Boolean Converter classes. */ -public class BooleanConverters { +public class BooleanConverters implements ConverterLoader { public static class BooleanToInteger extends AbstractConverter<Boolean, Integer> { @@ -84,4 +84,9 @@ } } + + public void loadConverters() { + Converters.loadContainedConverters(BooleanConverters.class); + } + } Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java?rev=835032&r1=835031&r2=835032&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java Wed Nov 11 19:35:34 2009 @@ -18,31 +18,19 @@ *******************************************************************************/ package org.ofbiz.base.conversion; -import java.math.BigDecimal; -import java.sql.Clob; +import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Iterator; -import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.TimeZone; import javax.imageio.spi.ServiceRegistry; import javolution.util.FastMap; import javolution.util.FastSet; -import org.ofbiz.base.conversion.BooleanConverters.*; -import org.ofbiz.base.conversion.CollectionConverters.*; -import org.ofbiz.base.conversion.DateTimeConverters.*; -import org.ofbiz.base.conversion.NumberConverters.*; -import org.ofbiz.base.conversion.MiscConverters.*; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.ObjectType; -import org.ofbiz.base.util.TimeDuration; - -import com.ibm.icu.util.Calendar; /** A <code>Converter</code> factory and repository. */ public class Converters { @@ -53,90 +41,16 @@ protected static final Set<String> noConversions = FastSet.newInstance(); protected static final Converter<Object, Object> nullConverter = new NullConverter(); - // If Arrays aren't converted when using RMI, then comment out the next line - public static final Converter<Object[], List<?>> ArrayToList = new ArrayToList(); - public static final Converter<BigDecimal, Double> BigDecimalToDouble = new BigDecimalToDouble(); - public static final Converter<BigDecimal, Float> BigDecimalToFloat = new BigDecimalToFloat(); - public static final Converter<BigDecimal, Integer> BigDecimalToInteger = new BigDecimalToInteger(); - public static final Converter<BigDecimal, List<BigDecimal>> BigDecimalToList = new BigDecimalToList(); - public static final Converter<BigDecimal, Long> BigDecimalToLong = new BigDecimalToLong(); - public static final Converter<BigDecimal, Set<BigDecimal>> BigDecimalToSet = new BigDecimalToSet(); - public static final LocalizedConverter<BigDecimal, String> BigDecimalToString = new BigDecimalToString(); - public static final Converter<Boolean, Integer> BooleanToInteger = new BooleanToInteger(); - public static final Converter<Boolean, String> BooleanToString = new BooleanToString(); - public static final Converter<Calendar, Long> CalendarToLong = new CalendarToLong(); - public static final Converter<Calendar, String> CalendarToString = new CalendarToString(); - public static final Converter<Clob, String> ClobToString = new ClobToString(); - public static final Converter<java.util.Date, Long> DateToLong = new DateToLong(); - public static final LocalizedConverter<java.util.Date, String> DateToString = new DateToString(); - public static final Converter<Double, BigDecimal> DoubleToBigDecimal = new DoubleToBigDecimal(); - public static final Converter<Double, Float> DoubleToFloat = new DoubleToFloat(); - public static final Converter<Double, Integer> DoubleToInteger = new DoubleToInteger(); - public static final Converter<Double, List<Double>> DoubleToList = new DoubleToList(); - public static final Converter<Double, Long> DoubleToLong = new DoubleToLong(); - public static final Converter<Double, Set<Double>> DoubleToSet = new DoubleToSet(); - public static final LocalizedConverter<Double, String> DoubleToString = new DoubleToString(); - public static final Converter<TimeDuration, String> DurationToString = new DurationToString(); - public static final Converter<Float, BigDecimal> FloatToBigDecimal = new FloatToBigDecimal(); - public static final Converter<Float, Double> FloatToDouble = new FloatToDouble(); - public static final Converter<Float, Integer> FloatToInteger = new FloatToInteger(); - public static final Converter<Float, List<Float>> FloatToList = new FloatToList(); - public static final Converter<Float, Long> FloatToLong = new FloatToLong(); - public static final Converter<Float, Set<Float>> FloatToSet = new FloatToSet(); - public static final LocalizedConverter<Float, String> FloatToString = new FloatToString(); - public static final Converter<Integer, BigDecimal> IntegerToBigDecimal = new IntegerToBigDecimal(); - public static final Converter<Integer, Boolean> IntegerToBoolean = new IntegerToBoolean(); - public static final Converter<Integer, Double> IntegerToDouble = new IntegerToDouble(); - public static final Converter<Integer, Float> IntegerToFloat = new IntegerToFloat(); - public static final Converter<Integer, List<Integer>> IntegerToList = new IntegerToList(); - public static final Converter<Integer, Long> IntegerToLong = new IntegerToLong(); - public static final Converter<Integer, Set<Integer>> IntegerToSet = new IntegerToSet(); - public static final LocalizedConverter<Integer, String> IntegerToString = new IntegerToString(); - public static final Converter<List<?>, String> ListToString = new ListToString(); - public static final Converter<Locale, String> LocaleToString = new LocaleToString(); - public static final Converter<Long, BigDecimal> LongToBigDecimal = new LongToBigDecimal(); - public static final Converter<Long, Calendar> LongToCalendar = new LongToCalendar(); - public static final Converter<Long, Double> LongToDouble = new LongToDouble(); - public static final Converter<Long, Float> LongToFloat = new LongToFloat(); - public static final Converter<Long, Integer> LongToInteger = new LongToInteger(); - public static final Converter<Long, List<Long>> LongToList = new LongToList(); - public static final Converter<Long, Set<Long>> LongToSet = new LongToSet(); - public static final LocalizedConverter<Long, String> LongToString = new LongToString(); - public static final Converter<Map<?, ?>, List<Map<?,?>>> MapToList = new MapToList(); - public static final Converter<Map<?, ?>, Set<Map<?,?>>> MapToSet = new MapToSet(); - public static final Converter<Map<?, ?>, String> MapToString = new MapToString(); - public static final Converter<Number, java.util.Date> NumberToDate = new NumberToDate(); - public static final Converter<Number, TimeDuration> NumberToDuration = new NumberToDuration(); - public static final Converter<Number, java.sql.Date> NumberToSqlDate = new NumberToSqlDate(); - public static final Converter<Number, java.sql.Time> NumberToSqlTime = new NumberToSqlTime(); - public static final Converter<Number, java.sql.Timestamp> NumberToTimestamp = new NumberToTimestamp(); - public static final LocalizedConverter<java.sql.Date, String> SqlDateToString = new SqlDateToString(); - public static final LocalizedConverter<java.sql.Time, String> SqlTimeToString = new SqlTimeToString(); - public static final LocalizedConverter<String, BigDecimal> StringToBigDecimal = new StringToBigDecimal(); - public static final Converter<String, Boolean> StringToBoolean = new StringToBoolean(); - public static final LocalizedConverter<String, Calendar> StringToCalendar = new StringToCalendar(); - public static final LocalizedConverter<String, java.util.Date> StringToDate = new StringToDate(); - public static final LocalizedConverter<String, Double> StringToDouble = new StringToDouble(); - public static final Converter<String, TimeDuration> StringToDuration = new StringToDuration(); - public static final LocalizedConverter<String, Float> StringToFloat = new StringToFloat(); - public static final LocalizedConverter<String, Integer> StringToInteger = new StringToInteger(); - public static final Converter<String, List<?>> StringToList = new StringToList(); - public static final Converter<String, Locale> StringToLocale = new StringToLocale(); - public static final LocalizedConverter<String, Long> StringToLong = new StringToLong(); - public static final Converter<String, Map<?, ?>> StringToMap = new StringToMap(); - public static final Converter<String, Set<?>> StringToSet = new StringToSet(); - public static final LocalizedConverter<String, java.sql.Date> StringToSqlDate = new StringToSqlDate(); - public static final LocalizedConverter<String, java.sql.Time> StringToSqlTime = new StringToSqlTime(); - public static final LocalizedConverter<String, java.sql.Timestamp> StringToTimestamp = new StringToTimestamp(); - public static final Converter<String, TimeZone> StringToTimeZone = new StringToTimeZone(); - public static final Converter<TimeZone, String> TimeZoneToString = new TimeZoneToString(); - static { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Iterator<ConverterLoader> converterLoaders = ServiceRegistry.lookupProviders(ConverterLoader.class, loader); while (converterLoaders.hasNext()) { - ConverterLoader converterLoader = converterLoaders.next(); - converterLoader.loadConverters(); + try { + ConverterLoader converterLoader = converterLoaders.next(); + converterLoader.loadConverters(); + } catch (Exception e) { + Debug.logError(e, module); + } } } @@ -186,6 +100,24 @@ return (Converter<S, T>) result; } + /** Load all classes that implement <code>Converter</code> and are + * contained in <code>containerClass</code>. + * + * @param containerClass + */ + public static void loadContainedConverters(Class<?> containerClass) { + Class<?>[] classArray = containerClass.getClasses(); + for (int i = 0; i < classArray.length; i++) { + try { + if ((classArray[i].getModifiers() & Modifier.ABSTRACT) == 0) { + classArray[i].newInstance(); + } + } catch (Exception e) { + Debug.logError(e, module); + } + } + } + /** Registers a <code>Converter</code> instance to be used by the * {@link org.ofbiz.base.conversion.Converters#getConverter(Class, Class)} * method. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java?rev=835032&r1=835031&r2=835032&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java Wed Nov 11 19:35:34 2009 @@ -29,7 +29,7 @@ import com.ibm.icu.util.Calendar; /** Date/time Converter classes. */ -public class DateTimeConverters { +public class DateTimeConverters implements ConverterLoader { public static class CalendarToLong extends AbstractConverter<Calendar, Long> { @@ -82,6 +82,22 @@ } + public static class DateToSqlDate extends AbstractConverter<java.util.Date, java.sql.Date> { + + public java.sql.Date convert(java.util.Date obj) throws ConversionException { + return new java.sql.Date(obj.getTime()); + } + + public Class<java.util.Date> getSourceClass() { + return java.util.Date.class; + } + + public Class<java.sql.Date> getTargetClass() { + return java.sql.Date.class; + } + + } + public static class DateToString extends GenericLocalizedConverter<java.util.Date, String> { public String convert(java.util.Date obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException { @@ -104,6 +120,22 @@ } + public static class DateToTimestamp extends AbstractConverter<java.util.Date, java.sql.Timestamp> { + + public java.sql.Timestamp convert(java.util.Date obj) throws ConversionException { + return new java.sql.Timestamp(obj.getTime()); + } + + public Class<java.util.Date> getSourceClass() { + return java.util.Date.class; + } + + public Class<java.sql.Timestamp> getTargetClass() { + return java.sql.Timestamp.class; + } + + } + public static class DurationToString extends AbstractConverter<TimeDuration, String> { public String convert(TimeDuration obj) throws ConversionException { @@ -236,6 +268,22 @@ } + public static class SqlDateToDate extends AbstractConverter<java.sql.Date, java.util.Date> { + + public java.util.Date convert(java.sql.Date obj) throws ConversionException { + return new java.util.Date(obj.getTime()); + } + + public Class<java.sql.Date> getSourceClass() { + return java.sql.Date.class; + } + + public Class<java.util.Date> getTargetClass() { + return java.util.Date.class; + } + + } + public static class SqlDateToString extends GenericLocalizedConverter<java.sql.Date, String> { public String convert(java.sql.Date obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException { @@ -471,6 +519,38 @@ } + public static class TimestampToDate extends AbstractConverter<java.sql.Timestamp, java.util.Date> { + + public java.util.Date convert(java.sql.Timestamp obj) throws ConversionException { + return new java.sql.Timestamp(obj.getTime()); + } + + public Class<java.sql.Timestamp> getSourceClass() { + return java.sql.Timestamp.class; + } + + public Class<java.util.Date> getTargetClass() { + return java.util.Date.class; + } + + } + + public static class TimestampToSqlDate extends AbstractConverter<java.sql.Timestamp, java.sql.Date> { + + public java.sql.Date convert(java.sql.Timestamp obj) throws ConversionException { + return new java.sql.Date(obj.getTime()); + } + + public Class<java.sql.Timestamp> getSourceClass() { + return java.sql.Timestamp.class; + } + + public Class<java.sql.Date> getTargetClass() { + return java.sql.Date.class; + } + + } + public static class TimeZoneToString extends AbstractConverter<TimeZone, String> { public String convert(TimeZone obj) throws ConversionException { @@ -487,4 +567,8 @@ } + public void loadConverters() { + Converters.loadContainedConverters(DateTimeConverters.class); + } + } Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java?rev=835032&r1=835031&r2=835032&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java Wed Nov 11 19:35:34 2009 @@ -26,7 +26,7 @@ import org.ofbiz.base.util.UtilMisc; /** Miscellaneous Converter classes. */ -public class MiscConverters { +public class MiscConverters implements ConverterLoader { public static final int CHAR_BUFFER_SIZE = 4096; @@ -102,4 +102,8 @@ } + public void loadConverters() { + Converters.loadContainedConverters(MiscConverters.class); + } + } Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java?rev=835032&r1=835031&r2=835032&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java Wed Nov 11 19:35:34 2009 @@ -30,7 +30,9 @@ import javolution.util.FastSet; /** Number Converter classes. */ -public class NumberConverters { +public class NumberConverters implements ConverterLoader { + + public static final String module = NumberConverters.class.getName(); public static abstract class AbstractToNumberConverter<S, T> extends AbstractUsesLocaleConverter<S, T> { @@ -722,4 +724,9 @@ } } + + public void loadConverters() { + Converters.loadContainedConverters(NumberConverters.class); + } + } Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java?rev=835032&r1=835031&r2=835032&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java Wed Nov 11 19:35:34 2009 @@ -27,15 +27,12 @@ import org.ofbiz.base.conversion.AbstractConverter; import org.ofbiz.base.conversion.ConversionException; import org.ofbiz.base.conversion.ConverterLoader; -import org.ofbiz.base.util.Debug; import org.ofbiz.entity.GenericEntity; import org.ofbiz.entity.GenericValue; /** Entity Engine <code>Converter</code> classes. */ public class Converters implements ConverterLoader { - public static final String module = Converters.class.getName(); - public static class GenericValueToList extends AbstractConverter<GenericValue, List<GenericValue>> { public List<GenericValue> convert(GenericValue obj) throws ConversionException { @@ -123,14 +120,7 @@ } public void loadConverters() { - Class<?>[] classArray = Converters.class.getClasses(); - for (int i = 0; i < classArray.length; i++) { - try { - classArray[i].newInstance(); - } catch (Exception e) { - Debug.logError(e, module); - } - } + org.ofbiz.base.conversion.Converters.loadContainedConverters(Converters.class); } } |
Free forum by Nabble | Edit this page |