View Javadoc
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  
19  import org.apache.commons.configuration2.beanutils.BeanHelper;
20  import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
21  
22  /**
23   * <p>
24   * A specialized implementation of {@code DefaultParametersHandler} that copies the properties of a
25   * {@link BuilderParameters} object (passed at construction time) onto the object to be initialized.
26   * </p>
27   * <p>
28   * Using this handler implementation makes specifying default values pretty straight-forward: Just create a
29   * corresponding parameters object, initialize it as desired, and pass it to this class. When invoked the handler uses
30   * functionality from <em>Commons BeanUtils</em> to copy all properties defined in the associated parameters object onto
31   * the target object. This is based on reflection. Properties not available for the target object are silently ignored.
32   * If an exception occurs during the copy operation, it is re-thrown as a runtime exception.
33   * </p>
34   * <p>
35   * Note that there is no default way to create a defensive copy of the passed in parameters object; therefore, the
36   * reference is stored. This makes it possible to change the parameters object later on, and the changes will be
37   * effective when initializing objects afterwards. Client code should not rely on this feature.
38   * </p>
39   *
40   * @since 2.0
41   */
42  public class CopyObjectDefaultHandler implements DefaultParametersHandler<Object> {
43      /** The source object with the properties to be initialized. */
44      private final BuilderParameters source;
45  
46      /**
47       * Creates a new instance of {@code CopyObjectDefaultHandler} and initializes it with the specified source object. The
48       * properties defined by the source object are copied onto the objects to be initialized.
49       *
50       * @param src the source object (must not be <b>null</b>)
51       * @throws IllegalArgumentException if the source object is <b>null</b>
52       */
53      public CopyObjectDefaultHandler(final BuilderParameters src) {
54          if (src == null) {
55              throw new IllegalArgumentException("Source object must not be null!");
56          }
57          source = src;
58      }
59  
60      /**
61       * Gets the source object of this handler. This is the object whose properties are copied on the objects to be
62       * initialized.
63       *
64       * @return the source object of this {@code CopyObjectDefaultHandler}
65       */
66      public BuilderParameters getSource() {
67          return source;
68      }
69  
70      /**
71       * {@inheritDoc} This implementation uses {@code PropertyUtils.copyProperties()} to copy all defined properties from the
72       * source object onto the passed in parameters object. Both the map with properties (obtained via the
73       * {@code getParameters()} method of the source parameters object) and other properties of the source object are copied.
74       *
75       * @throws ConfigurationRuntimeException if an exception occurs
76       * @see BuilderParameters#getParameters()
77       */
78      @Override
79      public void initializeDefaults(final Object parameters) {
80          try {
81              BeanHelper.copyProperties(parameters, getSource().getParameters());
82              BeanHelper.copyProperties(parameters, getSource());
83          } catch (final Exception e) {
84              // Handle all reflection-related exceptions the same way
85              throw new ConfigurationRuntimeException(e);
86          }
87      }
88  }