View Javadoc

1   /*
2    * $Id: EndpointConverter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.converters;
12  
13  import org.mule.MuleManager;
14  import org.mule.impl.endpoint.MuleEndpoint;
15  import org.mule.impl.endpoint.MuleEndpointURI;
16  import org.mule.umo.endpoint.UMOEndpoint;
17  import org.mule.umo.endpoint.UMOEndpointURI;
18  import org.mule.umo.endpoint.UMOImmutableEndpoint;
19  import org.mule.umo.manager.UMOManager;
20  
21  import org.apache.commons.beanutils.ConversionException;
22  import org.apache.commons.beanutils.Converter;
23  
24  /**
25   * <code>EndpointConverter</code> TODO
26   */
27  public class EndpointConverter implements Converter
28  {
29  
30      // --------------------------------------------------------- Public Methods
31  
32      /**
33       * Convert the specified input object into an output object of the specified
34       * type.
35       * 
36       * @param type Data type to which this value should be converted
37       * @param value The input value to be converted
38       * @throws ConversionException if conversion cannot be performed successfully
39       */
40      public Object convert(Class type, Object value)
41      {
42          UMOManager manager = MuleManager.getInstance();
43          if (value == null)
44          {
45              throw new ConversionException("No value specified");
46          }
47          if (value instanceof UMOEndpoint)
48          {
49              return (value);
50          }
51          try
52          {
53              String endpointString = manager.lookupEndpointIdentifier(value.toString(), value.toString());
54              UMOImmutableEndpoint globalEndpoint = (UMOImmutableEndpoint)manager.getEndpoints().get(
55                  endpointString);
56              if (globalEndpoint == null)
57              {
58                  UMOEndpointURI endpointUri = new MuleEndpointURI(endpointString);
59                  if (!endpointString.equals(value.toString()))
60                  {
61                      endpointUri.setEndpointName(value.toString());
62                  }
63                  UMOEndpoint endpoint = MuleEndpoint.createEndpointFromUri(endpointUri, null);
64                  // If the value was an endpoint identifier reference then set
65                  // the
66                  // reference as the name of the endpoint
67                  if (endpointUri.getEndpointName() == null && !endpointString.equals(value.toString()))
68                  {
69                      endpoint.setName(value.toString());
70                  }
71                  return endpoint;
72              }
73              else
74              {
75                  // Global endpoints are late bound to objects because they are
76                  // cloned by
77                  // the Mule Manager. So e return null here and the endpoint will
78                  // be set later
79                  return null;
80              }
81          }
82          catch (Exception e)
83          {
84              throw new ConversionException(e);
85          }
86      }
87  }