KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplet > adapter > impl > URICopletAdapter


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.coplet.adapter.impl;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.activity.Disposable;
25 import org.apache.avalon.framework.activity.Initializable;
26 import org.apache.avalon.framework.context.Context;
27 import org.apache.avalon.framework.context.ContextException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.cocoon.ProcessingException;
32 import org.apache.cocoon.components.ContextHelper;
33 import org.apache.cocoon.components.notification.Notifying;
34 import org.apache.cocoon.components.notification.NotifyingBuilder;
35 import org.apache.cocoon.components.source.SourceUtil;
36 import org.apache.cocoon.environment.ObjectModelHelper;
37 import org.apache.cocoon.portal.Constants;
38 import org.apache.cocoon.portal.PortalService;
39 import org.apache.cocoon.portal.coplet.CopletInstanceData;
40 import org.apache.cocoon.portal.event.CopletInstanceEvent;
41 import org.apache.cocoon.portal.event.EventManager;
42 import org.apache.cocoon.portal.event.Receiver;
43 import org.apache.excalibur.source.Source;
44 import org.apache.excalibur.source.SourceResolver;
45 import org.xml.sax.ContentHandler JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47
48 /**
49  * This is the adapter to use pipelines as coplets
50  *
51  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
52  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
53  *
54  * @version CVS $Id: URICopletAdapter.java 232769 2005-08-15 07:51:18Z cziegeler $
55  */

56 public class URICopletAdapter
57     extends AbstractCopletAdapter
58     implements Disposable, Receiver, Initializable, Contextualizable {
59     
60     /** The source resolver */
61     protected SourceResolver resolver;
62     
63     /** The application context */
64     protected Context context;
65     
66     /**
67      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
68      */

69     public void contextualize(Context context) throws ContextException {
70         this.context = context;
71     }
72     
73     /**
74      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
75      */

76     public void service(ServiceManager manager) throws ServiceException {
77         super.service( manager );
78         this.resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE);
79     }
80     
81     /**
82      * @see org.apache.cocoon.portal.coplet.adapter.impl.AbstractCopletAdapter#streamContent(org.apache.cocoon.portal.coplet.CopletInstanceData, org.xml.sax.ContentHandler)
83      */

84     public void streamContent(CopletInstanceData coplet, ContentHandler JavaDoc contentHandler)
85     throws SAXException JavaDoc {
86         final String JavaDoc uri = (String JavaDoc)coplet.getCopletData().getAttribute("uri");
87         if ( uri == null ) {
88             throw new SAXException JavaDoc("No URI for coplet data "+coplet.getCopletData().getId()+" found.");
89         }
90         this.streamContent( coplet, uri, contentHandler);
91     }
92
93     public void streamContent(final CopletInstanceData coplet,
94                               final String JavaDoc uri,
95                               final ContentHandler JavaDoc contentHandler)
96     throws SAXException JavaDoc {
97         Source copletSource = null;
98         PortalService portalService = null;
99         try {
100             if (uri.startsWith("cocoon:")) {
101                 portalService = (PortalService)this.manager.lookup(PortalService.ROLE);
102
103                 Boolean JavaDoc handlePars = (Boolean JavaDoc)this.getConfiguration( coplet, "handleParameters", Boolean.FALSE);
104                 
105                 String JavaDoc sourceUri = uri;
106                 
107                 if ( handlePars.booleanValue() ) {
108                     List JavaDoc list = (List JavaDoc) portalService.getTemporaryAttribute(URICopletAdapter.class.getName());
109                     if ( list != null && list.contains( coplet )) {
110                         // add parameters
111
if ( uri.startsWith("cocoon:raw:") ) {
112                             sourceUri = "cocoon:" + uri.substring(11);
113                         }
114                     } else {
115                         // remove parameters
116
if (!uri.startsWith("cocoon:raw:") ) {
117                             sourceUri = "cocoon:raw:" + uri.substring(7);
118                         }
119                     }
120                 }
121                 
122                 HashMap JavaDoc par = new HashMap JavaDoc();
123                 par.put(Constants.PORTAL_NAME_KEY, portalService.getPortalName());
124                 par.put(Constants.COPLET_ID_KEY, coplet.getId());
125             
126                 copletSource = this.resolver.resolveURI(sourceUri, null, par);
127             } else {
128                 copletSource = this.resolver.resolveURI(uri);
129             }
130             SourceUtil.toSAX(copletSource, contentHandler);
131         } catch (IOException JavaDoc ioe) {
132             throw new SAXException JavaDoc("IOException", ioe);
133         } catch (ProcessingException pe) {
134             throw new SAXException JavaDoc("ProcessingException", pe);
135         } catch (ServiceException ce) {
136             throw new SAXException JavaDoc("ServiceException", ce);
137         } finally {
138             this.resolver.release(copletSource);
139             this.manager.release(portalService);
140         }
141     }
142
143     /**
144      * @see org.apache.avalon.framework.activity.Disposable#dispose()
145      */

