View Javadoc

1   /*
2    * $Id: TagProcessor.java 11728 2008-05-13 07:31:11Z 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.processor;
12  
13  import org.mule.tools.visualizer.config.GraphEnvironment;
14  import org.mule.tools.visualizer.util.MuleTag;
15  import org.mule.util.StringUtils;
16  
17  import com.oy.shared.lm.graph.Graph;
18  import com.oy.shared.lm.graph.GraphEdge;
19  import com.oy.shared.lm.graph.GraphNode;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.jdom.Attribute;
25  import org.jdom.Element;
26  
27  public abstract class TagProcessor
28  {
29  
30      // TODO - serious weirdness here, with a  static value set in a constructor...
31      private static GraphEnvironment environment;
32  
33      public TagProcessor(GraphEnvironment env)
34      {
35          setEnvironment(env);
36      }
37  
38      public abstract void process(Graph graph, Element currentElement, GraphNode parent);
39  
40      public static void appendProperties(Element element, StringBuffer caption)
41      {
42          Element properties = element.getChild(MuleTag.ELEMENT_PROPERTIES);
43          if (properties != null)
44          {
45              for (Iterator iterator = properties.getChildren(MuleTag.ELEMENT_PROPERTY).iterator(); iterator
46                  .hasNext();)
47              {
48                  Element property = (Element) iterator.next();
49                  caption.append(property.getAttributeValue(MuleTag.ATTRIBUTE_NAME) + " :"
50                                  + lookupPropertyTemplate(property.getAttributeValue(MuleTag.ATTRIBUTE_VALUE))
51                                  + "\n");
52              }
53          }
54          for (Iterator iterator = element.getAttributes().iterator(); iterator.hasNext();)
55          {
56              Attribute a = (Attribute) iterator.next();
57              if (!ignoreAttribute(a.getName()))
58              {
59                  caption.append(a.getName() + " :" + a.getValue() + "\n");
60              }
61          }
62      }
63  
64      protected static boolean ignoreAttribute(String name)
65      {
66          if (name == null || "".equals(name))
67          {
68              return true;
69          }
70          for (Iterator iterator = getEnvironment().getConfig().getIgnoredAttributes().iterator(); iterator
71              .hasNext();)
72          {
73              String s = (String) iterator.next();
74              if (name.equals(s))
75              {
76                  return true;
77              }
78  
79          }
80          return false;
81      }
82  
83      public static void appendDescription(Element e, StringBuffer caption)
84      {
85          Element description = e.getChild(MuleTag.ELEMENT_DESCRIPTION);
86          if (description != null)
87          {
88              caption.append("\n-------------------\n").append(description.getText()).append("\n");
89          }
90      }
91  
92      protected void appendProfiles(Element descriptor, StringBuffer caption)
93      {
94          List elements = descriptor.getChildren(MuleTag.ELEMENT_THREADING_PROFILE);
95          for (Iterator iterator = elements.iterator(); iterator.hasNext();)
96          {
97              Element threadingProfile = (Element) iterator.next();
98              if (threadingProfile != null)
99              {
100                 appendAttribute(threadingProfile, "maxBufferSize", caption);
101                 appendAttribute(threadingProfile, "threadTTL", caption);
102                 appendAttribute(threadingProfile, "maxThreadsActive", caption);
103                 appendAttribute(threadingProfile, "maxThreadsIdle", caption);
104                 appendAttribute(threadingProfile, "id", caption);
105             }
106         }
107         Element poolingProfile = descriptor.getChild(MuleTag.ELEMENT_POOLING_PROFILE);
108         if (poolingProfile != null)
109         {
110             appendAttribute(poolingProfile, "exhaustedAction", caption);
111             appendAttribute(poolingProfile, "maxActive", caption);
112             appendAttribute(poolingProfile, "maxIdle", caption);
113             appendAttribute(poolingProfile, "maxWait", caption);
114         }
115 
116         Element queueProfile = descriptor.getChild(MuleTag.ELEMENT_QUEUE_PROFILE);
117         if (queueProfile != null)
118         {
119             appendAttribute(queueProfile, "maxOutstandingMessages", caption);
120             appendAttribute(queueProfile, "persistent", caption);
121         }
122     }
123 
124     protected void appendAttribute(Element e, String name, StringBuffer caption)
125     {
126         if (e.getAttribute(name) == null)
127         {
128             return;
129         }
130         String value = e.getAttributeValue(name);
131         if (value != null)
132         {
133             caption.append(name + " = "
134                             + (StringUtils.EMPTY.equals(value) ? "\"\"" : lookupPropertyTemplate(value))
135                             + "\n");
136         }
137     }
138 
139     public static void addEdge(Graph graph, GraphNode src, GraphNode dest, String caption, boolean twoway)
140     {
141         GraphEdge ge = graph.addEdge(src, dest);
142         if (twoway)
143         {
144             ge.getInfo().setArrowTailNormal();
145         }
146 
147         if (caption != null)
148         {
149             if ("in".equalsIgnoreCase(caption) && twoway)
150             {
151                 caption += " / out";
152             }
153             else
154             {
155                 if ("out".equalsIgnoreCase(caption) && twoway)
156                 {
157                     caption += " / in";
158                 }
159             }
160             ge.getInfo().setCaption(caption);
161         }
162     }
163 
164     public static void addRelation(Graph graph, GraphNode src, GraphNode dest, String caption)
165     {
166         if (null != src && null != dest)
167         {
168             GraphEdge ge = graph.addEdge(src, dest);
169             ge.getInfo().setArrowHeadNone();
170             if (caption != null)
171             {
172                 ge.getInfo().setCaption(caption);
173             }
174         }
175         else
176         {
177             environment.log("Null relation: " + describeNode(src) + " / " + describeNode(dest)
178                     + " (" + caption + ")");
179         }
180     }
181 
182     private static String describeNode(GraphNode node)
183     {
184         if (null == node)
185         {
186             return "-";
187         }
188         else
189         {
190             return node.getId() + ":" + node.getInfo().getCaption();
191         }
192     }
193 
194     public boolean isTwoWay(Element e)
195     {
196         if (e == null)
197         {
198             return getEnvironment().isDefaultTwoWay();
199         }
200         return ("true".equalsIgnoreCase(e.getAttributeValue(MuleTag.ATTRIBUTE_SYNCHRONOUS)) || getEnvironment()
201             .isDefaultTwoWay());
202     }
203 
204     protected static String lookupPropertyTemplate(String template)
205     {
206         if (template == null)
207         {
208             return null;
209         }
210         String value = getEnvironment().getProperties().getProperty(template, null);
211         if (value == null && template.startsWith("${"))
212         {
213             value = getEnvironment().getProperties().getProperty(template.substring(2, template.length() - 1),
214                 template);
215         }
216         else
217         {
218             value = template;
219         }
220         return value;
221     }
222 
223     protected static void setEnvironment(GraphEnvironment environment)
224     {
225         TagProcessor.environment = environment;
226     }
227 
228     public static GraphEnvironment getEnvironment()
229     {
230         return environment;
231     }
232 }