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 /**
47 * A specialized {@code Iterator} implementation which delegates to an underlying iterator, but does not support the
48 * {@code remove()} method.
49 */
50 private static final class ImmutableIterator implements Iterator<Object> {
51
52 /** The underlying iterator. */
53 private final Iterator<?> wrappedIterator;
54
55 /**
56 * Creates a new instance of {@code ImmutableIterator} and sets the underlying iterator.
57 *
58 * @param it the underlying iterator
59 */
60 public ImmutableIterator(final Iterator<?> it) {
61 wrappedIterator = it;
62 }
63
64 /**
65 * {@inheritDoc} This implementation just delegates to the underlying iterator.
66 */
67 @Override
68 public boolean hasNext() {
69 return wrappedIterator.hasNext();
70 }
71
72 /**
73 * {@inheritDoc} This implementation just delegates to the underlying iterator.
74 */
75 @Override
76 public Object next() {
77 return wrappedIterator.next();
78 }
79
80 /**
81 * {@inheritDoc} This implementation just throws an exception: removing objects is not supported.
82 */
83 @Override
84 public void remove() {
85 throw new UnsupportedOperationException("remove() operation not supported.");
86 }
87 }
88
89 /**
90 * Handles the result from the method invocation on the wrapped configuration. This implementation wraps result objects
91 * if necessary so that the underlying configuration cannot be manipulated.
92 *
93 * @param result the result object
94 * @return the processed result object
95 */
96 private static Object handleResult(final Object result) {
97 if (result instanceof Iterator) {
98 return new ImmutableIterator((Iterator<?>) result);
99 }
100 return result;
101 }
102
103 /** The underlying configuration object. */
104 private final Configuration wrappedConfiguration;
105
106 /**
107 * Creates a new instance of {@code ImmutableConfigurationInvocationHandler} and initializes it with the wrapped
108 * configuration object.
109 *
110 * @param configuration the wrapped {@code Configuration} (must not be <strong>null</strong>)
111 * @throws NullPointerException if the {@code Configuration} is <strong>null</strong>
112 */
113 public ImmutableConfigurationInvocationHandler(final Configuration configuration) {
114 wrappedConfiguration = Objects.requireNonNull(configuration, "configuration");
115 }
116
117 /**
118 * {@inheritDoc} This implementation delegates to the wrapped configuration object. Result objects are wrapped if
119 * necessary.
120 */
121 @Override
122 public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
123 try {
124 return handleResult(method.invoke(wrappedConfiguration, args));
125 } catch (final InvocationTargetException e) {
126 // unwrap
127 throw e.getCause();
128 }
129 }
130 }