View Javadoc

1   /*
2    * $Id: EndpointReference.java 9539 2007-11-01 14:07:28Z akuzmin $
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 CPAL v1.0
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.mule.MuleManager;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.impl.endpoint.MuleEndpoint;
16  import org.mule.impl.endpoint.MuleEndpointURI;
17  import org.mule.umo.UMOFilter;
18  import org.mule.umo.UMOTransactionConfig;
19  import org.mule.umo.endpoint.UMOEndpoint;
20  import org.mule.umo.lifecycle.InitialisationException;
21  import org.mule.umo.transformer.UMOTransformer;
22  import org.mule.util.MuleObjectHelper;
23  
24  import java.lang.reflect.Method;
25  import java.util.Map;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * <code>EndpointReference</code> maintains a endpoint reference. Endpoints are
32   * cloned when they are looked up for the manager, if there are container properties
33   * or transformers set on the Endpoint the clone will have an inconsistent state if
34   * the transformers or container properties have not been resolved. This class holds
35   * the refernece and is invoked after the container properties/transformers are
36   * resolved.
37   */
38  public class EndpointReference
39  {
40      /**
41       * logger used by this class
42       */
43      protected static final Log logger = LogFactory.getLog(EndpointReference.class);
44  
45      private String propertyName;
46      private String endpointName;
47      private String address;
48      private String transformer;
49      private String responseTransformer;
50      private String createConnector;
51      private Object object;
52      private Map properties;
53      private UMOFilter filter;
54      private UMOTransactionConfig transactionConfig;
55  
56      public EndpointReference(String propertyName,
57                               String endpointName,
58                               String address,
59                               String transformer,
60                               String responseTransformer,
61                               String createConnector,
62                               Object object)
63      {
64          this.propertyName = propertyName;
65          this.endpointName = endpointName;
66          this.address = address;
67          this.transformer = transformer;
68          this.responseTransformer = responseTransformer;
69          this.object = object;
70          this.createConnector = createConnector;
71      }
72  
73      public String getPropertyName()
74      {
75          return propertyName;
76      }
77  
78      public String getEndpointName()
79      {
80          return endpointName;
81      }
82  
83      public Object getObject()
84      {
85          return object;
86      }
87  
88      public UMOTransactionConfig getTransactionConfig()
89      {
90          return transactionConfig;
91      }
92  
93      public void setTransactionConfig(UMOTransactionConfig transactionConfig)
94      {
95          this.transactionConfig = transactionConfig;
96      }
97  
98      public UMOFilter getFilter()
99      {
100         return filter;
101     }
102 
103     public void setFilter(UMOFilter filter)
104     {
105         this.filter = filter;
106     }
107 
108     public Map getProperties()
109     {
110         return properties;
111     }
112 
113     public void setProperties(Map properties)
114     {
115         this.properties = properties;
116     }
117 
118     public String getCreateConnector()
119     {
120         return createConnector;
121     }
122 
123     public void setCreateConnector(String createConnector)
124     {
125         this.createConnector = createConnector;
126     }
127 
128     public void resolveEndpoint() throws InitialisationException
129     {
130         try
131         {
132             MuleEndpoint ep = (MuleEndpoint) MuleManager.getInstance().lookupEndpoint(endpointName);
133             if (ep == null)
134             {
135                 throw new InitialisationException(
136                     CoreMessages.objectNotRegisteredWithManager("Endpoint '" + endpointName + "'"), this);
137             }
138             if (address != null)
139             {
140                 if (logger.isDebugEnabled())
141                 {
142                     logger.debug("Overloading endpoint uri for: " + endpointName + " from "
143                                  + ep.getEndpointURI().toString() + " to " + address);
144                 }
145                 ep.setEndpointURI(new MuleEndpointURI(address));
146             }
147             if (createConnector != null)
148             {
149                 if (logger.isDebugEnabled())
150                 {
151                     logger.debug("Overloading createConnector property for endpoint: " + endpointName
152                                  + " from " + ep.getCreateConnector() + " to " + createConnector);
153                 }
154                 ep.setCreateConnectorAsString(createConnector);
155             }
156             if (transformer != null)
157             {
158                 if (logger.isDebugEnabled())
159                 {
160                     logger.debug("Overloading Transformer for: " + endpointName + " from "
161                                  + ep.getTransformer() + " to " + transformer);
162                 }
163                 UMOTransformer trans = MuleObjectHelper.getTransformer(transformer, " ");
164                 ep.setTransformer(trans);
165             }
166 
167             if (responseTransformer != null)
168             {
169                 if (logger.isDebugEnabled())
170                 {
171                     logger.debug("Overloading responseTransformer for: " + endpointName + " from "
172                                  + ep.getResponseTransformer() + " to " + responseTransformer);
173                 }
174                 UMOTransformer trans = MuleObjectHelper.getTransformer(responseTransformer, " ");
175                 ep.setResponseTransformer(trans);
176             }
177 
178             if (filter != null)
179             {
180                 ep.setFilter(filter);
181             }
182             if (properties != null)
183             {
184                 ep.getProperties().putAll(properties);
185             }
186             if (transactionConfig != null)
187             {
188                 ep.setTransactionConfig(transactionConfig);
189             }
190 
191             Method m = object.getClass().getMethod(propertyName, new Class[]{UMOEndpoint.class});
192             if (m == null)
193             {
194                 throw new InitialisationException(
195                     CoreMessages.methodWithParamsNotFoundOnObject(propertyName, 
196                         UMOEndpoint.class, object.getClass()), this);
197             }
198 
199             m.invoke(object, new Object[]{ep});
200         }
201         catch (InitialisationException e)
202         {
203             throw e;
204         }
205         catch (Exception e)
206         {
207             throw new InitialisationException(
208                 CoreMessages.cannotSetPropertyOnObjectWithParamType(propertyName, 
209                     object.getClass(), UMOEndpoint.class), e, this);
210         }
211     }
212 
213     public String toString()
214     {
215         return "EndpointReference{" + "propertyName='" + propertyName + "'" + ", endpointName='"
216                + endpointName + "'" + ", address='" + address + "'" + ", transformer='" + transformer + "'"
217                + ",  responseTransformer='" + responseTransformer + "'" + ", object=" + object
218                + ", properties=" + properties + ", filter=" + filter + ", transactionConfig="
219                + transactionConfig + "}";
220     }
221 }