1 16 package org.apache.cocoon.portal.coplet.adapter.impl; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.logger.AbstractLogEnabled; 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.avalon.framework.service.Serviceable; 25 import org.apache.avalon.framework.thread.ThreadSafe; 26 import org.apache.cocoon.components.thread.RunnableManager; 27 import org.apache.cocoon.environment.CocoonRunnable; 28 import org.apache.cocoon.portal.coplet.CopletData; 29 import org.apache.cocoon.portal.coplet.CopletInstanceData; 30 import org.apache.cocoon.portal.coplet.adapter.CopletAdapter; 31 import org.apache.cocoon.xml.SaxBuffer; 32 import org.apache.cocoon.xml.XMLUtils; 33 import org.xml.sax.ContentHandler ; 34 import org.xml.sax.SAXException ; 35 import EDU.oswego.cs.dl.util.concurrent.CountDown; 36 37 68 public abstract class AbstractCopletAdapter 69 extends AbstractLogEnabled 70 implements CopletAdapter, ThreadSafe, Serviceable { 71 72 73 protected ServiceManager manager; 74 75 78 public void service(ServiceManager manager) throws ServiceException { 79 this.manager = manager; 80 } 81 82 87 protected Object getConfiguration(CopletInstanceData coplet, String key) { 88 CopletData copletData = coplet.getCopletData(); 89 Object data = copletData.getAttribute( key ); 90 if ( data == null) { 91 data = copletData.getCopletBaseData().getCopletConfig().get( key ); 92 } 93 return data; 94 } 95 96 102 protected Object getConfiguration(CopletInstanceData coplet, 103 String key, 104 Object defaultValue) { 105 Object data = this.getConfiguration(coplet, key); 106 if ( data == null ) { 107 data = defaultValue; 108 } 109 return data; 110 } 111 112 115 public abstract void streamContent(CopletInstanceData coplet, 116 ContentHandler contentHandler) 117 throws SAXException ; 118 119 127 public void toSAX(CopletInstanceData coplet, ContentHandler contentHandler) 128 throws SAXException { 129 final long startTime = System.currentTimeMillis(); 130 Boolean bool = (Boolean ) this.getConfiguration( coplet, "buffer" ); 131 Integer timeout = (Integer ) this.getConfiguration( coplet, "timeout"); 132 if ( timeout != null ) { 133 bool = Boolean.TRUE; 135 } 136 137 if ( bool != null && bool.booleanValue() ) { 138 boolean read = false; 139 SaxBuffer buffer = new SaxBuffer(); 140 Exception error = null; 141 try { 142 143 if ( timeout != null ) { 144 final int milli = timeout.intValue() * 1000; 145 LoaderThread loader = new LoaderThread(this, coplet, buffer); 146 final RunnableManager runnableManager = (RunnableManager)this.manager.lookup( RunnableManager.ROLE ); 147 try { 148 runnableManager.execute( new CocoonRunnable(loader) ); 149 } finally { 150 this.manager.release( runnableManager ); 151 } 152 try { 153 read = loader.join( milli ); 154 } catch (InterruptedException ignore) { 155 } 157 error = loader.exception; 158 if ( error != null && this.getLogger().isWarnEnabled() ) { 159 this.getLogger().warn("Unable to get content of coplet: " + coplet.getId(), error); 160 } 161 } else { 162 this.streamContent( coplet, buffer ); 163 read = true; 164 } 165 } catch (Exception exception ) { 166 error = exception; 167 this.getLogger().warn("Unable to get content of coplet: " + coplet.getId(), exception); 168 } 169 170 if ( read ) { 171 buffer.toSAX( contentHandler ); 172 } else { 173 if ( !this.renderErrorContent(coplet, contentHandler, error)) { 174 contentHandler.startDocument(); 176 XMLUtils.startElement( contentHandler, "p"); 177 XMLUtils.data( contentHandler, "The coplet " + coplet.getId() + " is currently not available."); 178 XMLUtils.endElement(contentHandler, "p"); 179 contentHandler.endDocument(); 180 } 181 } 182 } else { 183 this.streamContent( coplet, contentHandler ); 184 } 185 if ( this.getLogger().isInfoEnabled() ) { 186 final long msecs = System.currentTimeMillis() - startTime; 187 this.getLogger().info("Streamed coplet " + coplet.getCopletData().getId() + 188 " (instance " + coplet.getId() + ") in " + msecs + "ms."); 189 } 190 } 191 192 195 public void init(CopletInstanceData coplet) { 196 } 198 199 202 public void destroy(CopletInstanceData coplet) { 203 } 205 206 209 public void login(CopletInstanceData coplet) { 210 Iterator iter = coplet.getCopletData().getAttributes().entrySet().iterator(); 212 while ( iter.hasNext() ) { 213 Map.Entry entry = (Map.Entry )iter.next(); 214 if ( entry.getKey().toString().startsWith("temporary:") ) { 215 coplet.setTemporaryAttribute(entry.getKey().toString().substring(10), 216 entry.getValue()); 217 } 218 } 219 } 220 221 224 public void logout(CopletInstanceData coplet) { 225 } 227 228 236 protected boolean renderErrorContent(CopletInstanceData coplet, 237 ContentHandler handler, 238 Exception error) 239 throws SAXException { 240 return false; 241 } 242 } 243 244 final class LoaderThread implements Runnable { 245 246 private final AbstractCopletAdapter adapter; 247 private final ContentHandler handler; 248 private final CopletInstanceData coplet; 249 private final CountDown finished; 250 Exception exception; 251 252 public LoaderThread(AbstractCopletAdapter adapter, 253 CopletInstanceData coplet, 254 ContentHandler handler) { 255 this.adapter = adapter; 256 this.coplet = coplet; 257 this.handler = handler; 258 this.finished = new CountDown( 1 ); 259 } 260 261 public void run() { 262 try { 263 adapter.streamContent( this.coplet, this.handler ); 264 } catch (Exception local) { 265 this.exception = local; 266 } finally { 267 this.finished.release(); 268 } 269 } 270 271 boolean join( final long milis ) 272 throws InterruptedException { 273 return this.finished.attempt( milis ); 274 } 275 276 } 277 | Popular Tags |