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: package org.apache.cocoon.portal.transformation;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.parameters.ParameterException;
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.cocoon.environment.ObjectModelHelper;
025: import org.apache.cocoon.portal.Constants;
026: import org.apache.cocoon.portal.PortalService;
027: import org.apache.cocoon.portal.coplet.CopletInstanceData;
028: import org.apache.cocoon.transformation.AbstractSAXTransformer;
029: import org.xml.sax.SAXException;
030:
031: /**
032: * Abstract transformer implementation that provides some useful methods and
033: * functionality. The portal service is stored in the instance variable
034: * {@link #portalService} and can be used.
035: * There are some methods to fetch a coplet instance data. {@link #getCopletInstanceData()}
036: * tries to get the instance associated with the current request and
037: * {@link #getCopletInstanceData(String)} fetches an instance with a given id.
038: *
039: * If you want to get the coplet instance data associated with the current request,
040: * there are three possibilities how the transformer obtains the information required
041: * for getting the coplet instance data - or more precisly its id:<br><br>
042: * 1) If it is used within a coplet pipeline and this pipeline is called using
043: * the "cocoon:" protocol, all required information is passed automatically.<br>
044: * 2) The id can be passed to the transformer as sitemap paremeters in the following way:
045: * <pre><map:transform type="coplet">
046: * <map:parameter name="copletId" type="examplecoplet"/>
047: * </map:transform></pre>
048: * 3) Any component can set the id as a string in the object model of the current request.
049: * This is the name of the key to be used: {@link org.apache.cocoon.portal.Constants#COPLET_ID_KEY}.
050: *
051: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
052: * @version CVS $Id: AbstractCopletTransformer.java 433543 2006-08-22 06:22:54Z crossley $
053: */
054: public abstract class AbstractCopletTransformer extends
055: AbstractSAXTransformer {
056:
057: /**
058: * Parameter name for the coplet id.
059: */
060: public static final String COPLET_ID_PARAM = "copletId";
061:
062: /** The portal service. @since 2.1.8 */
063: protected PortalService portalService;
064:
065: /**
066: * Try to get the coplet instance data belonging to the current request
067: * @return The coplet instance data
068: * @throws SAXException If an errors occurs or the instance data is not available
069: */
070: protected CopletInstanceData getCopletInstanceData()
071: throws SAXException {
072: CopletInstanceData cid = this .getCopletInstanceData(null);
073: if (cid == null) {
074: throw new SAXException(
075: "Could not find coplet instance data for the current pipeline.");
076: }
077: return cid;
078: }
079:
080: /**
081: * Get the portal service.
082: * @deprecated Use directly the instance variable.
083: */
084: protected PortalService getPortalService() throws SAXException {
085: return this .portalService;
086: }
087:
088: /**
089: * Try to get the coplet instance data with the given id
090: * @param copletId The id of the coplet instance or null if this transformer
091: * is used inside a coplet pipeline
092: * @return The coplet instance data or null
093: * @throws SAXException If an error occurs
094: */
095: protected CopletInstanceData getCopletInstanceData(String copletId)
096: throws SAXException {
097: final Map context = (Map) objectModel
098: .get(ObjectModelHelper.PARENT_CONTEXT);
099:
100: if (copletId == null) {
101: // determine coplet id
102: if (context != null) {
103: copletId = (String) context
104: .get(Constants.COPLET_ID_KEY);
105: } else {
106: copletId = (String) objectModel
107: .get(Constants.COPLET_ID_KEY);
108: if (copletId == null) {
109: try {
110: copletId = this .parameters
111: .getParameter(COPLET_ID_PARAM);
112:
113: } catch (ParameterException e) {
114: throw new SAXException(
115: "copletId must be passed as parameter or in the object model within the parent context.");
116: }
117: }
118: }
119: }
120: if (copletId == null) {
121: throw new SAXException(
122: "copletId must be passed as parameter or in the object model within the parent context.");
123: }
124:
125: CopletInstanceData object = this .portalService
126: .getComponentManager().getProfileManager()
127: .getCopletInstanceData(copletId);
128:
129: return object;
130: }
131:
132: /**
133: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
134: */
135: public void service(ServiceManager manager) throws ServiceException {
136: super .service(manager);
137: this .portalService = (PortalService) this .manager
138: .lookup(PortalService.ROLE);
139: }
140:
141: /**
142: * @see org.apache.avalon.framework.activity.Disposable#dispose()
143: */
144: public void dispose() {
145: if (this.portalService != null) {
146: this.manager.release(this.portalService);
147: this.portalService = null;
148: }
149: super.dispose();
150: }
151: }
|