001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.cocoon.components.modules.input;
019:
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.configuration.Configuration;
023: import org.apache.avalon.framework.configuration.ConfigurationException;
024: import org.apache.excalibur.source.SourceResolver;
025: import org.apache.lenya.cms.publication.Publication;
026:
027: /**
028: * This module uses publication templating to resolve the real path for a resource. The current
029: * publication ID can be provided as a parameter: <code>{fallback:{pub-id}:foo/bar}</code>. This
030: * is especially useful for cocoon:// request which are triggered from non-environment components
031: * (e.g. the scheduler).
032: *
033: * @version $Id: PublicationTemplateFallbackModule.java 473861 2006-11-12 03:51:14Z gregor $
034: */
035: public class PublicationTemplateFallbackModule extends
036: AbstractPageEnvelopeModule {
037:
038: /**
039: * Ctor.
040: */
041: public PublicationTemplateFallbackModule() {
042: super ();
043: }
044:
045: /**
046: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
047: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
048: */
049: public Object getAttribute(final String name,
050: Configuration modeConf, Map objectModel)
051: throws ConfigurationException {
052:
053: if (getLogger().isDebugEnabled()) {
054: getLogger().debug(
055: "Resolving publication template for file [" + name
056: + "]");
057: }
058:
059: String resolvedUri = null;
060:
061: try {
062: String targetUri = null;
063:
064: // check if publication ID is provided in attribute name
065: if (name.indexOf(":") > -1) {
066: String[] parts = name.split(":");
067: if (parts.length > 2) {
068: throw new RuntimeException(
069: "The attribute may not contain more than one colons!");
070: }
071: String publicationId = parts[0];
072: targetUri = parts[1];
073:
074: if (getLogger().isDebugEnabled()) {
075: getLogger().debug(
076: "Publication ID provided explicitely: ["
077: + publicationId + "]");
078: }
079:
080: } else {
081: targetUri = name;
082: }
083:
084: SourceResolver resolver = null;
085: try {
086: resolver = (SourceResolver) this .manager
087: .lookup(SourceResolver.ROLE);
088: resolvedUri = resolver.resolveURI(
089: "fallback://" + targetUri).getURI();
090: } finally {
091: if (resolver != null) {
092: this .manager.release(resolver);
093: }
094: }
095:
096: } catch (final Exception e) {
097: String message = "Resolving path [" + name + "] failed: ";
098: getLogger().error(message, e);
099: throw new ConfigurationException(message, e);
100: }
101: return resolvedUri;
102: }
103:
104: /**
105: * Returns the base URI for a certain publication.
106: * @param publication The publication.
107: * @return A string.
108: */
109: public static String getBaseURI(Publication publication) {
110: String publicationUri = "context://"
111: + Publication.PUBLICATION_PREFIX_URI + "/"
112: + publication.getId();
113: return publicationUri;
114: }
115:
116: /**
117: * Returns the base URI for a certain publication including the prefix "lenya".
118: * @param publication The publication.
119: * @return A string.
120: */
121: protected String getLenyaBaseURI(Publication publication) {
122: String publicationUri = "context://"
123: + Publication.PUBLICATION_PREFIX_URI + "/"
124: + publication.getId() + "/lenya";
125: return publicationUri;
126: }
127:
128: }
|