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 * http://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
18 package org.apache.commons.proxy.provider;
19
20 import org.apache.commons.proxy.ObjectProvider;
21 import org.apache.commons.proxy.ProxyUtils;
22 import org.apache.commons.proxy.exception.ObjectProviderException;
23
24 import java.io.Serializable;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27
28 /**
29 * Merely calls <code>clone()</code> (reflectively) on the given {@link Cloneable} object.
30 *
31 * @author James Carman
32 * @since 1.0
33 */
34 public class CloningProvider implements ObjectProvider, Serializable
35 {
36 //**********************************************************************************************************************
37 // Fields
38 //**********************************************************************************************************************
39
40 private final Cloneable cloneable;
41 private Method cloneMethod;
42
43 //**********************************************************************************************************************
44 // Constructors
45 //**********************************************************************************************************************
46
47 /**
48 * Constructs a provider which returns clone copies of the specified {@link Cloneable}
49 * object.
50 *
51 * @param cloneable the object to clone
52 */
53 public CloningProvider( Cloneable cloneable )
54 {
55 this.cloneable = cloneable;
56 }
57
58 //**********************************************************************************************************************
59 // ObjectProvider Implementation
60 //**********************************************************************************************************************
61
62
63 public Object getObject()
64 {
65 try
66 {
67 return getCloneMethod().invoke(cloneable, ProxyUtils.EMPTY_ARGUMENTS);
68 }
69 catch( IllegalAccessException e )
70 {
71 throw new ObjectProviderException(
72 "Class " + cloneable.getClass().getName() + " does not have a public clone() method.", e);
73 }
74 catch( InvocationTargetException e )
75 {
76 throw new ObjectProviderException(
77 "Attempt to clone object of type " + cloneable.getClass().getName() + " threw an exception.", e);
78 }
79 }
80
81 //**********************************************************************************************************************
82 // Getter/Setter Methods
83 //**********************************************************************************************************************
84
85 private synchronized Method getCloneMethod()
86 {
87 if( cloneMethod == null )
88 {
89 try
90 {
91 cloneMethod = cloneable.getClass().getMethod("clone", ProxyUtils.EMPTY_ARGUMENT_TYPES);
92 }
93 catch( NoSuchMethodException e )
94 {
95 throw new ObjectProviderException(
96 "Class " + cloneable.getClass().getName() + " does not have a public clone() method.");
97 }
98 }
99 return cloneMethod;
100 }
101 }