KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > builders > ObjectGetOrCreateRule


1 /*
2  * $Id: ObjectGetOrCreateRule.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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 JavaDoc;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23
24 /**
25  * A digester rule that will either create an object of look it up from a container.
26  *
27  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
28  * @version $Revision: 3798 $
29  */

30 public class ObjectGetOrCreateRule extends ObjectCreateRule
31 {
32     public static final String JavaDoc DEFAULT_REF_ATTRIBUTE = "ref";
33     public static final String JavaDoc DEFAULT_CLASSNAME_ATTRIBUTE = "className";
34     protected String JavaDoc refAttrib = DEFAULT_REF_ATTRIBUTE;
35     protected String JavaDoc classAttrib = DEFAULT_CLASSNAME_ATTRIBUTE;
36     protected boolean classRefRequired = false;
37     protected String JavaDoc containerMethodName;
38     protected UMOContainerContext context;
39     protected String JavaDoc containerAttrib;
40
41     public ObjectGetOrCreateRule(String JavaDoc defaultImpl, String JavaDoc className, String JavaDoc containerMethodName)
42     {
43         this(defaultImpl, className, DEFAULT_REF_ATTRIBUTE, false, containerMethodName);
44     }
45
46     public ObjectGetOrCreateRule(String JavaDoc defaultImpl,
47                                  String JavaDoc className,
48                                  boolean classRefRequired,
49                                  String JavaDoc containerMethodName)
50     {
51         this(defaultImpl, className, DEFAULT_REF_ATTRIBUTE, classRefRequired, containerMethodName);
52     }
53
54     public ObjectGetOrCreateRule(String JavaDoc defaultImpl,
55                                  String JavaDoc className,
56                                  String JavaDoc refAttrib,
57                                  boolean classRefRequired,
58                                  String JavaDoc containerMethodName)
59     {
60         super(defaultImpl, className);
61         this.refAttrib = refAttrib;
62         this.classRefRequired = classRefRequired;
63         this.containerMethodName = containerMethodName;
64     }
65
66     public ObjectGetOrCreateRule(String JavaDoc defaultImpl,
67                                  String JavaDoc className,
68                                  String JavaDoc refAttrib,
69                                  String JavaDoc classAttrib,
70                                  boolean classRefRequired,
71                                  String JavaDoc 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 JavaDoc defaultImpl,
81                                  String JavaDoc className,
82                                  String JavaDoc refAttrib,
83                                  String JavaDoc containerAttrib,
84                                  String JavaDoc classAttrib,
85                                  boolean classRefRequired,
86                                  String JavaDoc 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     /**
97      * This method is deprecated in the Digester API however the API still uses it
98      * and we must overload it in order to customse the ObjectCreateRuleBehaviour
99      *
100      * @param attributes
101      * @throws Exception
102      */

103     public void begin(Attributes JavaDoc attributes) throws Exception JavaDoc
104     {
105
106         String JavaDoc ref = attributes.getValue(refAttrib);
107         String JavaDoc container = null;
108         if (containerAttrib != null)
109         {
110             container = attributes.getValue(containerAttrib);
111         }
112         if (ref != null)
113         {
114             Object JavaDoc cRef = ref;
115             if (container != null)
116             {
117                 cRef = new ContainerKeyPair(container, ref);
118             }
119             Object JavaDoc obj = getContainer().getComponent(cRef);
120             digester.push(obj);
121         }
122         else
123         {
124             String JavaDoc 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 JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
140     {
141         if (context == null)
142         {
143             Object JavaDoc root = digester.getRoot();
144             context = (UMOContainerContext)MethodUtils.invokeMethod(root, containerMethodName, null);
145             if (context == null)
146             {
147                 throw new NullPointerException JavaDoc(
148                     new Message(Messages.X_IS_NULL, "Container context").toString());
149             }
150         }
151         return context;
152     }
153 }
154
Popular Tags