KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > applications > server > ParameterTransformation


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.applications.server;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.sslexplorer.boot.XMLElement;
31
32 public class ParameterTransformation {
33     
34     final static Log log = LogFactory.getLog(ParameterTransformation.class);
35
36     XMLElement el;
37     ServerLauncher launcher;
38     Transformation trans;
39     String JavaDoc outputParam;
40     String JavaDoc inputParam;
41
42     ParameterTransformation(XMLElement el, ServerLauncher launcher) throws IOException JavaDoc {
43
44         if (el.getAttribute("class") == null || el.getAttribute("input") == null || el.getAttribute("output") == null)
45             throw new IOException JavaDoc("<transform> element requires class, input and output attributes!");
46
47         this.el = el;
48         this.launcher = launcher;
49
50     }
51
52     public void processTransformation() throws IOException JavaDoc {
53
54         String JavaDoc classFile = el.getAttribute("class").toString();
55         inputParam = el.getAttribute("input").toString();
56         outputParam = el.getAttribute("output").toString();
57
58         File JavaDoc f = new File JavaDoc(launcher.getInstallDir(), classFile + ".class");
59
60         if (!f.exists())
61             throw new IOException JavaDoc(classFile + " does not exist! Did you forget to place the .class file in a <file> element?");
62
63         FileInputStream JavaDoc in = new FileInputStream JavaDoc(f);
64         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
65
66         byte[] buf = new byte[16384];
67         int read;
68
69         while ((read = in.read(buf)) > -1) {
70             out.write(buf, 0, read);
71         }
72
73         in.close();
74         buf = out.toByteArray();
75         try {
76
77             log.debug("Loading transformation class " + classFile);
78             Class JavaDoc cls = ByteArrayClassLoader.getInstance().createFromByteArray(classFile, buf, 0, buf.length);
79
80             log.debug("Creating transformation instance");
81             Transformation t = (Transformation) cls.newInstance();
82
83             log.debug("Invoking transformation");
84             launcher.addParameter(outputParam, t.transform(launcher.replaceTokens(inputParam)));
85         } catch (Exception JavaDoc ex) {
86             log.debug("Exception in Transformation class: " + ex.getMessage());
87         }
88
89     }
90 }
91
Popular Tags