KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > source > CopletSource


1 /*
2  * Copyright 1999-2002,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.portal.source;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
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 JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41
42 /**
43  * This is the source implementation of the coplet source
44  *
45  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
46  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
47  *
48  * @version CVS $Id: CopletSource.java 123407 2004-12-27 13:51:59Z cziegeler $
49  */

50 public class CopletSource
51     implements Source, XMLizable, Serviceable, Contextualizable {
52
53     protected ServiceManager manager;
54     
55     protected Context context;
56     
57     protected String JavaDoc uri;
58     protected String JavaDoc copletControllerName;
59     protected CopletInstanceData copletInstanceData;
60     
61     /** The used protocol */
62     protected String JavaDoc scheme;
63     
64     /* (non-Javadoc)
65      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
66      */

67     public void service(ServiceManager manager) throws ServiceException {
68         this.manager = manager;
69     }
70
71     /* (non-Javadoc)
72      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
73      */

74     public void contextualize(Context context) throws ContextException {
75         this.context = context;
76     }
77     
78     public CopletSource(String JavaDoc location, String JavaDoc 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     /**
87      * @see org.apache.excalibur.source.Source#getInputStream()
88      */

89     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, 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 JavaDoc os = new ByteArrayOutputStream JavaDoc();
98                 serializer.setOutputStream(os);
99                 this.toSAX(serializer);
100                 return new ByteArrayInputStream JavaDoc(os.toByteArray());
101             } catch (SAXException JavaDoc 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     /**
117      * @see org.apache.excalibur.source.Source#getURI()
118      */

119     public String JavaDoc getURI() {
120         return this.uri;
121     }
122
123     /**
124      * @see org.apache.excalibur.source.Source#getValidity()
125      */

126     public SourceValidity getValidity() {
127         return null;
128     }
129
130     /**
131      * @see org.apache.excalibur.source.Source#refresh()
132      */

133     public void refresh() {
134         // nothing to do
135
}
136
137     /**
138      * @see org.apache.excalibur.source.Source#getMimeType()
139      */

140     public String JavaDoc getMimeType() {
141         return null;
142     }
143
144     /**
145      * @see org.apache.excalibur.source.Source#getContentLength()
146      */

147     public long getContentLength() {
148         return -1;
149     }
150
151     /**
152      * @see org.apache.excalibur.source.Source#getLastModified()
153      */

154     public long getLastModified() {
155         return 0;
156     }
157
158     /**
159      * @see org.apache.excalibur.xml.sax.XMLizable#toSAX(ContentHandler)
160      */

161     public void toSAX(ContentHandler JavaDoc handler)
162     throws SAXException JavaDoc {
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 JavaDoc("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     /**
182      * @see org.apache.excalibur.source.Source#exists()
183      */

184     public boolean exists() {
185         return true;
186     }
187
188     /**
189      * @see org.apache.excalibur.source.Source#getScheme()
190      */

191     public String JavaDoc getScheme() {
192         return this.scheme;
193     }
194
195 }
196
Popular Tags