View Javadoc

1   /*
2    * $Id: EndpointRegistry.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.tools.visualizer.components;
12  
13  import org.mule.tools.visualizer.config.GraphEnvironment;
14  
15  import com.oy.shared.lm.graph.GraphNode;
16  
17  import java.util.ArrayList;
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.StringTokenizer;
23  
24  public class EndpointRegistry
25  {
26  
27      private Map endpoints = null;
28      private GraphEnvironment env;
29  
30      public EndpointRegistry(GraphEnvironment env)
31      {
32          this.env = env;
33          endpoints = new HashMap();
34      }
35  
36      public GraphNode[] getVirtualEndpoint(String componentName)
37      {
38  
39          List nodesList = new ArrayList();
40          String mappedUri = env.getConfig().getMappings().getProperty(componentName);
41          if (mappedUri != null)
42          {
43              StringTokenizer stringTokenizer = new StringTokenizer(mappedUri, ",");
44              while (stringTokenizer.hasMoreTokens())
45              {
46                  String s = stringTokenizer.nextToken();
47                  env.log("Mapping virtual endpoint '" + s + "' for component '" + componentName + "'");
48                  GraphNode n = getEndpoint(s, componentName);
49                  if (n != null)
50                  {
51                      nodesList.add(n);
52                  }
53              }
54          }
55  
56          GraphNode[] nodes;
57          if (nodesList.size() > 0)
58          {
59              nodes = new GraphNode[nodesList.size()];
60              nodes = (GraphNode[]) nodesList.toArray(nodes);
61          }
62          else
63          {
64              nodes = new GraphNode[]{};
65          }
66          return nodes;
67      }
68  
69      public GraphNode getEndpoint(String uri, String componentName)
70      {
71          env.log("retrieving endpoint for " + uri + " / " + componentName);
72          GraphNode n = getEqualsMapping(uri, componentName);
73          if (n != null)
74          {
75              env.log("found equals mapping: " + n.getId());
76          }
77          else
78          {
79              n = (GraphNode) endpoints.get(uri);
80              if (null != n)
81              {
82                  env.log("found direct match: " + n.getId());
83              }
84              else
85              {
86                  for (Iterator iterator = endpoints.keySet().iterator(); iterator.hasNext();)
87                  {
88                      String s = (String) iterator.next();
89                      if (s.startsWith(uri + "/" + componentName))
90                      {
91                          n = (GraphNode) endpoints.get(s);
92                          env.log("found prefix: " + n.getId() +"; " + s);
93                      }
94                  }
95              }
96          }
97  
98          return n;
99      }
100 
101     private GraphNode getEqualsMapping(String uri, String componentName)
102     {
103         String equalsMapping = env.getConfig().getMappings().getProperty(uri + ".equals");
104         if (equalsMapping != null)
105         {
106             env.log("Mapping equivalent endpoint '" + equalsMapping + "' to '" + uri + "'");
107             return getEndpoint(equalsMapping, componentName);
108         }
109         return null;
110     }
111 
112     public void addEndpoint(String url, GraphNode out)
113     {
114         env.log("adding endpoint: " + out.getId() + "; " + url);
115         endpoints.put(url, out);
116     }
117 }