Coverage Report - org.seasar.cubby.dxo.impl.RequestParameterConverterFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestParameterConverterFactoryImpl
94%
58/62
67%
20/30
0
RequestParameterConverterFactoryImpl$DisregardExceptionConverterWrapper
60%
6/10
N/A
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.cubby.dxo.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.seasar.extension.dxo.converter.ConversionContext;
 24  
 import org.seasar.extension.dxo.converter.Converter;
 25  
 import org.seasar.extension.dxo.converter.impl.ConverterFactoryImpl;
 26  
 import org.seasar.framework.container.ComponentDef;
 27  
 import org.seasar.framework.container.S2Container;
 28  
 import org.seasar.framework.container.factory.S2ContainerFactory;
 29  
 import org.seasar.framework.log.Logger;
 30  
 import org.seasar.framework.util.DisposableUtil;
 31  
 import org.seasar.framework.util.ResourceUtil;
 32  
 
 33  
 /**
 34  
  * 
 35  
  * @author baba
 36  
  * 
 37  
  */
 38  38
 public class RequestParameterConverterFactoryImpl extends ConverterFactoryImpl {
 39  
 
 40  1
         private static final Logger logger = Logger
 41  
                         .getLogger(RequestParameterConverterFactoryImpl.class);
 42  
 
 43  1
         private static final Converter[] EMPTY_CONVERTER_ARRAY = new Converter[0];
 44  
 
 45  1
         private static final Map<Converter, Converter> wrapperCache = new HashMap<Converter, Converter>();
 46  
 
 47  38
         private final List<String> converterContainerPaths = new ArrayList<String>();
 48  
 
 49  38
         private final List<S2Container> containers = new ArrayList<S2Container>();
 50  
 
 51  38
         private boolean includeApplicationConverters = true;
 52  
 
 53  38
         private boolean initialized = false;
 54  
 
 55  
         public void addConverterContainerPath(final String converterContainerPath) {
 56  114
                 this.converterContainerPaths.add(converterContainerPath);
 57  114
         }
 58  
 
 59  
         public void setIncludeApplicationConverters(
 60  
                         final boolean includeApplicationConverters) {
 61  38
                 this.includeApplicationConverters = includeApplicationConverters;
 62  38
         }
 63  
 
 64  
         @Override
 65  
         public void initialize() {
 66  56
                 if (initialized) {
 67  47
                         return;
 68  
                 }
 69  
 
 70  9
                 super.initialize();
 71  
 
 72  9
                 final Map<Class<? extends Converter>, Converter> converterMap = new HashMap<Class<? extends Converter>, Converter>();
 73  9
                 for (final String path : this.converterContainerPaths) {
 74  27
                         if (ResourceUtil.isExist(path)) {
 75  27
                                 if (logger.isDebugEnabled()) {
 76  27
                                         logger.log("DCUB0011", new Object[] { path });
 77  
                                 }
 78  27
                                 final S2Container container = S2ContainerFactory.create(path);
 79  27
                                 containers.add(container);
 80  
                                 for (final ComponentDef componentDef : container
 81  252
                                                 .findAllComponentDefs(Converter.class)) {
 82  225
                                         final Converter converter = (Converter) componentDef
 83  
                                                         .getComponent();
 84  225
                                         converterMap.put(converter.getClass(), converter);
 85  225
                                         if (logger.isDebugEnabled()) {
 86  225
                                                 logger.log("DCUB0010", new Object[] { converter });
 87  
                                         }
 88  
                                 }
 89  27
                                 if (logger.isDebugEnabled()) {
 90  27
                                         logger
 91  
                                                         .log("DCUB0012",
 92  
                                                                         new Object[] { container.getPath() });
 93  
                                 }
 94  27
                         }
 95  
                 }
 96  
 
 97  9
                 if (includeApplicationConverters) {
 98  9
                         final List<Converter> converterList = new ArrayList<Converter>(
 99  
                                         converterMap.values());
 100  9
                         if (logger.isDebugEnabled()) {
 101  9
                                 logger.log("DCUB0013", null);
 102  
                         }
 103  207
                         for (final Converter converter : converters) {
 104  198
                                 if (!converterMap.containsKey(converter.getClass())) {
 105  0
                                         converterList.add(converter);
 106  0
                                         if (logger.isDebugEnabled()) {
 107  0
                                                 logger.log("DCUB0010", new Object[] { converter });
 108  
                                         }
 109  
                                 }
 110  
                         }
 111  9
                         if (logger.isDebugEnabled()) {
 112  9
                                 logger.log("DCUB0014", null);
 113  
                         }
 114  9
                         converters = converterList.toArray(EMPTY_CONVERTER_ARRAY);
 115  9
                 } else {
 116  0
                         converters = converterMap.values().toArray(EMPTY_CONVERTER_ARRAY);
 117  
                 }
 118  
 
 119  9
                 DisposableUtil.add(this);
 120  9
                 initialized = true;
 121  9
         }
 122  
 
 123  
         @Override
 124  
         public void dispose() {
 125  18
                 wrapperCache.clear();
 126  18
                 for (final S2Container container : this.containers) {
 127  27
                         container.destroy();
 128  
                 }
 129  18
                 containers.clear();
 130  18
                 super.dispose();
 131  18
                 initialized = false;
 132  18
         }
 133  
 
 134  
         @Override
 135  
         @SuppressWarnings("unchecked")
 136  
         public Converter getConverter(final Class sourceClass, final Class destClass) {
 137  28
                 initialize();
 138  28
                 final Converter converter = super.getConverter(sourceClass, destClass);
 139  
                 final Converter wrapper;
 140  28
                 if (wrapperCache.containsKey(converter)) {
 141  9
                         wrapper = wrapperCache.get(converter);
 142  
                 } else {
 143  19
                         wrapper = new DisregardExceptionConverterWrapper(converter);
 144  19
                         wrapperCache.put(converter, wrapper);
 145  
                 }
 146  28
                 return wrapper;
 147  
         }
 148  
 
 149  38
         static class DisregardExceptionConverterWrapper implements Converter {
 150  
 
 151  
                 private final Converter converter;
 152  
 
 153  19
                 public DisregardExceptionConverterWrapper(final Converter converter) {
 154  19
                         this.converter = converter;
 155  19
                 }
 156  
 
 157  
                 @SuppressWarnings("unchecked")
 158  
                 public Object convert(final Object source, final Class destClass,
 159  
                                 final ConversionContext context) {
 160  
                         try {
 161  23
                                 return converter.convert(source, destClass, context);
 162  0
                         } catch (final Exception e) {
 163  0
                                 return null;
 164  
                         }
 165  
                 }
 166  
 
 167  
                 public void convert(final Object source, final Object dest,
 168  
                                 final ConversionContext context) {
 169  5
                         converter.convert(source, dest, context);
 170  5
                 }
 171  
 
 172  
                 @SuppressWarnings("unchecked")
 173  
                 public Class getDestClass() {
 174  0
                         return converter.getDestClass();
 175  
                 }
 176  
 
 177  
                 @SuppressWarnings("unchecked")
 178  
                 public Class[] getSourceClasses() {
 179  0
                         return converter.getSourceClasses();
 180  
                 }
 181  
 
 182  
         }
 183  
 
 184  
 }