KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > AvalonToCocoonSourceInvocationHandler


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 org.apache.cocoon.components.source.impl;
17
18 import org.apache.avalon.framework.component.ComponentManager;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.components.source.SourceUtil;
21 import org.apache.cocoon.environment.Environment;
22 import org.apache.cocoon.environment.ModifiableSource;
23 import org.apache.excalibur.source.Source;
24 import org.apache.excalibur.source.SourceException;
25 import org.apache.excalibur.source.SourceResolver;
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.lang.reflect.InvocationHandler JavaDoc;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.lang.reflect.Proxy JavaDoc;
36
37 /**
38  * An <code>InvocationHandler</code> which acts as a proxy for excalibur
39  * <code>Source</code> objects to make them compatible with the cocoon
40  * <code>Source</code> interface.
41  * Much of the code was taken from {@link AvalonToCocoonSource}.
42  *
43  * @author Stefan K&ouml;hler
44  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
45  * @version CVS $Id: AvalonToCocoonSourceInvocationHandler.java 30932 2004-07-29 17:35:38Z vgritsenko $
46  */

47 public class AvalonToCocoonSourceInvocationHandler
48 implements InvocationHandler JavaDoc {
49
50     /** The real source */
51     protected Source source;
52
53     /** The source resolver */
54     protected SourceResolver resolver;
55
56     /** The environment */
57     protected Environment environment;
58
59     /** The manager */
60     protected ComponentManager manager;
61     
62     /**
63      * Constructor
64      */

65     public AvalonToCocoonSourceInvocationHandler(Source source,
66                                                   SourceResolver resolver,
67                                                   Environment environment,
68                                                   ComponentManager manager) {
69         this.source = source;
70         this.resolver = resolver;
71         this.environment = environment;
72         this.manager = manager;
73     }
74
75     /**
76      * Processes a method invocation on a proxy instance and returns the result.
77      * It invokes the corresponding method of the wrapped excalibur source.
78      *
79      * @param proxy the Cocoon source proxy instance that the method was invoked on
80      * @param method the Method instance corresponding to the interface method
81      * invoked on the proxy instance.
82      * @param args the arguments for the interface method
83      * @return the result of the proxy method
84      */

85     public Object JavaDoc invoke( Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args )
86     throws Throwable JavaDoc {
87         try {
88             if (method.getName().equals("getInputStream")) {
89                 return this.getInputStream();
90             } else if (method.getName().equals("getInputSource")) {
91                 return this.getInputSource();
92             } else if (method.getName().equals("getSystemId")) {
93                 return this.getSystemId();
94             } else if(method.getName().equals("recycle")) {
95                 this.recycle();
96                 return null;
97             } else if(method.getName().equals("toSAX")) {
98                 this.toSAX((ContentHandler JavaDoc) args[0]);
99                 return null;
100             } else{
101                 return method.invoke(source, args);
102             }
103         }
104         catch ( InvocationTargetException JavaDoc e ){
105             throw e.getTargetException();
106         }
107     }
108
109     /**
110      * Return an <code>InputStream</code> object to read from the source.
111      */

112     public InputStream JavaDoc getInputStream()
113     throws ProcessingException, IOException JavaDoc {
114         try {
115             return this.source.getInputStream();
116         } catch (SourceException e) {
117             throw SourceUtil.handle(e);
118         }
119     }
120
121     /**
122      * Return an <code>InputSource</code> object to read the XML
123      * content.
124      *
125      * @return an <code>InputSource</code> value
126      * @exception ProcessingException if an error occurs
127      * @exception IOException if an error occurs
128      */

129     public InputSource JavaDoc getInputSource()
130     throws ProcessingException, IOException JavaDoc {
131         try {
132             InputSource JavaDoc newObject = new InputSource JavaDoc(this.source.getInputStream());
133             newObject.setSystemId(this.getSystemId());
134             return newObject;
135         } catch (SourceException se) {
136             throw SourceUtil.handle(se);
137         }
138     }
139
140     /**
141      * Return the unique identifer for this source
142      */

143     public String JavaDoc getSystemId() {
144         return this.source.getURI();
145     }
146
147     public void recycle() {
148         this.resolver.release(this.source);
149         this.source = null;
150         this.environment = null;
151     }
152
153     public void refresh() {
154         this.source.refresh();
155     }
156
157     /**
158      * Stream content to a content handler or to an XMLConsumer.
159      *
160      * @throws SAXException if failed to parse source document.
161      */

162     public void toSAX(ContentHandler JavaDoc handler)
163     throws SAXException JavaDoc {
164         try {
165             SourceUtil.parse(this.manager, this.source, handler);
166         } catch (ProcessingException pe) {
167             throw new SAXException JavaDoc("ProcessingException during streaming.", pe);
168         } catch (IOException JavaDoc ioe) {
169             throw new SAXException JavaDoc("IOException during streaming.", ioe);
170         }
171     }
172
173     /**
174      * Creates a dynamic proxy for an excalibur <code>Source</code> object to
175      * make it behave like a cocoon <code>Source</code>.
176      * @param source the source object to be wrapped
177      * @return a proxy object which implements the cocoon <code>Source</code>
178      * interface and all of the interfaces that the wrapped object
179      * implements
180      * @throws SourceException in case of an error
181      */

182     public static org.apache.cocoon.environment.Source createProxy(Source source,
183                SourceResolver resolver,
184                Environment environment,
185                ComponentManager manager)
186     throws SourceException{
187         Class JavaDoc[] sourceInterfaces = source.getClass().getInterfaces();
188         Class JavaDoc[] proxyInterfaces = new Class JavaDoc[sourceInterfaces.length+2];
189
190         for(int i=0; i < sourceInterfaces.length; i++) {
191             proxyInterfaces[i] = sourceInterfaces[i];
192         }
193         
194         proxyInterfaces[sourceInterfaces.length] = org.apache.cocoon.environment.Source.class;
195         proxyInterfaces[sourceInterfaces.length+1] = ModifiableSource.class;
196
197         InvocationHandler JavaDoc invocationHandler = new AvalonToCocoonSourceInvocationHandler(
198              source,
199              resolver,
200              environment,
201              manager
202         );
203
204         try {
205             org.apache.cocoon.environment.Source proxy;
206             proxy = (org.apache.cocoon.environment.Source)Proxy.newProxyInstance(source.getClass().getClassLoader(),
207                                                   proxyInterfaces, invocationHandler);
208             return proxy;
209         } catch(Exception JavaDoc e){
210             throw new SourceException("Error creating proxy object", e);
211         }
212
213     }
214
215 }
216
217
Popular Tags