    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.builder;
    018
    019import org.apache.commons.configuration2.beanutils.BeanHelper;
    020import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
    021
    022/**
    023 * <p>
    024 * A specialized implementation of {@code DefaultParametersHandler} that copies the properties of a
    025 * {@link BuilderParameters} object (passed at construction time) onto the object to be initialized.
    026 * </p>
    027 * <p>
    028 * Using this handler implementation makes specifying default values pretty straight-forward: Just create a
    029 * corresponding parameters object, initialize it as desired, and pass it to this class. When invoked the handler uses
    030 * functionality from <em>Commons BeanUtils</em> to copy all properties defined in the associated parameters object onto
    031 * the target object. This is based on reflection. Properties not available for the target object are silently ignored.
    032 * If an exception occurs during the copy operation, it is re-thrown as a runtime exception.
    033 * </p>
    034 * <p>
    035 * Note that there is no default way to create a defensive copy of the passed in parameters object; therefore, the
    036 * reference is stored. This makes it possible to change the parameters object later on, and the changes will be
    037 * effective when initializing objects afterwards. Client code should not rely on this feature.
    038 * </p>
    039 *
    040 * @since 2.0
    041 */
    042public class CopyObjectDefaultHandler implements DefaultParametersHandler<Object> {
    043    /** The source object with the properties to be initialized. */
    044    private final BuilderParameters source;
    045
    046    /**
    047     * Creates a new instance of {@code CopyObjectDefaultHandler} and initializes it with the specified source object. The
    048     * properties defined by the source object are copied onto the objects to be initialized.
    049     *
    050     * @param src the source object (must not be <strong>null</strong>)
    051     * @throws IllegalArgumentException if the source object is <strong>null</strong>
    052     */
    053    public CopyObjectDefaultHandler(final BuilderParameters src) {
    054        if (src == null) {
    055            throw new IllegalArgumentException("Source object must not be null!");
    056        }
    057        source = src;
    058    }
    059
    060    /**
    061     * Gets the source object of this handler. This is the object whose properties are copied on the objects to be
    062     * initialized.
    063     *
    064     * @return the source object of this {@code CopyObjectDefaultHandler}
    065     */
    066    public BuilderParameters getSource() {
    067        return source;
    068    }
    069
    070    /**
    071     * {@inheritDoc} This implementation uses {@code PropertyUtils.copyProperties()} to copy all defined properties from the
    072     * source object onto the passed in parameters object. Both the map with properties (obtained via the
    073     * {@code getParameters()} method of the source parameters object) and other properties of the source object are copied.
    074     *
    075     * @throws ConfigurationRuntimeException if an exception occurs
    076     * @see BuilderParameters#getParameters()
    077     */
    078    @Override
    079    public void initializeDefaults(final Object parameters) {
    080        try {
    081            BeanHelper.copyProperties(parameters, getSource().getParameters());
    082            BeanHelper.copyProperties(parameters, getSource());
    083        } catch (final Exception e) {
    084            // Handle all reflection-related exceptions the same way
    085            throw new ConfigurationRuntimeException(e);
    086        }
    087    }
    088}