1 16 package org.apache.cocoon.portal.coplet.adapter.impl; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 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 ; 46 import org.xml.sax.SAXException ; 47 48 56 public class URICopletAdapter 57 extends AbstractCopletAdapter 58 implements Disposable, Receiver, Initializable, Contextualizable { 59 60 61 protected SourceResolver resolver; 62 63 64 protected Context context; 65 66 69 public void contextualize(Context context) throws ContextException { 70 this.context = context; 71 } 72 73 76 public void service(ServiceManager manager) throws ServiceException { 77 super.service( manager ); 78 this.resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE); 79 } 80 81 84 public void streamContent(CopletInstanceData coplet, ContentHandler contentHandler) 85 throws SAXException { 86 final String uri = (String )coplet.getCopletData().getAttribute("uri"); 87 if ( uri == null ) { 88 throw new SAXException ("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 uri, 95 final ContentHandler contentHandler) 96 throws SAXException { 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 handlePars = (Boolean )this.getConfiguration( coplet, "handleParameters", Boolean.FALSE); 104 105 String sourceUri = uri; 106 107 if ( handlePars.booleanValue() ) { 108 List list = (List ) portalService.getTemporaryAttribute(URICopletAdapter.class.getName()); 109 if ( list != null && list.contains( coplet )) { 110 if ( uri.startsWith("cocoon:raw:") ) { 112 sourceUri = "cocoon:" + uri.substring(11); 113 } 114 } else { 115 if (!uri.startsWith("cocoon:raw:") ) { 117 sourceUri = "cocoon:raw:" + uri.substring(7); 118 } 119 } 120 } 121 122 HashMap par = new HashMap (); 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 ioe) { 132 throw new SAXException ("IOException", ioe); 133 } catch (ProcessingException pe) { 134 throw new SAXException ("ProcessingException", pe); 135 } catch (ServiceException ce) { 136 throw new SAXException ("ServiceException", ce); 137 } finally { 138 this.resolver.release(copletSource); 139 this.manager.release(portalService); 140 } 141 } 142 143 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 ignore) { 153 } 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 167 public void inform(CopletInstanceEvent event, PortalService service) { 168 List list = (List )service.getTemporaryAttribute(URICopletAdapter.class.getName()); 169 if ( list == null ) { 170 list = new ArrayList (); 171 } 172 if ( !list.contains(event.getTarget()) ) { 173 list.add(event.getTarget()); 174 } 175 service.setTemporaryAttribute(URICopletAdapter.class.getName(), list); 176 } 177 178 181 public void initialize() throws Exception { 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 199 protected boolean renderErrorContent(CopletInstanceData coplet, 200 ContentHandler handler, 201 Exception error) 202 throws SAXException { 203 final String uri = (String ) this.getConfiguration(coplet, "error-uri"); 204 if ( uri != null ) { 205 208 if ( uri.startsWith("cocoon:") && error != null) { 209 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 ignore) { 216 } finally { 218 this.manager.release(notifyingBuilder); 219 } 220 221 final Map objectModel = ContextHelper.getObjectModel(this.context); 222 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 |