KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > descriptors > copying > CloneCopyPolicy


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.descriptors.copying;
23
24 import java.lang.reflect.*;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedActionException JavaDoc;
27
28 import oracle.toplink.essentials.exceptions.*;
29 import oracle.toplink.essentials.internal.helper.*;
30 import oracle.toplink.essentials.queryframework.ObjectBuildingQuery;
31 import oracle.toplink.essentials.sessions.*;
32 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
33 import oracle.toplink.essentials.internal.descriptors.ObjectBuilder;
34 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
35 import oracle.toplink.essentials.internal.security.PrivilegedMethodInvoker;
36
37 /**
38  * <p><b>Purpose</b>: Creates a clone through a clone method.
39  */

40 public class CloneCopyPolicy extends AbstractCopyPolicy {
41
42     /** Allow for clone method to be specified. */
43     protected String JavaDoc methodName;
44     protected String JavaDoc workingCopyMethodName;
45     protected transient Method method;
46     protected transient Method workingCopyMethod;
47
48     public CloneCopyPolicy() {
49         super();
50     }
51
52     /**
53      * Clone through calling the clone method.
54      */

55     public Object JavaDoc buildClone(Object JavaDoc domainObject, Session session) throws DescriptorException {
56         // Must allow for null clone method for 9.0.4 deployment XML.
57
if (this.getMethodName() == null) {
58             return getDescriptor().getObjectBuilder().buildNewInstance();
59         }
60         try {
61             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
62                 try {
63                     return AccessController.doPrivileged(new PrivilegedMethodInvoker(this.getMethod(), domainObject, new Object JavaDoc[0]));
64                 } catch (PrivilegedActionException JavaDoc exception) {
65                     Exception JavaDoc throwableException = exception.getException();
66                     if (throwableException instanceof IllegalAccessException JavaDoc) {
67                         throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException);
68                     } else {
69                         throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException);
70
71                     }
72                 }
73             } else {
74                 return PrivilegedAccessHelper.invokeMethod(this.getMethod(), domainObject, new Object JavaDoc[0]);
75             }
76         } catch (IllegalAccessException JavaDoc exception) {
77             throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception);
78         } catch (InvocationTargetException exception) {
79             throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception);
80         }
81     }
82
83     /**
84      * Clone through the workingCopyClone method, or if not specified the clone method.
85      */

86     public Object JavaDoc buildWorkingCopyClone(Object JavaDoc domainObject, Session session) throws DescriptorException {
87         if (this.getWorkingCopyMethodName() == null) {
88             //not implemented to perform special operations.
89
return this.buildClone(domainObject, session);
90         }
91
92         try {
93             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
94                 try {
95                     return AccessController.doPrivileged(new PrivilegedMethodInvoker(this.getWorkingCopyMethod(), domainObject, new Object JavaDoc[0]));
96                 } catch (PrivilegedActionException JavaDoc exception) {
97                     Exception JavaDoc throwableException = exception.getException();
98                     if (throwableException instanceof IllegalAccessException JavaDoc) {
99                         throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException);
100                     } else {
101                         throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException);
102                     }
103                 }
104             } else {
105                 return PrivilegedAccessHelper.invokeMethod(this.getWorkingCopyMethod(), domainObject, new Object JavaDoc[0]);
106             }
107         
108         } catch (IllegalAccessException JavaDoc exception) {
109             throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception);
110         } catch (InvocationTargetException exception) {
111             throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception);
112         }
113     }
114
115     /**
116      * Create a new instance, unless a workingCopyClone method is specified, then build a new instance and clone it.
117      */

118     public Object JavaDoc buildWorkingCopyCloneFromRow(Record row, ObjectBuildingQuery query) throws DescriptorException {
119         // For now must preserve CMP code which builds heavy clones with a context.
120
// Also preserve for clients who use the copy policy.
121
ObjectBuilder builder = getDescriptor().getObjectBuilder();
122         if (getWorkingCopyMethodName() != null) {
123             Object JavaDoc original = builder.buildNewInstance();
124             builder.buildAttributesIntoShallowObject(original, (AbstractRecord)row, query);
125             return buildWorkingCopyClone(original, query.getSession());
126         } else {
127             return builder.buildNewInstance();
128         }
129     }
130
131     /**
132      * Return the clone method.
133      */

134     protected Method getMethod() {
135         return method;
136     }
137
138     /**
139      * Return the clone method name.
140      */

141     public String JavaDoc getMethodName() {
142         return methodName;
143     }
144
145     /**
146      * Return the workingCopyClone method.
147      * This is used to clone within a unit of work.
148      */

149     protected Method getWorkingCopyMethod() {
150         return workingCopyMethod;
151     }
152
153     /**
154      * Return the workingCopyClone method name.
155      * This is used to clone within a unit of work.
156      */

157     public String JavaDoc getWorkingCopyMethodName() {
158         return workingCopyMethodName;
159     }
160
161     /**
162      * Validate and build the methods.
163      */

164     public void initialize(Session session) throws DescriptorException {
165         try {
166             // Must allow for null clone method for 9.0.4 deployment XML.
167
if (this.getMethodName() != null) {
168                 this.setMethod(Helper.getDeclaredMethod(this.getDescriptor().getJavaClass(), this.getMethodName(), new Class JavaDoc[0]));
169             }
170         } catch (NoSuchMethodException JavaDoc exception) {
171             session.getIntegrityChecker().handleError(DescriptorException.noSuchMethodWhileInitializingCopyPolicy(this.getMethodName(), this.getDescriptor(), exception));
172         } catch (SecurityException JavaDoc exception) {
173             session.getIntegrityChecker().handleError(DescriptorException.securityWhileInitializingCopyPolicy(this.getMethodName(), this.getDescriptor(), exception));
174         }
175         if (this.getWorkingCopyMethodName() != null) {
176             try {
177                 this.setWorkingCopyMethod(Helper.getDeclaredMethod(this.getDescriptor().getJavaClass(), this.getWorkingCopyMethodName(), new Class JavaDoc[0]));
178             } catch (NoSuchMethodException JavaDoc exception) {
179                 session.getIntegrityChecker().handleError(DescriptorException.noSuchMethodWhileInitializingCopyPolicy(this.getMethodName(), this.getDescriptor(), exception));
180             } catch (SecurityException JavaDoc exception) {
181                 session.getIntegrityChecker().handleError(DescriptorException.securityWhileInitializingCopyPolicy(this.getMethodName(), this.getDescriptor(), exception));
182             }
183         }
184     }
185
186     /**
187      * Set the clone method.
188      */

189     protected void setMethod(Method method) {
190         this.method = method;
191     }
192
193     /**
194      * Set the clone method name.
195      */

196     public void setMethodName(String JavaDoc methodName) {
197         this.methodName = methodName;
198     }
199
200     /**
201      * Set the workingCopyClone method.
202      * This is used to clone within a unit of work.
203      */

204     protected void setWorkingCopyMethod(Method method) {
205         this.workingCopyMethod = method;
206     }
207
208     /**
209      * Set the workingCopyClone method name.
210      * This is used to clone within a unit of work.
211      */

212     public void setWorkingCopyMethodName(String JavaDoc methodName) {
213         this.workingCopyMethodName = methodName;
214     }
215
216     /**
217      * Return false as a shallow clone is returned, not a new instance.
218      */

219     public boolean buildsNewInstance() {
220         return getMethodName() == null;
221     }
222
223     public String JavaDoc toString() {
224         return Helper.getShortClassName(this) + "(" + this.getMethodName() + ")";
225     }
226 }
227
Popular Tags