001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.accessplan;
017:
018: import java.util.Map;
019:
020: /**
021: * Factory to get an accesss plan appropriate for an object
022: */
023: public class AccessPlanFactory {
024:
025: private static boolean bytecodeEnhancementEnabled = false;
026:
027: private AccessPlanFactory() {
028: }
029:
030: /**
031: * Creates an access plan for working with a bean
032: *
033: * @param clazz
034: * @param propertyNames
035: * @return An access plan
036: */
037: public static AccessPlan getAccessPlan(Class clazz,
038: String[] propertyNames) {
039: AccessPlan plan;
040:
041: boolean complex = false;
042:
043: if (clazz == null || propertyNames == null) {
044: complex = true;
045: } else {
046: for (int i = 0; i < propertyNames.length; i++) {
047: if (propertyNames[i].indexOf('[') > -1
048: || propertyNames[i].indexOf('.') > -1) {
049: complex = true;
050: break;
051: }
052: }
053: }
054:
055: if (complex) {
056: plan = new ComplexAccessPlan(clazz, propertyNames);
057: } else if (Map.class.isAssignableFrom(clazz)) {
058: plan = new MapAccessPlan(clazz, propertyNames);
059: } else {
060: // Possibly causes bug 945746 --but the bug is unconfirmed (can't be reproduced)
061: if (bytecodeEnhancementEnabled) {
062: try {
063: plan = new EnhancedPropertyAccessPlan(clazz,
064: propertyNames);
065: } catch (Throwable t) {
066: try {
067: plan = new PropertyAccessPlan(clazz,
068: propertyNames);
069: } catch (Throwable t2) {
070: plan = new ComplexAccessPlan(clazz,
071: propertyNames);
072: }
073: }
074: } else {
075: try {
076: plan = new PropertyAccessPlan(clazz, propertyNames);
077: } catch (Throwable t) {
078: plan = new ComplexAccessPlan(clazz, propertyNames);
079: }
080: }
081: }
082: return plan;
083: }
084:
085: /**
086: * Tells whether or not bytecode enhancement (CGLIB, etc) is enabled
087: *
088: * @return true if bytecode enhancement is enabled
089: */
090: public static boolean isBytecodeEnhancementEnabled() {
091: return bytecodeEnhancementEnabled;
092: }
093:
094: /**
095: * Turns on or off bytecode enhancement (CGLIB, etc)
096: *
097: * @param bytecodeEnhancementEnabled - the switch
098: */
099: public static void setBytecodeEnhancementEnabled(
100: boolean bytecodeEnhancementEnabled) {
101: AccessPlanFactory.bytecodeEnhancementEnabled = bytecodeEnhancementEnabled;
102: }
103:
104: }
|