001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.source;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import org.apache.avalon.framework.context.Context;
025: import org.apache.avalon.framework.context.ContextException;
026: import org.apache.avalon.framework.context.Contextualizable;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.ServiceSelector;
030: import org.apache.avalon.framework.service.Serviceable;
031: import org.apache.cocoon.CascadingIOException;
032: import org.apache.cocoon.components.ContextHelper;
033: import org.apache.cocoon.portal.coplet.CopletInstanceData;
034: import org.apache.cocoon.portal.coplet.adapter.CopletAdapter;
035: import org.apache.cocoon.serialization.Serializer;
036: import org.apache.excalibur.source.Source;
037: import org.apache.excalibur.source.SourceNotFoundException;
038: import org.apache.excalibur.source.SourceValidity;
039: import org.apache.excalibur.xml.sax.XMLizable;
040: import org.xml.sax.ContentHandler;
041: import org.xml.sax.SAXException;
042:
043: /**
044: * This is the source implementation of the coplet source
045: *
046: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
047: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
048: *
049: * @version CVS $Id: CopletSource.java 433543 2006-08-22 06:22:54Z crossley $
050: */
051: public class CopletSource implements Source, XMLizable, Serviceable,
052: Contextualizable {
053:
054: protected ServiceManager manager;
055:
056: protected Context context;
057:
058: protected String uri;
059: protected String copletControllerName;
060: protected CopletInstanceData copletInstanceData;
061:
062: /** The used protocol */
063: protected String scheme;
064:
065: /* (non-Javadoc)
066: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
067: */
068: public void service(ServiceManager manager) throws ServiceException {
069: this .manager = manager;
070: }
071:
072: /* (non-Javadoc)
073: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
074: */
075: public void contextualize(Context context) throws ContextException {
076: this .context = context;
077: }
078:
079: public CopletSource(String location, String protocol,
080: CopletInstanceData coplet) {
081: this .uri = location;
082: this .scheme = (protocol == null ? "coplet" : protocol);
083: this .copletInstanceData = coplet;
084: this .copletControllerName = this .copletInstanceData
085: .getCopletData().getCopletBaseData()
086: .getCopletAdapterName();
087: }
088:
089: /**
090: * @see org.apache.excalibur.source.Source#getInputStream()
091: */
092: public InputStream getInputStream() throws IOException,
093: SourceNotFoundException {
094: try {
095: ServiceManager sitemapManager = (ServiceManager) this .context
096: .get(ContextHelper.CONTEXT_SITEMAP_SERVICE_MANAGER);
097: ServiceSelector serializerSelector = null;
098: Serializer serializer = null;
099: try {
100: serializerSelector = (ServiceSelector) sitemapManager
101: .lookup(Serializer.ROLE + "Selector");
102: serializer = (Serializer) serializerSelector
103: .select("xml");
104: ByteArrayOutputStream os = new ByteArrayOutputStream();
105: serializer.setOutputStream(os);
106: this .toSAX(serializer);
107: return new ByteArrayInputStream(os.toByteArray());
108: } catch (SAXException se) {
109: throw new CascadingIOException(
110: "Unable to stream content.", se);
111: } catch (ServiceException ce) {
112: throw new CascadingIOException(
113: "Unable to get components for serializing.", ce);
114: } finally {
115: if (serializer != null) {
116: serializerSelector.release(serializer);
117: }
118: sitemapManager.release(serializerSelector);
119: }
120: } catch (ContextException ce) {
121: throw new CascadingIOException(
122: "Unable to get service manager.", ce);
123: }
124: }
125:
126: /**
127: * @see org.apache.excalibur.source.Source#getURI()
128: */
129: public String getURI() {
130: return this .uri;
131: }
132:
133: /**
134: * @see org.apache.excalibur.source.Source#getValidity()
135: */
136: public SourceValidity getValidity() {
137: return null;
138: }
139:
140: /**
141: * @see org.apache.excalibur.source.Source#refresh()
142: */
143: public void refresh() {
144: // nothing to do
145: }
146:
147: /**
148: * @see org.apache.excalibur.source.Source#getMimeType()
149: */
150: public String getMimeType() {
151: return null;
152: }
153:
154: /**
155: * @see org.apache.excalibur.source.Source#getContentLength()
156: */
157: public long getContentLength() {
158: return -1;
159: }
160:
161: /**
162: * @see org.apache.excalibur.source.Source#getLastModified()
163: */
164: public long getLastModified() {
165: return 0;
166: }
167:
168: /**
169: * @see org.apache.excalibur.xml.sax.XMLizable#toSAX(ContentHandler)
170: */
171: public void toSAX(ContentHandler handler) throws SAXException {
172: ServiceSelector copletAdapterSelector = null;
173: CopletAdapter copletAdapter = null;
174: try {
175: copletAdapterSelector = (ServiceSelector) this .manager
176: .lookup(CopletAdapter.ROLE + "Selector");
177: copletAdapter = (CopletAdapter) copletAdapterSelector
178: .select(this .copletControllerName);
179:
180: copletAdapter.toSAX(this .copletInstanceData, handler);
181: } catch (ServiceException ce) {
182: throw new SAXException(
183: "Unable to lookup coplet adaptor or adaptor selector.",
184: ce);
185: } finally {
186: if (null != copletAdapter) {
187: copletAdapterSelector.release(copletAdapter);
188: }
189: this .manager.release(copletAdapterSelector);
190: }
191:
192: }
193:
194: /**
195: * @see org.apache.excalibur.source.Source#exists()
196: */
197: public boolean exists() {
198: return true;
199: }
200:
201: /**
202: * @see org.apache.excalibur.source.Source#getScheme()
203: */
204: public String getScheme() {
205: return this.scheme;
206: }
207:
208: }
|