1 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 ; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.SAXException ; 29 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.lang.reflect.InvocationHandler ; 33 import java.lang.reflect.InvocationTargetException ; 34 import java.lang.reflect.Method ; 35 import java.lang.reflect.Proxy ; 36 37 47 public class AvalonToCocoonSourceInvocationHandler 48 implements InvocationHandler { 49 50 51 protected Source source; 52 53 54 protected SourceResolver resolver; 55 56 57 protected Environment environment; 58 59 60 protected ComponentManager manager; 61 62 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 85 public Object invoke( Object proxy, Method method, Object [] args ) 86 throws Throwable { 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 ) args[0]); 99 return null; 100 } else{ 101 return method.invoke(source, args); 102 } 103 } 104 catch ( InvocationTargetException e ){ 105 throw e.getTargetException(); 106 } 107 } 108 109 112 public InputStream getInputStream() 113 throws ProcessingException, IOException { 114 try { 115 return this.source.getInputStream(); 116 } catch (SourceException e) { 117 throw SourceUtil.handle(e); 118 } 119 } 120 121 129 public InputSource getInputSource() 130 throws ProcessingException, IOException { 131 try { 132 InputSource newObject = new InputSource (this.source.getInputStream()); 133 newObject.setSystemId(this.getSystemId()); 134 return newObject; 135 } catch (SourceException se) { 136 throw SourceUtil.handle(se); 137 } 138 } 139 140 143 public String 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 162 public void toSAX(ContentHandler handler) 163 throws SAXException { 164 try { 165 SourceUtil.parse(this.manager, this.source, handler); 166 } catch (ProcessingException pe) { 167 throw new SAXException ("ProcessingException during streaming.", pe); 168 } catch (IOException ioe) { 169 throw new SAXException ("IOException during streaming.", ioe); 170 } 171 } 172 173 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 [] sourceInterfaces = source.getClass().getInterfaces(); 188 Class [] proxyInterfaces = new Class [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 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 e){ 210 throw new SourceException("Error creating proxy object", e); 211 } 212 213 } 214 215 } 216 217 | Popular Tags |