KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > generation > AbstractCopletGenerator


1 /*
2  * Copyright 1999-2002,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.portal.generation;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.parameters.ParameterException;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.cocoon.environment.ObjectModelHelper;
24 import org.apache.cocoon.generation.ServiceableGenerator;
25 import org.apache.cocoon.portal.Constants;
26 import org.apache.cocoon.portal.PortalService;
27 import org.apache.cocoon.portal.coplet.CopletInstanceData;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * Abstract generator implementation that provides a method getCopletInstanceData().
32  * There are two possibilities how the generator obtains the information required for
33  * getting the coplet instance data:<br><br>
34  * 1) If it is used within a coplet pipeline and this pipeline is called using the "cocoon:" protocol,
35  * all required information are passed automatically.<br>
36  * 2) Otherwise the portal name and the coplet id must be passed to the generator
37  * as paremeters in the following way:
38  *
39  * <pre>&lt;map:generator type="coplet"&gt;
40  * &lt;map:parameter name="portalName" type="exampleportal"/&gt;
41  * &lt;map:parameter name="copletId" type="examplecoplet"/&gt;
42  * &lt;/map:generator&gt;</pre>
43  *
44  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
45  * @version CVS $Id: AbstractCopletTransformer.java 30941 2004-07-29 19:56:58Z vgritsenko $
46  */

47 public abstract class AbstractCopletGenerator
48 extends ServiceableGenerator {
49
50     /**
51      * Parameter name.
52      */

53     public static final String JavaDoc COPLET_ID_PARAM = "copletId";
54
55     /**
56      * Parameter name.
57      */

58     public static final String JavaDoc PORTAL_NAME_PARAM = "portalName";
59
60     /** The portal service. @since 2.1.8 */
61     protected PortalService portalService;
62     
63     /**
64      * Try to get the coplet instance data belonging to the current request
65      * @return The coplet instance data
66      * @throws SAXException If an errors occurs or the instance data is not available
67      */

68     protected CopletInstanceData getCopletInstanceData()
69     throws SAXException JavaDoc {
70         CopletInstanceData cid = this.getCopletInstanceData(null);
71         if ( cid == null ) {
72             throw new SAXException JavaDoc("Could not find coplet instance data for the current pipeline.");
73         }
74         return cid;
75     }
76     
77     
78     /**
79      * Get the portal service
80      */

81     protected PortalService getPortalService() {
82         return this.portalService;
83     }
84     
85     
86     /**
87      * Try to get the coplet instance data with the given id
88      * @param copletId The id of the coplet instance or null if this transformer
89      * is used inside a coplet pipeline
90      * @return The coplet instance data or null
91      * @throws SAXException If an error occurs
92      */

93     protected CopletInstanceData getCopletInstanceData(String JavaDoc copletId)
94     throws SAXException JavaDoc {
95         final Map JavaDoc context = (Map JavaDoc)objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
96         
97         if ( copletId == null ) {
98             // determine coplet id
99
if (context != null) {
100                 copletId = (String JavaDoc)context.get(Constants.COPLET_ID_KEY);
101             } else {
102                 copletId = (String JavaDoc)objectModel.get(Constants.COPLET_ID_KEY);
103                 if ( copletId == null ) {
104                     try {
105                         copletId = this.parameters.getParameter(COPLET_ID_PARAM);
106                             
107                     } catch (ParameterException e) {
108                         throw new SAXException JavaDoc("copletId must be passed as parameter or in the object model within the parent context.");
109                     }
110                 }
111             }
112         }
113         if (copletId == null) {
114             throw new SAXException JavaDoc("copletId must be passed as parameter or in the object model within the parent context.");
115         }
116
117         CopletInstanceData object = this.getPortalService().getComponentManager().getProfileManager().getCopletInstanceData( copletId );
118             
119         return object;
120     }
121
122     /**
123      * @see org.apache.avalon.framework.activity.Disposable#dispose()
124      */

125     public void dispose() {
126         if ( this.manager != null ) {
127             this.manager.release(this.portalService);
128             this.portalService = null;
129         }
130         super.dispose();
131     }
132
133     /**
134      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
135      */

136     public void service(ServiceManager manager) throws ServiceException {
137         super.service(manager);
138         this.portalService = (PortalService)this.manager.lookup(PortalService.ROLE);
139     }
140 }
141
Popular Tags