1 10 11 package org.mule.config.builders; 12 13 import org.apache.commons.beanutils.MethodUtils; 14 import org.apache.commons.digester.ObjectCreateRule; 15 import org.mule.config.ConfigurationException; 16 import org.mule.config.i18n.Message; 17 import org.mule.config.i18n.Messages; 18 import org.mule.impl.container.ContainerKeyPair; 19 import org.mule.umo.manager.UMOContainerContext; 20 import org.xml.sax.Attributes ; 21 22 import java.lang.reflect.InvocationTargetException ; 23 24 30 public class ObjectGetOrCreateRule extends ObjectCreateRule 31 { 32 public static final String DEFAULT_REF_ATTRIBUTE = "ref"; 33 public static final String DEFAULT_CLASSNAME_ATTRIBUTE = "className"; 34 protected String refAttrib = DEFAULT_REF_ATTRIBUTE; 35 protected String classAttrib = DEFAULT_CLASSNAME_ATTRIBUTE; 36 protected boolean classRefRequired = false; 37 protected String containerMethodName; 38 protected UMOContainerContext context; 39 protected String containerAttrib; 40 41 public ObjectGetOrCreateRule(String defaultImpl, String className, String containerMethodName) 42 { 43 this(defaultImpl, className, DEFAULT_REF_ATTRIBUTE, false, containerMethodName); 44 } 45 46 public ObjectGetOrCreateRule(String defaultImpl, 47 String className, 48 boolean classRefRequired, 49 String containerMethodName) 50 { 51 this(defaultImpl, className, DEFAULT_REF_ATTRIBUTE, classRefRequired, containerMethodName); 52 } 53 54 public ObjectGetOrCreateRule(String defaultImpl, 55 String className, 56 String refAttrib, 57 boolean classRefRequired, 58 String containerMethodName) 59 { 60 super(defaultImpl, className); 61 this.refAttrib = refAttrib; 62 this.classRefRequired = classRefRequired; 63 this.containerMethodName = containerMethodName; 64 } 65 66 public ObjectGetOrCreateRule(String defaultImpl, 67 String className, 68 String refAttrib, 69 String classAttrib, 70 boolean classRefRequired, 71 String containerMethodName) 72 { 73 super(defaultImpl, className); 74 this.refAttrib = refAttrib; 75 this.classRefRequired = classRefRequired; 76 this.containerMethodName = containerMethodName; 77 this.classAttrib = classAttrib; 78 } 79 80 public ObjectGetOrCreateRule(String defaultImpl, 81 String className, 82 String refAttrib, 83 String containerAttrib, 84 String classAttrib, 85 boolean classRefRequired, 86 String containerMethodName) 87 { 88 super(defaultImpl, className); 89 this.refAttrib = refAttrib; 90 this.containerAttrib = containerAttrib; 91 this.classRefRequired = classRefRequired; 92 this.containerMethodName = containerMethodName; 93 this.classAttrib = classAttrib; 94 } 95 96 103 public void begin(Attributes attributes) throws Exception 104 { 105 106 String ref = attributes.getValue(refAttrib); 107 String container = null; 108 if (containerAttrib != null) 109 { 110 container = attributes.getValue(containerAttrib); 111 } 112 if (ref != null) 113 { 114 Object cRef = ref; 115 if (container != null) 116 { 117 cRef = new ContainerKeyPair(container, ref); 118 } 119 Object obj = getContainer().getComponent(cRef); 120 digester.push(obj); 121 } 122 else 123 { 124 String classRef = attributes.getValue(classAttrib); 125 if (classRef == null && classRefRequired) 126 { 127 throw new ConfigurationException(new Message( 128 Messages.MUST_SPECIFY_REF_ATTRIB_X_OR_CLASS_ATTRIB_X_FOR_X, refAttrib, classAttrib, 129 this.digester.getCurrentElementName())); 130 } 131 else 132 { 133 super.begin(attributes); 134 } 135 } 136 } 137 138 protected UMOContainerContext getContainer() 139 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException 140 { 141 if (context == null) 142 { 143 Object root = digester.getRoot(); 144 context = (UMOContainerContext)MethodUtils.invokeMethod(root, containerMethodName, null); 145 if (context == null) 146 { 147 throw new NullPointerException ( 148 new Message(Messages.X_IS_NULL, "Container context").toString()); 149 } 150 } 151 return context; 152 } 153 } 154 | Popular Tags |