KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > util > ParameterTransformation


1 package com.sslexplorer.agent.client.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7
8 /**
9  * Processes <transform> elements in application extension descriptors and
10  * dynamically loads a transform class to process the input and output provided.
11  *
12  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
13  */

14 public class ParameterTransformation {
15
16     private XMLElement el;
17     private AbstractApplicationLauncher launcher;
18     private String JavaDoc outputParam;
19     private String JavaDoc inputParam;
20
21     /**
22      * Constructor. The processing won't take place until {@link #processTransformation()}
23      * is invoked.
24      *
25      * @param el
26      * @param launcher
27      * @throws IOException
28      */

29     ParameterTransformation(XMLElement el, AbstractApplicationLauncher launcher) throws IOException JavaDoc {
30
31         if (el.getAttribute("class") == null || el.getAttribute("input") == null || el.getAttribute("output") == null) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
32
throw new IOException JavaDoc(Messages.getString("ParameterTransformation.transformRequiresClassInputAndOutputAttributes")); //$NON-NLS-1$
33

34         this.el = el;
35         this.launcher = launcher;
36
37     }
38
39     /**
40      * Process the transformation.
41      *
42      * @throws IOException on any error
43      */

44     public void processTransformation() throws IOException JavaDoc {
45
46         String JavaDoc classFile = el.getAttribute("class").toString(); //$NON-NLS-1$
47
inputParam = el.getAttribute("input").toString(); //$NON-NLS-1$
48
outputParam = el.getAttribute("output").toString(); //$NON-NLS-1$
49

50         File JavaDoc f = new File JavaDoc(launcher.getInstallDir(), classFile + ".class"); //$NON-NLS-1$
51

52         if (!f.exists())
53             throw new IOException JavaDoc(classFile + Messages.getString("ParameterTransformation.8")); //$NON-NLS-1$
54

55         FileInputStream JavaDoc in = new FileInputStream JavaDoc(f);
56         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
57
58         byte[] buf = new byte[16384];
59         int read;
60
61         while ((read = in.read(buf)) > -1) {
62             out.write(buf, 0, read);
63         }
64
65         in.close();
66         buf = out.toByteArray();
67         try {
68
69             launcher.events.debug(Messages.getString("ParameterTransformation.loadingTransformationClass") + classFile); //$NON-NLS-1$
70
Class JavaDoc cls = ByteArrayClassLoader.getInstance().createFromByteArray(classFile, buf, 0, buf.length);
71
72             launcher.events.debug(Messages.getString("ParameterTransformation.creatingTransformationInstance")); //$NON-NLS-1$
73
Transformation t = (Transformation) cls.newInstance();
74
75             launcher.events.debug(Messages.getString("ParameterTransformation.invokingTransformation")); //$NON-NLS-1$
76
launcher.addParameter(outputParam, t.transform(launcher.replaceTokens(inputParam)));
77         } catch (Exception JavaDoc ex) {
78             launcher.events.debug(Messages.getString("ParameterTransformation.exceptionInTransformation") + ex.getMessage()); //$NON-NLS-1$
79
}
80
81     }
82 }
83
Popular Tags