Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/satyr/tools/ComponentDisplay.java


1   /*
2    * ComponentDisplay.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.satyr.tools;
11  
12  import java.io.*;
13  
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.Iterator;
17  import java.util.HashMap;
18  import java.util.HashSet;
19  import java.util.Map;
20  
21  import com.aendvari.cerberus.component.assembly.*;
22  import com.aendvari.cerberus.component.descriptor.*;
23  import com.aendvari.cerberus.component.descriptor.parser.*;
24  import com.aendvari.cerberus.component.directory.*;
25  
26  import com.aendvari.hermes.broker.*;
27  
28  import com.aendvari.common.model.*;
29  import com.aendvari.common.model.osm.*;
30  
31  /**
32   * <p>Displays all components within an application configuration.</p>
33   *
34   * @author  Trevor Milne
35   *
36   */
37  
38  public class ComponentDisplay
39  {
40    public static void main(String args[])
41    {
42      // check parameters
43      if ((args.length != 2) && (args.length != 3))
44      {
45        System.out.println("usage: ComponentDisplay assembly output [xml | text]");
46        System.out.println();
47        System.exit(0);
48      }
49  
50      try
51      {
52        // read and parse descriptors
53        FileInputStream input = new FileInputStream(args[0]);
54        AssemblyParser parser = new AssemblyParser();
55  
56        AssemblyDescriptor assemblyDescriptor = parser.parse(input);
57        MessageBroker broker = new MessageBroker();
58        MessageBrokerConnection connection = broker.createConnection();
59  
60        AssemblyContext context = new AssemblyContext(broker);
61        ComponentDirectory directory = new ComponentDirectory();
62  
63        ComponentAssembler assembler = new ComponentAssembler(context, assemblyDescriptor, directory);
64        assembler.assemble();
65  
66        // get application components
67        Collection components = directory.getComponents();
68  
69        // create message lookups
70        HashMap messageSenders = new HashMap();
71        HashMap messageReceivers = new HashMap();
72  
73        Iterator componentIterator = components.iterator();
74  
75        while (componentIterator.hasNext())
76        {
77          ComponentDescriptor descriptor = (ComponentDescriptor)componentIterator.next();
78  
79          Collection messages = descriptor.getMessages();
80          Iterator messageIterator = messages.iterator();
81  
82          while (messageIterator.hasNext())
83          {
84            ComponentDescriptor.Message message = (ComponentDescriptor.Message)messageIterator.next();
85  
86            // skip empty topics
87            if (message.getTopic().length() == 0) continue;
88  
89            // add message/descriptor association to lookup
90  
91            if (message.getType() == ComponentDescriptor.Message.Type.Send)
92            {
93              HashSet senders = null;
94  
95              // retrieve sending components from lookup, create entry if required
96              if (messageSenders.containsKey(message.getTopic()))
97              {
98                senders = (HashSet)messageSenders.get(message.getTopic());
99              }
100             else
101             {
102               senders = new HashSet();
103               messageSenders.put(message.getTopic(), senders);
104             }
105 
106             // add component to sender list
107             senders.add(descriptor.getName());
108           }
109           else
110           {
111             ArrayList receivers = null;
112 
113             // retrieve receiving components from lookup, create entry if required
114             if (messageReceivers.containsKey(message.getTopic()))
115             {
116               receivers = (ArrayList)messageReceivers.get(message.getTopic());
117             }
118             else
119             {
120               receivers = new ArrayList();
121               messageReceivers.put(message.getTopic(), receivers);
122             }
123 
124             // add component to sender list
125             receivers.add(descriptor.getName());
126           }
127         }
128       }
129 
130       // create output file
131       FileOutputStream outputStream = new FileOutputStream(args[1]);
132       PrintWriter outputWriter = new PrintWriter(outputStream);
133 
134       // check output format
135       if ((args.length == 2) || (args[2].equalsIgnoreCase("xml")))
136       {
137         generateXml(components, messageSenders, messageReceivers, outputWriter);
138       }
139       else
140       if (args[2].equalsIgnoreCase("text"))
141       {
142         generateText(components, messageSenders, messageReceivers, outputWriter);
143       }
144       else
145       {
146         System.err.println("Unknown output type '" + args[2] + "'.");
147         System.exit(1);
148       }
149     }
150     catch (Exception exception)
151     {
152       exception.printStackTrace(System.out);
153     }
154   }
155 
156   /**
157    * Generates a text version of the component descriptions.
158    *
159    * @param    components            The {@link ComponentDescriptor ComponentDescriptors} to output.
160    * @param    messageSenders          Components that send messages.
161    * @param    messageReceivers        Components that receiver messages.
162    * @param    output              The output stream.
163    *
164    * @exception  IOException            A I/O error occured.
165    *
166    */
167 
168   protected static void generateText(Collection components, Map messageSenders, Map messageReceivers, PrintWriter output)
169     throws IOException
170   {
171     // display descriptor of each component
172     Iterator componentIterator = components.iterator();
173 
174     while (componentIterator.hasNext())
175     {
176       ComponentDescriptor descriptor = (ComponentDescriptor)componentIterator.next();
177 
178       // display basic information
179       output.println("Name:  " + descriptor.getName());
180 
181       if (descriptor.getBackingClass() == null)
182         output.println("Class: None");
183       else
184         output.println("Class: " + descriptor.getBackingClass());
185 
186       // display attributes
187       output.println();
188       output.println("Attributes");
189       output.println();
190 
191       Collection attributes = descriptor.getAttributes();
192       Iterator attributeIterator = attributes.iterator();
193 
194       while (attributeIterator.hasNext())
195       {
196         ComponentDescriptor.Attribute attribute = (ComponentDescriptor.Attribute)attributeIterator.next();
197 
198         output.println("Name:  " + attribute.getName());
199 
200         if (attribute.getAccess() == ComponentDescriptor.Attribute.Access.Public)
201         {
202           output.println("Access: Public");
203         }
204         else
205         if (attribute.getAccess() == ComponentDescriptor.Attribute.Access.Private)
206         {
207           output.println("Access: Private");
208         }
209         else
210         {
211           output.println("Access: Unknown");
212         }
213 
214         output.println("Value: " + attribute.getValue());
215         output.println();
216       }
217 
218       // display messages
219       output.println("Messages");
220       output.println();
221 
222       Collection messages = descriptor.getMessages();
223       Iterator messageIterator = messages.iterator();
224 
225       while (messageIterator.hasNext())
226       {
227         ComponentDescriptor.Message message = (ComponentDescriptor.Message)messageIterator.next();
228 
229         output.println("Name:  " + message.getName());
230 
231         // show access control
232         if (message.getAccess() == ComponentDescriptor.Message.Access.Public)
233         {
234           output.println("Access: Public");
235         }
236         else
237         if (message.getAccess() == ComponentDescriptor.Message.Access.Private)
238         {
239           output.println("Access: Private");
240         }
241         else
242         {
243           output.println("Access: Unknown");
244         }
245 
246         // show topic
247         output.println("Topic: " + message.getTopic());
248 
249         // show related components
250         if (message.getType() == ComponentDescriptor.Message.Type.Send)
251         {
252           output.println("Type:  Send");
253 
254           // retrieve the receiving components from the lookup
255           Collection receivers = (Collection)messageReceivers.get(message.getTopic());
256 
257           output.println("Received By: ");
258 
259           // check for no receivers
260           if (receivers == null)
261           {
262             output.println("     None");
263           }
264           else
265           // check if any receives are associated
266           if (receivers.size() > 0)
267           {
268             // display receiving components
269             Iterator receiverIterator = receivers.iterator();
270 
271             while (receiverIterator.hasNext())
272             {
273               String receiver = (String)receiverIterator.next();
274 
275               output.println("     " + receiver);
276             }
277           }
278         }
279         else
280         {
281           output.println("Type:  Receive");
282 
283           // retrieve the sending components from the lookup
284           Collection senders = (Collection)messageSenders.get(message.getTopic());
285 
286           output.println("Sent By: ");
287 
288           // check for no senders
289           if (senders == null)
290           {
291             output.println("     None");
292           }
293           else
294           // check if any senders are associated
295           if (senders.size() > 0)
296           {
297             // display sending components
298             Iterator senderIterator = senders.iterator();
299 
300             while (senderIterator.hasNext())
301             {
302               String sender = (String)senderIterator.next();
303 
304               output.println("     " + sender);
305             }
306           }
307         }
308 
309         output.println();
310       }
311 
312       output.println();
313       output.println("----------------------------------------------------");
314       output.println();
315 
316       output.flush();
317     }
318   }
319 
320   /**
321    * Generates an XML version of the component descriptions.
322    *
323    * @param    components            The {@link ComponentDescriptor ComponentDescriptors} to output.
324    * @param    messageSenders          Components that send messages.
325    * @param    messageReceivers        Components that receiver messages.
326    * @param    output              The output stream.
327    *
328    * @exception  IOException            A I/O error occured.
329    *
330    */
331 
332   protected static void generateXml(Collection components, Map messageSenders, Map messageReceivers, Writer output)
333     throws IOException
334   {
335     // generate XML
336     ModelTree modelTree = new OsmModelTree();
337 
338     // create base node
339     ModelNode baseNode = modelTree.createNode("configuration");
340     modelTree.getRootNode().appendChild(baseNode);
341 
342     // insert descriptor of each component
343     Iterator componentIterator = components.iterator();
344 
345     while (componentIterator.hasNext())
346     {
347       ComponentDescriptor descriptor = (ComponentDescriptor)componentIterator.next();
348 
349       // create top node
350       ModelNode componentNode = modelTree.createNode("component");
351       baseNode.appendChild(componentNode);
352 
353       // name
354       componentNode.appendChild(modelTree.createNode("name", descriptor.getName()));
355 
356       // backing class
357       if (descriptor.getBackingClass() == null)
358       {
359         componentNode.appendChild(modelTree.createNode("class", "None"));
360       }
361       else
362       {
363         componentNode.appendChild(modelTree.createNode("class", descriptor.getBackingClass()));
364       }
365 
366       // attributes
367       Collection attributes = descriptor.getAttributes();
368       Iterator attributeIterator = attributes.iterator();
369 
370       while (attributeIterator.hasNext())
371       {
372         ComponentDescriptor.Attribute attribute = (ComponentDescriptor.Attribute)attributeIterator.next();
373 
374         // create node
375         ModelNode attributeNode = modelTree.createNode("attribute");
376         componentNode.appendChild(attributeNode);
377 
378         // name
379         attributeNode.appendChild(modelTree.createNode("name", attribute.getName()));
380 
381         // access control
382         if (attribute.getAccess() == ComponentDescriptor.Attribute.Access.Public)
383         {
384           attributeNode.appendChild(modelTree.createNode("access", "public"));
385         }
386         else
387         if (attribute.getAccess() == ComponentDescriptor.Attribute.Access.Private)
388         {
389           attributeNode.appendChild(modelTree.createNode("access", "private"));
390         }
391         else
392         {
393           attributeNode.appendChild(modelTree.createNode("access", "unknown"));
394         }
395 
396         // value
397         attributeNode.appendChild(modelTree.createNode("value", attribute.getValue()));
398       }
399 
400       // messages
401       Collection messages = descriptor.getMessages();
402       Iterator messageIterator = messages.iterator();
403 
404       while (messageIterator.hasNext())
405       {
406         ComponentDescriptor.Message message = (ComponentDescriptor.Message)messageIterator.next();
407 
408         // create node
409         ModelNode messageNode = modelTree.createNode("message");
410         componentNode.appendChild(messageNode);
411 
412         // name
413         messageNode.appendChild(modelTree.createNode("name", message.getName()));
414 
415         // show access control
416         if (message.getAccess() == ComponentDescriptor.Message.Access.Public)
417         {
418           messageNode.appendChild(modelTree.createNode("access", "public"));
419         }
420         else
421         if (message.getAccess() == ComponentDescriptor.Message.Access.Private)
422         {
423           messageNode.appendChild(modelTree.createNode("access", "private"));
424         }
425         else
426         {
427           messageNode.appendChild(modelTree.createNode("access", "unknown"));
428         }
429 
430         // topic
431         messageNode.appendChild(modelTree.createNode("topic", message.getTopic()));
432 
433         // related components
434         if (message.getType() == ComponentDescriptor.Message.Type.Send)
435         {
436           // send message type
437           messageNode.appendChild(modelTree.createNode("type", "send"));
438 
439           // retrieve the receiving components from the lookup
440           Collection receivers = (Collection)messageReceivers.get(message.getTopic());
441 
442           // insert receiving components
443           Iterator receiverIterator = receivers.iterator();
444 
445           while (receiverIterator.hasNext())
446           {
447             String receiver = (String)receiverIterator.next();
448             messageNode.appendChild(modelTree.createNode("receiver", receiver));
449           }
450         }
451         else
452         {
453           // receive message type
454           messageNode.appendChild(modelTree.createNode("type", "receive"));
455 
456           // retrieve the sending components from the lookup
457           Collection senders = (Collection)messageSenders.get(message.getTopic());
458 
459           // display sending components
460           Iterator senderIterator = senders.iterator();
461 
462           while (senderIterator.hasNext())
463           {
464             String sender = (String)senderIterator.next();
465             messageNode.appendChild(modelTree.createNode("sender", sender));
466           }
467         }
468       }
469     }
470 
471     // write XML to output
472     ModelUtil.writeNode(baseNode, output);
473     output.flush();
474   }
475 }
476