1 16 package org.apache.cocoon.portal.source; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 import org.apache.avalon.framework.context.Context; 24 import org.apache.avalon.framework.context.ContextException; 25 import org.apache.avalon.framework.context.Contextualizable; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.ServiceSelector; 29 import org.apache.avalon.framework.service.Serviceable; 30 import org.apache.cocoon.CascadingIOException; 31 import org.apache.cocoon.components.ContextHelper; 32 import org.apache.cocoon.portal.coplet.CopletInstanceData; 33 import org.apache.cocoon.portal.coplet.adapter.CopletAdapter; 34 import org.apache.cocoon.serialization.Serializer; 35 import org.apache.excalibur.source.Source; 36 import org.apache.excalibur.source.SourceNotFoundException; 37 import org.apache.excalibur.source.SourceValidity; 38 import org.apache.excalibur.xml.sax.XMLizable; 39 import org.xml.sax.ContentHandler ; 40 import org.xml.sax.SAXException ; 41 42 50 public class CopletSource 51 implements Source, XMLizable, Serviceable, Contextualizable { 52 53 protected ServiceManager manager; 54 55 protected Context context; 56 57 protected String uri; 58 protected String copletControllerName; 59 protected CopletInstanceData copletInstanceData; 60 61 62 protected String scheme; 63 64 67 public void service(ServiceManager manager) throws ServiceException { 68 this.manager = manager; 69 } 70 71 74 public void contextualize(Context context) throws ContextException { 75 this.context = context; 76 } 77 78 public CopletSource(String location, String protocol, 79 CopletInstanceData coplet) { 80 this.uri = location; 81 this.scheme = (protocol == null ? "coplet" : protocol); 82 this.copletInstanceData = coplet; 83 this.copletControllerName = this.copletInstanceData.getCopletData().getCopletBaseData().getCopletAdapterName(); 84 } 85 86 89 public InputStream getInputStream() throws IOException , SourceNotFoundException { 90 try { 91 ServiceManager sitemapManager = (ServiceManager) this.context.get(ContextHelper.CONTEXT_SITEMAP_SERVICE_MANAGER); 92 ServiceSelector serializerSelector = null; 93 Serializer serializer = null; 94 try { 95 serializerSelector = (ServiceSelector) sitemapManager.lookup(Serializer.ROLE+"Selector"); 96 serializer = (Serializer) serializerSelector.select("xml"); 97 ByteArrayOutputStream os = new ByteArrayOutputStream (); 98 serializer.setOutputStream(os); 99 this.toSAX(serializer); 100 return new ByteArrayInputStream (os.toByteArray()); 101 } catch (SAXException se) { 102 throw new CascadingIOException("Unable to stream content.", se); 103 } catch (ServiceException ce) { 104 throw new CascadingIOException("Unable to get components for serializing.", ce); 105 } finally { 106 if ( serializer != null ) { 107 serializerSelector.release(serializer); 108 } 109 sitemapManager.release(serializerSelector); 110 } 111 } catch (ContextException ce) { 112 throw new CascadingIOException("Unable to get service manager.", ce); 113 } 114 } 115 116 119 public String getURI() { 120 return this.uri; 121 } 122 123 126 public SourceValidity getValidity() { 127 return null; 128 } 129 130 133 public void refresh() { 134 } 136 137 140 public String getMimeType() { 141 return null; 142 } 143 144 147 public long getContentLength() { 148 return -1; 149 } 150 151 154 public long getLastModified() { 155 return 0; 156 } 157 158 161 public void toSAX(ContentHandler handler) 162 throws SAXException { 163 ServiceSelector copletAdapterSelector = null; 164 CopletAdapter copletAdapter = null; 165 try { 166 copletAdapterSelector = (ServiceSelector)this.manager.lookup(CopletAdapter.ROLE+"Selector"); 167 copletAdapter = (CopletAdapter)copletAdapterSelector.select(this.copletControllerName); 168 169 copletAdapter.toSAX(this.copletInstanceData, handler); 170 } catch (ServiceException ce) { 171 throw new SAXException ("Unable to lookup coplet adaptor or adaptor selector.", ce); 172 } finally { 173 if ( null != copletAdapter ) { 174 copletAdapterSelector.release( copletAdapter ); 175 } 176 this.manager.release(copletAdapterSelector); 177 } 178 179 } 180 181 184 public boolean exists() { 185 return true; 186 } 187 188 191 public String getScheme() { 192 return this.scheme; 193 } 194 195 } 196 | Popular Tags |