KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > url > ContextURLFactory


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.url;
17
18 import org.apache.avalon.framework.context.ContextException;
19 import org.apache.avalon.framework.context.Contextualizable;
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21
22 import org.apache.cocoon.Constants;
23 import org.apache.cocoon.environment.Context;
24
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27
28 /**
29  * @deprecated by the new source resolving of avalon excalibur
30  *
31  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
32  * @version CVS $Id: ContextURLFactory.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public class ContextURLFactory extends AbstractLogEnabled implements URLFactory, Contextualizable {
35
36     /**
37      * The context
38      */

39     protected org.apache.avalon.framework.context.Context context;
40
41     /**
42      * Create a URL from a location. This method supports the
43      * <i>context://</i> pseudo-protocol for loading
44      * resources accessible from the context root path
45      *
46      * @param location The location
47      * @return The URL pointed to by the location
48      * @exception MalformedURLException If the location is malformed
49      */

50     public URL JavaDoc getURL(String JavaDoc location) throws MalformedURLException JavaDoc {
51         Context envContext = null;
52         try {
53             envContext = (Context)this.context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
54         } catch (ContextException e){
55             getLogger().error("ContextException in getURL",e);
56         }
57         if (envContext == null) {
58             getLogger().warn("no environment-context in application context (making an absolute URL)");
59             return new URL JavaDoc(location);
60         }
61         URL JavaDoc u = envContext.getResource("/" + location);
62         if (u != null)
63             return u;
64         else {
65             getLogger().info(location + " could not be found. (possible context problem)");
66             throw new MalformedURLException JavaDoc(location + " could not be found. (possible context problem)");
67         }
68     }
69
70     public URL JavaDoc getURL(URL JavaDoc base, String JavaDoc location) throws MalformedURLException JavaDoc {
71         return getURL(base.toExternalForm() + location);
72     }
73
74     /**
75      * Get the context
76      */

77     public void contextualize(org.apache.avalon.framework.context.Context context) throws ContextException {
78         if (this.context == null) {
79             this.context = context;
80         }
81     }
82 }
83
Popular Tags