KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.context.Contextualizable;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.Serviceable;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31 import org.apache.cocoon.Constants;
32 import org.apache.cocoon.environment.Context;
33 import org.apache.excalibur.source.Source;
34 import org.apache.excalibur.source.SourceException;
35 import org.apache.excalibur.source.SourceFactory;
36 import org.apache.excalibur.source.SourceResolver;
37 import org.apache.excalibur.source.SourceUtil;
38 import org.apache.excalibur.source.URIAbsolutizer;
39
40 /**
41  * A factory for the context protocol using the context of the servlet api.
42  * It builds the source by asking the environment context for the real URL
43  * (see {@link org.apache.cocoon.environment.Context#getResource(String)})
44  * and then resolving this real URL.
45  *
46  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
47  * @author <a HREF="http://www.apache.org/~sylvain">Sylvain Wallez</a>
48  * @version CVS $Id: ContextSourceFactory.java 30932 2004-07-29 17:35:38Z vgritsenko $
49  */

50 public class ContextSourceFactory
51 extends AbstractLogEnabled
52 implements SourceFactory,
53             Serviceable,
54             Contextualizable,
55             ThreadSafe,
56             URIAbsolutizer {
57
58     /** The context */
59     protected Context envContext;
60
61     /** The ServiceManager */
62     protected ServiceManager manager;
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(org.apache.avalon.framework.context.Context context)
75     throws ContextException {
76         this.envContext = (Context)context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
77     }
78
79     /* (non-Javadoc)
80      * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map)
81      */

82     public Source getSource( String JavaDoc location, Map JavaDoc parameters )
83     throws SourceException, MalformedURLException JavaDoc, IOException JavaDoc {
84         if( this.getLogger().isDebugEnabled() ) {
85             this.getLogger().debug( "Creating source object for " + location );
86         }
87
88         // Lookup resolver
89
SourceResolver resolver = null;
90         try {
91             resolver = (SourceResolver)this.manager.lookup( SourceResolver.ROLE );
92
93             // Remove the protocol and the first '/'
94
final int pos = location.indexOf(":/");
95             final String JavaDoc path = location.substring(pos+1);
96             
97             // fix for #24093, we don't give access to files outside the context:
98
if ( path.indexOf("../") != -1 ) {
99                 throw new MalformedURLException JavaDoc("Invalid path ('../' is not allowed) : " + path);
100             }
101             
102             URL JavaDoc u;
103             
104             // Try to get a file first and fall back to a resource URL
105
String JavaDoc actualPath = envContext.getRealPath(path);
106             if (actualPath != null) {
107                 u = new File JavaDoc(actualPath).toURL();
108             } else {
109                 u = envContext.getResource(path);
110             }
111
112             if (u != null) {
113                 return resolver.resolveURI(u.toExternalForm());
114                 
115             } else {
116                 String JavaDoc message = location + " could not be found. (possible context problem)";
117                 getLogger().info(message);
118                 throw new MalformedURLException JavaDoc(message);
119             }
120         } catch (ServiceException se) {
121             throw new SourceException("Unable to lookup source resolver.", se);
122         } finally {
123             this.manager.release( resolver );
124         }
125                 
126     }
127     
128     /* (non-Javadoc)
129      * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
130      */

131     public void release( Source source ) {
132         // In fact, this method should never be called as this factory
133
// returns a source object from a different factory. So that
134
// factory should release the source
135
if ( null != source ) {
136             if ( this.getLogger().isDebugEnabled() ) {
137                 this.getLogger().debug("Releasing source " + source.getURI());
138             }
139             SourceResolver resolver = null;
140             try {
141                 resolver = (SourceResolver)this.manager.lookup( SourceResolver.ROLE );
142                 resolver.release( source );
143             } catch (ServiceException ingore) {
144             } finally {
145                 this.manager.release( resolver );
146             }
147         }
148     }
149
150     /* (non-Javadoc)
151      * @see org.apache.excalibur.source.URIAbsolutizer#absolutize(java.lang.String, java.lang.String)
152      */

153     public String JavaDoc absolutize(String JavaDoc baseURI, String JavaDoc location) {
154         return SourceUtil.absolutize(baseURI, location, true);
155     }
156 }
157
Popular Tags