CopyObjectDefaultHandler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.builder;

  18. import org.apache.commons.configuration2.beanutils.BeanHelper;
  19. import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;

  20. /**
  21.  * <p>
  22.  * A specialized implementation of {@code DefaultParametersHandler} that copies the properties of a
  23.  * {@link BuilderParameters} object (passed at construction time) onto the object to be initialized.
  24.  * </p>
  25.  * <p>
  26.  * Using this handler implementation makes specifying default values pretty straight-forward: Just create a
  27.  * corresponding parameters object, initialize it as desired, and pass it to this class. When invoked the handler uses
  28.  * functionality from <em>Commons BeanUtils</em> to copy all properties defined in the associated parameters object onto
  29.  * the target object. This is based on reflection. Properties not available for the target object are silently ignored.
  30.  * If an exception occurs during the copy operation, it is re-thrown as a runtime exception.
  31.  * </p>
  32.  * <p>
  33.  * Note that there is no default way to create a defensive copy of the passed in parameters object; therefore, the
  34.  * reference is stored. This makes it possible to change the parameters object later on, and the changes will be
  35.  * effective when initializing objects afterwards. Client code should not rely on this feature.
  36.  * </p>
  37.  *
  38.  * @since 2.0
  39.  */
  40. public class CopyObjectDefaultHandler implements DefaultParametersHandler<Object> {
  41.     /** The source object with the properties to be initialized. */
  42.     private final BuilderParameters source;

  43.     /**
  44.      * Creates a new instance of {@code CopyObjectDefaultHandler} and initializes it with the specified source object. The
  45.      * properties defined by the source object are copied onto the objects to be initialized.
  46.      *
  47.      * @param src the source object (must not be <strong>null</strong>)
  48.      * @throws IllegalArgumentException if the source object is <strong>null</strong>
  49.      */
  50.     public CopyObjectDefaultHandler(final BuilderParameters src) {
  51.         if (src == null) {
  52.             throw new IllegalArgumentException("Source object must not be null!");
  53.         }
  54.         source = src;
  55.     }

  56.     /**
  57.      * Gets the source object of this handler. This is the object whose properties are copied on the objects to be
  58.      * initialized.
  59.      *
  60.      * @return the source object of this {@code CopyObjectDefaultHandler}
  61.      */
  62.     public BuilderParameters getSource() {
  63.         return source;
  64.     }

  65.     /**
  66.      * {@inheritDoc} This implementation uses {@code PropertyUtils.copyProperties()} to copy all defined properties from the
  67.      * source object onto the passed in parameters object. Both the map with properties (obtained via the
  68.      * {@code getParameters()} method of the source parameters object) and other properties of the source object are copied.
  69.      *
  70.      * @throws ConfigurationRuntimeException if an exception occurs
  71.      * @see BuilderParameters#getParameters()
  72.      */
  73.     @Override
  74.     public void initializeDefaults(final Object parameters) {
  75.         try {
  76.             BeanHelper.copyProperties(parameters, getSource().getParameters());
  77.             BeanHelper.copyProperties(parameters, getSource());
  78.         } catch (final Exception e) {
  79.             // Handle all reflection-related exceptions the same way
  80.             throw new ConfigurationRuntimeException(e);
  81.         }
  82.     }
  83. }