View Javadoc

1   /*
2    * $Id: MuleObjectHelper.java 7963 2007-08-21 08:53:15Z 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.util;
12  
13  import org.mule.MuleException;
14  import org.mule.MuleManager;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.impl.endpoint.MuleEndpoint;
17  import org.mule.routing.filters.EqualsFilter;
18  import org.mule.routing.filters.ObjectFilter;
19  import org.mule.routing.filters.WildcardFilter;
20  import org.mule.umo.endpoint.UMOEndpoint;
21  import org.mule.umo.endpoint.UMOImmutableEndpoint;
22  import org.mule.umo.manager.UMOManager;
23  import org.mule.umo.transformer.UMOTransformer;
24  
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.StringTokenizer;
28  
29  /**
30   * <code>MuleObjectHelper</code> is a helper class to assist in finding mule server
31   * objects, such as endpoint and transformers
32   */
33  // @ThreadSafe
34  public final class MuleObjectHelper
35  {
36  
37      /** Do not instanciate. */
38      private MuleObjectHelper ()
39      {
40          // no-op
41      }
42  
43      /**
44       * Builds a linked list of UMOTransformers.
45       * 
46       * @param list - a list of transformers separated by "delim"
47       * @param delim - the character used to delimit the transformers in the list
48       * @return an UMOTransformer whose method getNextTransformer() will return the
49       *         next transformer in the list.
50       * @throws MuleException
51       */
52      public static UMOTransformer getTransformer(String list, String delim) throws MuleException
53      {
54          StringTokenizer st = new StringTokenizer(list, delim);
55          UMOManager manager = MuleManager.getInstance();
56          UMOTransformer currentTrans = null;
57          UMOTransformer returnTrans = null;
58  
59          while (st.hasMoreTokens())
60          {
61              String key = st.nextToken().trim();
62              UMOTransformer tempTrans = manager.lookupTransformer(key);
63  
64              if (tempTrans == null)
65              {
66                  throw new MuleException(
67                      CoreMessages.objectNotRegisteredWithManager("Transformer: " + key));
68              }
69  
70              if (currentTrans == null)
71              {
72                  currentTrans = tempTrans;
73                  returnTrans = tempTrans;
74              }
75              else
76              {
77                  currentTrans.setNextTransformer(tempTrans);
78                  currentTrans = tempTrans;
79              }
80          }
81  
82          return returnTrans;
83      }
84  
85      public static UMOEndpoint getEndpointByProtocol(String protocol)
86      {
87          UMOImmutableEndpoint iprovider;
88          Map endpoints = MuleManager.getInstance().getEndpoints();
89          for (Iterator iterator = endpoints.values().iterator(); iterator.hasNext();)
90          {
91              iprovider = (UMOImmutableEndpoint) iterator.next();
92              if (iprovider.getProtocol().equals(protocol))
93              {
94                  return new MuleEndpoint(iprovider);
95              }
96          }
97          return null;
98      }
99  
100     public static UMOEndpoint getEndpointByEndpointUri(String endpointUri, boolean wildcardMatch)
101     {
102         ObjectFilter filter;
103 
104         if (wildcardMatch)
105         {
106             filter = new WildcardFilter(endpointUri);
107         }
108         else
109         {
110             filter = new EqualsFilter(endpointUri);
111         }
112 
113         UMOImmutableEndpoint iprovider;
114         Map endpoints = MuleManager.getInstance().getEndpoints();
115 
116         for (Iterator iterator = endpoints.values().iterator(); iterator.hasNext();)
117         {
118             iprovider = (UMOImmutableEndpoint) iterator.next();
119             if (filter.accept(iprovider.getEndpointURI()))
120             {
121                 return new MuleEndpoint(iprovider);
122             }
123         }
124 
125         return null;
126     }
127 
128 }