    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
    044    /** The source object with the properties to be initialized. */
    045    private final BuilderParameters source;
    046
    047    /**
    048     * Creates a new instance of {@code CopyObjectDefaultHandler} and initializes it with the specified source object. The
    049     * properties defined by the source object are copied onto the objects to be initialized.
    050     *
    051     * @param src the source object (must not be <strong>null</strong>)
    052     * @throws IllegalArgumentException if the source object is <strong>null</strong>
    053     */
    054    public CopyObjectDefaultHandler(final BuilderParameters src) {
    055        if (src == null) {
    056            throw new IllegalArgumentException("Source object must not be null!");
    057        }
    058        source = src;
    059    }
    060
    061    /**
    062     * Gets the source object of this handler. This is the object whose properties are copied on the objects to be
    063     * initialized.
    064     *
    065     * @return the source object of this {@code CopyObjectDefaultHandler}
    066     */
    067    public BuilderParameters getSource() {
    068        return source;
    069    }
    070
    071    /**
    072     * {@inheritDoc} This implementation uses {@code PropertyUtils.copyProperties()} to copy all defined properties from the
    073     * source object onto the passed in parameters object. Both the map with properties (obtained via the
    074     * {@code getParameters()} method of the source parameters object) and other properties of the source object are copied.
    075     *
    076     * @throws ConfigurationRuntimeException if an exception occurs
    077     * @see BuilderParameters#getParameters()
    078     */
    079    @Override
    080    public void initializeDefaults(final Object parameters) {
    081        try {
    082            BeanHelper.copyProperties(parameters, getSource().getParameters());
    083            BeanHelper.copyProperties(parameters, getSource());
    084        } catch (final Exception e) {
    085            // Handle all reflection-related exceptions the same way
    086            throw new ConfigurationRuntimeException(e);
    087        }
    088    }
    089}