146     public void dispose() {
147         if ( this.manager != null ) {
148             EventManager eventManager = null;
149             try {
150                 eventManager = (EventManager)this.manager.lookup(EventManager.ROLE);
151                 eventManager.unsubscribe(this);
152             } catch (Exception JavaDoc ignore) {
153                 // ignore
154
} finally {
155                 this.manager.release( eventManager );
156             }
157             
158             this.manager.release( this.resolver );
159             this.resolver = null;
160             this.manager = null;
161         }
162     }
163
164     /**
165      * @see Receiver
166      */

167     public void inform(CopletInstanceEvent event, PortalService service) {
168         List JavaDoc list = (List JavaDoc)service.getTemporaryAttribute(URICopletAdapter.class.getName());
169         if ( list == null ) {
170             list = new ArrayList JavaDoc();
171         }
172         if ( !list.contains(event.getTarget()) ) {
173             list.add(event.getTarget());
174         }
175         service.setTemporaryAttribute(URICopletAdapter.class.getName(), list);
176     }
177
178     /**
179      * @see org.apache.avalon.framework.activity.Initializable#initialize()
180      */

181     public void initialize() throws Exception JavaDoc {
182         EventManager eventManager = null;
183         try {
184             eventManager = (EventManager)this.manager.lookup(EventManager.ROLE);
185             eventManager.subscribe( this );
186         } finally {
187             this.manager.release( eventManager );
188         }
189     }
190
191     /**
192      * Render the error content for a coplet
193      * @param coplet The coplet instance data
194      * @param handler The content handler
195      * @param error The exception that occured
196      * @return True if the error content has been rendered, otherwise false
197      * @throws SAXException
198      */

199     protected boolean renderErrorContent(CopletInstanceData coplet,
200                                          ContentHandler JavaDoc handler,
201                                          Exception JavaDoc error)
202     throws SAXException JavaDoc {
203         final String JavaDoc uri = (String JavaDoc) this.getConfiguration(coplet, "error-uri");
204         if ( uri != null ) {
205             // TODO - if an error occured for this coplet, remember this
206
// and use directly the error-uri from now on
207

208             if ( uri.startsWith("cocoon:") && error != null) {
209                 // Create a Notifying
210
NotifyingBuilder notifyingBuilder = null;
211                 Notifying currentNotifying = null;
212                 try {
213                     notifyingBuilder= (NotifyingBuilder)this.manager.lookup(NotifyingBuilder.ROLE);
214                     currentNotifying = notifyingBuilder.build(this, error);
215                 } catch (Exception JavaDoc ignore) {
216                     // ignore
217
} finally {
218                     this.manager.release(notifyingBuilder);
219                 }
220
221                 final Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
222                 // Add it to the object model
223
if ( currentNotifying != null ) {
224                     objectModel.put(org.apache.cocoon.Constants.NOTIFYING_OBJECT, currentNotifying);
225                     objectModel.put(ObjectModelHelper.THROWABLE_OBJECT, error);
226                 }
227             
228                 try {
229                     this.streamContent( coplet, uri, handler);
230                 } finally {
231                     objectModel.remove(org.apache.cocoon.Constants.NOTIFYING_OBJECT);
232                     objectModel.remove(ObjectModelHelper.THROWABLE_OBJECT);
233                 }
234             } else {
235             
236                 this.streamContent( coplet, uri, handler);
237             }
238                         
239             return true;
240         }
241         return false;
242     }
243 }
244
Popular Tags