KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rmitest > RMIGenerator


1 /*
2 * Copyright 1999-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */

16 package rmitest;
17
18 // import the necessary classes from the java.io package
19
import java.io.IOException;
20 import java.io.StringReader;
21
22 // import the necessary classes from the java.rmi
23
import java.rmi.Naming;
24 import java.rmi.RemoteException;
25 import java.rmi.NotBoundException;
26
27 // import the necessary SAX classes
28
import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
30
31 // import of the classes used from Cocoon 2
32
import org.apache.cocoon.ProcessingException;
33 import org.apache.cocoon.generation.ComposerGenerator;
34
35 // import of the classes from the
36
// Avalon Framework
37
import org.apache.avalon.framework.parameters.Parameters;
38 import org.apache.avalon.framework.parameters.ParameterException;
39 import org.apache.avalon.framework.component.ComponentException;
40
41 // needed for obtaining parser in Cocoon
42
import org.apache.excalibur.xml.sax.SAXParser;
43
44 /**
45  * <p>
46  * The <code>RMIGenerator</code> is a generator that reads a String via RMI
47  * and generates SAX events.
48  * The RMIGenerator extends the <code>ComposerGenerator</code> class. This is
49  * done so we can access the <code>ComponentManager</code> to obtain a
50  * <code>SAXParser</code>.
51  * </p>
52  *
53  * <p>
54  * The methods invokes to obtain the String via RMI are defined in the
55  * <code>ServerFunctions</code> interface in resides in the same package
56  * as this generator. A RMI server application that wants to be able to
57  * &quot;feed&quot; XML data to this generator must implement the
58  * <code>ServerFunctions</code> interface.
59  * </p>
60  *
61  * <p>
62  * <b>Usage:</b>
63  * </p>
64  *
65  * <p>
66  * Suppose you declare this generator in your sitemap and name it
67  * <code>rmigenerator</code>. A typical example of use is the following:
68  * </p>
69  *
70  * <p>
71  * <pre>
72  * <code>
73  * &lt;map:match pattern=&quot;rmi/**.xml&quot;&gt;
74  * &lt;map:generate type=&quot;rmigenerator&quot; SRC=&quot;{1}.xml&quot;&gt;
75  * &lt;!-- host parameter where RMI server is running, REQUIRED --&gt;
76  * &lt;map:parameter name=&quot;host&quot; value=&quot;myhost.com&quot;/&gt;
77  * &lt;!-- bindname parameter, name to which RMI server is bound in remote rmiregistry, REQUIRED --&gt;
78  * &lt;map:paramater name=&quot;bindname&quot; value=&quot;RMIServer&quot;/&gt;
79  * &lt;!-- port parameter, at which port the rmiregistry is running --&gt;
80  * &lt;!-- at the remote host, OPTIONAL --&gt;
81  * &lt;!-- 1099 is the default, this is in fact not really needed --&gt;
82  * &lt;map:parameter name=&quot;port&quot; value=&quot;1099&quot;/&gt;
83  * &lt;map:generate/&gt;
84  * &lt;map:transform SRC=&quot;somestylesheet.xsl&quot;/&gt;
85  * &lt;map:serialize/&gt;
86  * &lt;/map:match&gt;
87  * </code>
88  * </pre>
89  * </p>
90  *
91  *
92  * @author <a HREF="mailto:Erwin.Hermans@cs.kuleuven.ac.be">Erwin Hermans</a>
93  * (Student Computer Science Department KULeuven, 2001-2002)
94  * @version CVS $Id: RMIGenerator.java 30932 2004-07-29 17:35:38Z vgritsenko $
95  */

96 public class RMIGenerator extends ComposerGenerator {
97
98     /**
99      * Generate SAX events based on the parameters and the source specified
100      * in the sitemap. If the <code>src</code> attribute is specified, the
101      * <code>getResource(String)</code> method is invoked, otherwise the
102      * <code>sayHello()</code> is invoked on the remote object.
103      *
104      */

105     public void generate () throws IOException, SAXException, ProcessingException {
106         String host;
107         
108         // lookup parameter 'host'
109
try {
110             host = parameters.getParameter("host");
111             // test if host is not the empty string
112
if (host == "") {
113                 throw new ParameterException("The parameter 'host' may not be the empty string");
114             }
115         } catch (ParameterException pe) {
116             // rethrow as a ProcessingException
117
throw new ProcessingException("Parameter 'host' not specified",pe);
118         }
119         
120         String bindname;
121         
122         // lookup parameter 'bindname'
123
try {
124             bindname = parameters.getParameter("bindname");
125             // test if bindname is not the empty string
126
if (bindname == "") {
127                 throw new ParameterException("The parameter 'bindname' may not be the empty string");
128             }
129         } catch (ParameterException pe) {
130             // rethrow as a ProcessingException
131
throw new ProcessingException("Parameter 'bindname' not specified",pe);
132         }
133         
134         String port = "";
135         // lookup parameter 'port'
136
try {
137             port = parameters.getParameter("port");
138             port = ":" + port;
139         } catch (ParameterException pe) {
140             // reset port to the empty string
141
// port is not required
142
port = "";
143         }
144         
145         try {
146             ServerFunctions obj = (ServerFunctions)Naming.lookup("//" + host + port + "/" + bindname);
147             String message = "";
148
149             // determine the method to invoke
150
// depending on value of source
151
if (this.source == null) {
152                 message = obj.sayHello();
153             } else {
154                 message = obj.getResource(this.source);
155             }
156             
157             SAXParser parser = null;
158             parser = (SAXParser)this.manager.lookup(SAXParser.ROLE);
159             InputSource inputSource = new InputSource(new StringReader(message));
160             parser.parse(inputSource,super.xmlConsumer);
161         } catch (NotBoundException nbe) {
162             throw new ProcessingException("Error looking up the RMI application server",nbe);
163         } catch (ComponentException ce) {
164             throw new ProcessingException("Error obtaining a SAXParser",ce);
165         }
166     }
167 }
168         
169
Popular Tags