KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.avalon.framework.logger.Logger;
29 import org.apache.cocoon.components.source.InspectableSource;
30 import org.apache.cocoon.components.source.SourceDescriptor;
31 import org.apache.cocoon.components.source.helpers.SourceProperty;
32 import org.apache.excalibur.source.ModifiableTraversableSource;
33 import org.apache.excalibur.source.Source;
34 import org.apache.excalibur.source.SourceException;
35 import org.apache.excalibur.source.SourceNotFoundException;
36 import org.apache.excalibur.source.SourceValidity;
37 import org.apache.excalibur.source.impl.validity.AggregatedValidity;
38
39 /**
40  * Source wrapper that enhances the wrapped sources with additional capabilities.
41  *
42  * <p>
43  * Currently this Source optionally adds inspectability
44  * through the InspectableSource interface.
45  * </p>
46  *
47  * <p>
48  * Wrapped sources must implement ModifiableTraversableSource.
49  * </p>
50  */

51 public class RepositorySource extends AbstractLogEnabled
52 implements Source, ModifiableTraversableSource, InspectableSource {
53
54     // the original source prefix
55
final String JavaDoc m_prefix;
56     // the wrapped source
57
final ModifiableTraversableSource m_delegate;
58     private final SourceDescriptor m_descriptor;
59     
60     // ---------------------------------------------------- Lifecycle
61

62     public RepositorySource(
63         final String JavaDoc prefix,
64         final ModifiableTraversableSource delegate,
65         final SourceDescriptor descriptor,
66         final Logger logger) throws SourceException {
67         
68         m_prefix = prefix;
69         m_delegate = delegate;
70         m_descriptor = descriptor;
71         enableLogging(logger);
72     }
73     
74     // ---------------------------------------------------- InspectableSource implementation
75

76     /**
77      * Get all source properties that are defined on the wrapped source.
78      * If the wrapped source is itself an InspectableSource the implementation
79      * will return the aggregate set that results from combining the properties
80      * returned from a delegate call to the wrapped source with the
81      * properties returned by the source descriptor.
82      */

83     public SourceProperty[] getSourceProperties() throws SourceException {
84         
85         final List JavaDoc properties = new ArrayList JavaDoc();
86         if (m_delegate instanceof InspectableSource) {
87             properties.addAll(Arrays.asList(((InspectableSource) m_delegate).getSourceProperties()));
88         }
89         if (m_descriptor != null) {
90             properties.addAll(Arrays.asList(m_descriptor.getSourceProperties(m_delegate)));
91         }
92         return (SourceProperty[]) properties.toArray(new SourceProperty[properties.size()]);
93     }
94     
95     /**
96      * Get the source property on the wrapped source. If the wrapped source implements
97      * InspectableSource the implementation will first try to get it from there.
98      * If it doesn't exist on the delegate it will try to find it using the source descriptor.
99      */

100     public SourceProperty getSourceProperty(String JavaDoc uri, String JavaDoc name) throws SourceException {
101         SourceProperty property = null;
102         if (m_delegate instanceof InspectableSource) {
103             property = ((InspectableSource) m_delegate).getSourceProperty(uri,name);
104         }
105         if (property == null && m_descriptor != null) {
106             property = m_descriptor.getSourceProperty(m_delegate,uri,name);
107         }
108         return property;
109     }
110     
111     /**
112      * Remove the source property on the wrapped source. If the wrapped source implements
113      * InspectableSource the implementation will try to remove the property on both
114      * the wrapped source directly and on the source descriptor.
115      */

116     public void removeSourceProperty(String JavaDoc uri, String JavaDoc name) throws SourceException {
117         if (m_delegate instanceof InspectableSource) {
118             ((InspectableSource) m_delegate).removeSourceProperty(uri,name);
119         }
120         if (m_descriptor != null) {
121             m_descriptor.removeSourceProperty(m_delegate,uri,name);
122         }
123     }
124     
125     /**
126      * Set the source property on the wrapped source. If the wrapped source implements
127      * InspectableSource set the property directly on the wrapped source. Otherwise
128      * set it on the SourceDescriptor.
129      */

130     public void setSourceProperty(SourceProperty property) throws SourceException {
131         if (m_delegate instanceof InspectableSource) {
132             ((InspectableSource) m_delegate).setSourceProperty(property);
133         } else if (m_descriptor != null) {
134             m_descriptor.setSourceProperty(m_delegate, property);
135         }
136     }
137     
138     
139     // ---------------------------------------------------- Source implementation
140

141     public boolean exists() {
142         return m_delegate.exists();
143     }
144     
145     public long getContentLength() {
146         return m_delegate.getContentLength();
147     }
148     
149     public InputStream JavaDoc getInputStream()
150         throws IOException JavaDoc, SourceNotFoundException {
151         return m_delegate.getInputStream();
152     }
153     
154     public long getLastModified() {
155         return m_delegate.getLastModified();
156     }
157     
158     public String JavaDoc getMimeType() {
159         return m_delegate.getMimeType();
160     }
161     
162     public String JavaDoc getScheme() {
163         return m_prefix;
164     }
165     
166     public String JavaDoc getURI() {
167         return m_prefix + ":" + m_delegate.getURI();
168     }
169     
170     /**
171      * Return a SourceValidity object describing
172      * the validity of this Source.
173      * <p>
174      * If the SourceDescriptor service is present, the resulting
175      * validity is an aggregated validity object containing both
176      * the validity describing the source itself _and_ one describing
177      * the validity of the SourceProperties managed by the SourceDescriptor.
178      * </p>
179      */

180     public SourceValidity getValidity() {
181         SourceValidity val1;
182         val1 = m_delegate.getValidity();
183         if (val1 != null && m_descriptor != null) {
184             SourceValidity val2 = m_descriptor.getValidity(m_delegate);
185             if (val2 != null) {
186                 AggregatedValidity result = new AggregatedValidity();
187                 result.add(val1);
188                 result.add(val2);
189                 return result;
190             }
191         }
192         return val1;
193     }
194     
195     public void refresh() {
196         m_delegate.refresh();
197     }
198     
199     
200     // ---------------------------------------------------- ModifiableTraversableSource
201

202     public Source getChild(String JavaDoc name) throws SourceException {
203         if (!m_delegate.isCollection()) return null;
204         ModifiableTraversableSource child = (ModifiableTraversableSource) m_delegate.getChild(name);
205         if (child == null) return null;
206         
207         return new RepositorySource(
208             m_prefix,
209             child,
210             m_descriptor,
211             getLogger()
212         );
213     }
214
215     public Collection JavaDoc getChildren() throws SourceException {
216         if (!m_delegate.isCollection()) return null;
217         Collection JavaDoc result = new ArrayList JavaDoc();
218         Iterator JavaDoc iter = m_delegate.getChildren().iterator();
219         while(iter.hasNext()) {
220             ModifiableTraversableSource child = (ModifiableTraversableSource) iter.next();
221             
222             result.add(
223                 new RepositorySource(
224                     m_prefix,
225                     child,
226                     m_descriptor,
227                     getLogger()
228                 )
229             );
230         }
231         return result;
232     }
233
234     public String JavaDoc getName() {
235         return m_delegate.getName();
236     }
237
238     public Source getParent() throws SourceException {
239         return new RepositorySource(
240             m_prefix,
241             (ModifiableTraversableSource) m_delegate.getParent(),
242             m_descriptor,
243             getLogger()
244         );
245     }
246
247     public boolean isCollection() {
248         return m_delegate.isCollection();
249     }
250
251     public void makeCollection() throws SourceException {
252         m_delegate.makeCollection();
253     }
254
255     public boolean canCancel(OutputStream JavaDoc out) {
256         return m_delegate.canCancel(out);
257     }
258
259     public void cancel(OutputStream JavaDoc out) throws IOException JavaDoc {
260         m_delegate.cancel(out);
261     }
262
263     public void delete() throws SourceException {
264         m_delegate.delete();
265     }
266
267     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
268         return m_delegate.getOutputStream();
269     }
270
271 }
272
Popular Tags