1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.tools.visualizer.service; |
11 | |
|
12 | |
import org.mule.MuleManager; |
13 | |
import org.mule.config.ConfigurationException; |
14 | |
import org.mule.impl.MuleMessage; |
15 | |
import org.mule.impl.UMODescriptorAware; |
16 | |
import org.mule.tools.visualizer.MuleVisualizer; |
17 | |
import org.mule.tools.visualizer.config.GraphConfig; |
18 | |
import org.mule.tools.visualizer.config.GraphEnvironment; |
19 | |
import org.mule.tools.visualizer.maven.MuleVisualizerPlugin; |
20 | |
import org.mule.umo.UMODescriptor; |
21 | |
import org.mule.umo.UMOEventContext; |
22 | |
import org.mule.umo.UMOMessage; |
23 | |
import org.mule.umo.lifecycle.Callable; |
24 | |
import org.mule.umo.lifecycle.Initialisable; |
25 | |
import org.mule.umo.lifecycle.InitialisationException; |
26 | |
|
27 | |
import java.io.File; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.List; |
31 | |
import java.util.Set; |
32 | |
|
33 | |
import javax.activation.DataHandler; |
34 | |
import javax.activation.FileDataSource; |
35 | |
|
36 | |
|
37 | 0 | public class VisualizerService extends MuleVisualizerPlugin implements Callable, Initialisable, UMODescriptorAware |
38 | |
{ |
39 | |
protected GraphConfig config; |
40 | |
protected GraphEnvironment environment; |
41 | |
protected MuleVisualizer visualizer; |
42 | |
protected UMODescriptor descriptor; |
43 | |
|
44 | |
public void setDescriptor(UMODescriptor descriptor) throws ConfigurationException |
45 | |
{ |
46 | 0 | this.descriptor = descriptor; |
47 | 0 | } |
48 | |
|
49 | |
public void initialise() throws InitialisationException |
50 | |
{ |
51 | |
try |
52 | |
{ |
53 | 0 | config = buildConfig(); |
54 | 0 | config.setOutputDirectory(MuleManager.getConfiguration().getWorkingDirectory() + File.pathSeparator + "visualizer"); |
55 | 0 | environment = new GraphEnvironment(config); |
56 | 0 | visualizer = new MuleVisualizer(environment); |
57 | |
} |
58 | 0 | catch (Exception e) |
59 | |
{ |
60 | 0 | throw new InitialisationException(e, this); |
61 | 0 | } |
62 | 0 | } |
63 | |
|
64 | |
public Object onCall(UMOEventContext eventContext) throws Exception |
65 | |
{ |
66 | 0 | UMOMessage msg = eventContext.getMessage(); |
67 | 0 | Set names = msg.getAttachmentNames(); |
68 | 0 | if(names.size()==0) |
69 | |
{ |
70 | 0 | throw new IllegalArgumentException("There were no files attached to process"); |
71 | |
} |
72 | |
|
73 | 0 | List files = new ArrayList(names.size()); |
74 | 0 | for (Iterator iterator = names.iterator(); iterator.hasNext();) |
75 | |
{ |
76 | 0 | String s = (String) iterator.next(); |
77 | 0 | DataHandler dh = msg.getAttachment(s); |
78 | 0 | if(dh.getDataSource().getContentType().startsWith("text/xml")) |
79 | |
{ |
80 | 0 | files.add(dh.getInputStream()); |
81 | |
} |
82 | 0 | } |
83 | 0 | if(files.size()==0) |
84 | |
{ |
85 | 0 | throw new IllegalArgumentException("There were no Xml attachments for email: " + msg.getProperty("subject")); |
86 | |
} |
87 | 0 | List results = visualizer.visualize(files); |
88 | 0 | UMOMessage result = new MuleMessage("Thanks for using Mule Visualizer!"); |
89 | 0 | if(results==null) |
90 | |
{ |
91 | 0 | return null; |
92 | |
} |
93 | |
|
94 | 0 | for (Iterator iterator = results.iterator(); iterator.hasNext();) |
95 | |
{ |
96 | 0 | String s = (String) iterator.next(); |
97 | 0 | File f= new File(s); |
98 | 0 | FileDataSource ds = new FileDataSource(f); |
99 | 0 | result.addAttachment(f.getName(), new DataHandler(ds)); |
100 | 0 | } |
101 | |
|
102 | |
|
103 | 0 | for (Iterator iterator = names.iterator(); iterator.hasNext();) |
104 | |
{ |
105 | 0 | String s = (String) iterator.next(); |
106 | 0 | result.addAttachment(s, msg.getAttachment(s)); |
107 | 0 | } |
108 | |
|
109 | 0 | return result; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
} |