1 16 package org.apache.cocoon.portal.impl; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.CascadingRuntimeException; 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.configuration.Configurable; 25 import org.apache.avalon.framework.configuration.Configuration; 26 import org.apache.avalon.framework.configuration.ConfigurationException; 27 import org.apache.avalon.framework.context.Context; 28 import org.apache.avalon.framework.logger.AbstractLogEnabled; 29 import org.apache.avalon.framework.service.ServiceException; 30 import org.apache.avalon.framework.service.ServiceManager; 31 import org.apache.avalon.framework.service.ServiceSelector; 32 import org.apache.avalon.framework.service.Serviceable; 33 import org.apache.avalon.framework.thread.ThreadSafe; 34 import org.apache.cocoon.portal.LinkService; 35 import org.apache.cocoon.portal.PortalComponentManager; 36 import org.apache.cocoon.portal.PortalManager; 37 import org.apache.cocoon.portal.PortalService; 38 import org.apache.cocoon.portal.coplet.CopletFactory; 39 import org.apache.cocoon.portal.event.EventManager; 40 import org.apache.cocoon.portal.layout.LayoutFactory; 41 import org.apache.cocoon.portal.layout.renderer.Renderer; 42 import org.apache.cocoon.portal.profile.ProfileManager; 43 44 55 public class DefaultPortalComponentManager 56 extends AbstractLogEnabled 57 implements PortalComponentManager, Serviceable, Disposable, ThreadSafe, Configurable { 58 59 60 protected ServiceManager manager; 61 62 63 protected final PortalService portalService; 64 65 protected String profileManagerRole; 66 protected ProfileManager profileManager; 67 68 protected String linkServiceRole; 69 protected LinkService linkService; 70 71 protected String rendererSelectorRole; 72 protected ServiceSelector rendererSelector; 73 74 protected Map renderers; 75 76 protected String copletFactoryRole; 77 protected CopletFactory copletFactory; 78 79 protected String layoutFactoryRole; 80 protected LayoutFactory layoutFactory; 81 82 protected String eventManagerRole; 83 protected EventManager eventManager; 84 85 protected String portalManagerRole; 86 protected PortalManager portalManager; 87 88 protected final Context context; 89 90 98 public DefaultPortalComponentManager(final PortalService service, Context context) { 99 this.portalService = service; 100 this.context = context; 101 } 102 103 106 public void service(ServiceManager manager) throws ServiceException { 107 this.manager = manager; 108 } 109 110 113 public LinkService getLinkService() { 114 if ( null == this.linkService ) { 115 try { 116 this.linkService = (LinkService)this.manager.lookup( this.linkServiceRole ); 117 } catch (ServiceException e) { 118 throw new CascadingRuntimeException("Unable to lookup link service with role " + this.linkServiceRole, e); 119 } 120 } 121 return this.linkService; 122 } 123 124 127 public ProfileManager getProfileManager() { 128 if ( null == this.profileManager ) { 129 try { 130 this.profileManager = (ProfileManager)this.manager.lookup( this.profileManagerRole ); 131 } catch (ServiceException e) { 132 throw new CascadingRuntimeException("Unable to lookup profile manager with role " + this.profileManagerRole, e); 133 } 134 } 135 return this.profileManager; 136 } 137 138 141 public EventManager getEventManager() { 142 if ( null == this.eventManager ) { 143 try { 144 this.eventManager = (EventManager)this.manager.lookup( this.eventManagerRole ); 145 } catch (ServiceException e) { 146 throw new CascadingRuntimeException("Unable to lookup event manager with role " + this.eventManagerRole, e); 147 } 148 } 149 return this.eventManager; 150 } 151 152 155 public void dispose() { 156 if (this.manager != null) { 157 if (this.rendererSelector != null) { 158 Iterator i = this.renderers.values().iterator(); 159 while (i.hasNext()) { 160 this.rendererSelector.release(i.next()); 161 } 162 this.manager.release(this.rendererSelector); 163 this.rendererSelector = null; 164 this.renderers = null; 165 } 166 this.manager.release(this.profileManager); 167 this.profileManager = null; 168 this.manager.release(this.linkService); 169 this.linkService = null; 170 this.manager.release(this.copletFactory); 171 this.copletFactory = null; 172 this.manager.release(this.layoutFactory); 173 this.layoutFactory = null; 174 this.manager.release(this.eventManager); 175 this.eventManager = null; 176 this.manager.release(this.portalManager); 177 this.portalManager = null; 178 this.manager = null; 179 } 180 } 181 182 185 public void configure(Configuration config) throws ConfigurationException { 186 this.profileManagerRole = config.getChild("profile-manager").getValue(ProfileManager.ROLE); 187 this.linkServiceRole = config.getChild("link-service").getValue(LinkService.ROLE); 188 this.rendererSelectorRole = config.getChild("renderer-selector").getValue(Renderer.ROLE+"Selector"); 189 this.copletFactoryRole = config.getChild("coplet-factory").getValue(CopletFactory.ROLE); 190 this.layoutFactoryRole = config.getChild("layout-factory").getValue(LayoutFactory.ROLE); 191 this.eventManagerRole = config.getChild("event-manager").getValue(EventManager.ROLE); 192 this.portalManagerRole = config.getChild("portal-manager").getValue(PortalManager.ROLE); 193 } 194 195 198 public Renderer getRenderer(String hint) { 199 if ( rendererSelector == null ) { 200 try { 201 this.rendererSelector = (ServiceSelector)this.manager.lookup( this.rendererSelectorRole ); 202 } catch (ServiceException e) { 203 throw new CascadingRuntimeException("Unable to lookup renderer selector with role " + this.rendererSelectorRole, e); 204 } 205 this.renderers = new HashMap (); 206 } 207 Renderer o = (Renderer) this.renderers.get( hint ); 208 if ( o == null ) { 209 try { 210 o = (Renderer) this.rendererSelector.select( hint ); 211 this.renderers.put( hint, o ); 212 } catch (ServiceException e) { 213 throw new CascadingRuntimeException("Unable to lookup renderer with hint " + hint, e); 214 } 215 } 216 return o; 217 } 218 219 222 public CopletFactory getCopletFactory() { 223 if ( null == this.copletFactory ) { 224 try { 225 this.copletFactory = (CopletFactory)this.manager.lookup( this.copletFactoryRole); 226 } catch (ServiceException e) { 227 throw new CascadingRuntimeException("Unable to lookup coplet factory with role " + this.copletFactoryRole, e); 228 } 229 } 230 return this.copletFactory; 231 } 232 233 236 public LayoutFactory getLayoutFactory() { 237 if ( null == this.layoutFactory ) { 238 try { 239 this.layoutFactory = (LayoutFactory)this.manager.lookup( this.layoutFactoryRole); 240 } catch (ServiceException e) { 241 throw new CascadingRuntimeException("Unable to lookup layout factory with role " + this.layoutFactoryRole, e); 242 } 243 } 244 return this.layoutFactory; 245 } 246 247 248 251 public PortalManager getPortalManager() { 252 if ( null == this.portalManager ) { 253 try { 254 this.portalManager = (PortalManager)this.manager.lookup( this.portalManagerRole); 255 } catch (ServiceException e) { 256 throw new CascadingRuntimeException("Unable to lookup portal manager with role " + this.portalManagerRole, e); 257 } 258 } 259 return this.portalManager; 260 } 261 262 265 public Context getComponentContext() { 266 return this.context; 267 } 268 } 269 | Popular Tags |