Coverage Report - org.seasar.teeda.core.application.ApplicationImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationImpl
69%
169/246
61%
57/94
2.093
 
 1  
 /*
 2  
  * Copyright 2004-2011 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.teeda.core.application;
 17  
 
 18  
 import java.math.BigDecimal;
 19  
 import java.math.BigInteger;
 20  
 import java.sql.Timestamp;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collection;
 23  
 import java.util.Collections;
 24  
 import java.util.Date;
 25  
 import java.util.HashMap;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 import java.util.Locale;
 29  
 import java.util.Map;
 30  
 
 31  
 import javax.faces.FacesException;
 32  
 import javax.faces.application.Application;
 33  
 import javax.faces.application.NavigationHandler;
 34  
 import javax.faces.application.StateManager;
 35  
 import javax.faces.application.ViewHandler;
 36  
 import javax.faces.component.UIComponent;
 37  
 import javax.faces.context.FacesContext;
 38  
 import javax.faces.convert.BigDecimalConverter;
 39  
 import javax.faces.convert.BigIntegerConverter;
 40  
 import javax.faces.convert.BooleanConverter;
 41  
 import javax.faces.convert.ByteConverter;
 42  
 import javax.faces.convert.CharacterConverter;
 43  
 import javax.faces.convert.Converter;
 44  
 import javax.faces.convert.DateTimeConverter;
 45  
 import javax.faces.convert.DoubleConverter;
 46  
 import javax.faces.convert.FloatConverter;
 47  
 import javax.faces.convert.IntegerConverter;
 48  
 import javax.faces.convert.LongConverter;
 49  
 import javax.faces.convert.ShortConverter;
 50  
 import javax.faces.el.MethodBinding;
 51  
 import javax.faces.el.PropertyResolver;
 52  
 import javax.faces.el.ReferenceSyntaxException;
 53  
 import javax.faces.el.ValueBinding;
 54  
 import javax.faces.el.VariableResolver;
 55  
 import javax.faces.event.ActionListener;
 56  
 import javax.faces.validator.Validator;
 57  
 
 58  
 import org.seasar.framework.util.AssertionUtil;
 59  
 import org.seasar.framework.util.ClassUtil;
 60  
 import org.seasar.framework.util.DateConversionUtil;
 61  
 import org.seasar.framework.util.StringUtil;
 62  
 import org.seasar.teeda.core.application.impl.DefaultComponentLookupStrategy;
 63  
 import org.seasar.teeda.core.convert.TimestampConverter;
 64  
 import org.seasar.teeda.core.el.MethodBindingFactory;
 65  
 import org.seasar.teeda.core.el.ValueBindingFactory;
 66  
 import org.seasar.teeda.core.exception.ConverterInstantiateFailureException;
 67  
 import org.seasar.teeda.core.exception.NoMethodBindingContextException;
 68  
 import org.seasar.teeda.core.exception.NoValueBindingContextException;
 69  
 import org.seasar.teeda.core.util.ApplicationUtil;
 70  
 import org.seasar.teeda.core.util.IteratorUtil;
 71  
 import org.seasar.teeda.core.util.PropertyDescUtil;
 72  
 
 73  
 /**
 74  
  * @author shot
 75  
  */
 76  
 public class ApplicationImpl extends Application implements
 77  
         ConfigurationSupport {
 78  
 
 79  1
     private static Map wellKnownConverters = new HashMap();
 80  
 
 81  21
     private ActionListener listener = null;
 82  
 
 83  21
     private Locale locale = null;
 84  
 
 85  21
     private String renderKitId = null;
 86  
 
 87  21
     private String bundle = null;
 88  
 
 89  21
     private NavigationHandler navigationHandler = null;
 90  
 
 91  21
     private PropertyResolver propertyResolver = null;
 92  
 
 93  21
     private VariableResolver variableResolver = null;
 94  
 
 95  21
     private ViewHandler viewHandler = null;
 96  
 
 97  21
     private StateManager stateManager = null;
 98  
 
 99  21
     private Map componentClassMap = Collections.synchronizedMap(new HashMap());
 100  
 
 101  21
     private Map converterIdMap = Collections.synchronizedMap(new HashMap());
 102  
 
 103  21
     private Map converterForClassMap = Collections
 104  
             .synchronizedMap(new HashMap());
 105  
 
 106  21
     private Map converterConfigurationMap = Collections
 107  
             .synchronizedMap(new HashMap());
 108  
 
 109  21
     private Map validatorMap = Collections.synchronizedMap(new HashMap());
 110  
 
 111  21
     private Collection supportedLocales = Collections.EMPTY_SET;
 112  
 
 113  21
     private ValueBindingFactory vbFactory = null;
 114  
 
 115  21
     private MethodBindingFactory mbFactory = null;
 116  
 
 117  21
     private ComponentLookupStrategy componentLookupStrategy = new DefaultComponentLookupStrategy();
 118  
 
 119  
     static {
 120  1
         registerWellKnownConverters();
 121  1
     }
 122  
 
 123  21
     public ApplicationImpl() {
 124  21
     }
 125  
 
 126  
     public static Converter getWellKnownConverter(Class clazz) {
 127  10
         return (Converter) wellKnownConverters.get(clazz);
 128  
     }
 129  
 
 130  
     protected static void registerWellKnownConverters() {
 131  1
         Converter converter = new BooleanConverter();
 132  16
         wellKnownConverters.put(Boolean.class, converter);
 133  1
         wellKnownConverters.put(boolean.class, converter);
 134  1
         converter = new ByteConverter();
 135  1
         wellKnownConverters.put(Byte.class, converter);
 136  1
         wellKnownConverters.put(byte.class, converter);
 137  1
         converter = new ShortConverter();
 138  1
         wellKnownConverters.put(Short.class, converter);
 139  1
         wellKnownConverters.put(short.class, converter);
 140  1
         converter = new IntegerConverter();
 141  1
         wellKnownConverters.put(Integer.class, converter);
 142  1
         wellKnownConverters.put(int.class, converter);
 143  1
         converter = new LongConverter();
 144  1
         wellKnownConverters.put(Long.class, converter);
 145  1
         wellKnownConverters.put(long.class, converter);
 146  1
         converter = new FloatConverter();
 147  1
         wellKnownConverters.put(Float.class, converter);
 148  1
         wellKnownConverters.put(float.class, converter);
 149  1
         converter = new DoubleConverter();
 150  1
         wellKnownConverters.put(Double.class, converter);
 151  1
         wellKnownConverters.put(double.class, converter);
 152  1
         converter = new BigIntegerConverter();
 153  1
         wellKnownConverters.put(BigInteger.class, converter);
 154  1
         converter = new BigDecimalConverter();
 155  1
         wellKnownConverters.put(BigDecimal.class, converter);
 156  1
         converter = new CharacterConverter();
 157  1
         wellKnownConverters.put(Character.class, converter);
 158  1
         wellKnownConverters.put(char.class, converter);
 159  1
         DateTimeConverter dateTimeConverter = new DateTimeConverter();
 160  1
         dateTimeConverter.setPattern(DateConversionUtil.getY4Pattern(Locale
 161  
                 .getDefault()));
 162  1
         wellKnownConverters.put(Date.class, dateTimeConverter);
 163  1
         TimestampConverter timestampConverter = new TimestampConverter();
 164  1
         timestampConverter.setPattern(DateConversionUtil.getY4Pattern(Locale
 165  
                 .getDefault()));
 166  1
         wellKnownConverters.put(Timestamp.class, timestampConverter);
 167  
 
 168  1
     }
 169  
 
 170  
     public ActionListener getActionListener() {
 171  0
         return listener;
 172  
     }
 173  
 
 174  
     public void setActionListener(ActionListener listener) {
 175  0
         AssertionUtil.assertNotNull("ActionListener is null.", listener);
 176  0
         this.listener = listener;
 177  0
     }
 178  
 
 179  
     public Locale getDefaultLocale() {
 180  0
         return locale;
 181  
     }
 182  
 
 183  
     public void setDefaultLocale(Locale locale) {
 184  0
         AssertionUtil.assertNotNull("Locale is null.", locale);
 185  0
         this.locale = locale;
 186  0
     }
 187  
 
 188  
     public String getDefaultRenderKitId() {
 189  0
         return renderKitId;
 190  
     }
 191  
 
 192  
     public void setDefaultRenderKitId(String renderKitId) {
 193  0
         this.renderKitId = renderKitId;
 194  0
     }
 195  
 
 196  
     public String getMessageBundle() {
 197  0
         return bundle;
 198  
     }
 199  
 
 200  
     public void setMessageBundle(String bundle) {
 201  0
         AssertionUtil.assertNotNull("MessageBundle is null.", bundle);
 202  0
         this.bundle = bundle;
 203  0
     }
 204  
 
 205  
     public NavigationHandler getNavigationHandler() {
 206  0
         return navigationHandler;
 207  
     }
 208  
 
 209  
     public void setNavigationHandler(NavigationHandler handler) {
 210  0
         AssertionUtil.assertNotNull("NavigationHandler is null.", handler);
 211  0
         this.navigationHandler = handler;
 212  0
     }
 213  
 
 214  
     public PropertyResolver getPropertyResolver() {
 215  0
         return propertyResolver;
 216  
     }
 217  
 
 218  
     public void setPropertyResolver(PropertyResolver resolver) {
 219  0
         AssertionUtil.assertNotNull("PropertyResolver is null.", resolver);
 220  0
         this.propertyResolver = resolver;
 221  0
     }
 222  
 
 223  
     public VariableResolver getVariableResolver() {
 224  0
         return variableResolver;
 225  
     }
 226  
 
 227  
     public void setVariableResolver(VariableResolver resolver) {
 228  1
         AssertionUtil.assertNotNull("VariableResolver is null.", resolver);
 229  1
         this.variableResolver = resolver;
 230  1
     }
 231  
 
 232  
     public ViewHandler getViewHandler() {
 233  0
         return viewHandler;
 234  
     }
 235  
 
 236  
     public void setViewHandler(ViewHandler handler) {
 237  0
         AssertionUtil.assertNotNull("ViewHandler is null.", handler);
 238  0
         this.viewHandler = handler;
 239  0
     }
 240  
 
 241  
     public StateManager getStateManager() {
 242  0
         return stateManager;
 243  
     }
 244  
 
 245  
     public void setStateManager(StateManager manager) {
 246  0
         AssertionUtil.assertNotNull("StateManager is null.", manager);
 247  0
         this.stateManager = manager;
 248  0
     }
 249  
 
 250  
     public void addComponent(String componentType, String componentClassName) {
 251  5
         if (StringUtil.isEmpty(componentType)) {
 252  0
             throw new NullPointerException("componentType is null.");
 253  
         }
 254  5
         if (StringUtil.isEmpty(componentClassName)) {
 255  0
             throw new NullPointerException("componentClassName is null.");
 256  
         }
 257  5
         Class clazz = ClassUtil.forName(componentClassName);
 258  5
         ApplicationUtil.verifyClassType(UIComponent.class, clazz);
 259  4
         componentClassMap.put(componentType, clazz);
 260  4
     }
 261  
 
 262  
     public UIComponent createComponent(String componentType)
 263  
             throws FacesException {
 264  3
         if (StringUtil.isEmpty(componentType)) {
 265  0
             throw new NullPointerException("componentType is null.");
 266  
         }
 267  3
         Object component = componentLookupStrategy
 268  
                 .getComponentByName(componentType);
 269  3
         if (component != null) {
 270  1
             return (UIComponent) component;
 271  
         }
 272  2
         Class componentClass = (Class) componentClassMap.get(componentType);
 273  2
         if (componentClass == null) {
 274  0
             throw new FacesException("Undefined component type:"
 275  
                     + componentType);
 276  
         }
 277  2
         return (UIComponent) ClassUtil.newInstance(componentClass);
 278  
     }
 279  
 
 280  
     public UIComponent createComponent(ValueBinding vb, FacesContext context,
 281  
             String componentType) throws FacesException {
 282  2
         Object obj = vb.getValue(context);
 283  2
         if (obj instanceof UIComponent) {
 284  1
             return (UIComponent) obj;
 285  
         } else {
 286  1
             UIComponent component = createComponent(componentType);
 287  1
             vb.setValue(context, component);
 288  1
             return component;
 289  
         }
 290  
     }
 291  
 
 292  
     public Iterator getComponentTypes() {
 293  0
         return componentClassMap.keySet().iterator();
 294  
     }
 295  
 
 296  
     public void addConverter(String converterId, String converterClassName) {
 297  5
         if (StringUtil.isEmpty(converterId)) {
 298  0
             throw new NullPointerException("converterId is null");
 299  
         }
 300  5
         if (StringUtil.isEmpty(converterClassName)) {
 301  0
             throw new NullPointerException("converterClass is null");
 302  
         }
 303  5
         Class clazz = ClassUtil.forName(converterClassName);
 304  5
         ApplicationUtil.verifyClassType(Converter.class, clazz);
 305  5
         converterIdMap.put(converterId, clazz);
 306  5
     }
 307  
 
 308  
     public void addConverter(Class targetClass, String converterClassName) {
 309  7
         AssertionUtil.assertNotNull("targetClass is null", targetClass);
 310  7
         if (StringUtil.isEmpty(converterClassName)) {
 311  0
             throw new NullPointerException("converterClass is null");
 312  
         }
 313  7
         Class clazz = ClassUtil.forName(converterClassName);
 314  7
         ApplicationUtil.verifyClassType(Converter.class, clazz);
 315  7
         converterForClassMap.put(targetClass, clazz);
 316  7
     }
 317  
 
 318  
     public Converter createConverter(String converterId) {
 319  6
         AssertionUtil.assertNotNull("converterId is null", converterId);
 320  5
         Object component = componentLookupStrategy
 321  
                 .getComponentByName(converterId);
 322  5
         if (component != null) {
 323  0
             return (Converter) component;
 324  
         }
 325  5
         Class clazz = (Class) converterIdMap.get(converterId);
 326  
         try {
 327  5
             Converter converter = createConverterByConverterClass(clazz);
 328  5
             setConverterPropertiesFor(converterId, converter);
 329  5
             return converter;
 330  0
         } catch (Exception e) {
 331  0
             Object[] args = { converterId };
 332  0
             throw new ConverterInstantiateFailureException(args);
 333  
         }
 334  
     }
 335  
 
 336  
     public Converter createConverter(Class targetClass) {
 337  8
         AssertionUtil.assertNotNull("targetClass is null", targetClass);
 338  7
         return doCreateConverterByTargetClass(targetClass);
 339  
     }
 340  
 
 341  
     public void addConverterConfiguration(String converterId,
 342  
             ConverterConfiguration converterConfiguration) {
 343  3
         if (StringUtil.isEmpty(converterId)) {
 344  0
             throw new NullPointerException("converterId is null.");
 345  
         }
 346  3
         AssertionUtil.assertNotNull("converterConfiguration is null",
 347  
                 converterConfiguration);
 348  3
         List list = getConverterConfigurationList(converterId);
 349  3
         list.add(converterConfiguration);
 350  3
     }
 351  
 
 352  
     public void addConverterConfiguration(Class targetClass,
 353  
             ConverterConfiguration converterConfiguration) {
 354  0
         AssertionUtil.assertNotNull("targetClass is null", targetClass);
 355  0
         AssertionUtil.assertNotNull("converterConfiguration is null",
 356  
                 converterConfiguration);
 357  0
         List list = getConverterConfigurationList(targetClass);
 358  0
         list.add(converterConfiguration);
 359  0
     }
 360  
 
 361  
     private List getConverterConfigurationList(Object key) {
 362  3
         List list = (List) converterConfigurationMap.get(key);
 363  3
         if (list == null) {
 364  3
             list = new ArrayList();
 365  3
             converterConfigurationMap.put(key, list);
 366  
         }
 367  3
         return list;
 368  
     }
 369  
 
 370  
     private Converter createConverterByConverterClass(Class converterClass) {
 371  
         try {
 372  11
             Converter converter = (Converter) ClassUtil
 373  
                     .newInstance(converterClass);
 374  11
             return converter;
 375  0
         } catch (Exception e) {
 376  0
             Object[] args = { converterClass.getName() };
 377  0
             throw new ConverterInstantiateFailureException(args);
 378  
         }
 379  
     }
 380  
 
 381  
     private Converter doCreateConverterByTargetClass(Class targetClass) {
 382  9
         Converter converter = getWellKnownConverter(targetClass);
 383  9
         if (converter != null) {
 384  1
             return converter;
 385  
         }
 386  8
         converter = createConverterByTargetClass(targetClass);
 387  8
         if (converter == null) {
 388  2
             converter = createConverterByInterface(targetClass);
 389  
         }
 390  8
         if (converter == null) {
 391  1
             converter = createConverterBySuperClass(targetClass);
 392  
         }
 393  8
         if (converter == null) {
 394  0
             converter = createConverterForPrimitive(targetClass);
 395  
         }
 396  8
         if (converter != null) {
 397  8
             setConverterPropertiesFor(targetClass, converter);
 398  
         }
 399  8
         return converter;
 400  
     }
 401  
 
 402  
     private void setConverterPropertiesFor(Object key, Converter converter) {
 403  13
         List list = (List) converterConfigurationMap.get(key);
 404  13
         for (Iterator itr = IteratorUtil.getIterator(list); itr.hasNext();) {
 405  3
             ConverterConfiguration config = (ConverterConfiguration) itr.next();
 406  3
             if (config != null) {
 407  3
                 String propertyName = config.getPropertyName();
 408  3
                 PropertyDescUtil.setValue(converter, propertyName, config
 409  
                         .getDefaultValue());
 410  
             }
 411  
         }
 412  13
     }
 413  
 
 414  
     protected Converter createConverterByTargetClass(Class targetClass) {
 415  8
         Class converterClass = (Class) converterForClassMap.get(targetClass);
 416  8
         if (converterClass != null) {
 417  6
             return createConverterByConverterClass(converterClass);
 418  
         }
 419  2
         return null;
 420  
     }
 421  
 
 422  
     protected Converter createConverterByInterface(Class targetClass) {
 423  2
         Class[] interfaces = targetClass.getInterfaces();
 424  2
         if (interfaces != null) {
 425  2
             for (int i = 0; i < interfaces.length; i++) {
 426  1
                 Converter converter = doCreateConverterByTargetClass(interfaces[i]);
 427  1
                 if (converter != null) {
 428  1
                     return converter;
 429  
                 }
 430  
             }
 431  
         }
 432  1
         return null;
 433  
     }
 434  
 
 435  
     protected Converter createConverterBySuperClass(Class targetClass) {
 436  1
         Class superClass = targetClass.getSuperclass();
 437  1
         if (superClass != null) {
 438  1
             return doCreateConverterByTargetClass(superClass);
 439  
         }
 440  0
         return null;
 441  
     }
 442  
 
 443  
     protected Converter createConverterForPrimitive(Class targetClass) {
 444  0
         Class primitiveClass = ClassUtil.getWrapperClass(targetClass);
 445  0
         if (primitiveClass != null) {
 446  0
             return doCreateConverterByTargetClass(primitiveClass);
 447  
         }
 448  0
         return null;
 449  
     }
 450  
 
 451  
     public Iterator getConverterIds() {
 452  0
         return converterIdMap.keySet().iterator();
 453  
     }
 454  
 
 455  
     public Iterator getConverterTypes() {
 456  0
         return converterForClassMap.keySet().iterator();
 457  
     }
 458  
 
 459  
     public Iterator getSupportedLocales() {
 460  0
         return supportedLocales.iterator();
 461  
     }
 462  
 
 463  
     public void setSupportedLocales(Collection supportedLocales) {
 464  0
         AssertionUtil.assertNotNull("suppoertedLocales is null",
 465  
                 supportedLocales);
 466  0
         this.supportedLocales = supportedLocales;
 467  0
     }
 468  
 
 469  
     public void addValidator(String validatorId, String validatorClassName) {
 470  2
         if (StringUtil.isEmpty(validatorId)) {
 471  0
             throw new NullPointerException("Validator id is null.");
 472  
         }
 473  2
         if (StringUtil.isEmpty(validatorClassName)) {
 474  0
             throw new NullPointerException("Validator class is null.");
 475  
         }
 476  2
         Class clazz = ClassUtil.forName(validatorClassName);
 477  2
         ApplicationUtil.verifyClassType(Validator.class, clazz);
 478  1
         validatorMap.put(validatorId, clazz);
 479  1
     }
 480  
 
 481  
     public Validator createValidator(String validatorId) throws FacesException {
 482  2
         AssertionUtil.assertNotNull("validatorId is null", validatorId);
 483  2
         Object component = componentLookupStrategy
 484  
                 .getComponentByName(validatorId);
 485  2
         if (component != null) {
 486  1
             return (Validator) component;
 487  
         }
 488  1
         Class validatorClass = (Class) validatorMap.get(validatorId);
 489  1
         if (validatorClass == null) {
 490  0
             throw new FacesException("Undefined validator class(validatorId = "
 491  
                     + validatorId + ")");
 492  
         }
 493  1
         return (Validator) ClassUtil.newInstance(validatorClass);
 494  
     }
 495  
 
 496  
     public Iterator getValidatorIds() {
 497  0
         return validatorMap.keySet().iterator();
 498  
     }
 499  
 
 500  
     public MethodBinding createMethodBinding(String ref, Class[] params)
 501  
             throws ReferenceSyntaxException {
 502  0
         AssertionUtil.assertNotNull("ref is null", ref);
 503  0
         if (mbFactory == null) {
 504  0
             throw new NoMethodBindingContextException(ref, params);
 505  
         }
 506  0
         return mbFactory.createMethodBinding(this, ref, params);
 507  
     }
 508  
 
 509  
     public ValueBinding createValueBinding(String ref)
 510  
             throws ReferenceSyntaxException {
 511  1
         AssertionUtil.assertNotNull("ref is null", ref);
 512  1
         if (vbFactory == null) {
 513  0
             throw new NoValueBindingContextException(ref);
 514  
         }
 515  1
         return vbFactory.createValueBinding(this, ref);
 516  
     }
 517  
 
 518  
     public void setValueBindingFactory(ValueBindingFactory vbContextFactory) {
 519  2
         this.vbFactory = vbContextFactory;
 520  2
     }
 521  
 
 522  
     public void setMethodBindingFactory(MethodBindingFactory mbContextFactory) {
 523  1
         this.mbFactory = mbContextFactory;
 524  1
     }
 525  
 
 526  
     public ValueBindingFactory getValueBindingFactory() {
 527  0
         return vbFactory;
 528  
     }
 529  
 
 530  
     public MethodBindingFactory getMethodBindingFactory() {
 531  0
         return mbFactory;
 532  
     }
 533  
 
 534  
     public ComponentLookupStrategy getComponentLookupStrategy() {
 535  0
         return componentLookupStrategy;
 536  
     }
 537  
 
 538  
     public void setComponentLookupStrategy(
 539  
             ComponentLookupStrategy componentLookupStrategy) {
 540  20
         this.componentLookupStrategy = componentLookupStrategy;
 541  20
     }
 542  
 
 543  
 }