source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/ModuleWrapper.java@ 9874

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/*
2 * ModuleWrapper.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
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 License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.service;
20import org.greenstone.gsdl3.core.ModuleInterface;
21
22// XML classes
23import javax.xml.parsers.DocumentBuilderFactory;
24import javax.xml.parsers.DocumentBuilder;
25import javax.xml.parsers.SAXParser;
26import org.apache.xerces.dom.TextImpl;
27import org.apache.xerces.parsers.DOMParser;
28import org.apache.xindice.xml.TextWriter;
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31import org.w3c.dom.Node;
32import org.w3c.dom.Node;
33import org.xml.sax.InputSource;
34import org.xml.sax.SAXException;
35import org.xml.sax.XMLReader;
36
37// java classes
38import java.io.StringReader;
39import java.io.File;
40import java.io.FileWriter;
41import java.io.IOException;
42import java.security.SecureRandom;
43
44/**
45 * A classes for logging and checking all traffic to and from a ModuleInterface
46 *
47 * if logCounter hasn't been initialised, initialise it to a random
48 * value. This has to use secure random rather than a normal random
49 * or every time the application is run the same numbers will be
50 * generated and the search for a free range will become a linear
51 * search rather than a hash.
52 *
53 *
54 * @author <a href="mailto:[email protected]">Stuart Yeates</a>
55 * @version $Revision: 9874 $
56 */
57public class ModuleWrapper
58 implements ModuleInterface
59{
60 /** the module we're wrapping */
61 protected ModuleInterface inner = null;
62 /** the module we're wrapping */
63 public ModuleInterface getInner(){return inner;};
64 /** the module we're wrapping */
65 public void setInner(ModuleInterface i){inner=i;};
66
67 /** The schema of the IN XML */
68 protected String fromSchema = null;
69 /** The schema of the IN XML */
70 public String getFromSchema(){return fromSchema;};
71 /** The schema of the IN XML */
72 public void setFromSchema(String s){fromSchema=s;};
73
74 /** The schema of the OUT XML */
75 protected String toSchema = null;
76 /** The schema of the OUT XML */
77 public String getToSchema(){return toSchema;};
78 /** The schema of the OUT XML */
79 public void setToSchema(String s){toSchema=s;};
80
81 /** Are we logging the XML ? */
82 protected boolean logging = true;
83 /** Are we logging the XML ? */
84 public boolean getLogging(){return logging;};
85 /** Are we logging the XML ? */
86 public void setLogging(boolean b){logging=b;};
87
88 /** Where we are logging the XML */
89 protected String logDirectory = "/tmp/";
90 /** Where we are logging the XML */
91 public String getLogDirectory(){return logDirectory;};
92 /** Where we are logging the XML */
93 public void setLogDirectory(String s){logDirectory=s;};
94
95 /** The number of the previous log file */
96 protected static long logCounter = 0;
97
98 /** The no-args constructor */
99 public ModuleWrapper(){
100
101 }
102
103 public void cleanUp() {}
104
105 /** The all-args constructor */
106 public ModuleWrapper(String in,String out, ModuleInterface inner){
107 this.setFromSchema(in);
108 this.setToSchema(out);
109 this.setInner(inner);
110 }
111
112 /**
113 * Process an XML request - as a String
114 *
115 * @param xmlIn the request to process
116 * @return the response - contains any error messages
117 * @see java.lang.String
118 */
119 public String process(String xmlIn) {
120
121 long logNumber = 0;
122 String xmlOut = "";
123 if (getLogging()) {
124 if (logCounter == 0)
125 logCounter = new SecureRandom().nextLong();
126 try {
127 logNumber = logCounter++;
128 String filename = getLogDirectory() + File.separator + logNumber + ".in";
129 File file = new File(filename);
130
131 while (file.exists()){
132 logCounter = new SecureRandom().nextLong();
133 logNumber = logCounter++;
134 filename = getLogDirectory() + File.separator + logNumber + ".in";
135 file = new File(filename);
136 }
137
138 FileWriter writer = new FileWriter(file);
139 writer.write(xmlIn);
140 } catch (IOException e) {
141 System.err.println("ModuleWrapper: caught exception: " +e );
142 }
143 }
144 xmlOut = processInner(xmlIn);
145
146 try {
147 String filename = getLogDirectory() + File.separator + logNumber + ".out";
148 File file = new File(filename);
149 FileWriter writer = new FileWriter(file);
150 writer.write(xmlOut);
151 } catch (IOException e) {
152 System.err.println("ModuleWrapper: caught exception: " +e );
153 }
154 return xmlOut;
155 }
156
157 /**
158 * Process an XML request - as a String
159 *
160 * @param xmlIn the request to process
161 * @return the response - contains any error messages
162 * @see java.lang.String
163 */
164 protected String processInner(String xmlIn)
165 {
166 try {
167 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
168 DocumentBuilder builder= factory.newDocumentBuilder();
169 DOMParser parser = new DOMParser();
170 try {
171
172 parser.setFeature("http://xml.org/sax/features/validation",true);
173 parser.setFeature("http://apache.org/xml/features/validation/schema",true);
174 parser.setFeature("http://apache.org/xml/features/validation/dynamic", true);
175 parser.setFeature("http://apache.org/xml/features/validation/schema", true);
176 parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
177 parser.setFeature("http://apache.org/xml/features/validation/schema/normalized-value", true);
178 parser.setFeature("http://apache.org/xml/features/validation/warn-on-duplicate-attdef", true);
179 parser.setFeature("http://apache.org/xml/features/validation/warn-on-undeclared-elemdef", true);
180 parser.setFeature("http://apache.org/xml/features/warn-on-duplicate-entitydef", true);
181 } catch (Exception a) {
182 System.err.println("unable to set feature:" +a);
183 a.printStackTrace();
184 }
185
186 // do the pre-call checking
187 InputSource xml_source = new InputSource(new StringReader(xmlIn));
188 try {
189 parser.parse(xml_source);
190 } catch (Exception e){
191 System.err.println("parsing error:" +e);
192 e.printStackTrace();
193 return "<response> <error class=\"ModuleWrapper\" code=1> Error: supplied string contained the parse error: " + e + " </error> </response>";
194 }
195
196 String xmlOut = inner.process(xmlIn);
197
198 // do the post-call checking
199 InputSource xmlResult = new InputSource(new StringReader(xmlIn));
200 try {
201 parser.parse(xmlResult);
202 } catch (Exception e){
203 System.err.println("parsing error:" +e);
204 e.printStackTrace();
205 return "<response> <error class=\"ModuleWrapper\" code=2> Error: returned string contained the parse error: " + e + "</error></response>";
206 }
207
208 return xmlOut;
209
210 } catch (Exception e){
211 System.err.println("other error:" +e);
212 e.printStackTrace();
213 return "<response> <error class=\"ModuleWrapper\" code=3>Error: Unknown error or warning: " + e + " </error></response>";
214 }
215 }
216
217 /**
218 * Process an XML request - as a DOM Element
219 *
220 * @param in the request to process
221 * @return the response - contains any error messages
222 * @see org.w3c.dom.Element
223 */
224 public Element process(Element xmlIn) {
225 throw new Error("Not implmented yet. Should be faked by stringizing the node.");
226 }
227}
228
229
230
231
232
233
234
235
236
Note: See TracBrowser for help on using the repository browser.