01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.url;
18:
19: import org.apache.avalon.framework.context.ContextException;
20: import org.apache.avalon.framework.context.Contextualizable;
21: import org.apache.avalon.framework.logger.AbstractLogEnabled;
22:
23: import org.apache.cocoon.Constants;
24: import org.apache.cocoon.environment.Context;
25:
26: import java.net.MalformedURLException;
27: import java.net.URL;
28:
29: /**
30: * @deprecated by the new source resolving of avalon excalibur
31: *
32: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
33: * @version CVS $Id: ContextURLFactory.java 433543 2006-08-22 06:22:54Z crossley $
34: */
35: public class ContextURLFactory extends AbstractLogEnabled implements
36: URLFactory, Contextualizable {
37:
38: /**
39: * The context
40: */
41: protected org.apache.avalon.framework.context.Context context;
42:
43: /**
44: * Create a URL from a location. This method supports the
45: * <i>context://</i> pseudo-protocol for loading
46: * resources accessible from the context root path
47: *
48: * @param location The location
49: * @return The URL pointed to by the location
50: * @exception MalformedURLException If the location is malformed
51: */
52: public URL getURL(String location) throws MalformedURLException {
53: Context envContext = null;
54: try {
55: envContext = (Context) this .context
56: .get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
57: } catch (ContextException e) {
58: getLogger().error("ContextException in getURL", e);
59: }
60: if (envContext == null) {
61: getLogger()
62: .warn(
63: "no environment-context in application context (making an absolute URL)");
64: return new URL(location);
65: }
66: URL u = envContext.getResource("/" + location);
67: if (u != null)
68: return u;
69: else {
70: getLogger()
71: .info(
72: location
73: + " could not be found. (possible context problem)");
74: throw new MalformedURLException(location
75: + " could not be found. (possible context problem)");
76: }
77: }
78:
79: public URL getURL(URL base, String location)
80: throws MalformedURLException {
81: return getURL(base.toExternalForm() + location);
82: }
83:
84: /**
85: * Get the context
86: */
87: public void contextualize(
88: org.apache.avalon.framework.context.Context context)
89: throws ContextException {
90: if (this.context == null) {
91: this.context = context;
92: }
93: }
94: }
|