KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > accessplan > AccessPlanFactory


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.accessplan;
17
18 import java.util.Map JavaDoc;
19
20 /**
21  * Factory to get an accesss plan appropriate for an object
22  */

23 public class AccessPlanFactory {
24
25   private static boolean bytecodeEnhancementEnabled = false;
26
27   private AccessPlanFactory() {
28   }
29
30   /**
31    * Creates an access plan for working with a bean
32    *
33    * @param clazz
34    * @param propertyNames
35    * @return An access plan
36    */

37   public static AccessPlan getAccessPlan(Class JavaDoc clazz, String JavaDoc[] propertyNames) {
38     AccessPlan plan;
39
40     boolean complex = false;
41
42     if (clazz == null || propertyNames == null) {
43       complex = true;
44     } else {
45       for (int i = 0; i < propertyNames.length; i++) {
46         if (propertyNames[i].indexOf('[') > -1
47             || propertyNames[i].indexOf('.') > -1) {
48           complex = true;
49           break;
50         }
51       }
52     }
53
54     if (complex) {
55       plan = new ComplexAccessPlan(clazz, propertyNames);
56     } else if (Map JavaDoc.class.isAssignableFrom(clazz)) {
57       plan = new MapAccessPlan(clazz, propertyNames);
58     } else {
59       // Possibly causes bug 945746 --but the bug is unconfirmed (can't be reproduced)
60
if (bytecodeEnhancementEnabled) {
61         try {
62           plan = new EnhancedPropertyAccessPlan(clazz, propertyNames);
63         } catch (Throwable JavaDoc t) {
64           try {
65             plan = new PropertyAccessPlan(clazz, propertyNames);
66           } catch (Throwable JavaDoc t2) {
67             plan = new ComplexAccessPlan(clazz, propertyNames);
68           }
69         }
70       } else {
71         try {
72           plan = new PropertyAccessPlan(clazz, propertyNames);
73         } catch (Throwable JavaDoc t) {
74           plan = new ComplexAccessPlan(clazz, propertyNames);
75         }
76       }
77     }
78     return plan;
79   }
80
81   /**
82    * Tells whether or not bytecode enhancement (CGLIB, etc) is enabled
83    *
84    * @return true if bytecode enhancement is enabled
85    */

86   public static boolean isBytecodeEnhancementEnabled() {
87     return bytecodeEnhancementEnabled;
88   }
89
90   /**
91    * Turns on or off bytecode enhancement (CGLIB, etc)
92    *
93    * @param bytecodeEnhancementEnabled - the switch
94    */

95   public static void setBytecodeEnhancementEnabled(boolean bytecodeEnhancementEnabled) {
96     AccessPlanFactory.bytecodeEnhancementEnabled = bytecodeEnhancementEnabled;
97   }
98
99 }
100
Popular Tags