Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » broker » view » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.broker.view;
   18   
   19   import java.io.PrintWriter;
   20   import java.util.Collection;
   21   import java.util.Iterator;
   22   
   23   import org.apache.activemq.broker.Broker;
   24   import org.apache.activemq.broker.ConnectionContext;
   25   import org.apache.activemq.broker.region.Destination;
   26   import org.apache.activemq.command.ActiveMQDestination;
   27   import org.apache.activemq.filter.DestinationMap;
   28   import org.apache.activemq.filter.DestinationMapNode;
   29   
   30   /**
   31    * @version $Revision: $
   32    */
   33   public class DestinationDotFileInterceptor extends DotFileInterceptorSupport {
   34   
   35       protected static final String ID_SEPARATOR = "_";
   36   
   37       public DestinationDotFileInterceptor(Broker next, String file) {
   38           super(next, file);
   39       }
   40   
   41       public Destination addDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception {
   42           Destination answer = super.addDestination(context, destination);
   43           generateFile();
   44           return answer;
   45       }
   46   
   47       public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout) throws Exception {
   48           super.removeDestination(context, destination, timeout);
   49           generateFile();
   50       }
   51   
   52       protected void generateFile(PrintWriter writer) throws Exception {
   53           ActiveMQDestination[] destinations = getDestinations();
   54   
   55           // lets split into a tree
   56           DestinationMap map = new DestinationMap();
   57   
   58           for (int i = 0; i < destinations.length; i++) {
   59               ActiveMQDestination destination = destinations[i];
   60               map.put(destination, destination);
   61           }
   62   
   63           // now lets navigate the tree
   64           writer.println("digraph \"ActiveMQ Destinations\" {");
   65           writer.println();
   66           writer.println("node [style = \"rounded,filled\", fontname=\"Helvetica-Oblique\"];");
   67           writer.println();
   68           writer.println("topic_root [fillcolor = deepskyblue, label = \"Topics\" ];");
   69           writer.println("queue_root [fillcolor = deepskyblue, label = \"Queues\" ];");
   70           writer.println();
   71   
   72           writer.println("subgraph queues {");
   73           writer.println("  node [fillcolor=red];     ");
   74           writer.println("  label = \"Queues\"");
   75           writer.println();
   76           printNodeLinks(writer, map.getQueueRootNode(), "queue");
   77           writer.println("}");
   78           writer.println();
   79   
   80           writer.println("subgraph topics {");
   81           writer.println("  node [fillcolor=green];     ");
   82           writer.println("  label = \"Topics\"");
   83           writer.println();
   84           printNodeLinks(writer, map.getTopicRootNode(), "topic");
   85           writer.println("}");
   86           writer.println();
   87   
   88           printNodes(writer, map.getQueueRootNode(), "queue");
   89           writer.println();
   90   
   91           printNodes(writer, map.getTopicRootNode(), "topic");
   92           writer.println();
   93   
   94           writer.println("}");
   95       }
   96   
   97       protected void printNodes(PrintWriter writer, DestinationMapNode node, String prefix) {
   98           String path = getPath(node);
   99           writer.print("  ");
  100           writer.print(prefix);
  101           writer.print(ID_SEPARATOR);
  102           writer.print(path);
  103           String label = path;
  104           if (prefix.equals("topic")) {
  105               label = "Topics";
  106           } else if (prefix.equals("queue")) {
  107               label = "Queues";
  108           }
  109           writer.print("[ label = \"");
  110           writer.print(label);
  111           writer.println("\" ];");
  112   
  113           Collection children = node.getChildren();
  114           for (Iterator iter = children.iterator(); iter.hasNext();) {
  115               DestinationMapNode child = (DestinationMapNode)iter.next();
  116               printNodes(writer, child, prefix + ID_SEPARATOR + path);
  117           }
  118       }
  119   
  120       protected void printNodeLinks(PrintWriter writer, DestinationMapNode node, String prefix) {
  121           String path = getPath(node);
  122           Collection children = node.getChildren();
  123           for (Iterator iter = children.iterator(); iter.hasNext();) {
  124               DestinationMapNode child = (DestinationMapNode)iter.next();
  125   
  126               writer.print("  ");
  127               writer.print(prefix);
  128               writer.print(ID_SEPARATOR);
  129               writer.print(path);
  130               writer.print(" -> ");
  131               writer.print(prefix);
  132               writer.print(ID_SEPARATOR);
  133               writer.print(path);
  134               writer.print(ID_SEPARATOR);
  135               writer.print(getPath(child));
  136               writer.println(";");
  137   
  138               printNodeLinks(writer, child, prefix + ID_SEPARATOR + path);
  139           }
  140       }
  141   
  142       protected String getPath(DestinationMapNode node) {
  143           String path = node.getPath();
  144           if (path.equals("*")) {
  145               return "root";
  146           }
  147           return path;
  148       }
  149   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » broker » view » [javadoc | source]