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;
18  
19  import java.lang.reflect.InvocationHandler;
20  import java.lang.reflect.InvocationTargetException;
21  import java.lang.reflect.Method;
22  import java.util.Iterator;
23  import java.util.Objects;
24  
25  /**
26   * <p>
27   * A specialized {@code InvocationHandler} implementation for supporting immutable configurations.
28   * </p>
29   * <p>
30   * An instance of this class is constructed with a reference to a {@code Configuration} object. All method invocations
31   * (which stem from the {@code ImmutableConfiguration} interface) are delegated to this object. That way all
32   * functionality is actually backed by the underlying {@code Configuration} implementation, but because the associated
33   * proxy only implements the {@code ImmutableConfiguration} interface manipulations are not possible.
34   * </p>
35   * <p>
36   * There is one caveat however: Some methods of the {@code ImmutableConfiguration} interface return an {@code Iterator}
37   * object. Using the iterator's {@code remove()} method it may be possible to remove keys from the underlying
38   * {@code Configuration} object. Therefore, in these cases a specialized {@code Iterator} is returned which does not
39   * support the remove operation.
40   * </p>
41   *
42   * @since 2.0
43   */
44  final class ImmutableConfigurationInvocationHandler implements InvocationHandler {
45      /**
46       * A specialized {@code Iterator} implementation which delegates to an underlying iterator, but does not support the
47       * {@code remove()} method.
48       */
49      private static final class ImmutableIterator implements Iterator<Object> {
50          /** The underlying iterator. */
51          private final Iterator<?> wrappedIterator;
52  
53          /**
54           * Creates a new instance of {@code ImmutableIterator} and sets the underlying iterator.
55           *
56           * @param it the underlying iterator
57           */
58          public ImmutableIterator(final Iterator<?> it) {
59              wrappedIterator = it;
60          }
61  
62          /**
63           * {@inheritDoc} This implementation just delegates to the underlying iterator.
64           */
65          @Override
66          public boolean hasNext() {
67              return wrappedIterator.hasNext();
68          }
69  
70          /**
71           * {@inheritDoc} This implementation just delegates to the underlying iterator.
72           */
73          @Override
74          public Object next() {
75              return wrappedIterator.next();
76          }
77  
78          /**
79           * {@inheritDoc} This implementation just throws an exception: removing objects is not supported.
80           */
81          @Override
82          public void remove() {
83              throw new UnsupportedOperationException("remove() operation not supported!");
84          }
85      }
86  
87      /**
88       * Handles the result from the method invocation on the wrapped configuration. This implementation wraps result objects
89       * if necessary so that the underlying configuration cannot be manipulated.
90       *
91       * @param result the result object
92       * @return the processed result object
93       */
94      private static Object handleResult(final Object result) {
95          if (result instanceof Iterator) {
96              return new ImmutableIterator((Iterator<?>) result);
97          }
98          return result;
99      }
100 
101     /** The underlying configuration object. */
102     private final Configuration wrappedConfiguration;
103 
104     /**
105      * Creates a new instance of {@code ImmutableConfigurationInvocationHandler} and initializes it with the wrapped
106      * configuration object.
107      *
108      * @param configuration the wrapped {@code Configuration} (must not be <strong>null</strong>)
109      * @throws NullPointerException if the {@code Configuration} is <strong>null</strong>
110      */
111     public ImmutableConfigurationInvocationHandler(final Configuration configuration) {
112         wrappedConfiguration = Objects.requireNonNull(configuration, "configuration");
113     }
114 
115     /**
116      * {@inheritDoc} This implementation delegates to the wrapped configuration object. Result objects are wrapped if
117      * necessary.
118      */
119     @Override
120     public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
121         try {
122             return handleResult(method.invoke(wrappedConfiguration, args));
123         } catch (final InvocationTargetException e) {
124             // unwrap
125             throw e.getCause();
126         }
127     }
128 }