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    *     https://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  
44      /** The source object with the properties to be initialized. */
45      private final BuilderParameters source;
46  
47      /**
48       * Creates a new instance of {@code CopyObjectDefaultHandler} and initializes it with the specified source object. The
49       * properties defined by the source object are copied onto the objects to be initialized.
50       *
51       * @param src the source object (must not be <strong>null</strong>)
52       * @throws IllegalArgumentException if the source object is <strong>null</strong>
53       */
54      public CopyObjectDefaultHandler(final BuilderParameters src) {
55          if (src == null) {
56              throw new IllegalArgumentException("Source object must not be null.");
57          }
58          source = src;
59      }
60  
61      /**
62       * Gets the source object of this handler. This is the object whose properties are copied on the objects to be
63       * initialized.
64       *
65       * @return the source object of this {@code CopyObjectDefaultHandler}
66       */
67      public BuilderParameters getSource() {
68          return source;
69      }
70  
71      /**
72       * {@inheritDoc} This implementation uses {@code PropertyUtils.copyProperties()} to copy all defined properties from the
73       * source object onto the passed in parameters object. Both the map with properties (obtained via the
74       * {@code getParameters()} method of the source parameters object) and other properties of the source object are copied.
75       *
76       * @throws ConfigurationRuntimeException if an exception occurs
77       * @see BuilderParameters#getParameters()
78       */
79      @Override
80      public void initializeDefaults(final Object parameters) {
81          try {
82              BeanHelper.copyProperties(parameters, getSource().getParameters());
83              BeanHelper.copyProperties(parameters, getSource());
84          } catch (final Exception e) {
85              // Handle all reflection-related exceptions the same way
86              throw new ConfigurationRuntimeException(e);
87          }
88      }
89  }