001: /*
002: * Copyright 2004 Hippo Webworks.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cocoon.source.impl;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.activity.Disposable;
025: import org.apache.avalon.framework.configuration.Configurable;
026: import org.apache.avalon.framework.configuration.Configuration;
027: import org.apache.avalon.framework.configuration.ConfigurationException;
028: import org.apache.avalon.framework.context.Context;
029: import org.apache.avalon.framework.context.ContextException;
030: import org.apache.avalon.framework.context.Contextualizable;
031: import org.apache.avalon.framework.service.ServiceException;
032: import org.apache.avalon.framework.service.ServiceManager;
033: import org.apache.avalon.framework.service.Serviceable;
034: import org.apache.avalon.framework.thread.ThreadSafe;
035: import org.apache.cocoon.Constants;
036: import org.apache.excalibur.source.Source;
037: import org.apache.excalibur.source.SourceFactory;
038: import org.apache.excalibur.source.SourceResolver;
039:
040: /**
041: * @author <a href="mailto:aschrijvers@hippo.nl">Ard Schrijvers</a>
042: */
043: public class ExtensionsSourceFactory implements SourceFactory,
044: Serviceable, Disposable, Contextualizable, ThreadSafe,
045: Configurable {
046:
047: private ServiceManager m_manager;
048: private SourceResolver m_resolver;
049: private String m_scheme;
050: private Context m_context;
051: private String m_extensionspath;
052: private boolean m_initialized = false;
053: private URL m_contextRoot;
054:
055: public Source getSource(String uri, Map params) throws IOException,
056: MalformedURLException {
057:
058: initialize();
059:
060: String uriPath;
061: int i = m_extensionspath.endsWith("/") ? 3 : 2;
062:
063: uriPath = uri.substring(m_scheme.length() + i);
064:
065: String totalPath = "";
066: if (m_extensionspath.startsWith("../")) {
067: // relative to site://
068: totalPath = m_resolver.resolveURI("site://").getURI()
069: .concat(m_extensionspath + uriPath);
070: } else {
071: totalPath = m_extensionspath + uriPath;
072: }
073:
074: String url = new URL(m_contextRoot, totalPath).getFile();
075:
076: return m_resolver.resolveURI(url);
077: }
078:
079: public void release(Source source) {
080: }
081:
082: public void service(ServiceManager manager) throws ServiceException {
083: m_manager = manager;
084: }
085:
086: public void contextualize(Context context) throws ContextException {
087: m_context = context;
088: }
089:
090: public void dispose() {
091: if (m_resolver != null) {
092: m_manager.release(m_resolver);
093: }
094: }
095:
096: private void initialize() {
097: synchronized (this ) {
098: if (!m_initialized) {
099: m_initialized = true;
100: doInitialize();
101: }
102: }
103: }
104:
105: protected void doInitialize() {
106:
107: try {
108: m_resolver = (SourceResolver) m_manager
109: .lookup(SourceResolver.ROLE);
110: org.apache.cocoon.environment.Context envContext = (org.apache.cocoon.environment.Context) m_context
111: .get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
112: m_contextRoot = new File(envContext.getRealPath("/"))
113: .toURL();
114: } catch (ServiceException e) {
115: final String msg = "Missing service dependency: "
116: + SourceResolver.ROLE;
117: throw new RuntimeException(msg);
118: } catch (MalformedURLException e) {
119: throw new RuntimeException(
120: "Error creating context root url", e);
121: } catch (ContextException e) {
122: throw new RuntimeException(
123: "Error resolving environment context", e);
124: }
125: }
126:
127: public void configure(Configuration configuration)
128: throws ConfigurationException {
129: m_scheme = configuration.getAttribute("name");
130: m_extensionspath = configuration.getChild("cms-extensionspath")
131: .getValue();
132:
133: }
134:
135: }
|