1 16 package org.apache.cocoon.portal.impl; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.servlet.ServletConfig ; 25 26 import org.apache.avalon.framework.activity.Disposable; 27 import org.apache.avalon.framework.configuration.Configurable; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.container.ContainerUtil; 31 import org.apache.avalon.framework.context.Context; 32 import org.apache.avalon.framework.context.ContextException; 33 import org.apache.avalon.framework.context.Contextualizable; 34 import org.apache.avalon.framework.logger.AbstractLogEnabled; 35 import org.apache.avalon.framework.service.ServiceException; 36 import org.apache.avalon.framework.service.ServiceManager; 37 import org.apache.avalon.framework.service.Serviceable; 38 import org.apache.avalon.framework.thread.ThreadSafe; 39 import org.apache.cocoon.components.ContextHelper; 40 import org.apache.cocoon.environment.Request; 41 import org.apache.cocoon.portal.PortalComponentManager; 42 import org.apache.cocoon.portal.PortalService; 43 import org.apache.cocoon.portal.layout.Layout; 44 import org.apache.cocoon.portal.layout.SkinDescription; 45 import org.apache.cocoon.servlet.CocoonServlet; 46 47 56 public class PortalServiceImpl 57 extends AbstractLogEnabled 58 implements Serviceable, 59 ThreadSafe, 60 PortalService, 61 Contextualizable, 62 Disposable, 63 Configurable { 64 65 protected Context context; 66 67 protected ServiceManager manager; 68 69 protected Map portalComponentManagers = new HashMap (); 70 71 protected Map portalConfigurations = new HashMap (); 72 73 protected Map skins = new HashMap (); 74 75 final protected static String KEY = PortalServiceImpl.class.getName(); 76 77 80 public void service(ServiceManager serviceManager) throws ServiceException { 81 this.manager = serviceManager; 82 } 83 84 protected PortalServiceInfo getInfo() { 85 final Request request = ContextHelper.getRequest( this.context ); 86 PortalServiceInfo info = (PortalServiceInfo) request.getAttribute(KEY); 87 if ( info == null ) { 88 info = new PortalServiceInfo(); 89 info.setup(ContextHelper.getObjectModel(this.context), this.portalComponentManagers); 90 request.setAttribute(KEY, info); 91 } 92 return info; 93 } 94 95 98 public String getPortalName() { 99 return this.getInfo().getPortalName(); 100 } 101 102 105 public void setPortalName(String value) { 106 this.getInfo().setPortalName(value); 107 } 108 109 112 public Object getAttribute(String key) { 113 return this.getInfo().getAttribute(key); 114 } 115 116 119 public void setAttribute(String key, Object value) { 120 this.getInfo().setAttribute(key, value); 121 } 122 123 126 public void removeAttribute(String key) { 127 this.getInfo().removeAttribute(key); 128 } 129 130 133 public Iterator getAttributeNames() { 134 return this.getInfo().getAttributeNames(); 135 } 136 137 140 public Object getTemporaryAttribute(String key) { 141 return this.getInfo().getTemporaryAttribute(key); 142 } 143 144 147 public void setTemporaryAttribute(String key, Object value) { 148 this.getInfo().setTemporaryAttribute(key, value); 149 } 150 151 154 public void removeTemporaryAttribute(String key) { 155 this.getInfo().removeTemporaryAttribute(key); 156 } 157 158 161 public Iterator getTemporaryAttributeNames() { 162 return this.getInfo().getTemporaryAttributeNames(); 163 } 164 165 168 public PortalComponentManager getComponentManager() { 169 return this.getInfo().getComponentManager(); 170 } 171 172 175 public void contextualize(Context context) throws ContextException { 176 this.context = context; 177 try { 179 final ServletConfig servletConfig = (ServletConfig ) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG); 180 servletConfig.getServletContext().setAttribute(PortalService.ROLE, this); 181 } catch (ContextException ignore) { 182 this.getLogger().warn("The portal service is not stored in the servlet config.", ignore); 186 } 187 } 188 189 192 public void dispose() { 193 final Iterator i = this.portalComponentManagers.values().iterator(); 194 while ( i.hasNext() ) { 195 ContainerUtil.dispose( i.next() ); 196 } 197 this.portalComponentManagers.clear(); 198 this.portalConfigurations.clear(); 199 try { 201 final ServletConfig servletConfig = (ServletConfig ) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG); 202 servletConfig.getServletContext().removeAttribute(PortalService.ROLE); 203 } catch (ContextException ignore) { 204 } 206 } 207 208 211 public void configure(Configuration config) throws ConfigurationException { 212 final Configuration[] portals = config.getChild("portals").getChildren("portal"); 213 for(int i=0; i < portals.length; i++ ) { 214 final Configuration current = portals[i]; 215 final String name = current.getAttribute("name"); 216 try { 217 PortalComponentManager c = new DefaultPortalComponentManager(this, this.context); 218 this.portalComponentManagers.put( name, c ); 219 ContainerUtil.enableLogging( c, this.getLogger() ); 220 ContainerUtil.contextualize( c, this.context ); 221 ContainerUtil.service( c, this.manager ); 222 ContainerUtil.configure( c, current ); 223 ContainerUtil.initialize( c ); 224 225 this.portalConfigurations.put( name, current ); 226 227 final List skinList = new ArrayList (); 229 this.skins.put(name, skinList); 230 final Configuration[] skinConfs = current.getChild("skins").getChildren("skin"); 231 if ( skinConfs != null ) { 232 for(int s=0;s<skinConfs.length;s++) { 233 final Configuration currentSkin = skinConfs[s]; 234 final String skinName = currentSkin.getAttribute("name"); 235 final SkinDescription desc = new SkinDescription(); 236 desc.setName(skinName); 237 desc.setBasePath(currentSkin.getAttribute("base-path")); 238 desc.setThumbnailPath(currentSkin.getChild("thumbnail-path").getValue(null)); 239 skinList.add(desc); 240 } 241 } 242 } catch (Exception e) { 243 throw new ConfigurationException("Unable to setup new portal component manager for portal " + name, e); 244 } 245 246 } 247 } 248 249 252 public void setEntryLayout(String layoutKey, Layout object) { 253 if ( layoutKey == null ) { 254 layoutKey = this.getDefaultLayoutKey(); 255 } 256 if ( object == null ) { 257 this.removeTemporaryAttribute("DEFAULT_LAYOUT:" + layoutKey); 258 } else { 259 this.setTemporaryAttribute("DEFAULT_LAYOUT:" + layoutKey, object); 260 } 261 } 262 263 266 public Layout getEntryLayout(String layoutKey) { 267 if ( layoutKey == null ) { 268 layoutKey = this.getDefaultLayoutKey(); 269 } 270 return (Layout)this.getTemporaryAttribute("DEFAULT_LAYOUT:" + layoutKey); 271 } 272 273 276 public void setDefaultLayoutKey(String layoutKey) { 277 if ( layoutKey == null ) { 278 this.removeAttribute("default-layout-key"); 279 } else { 280 this.setAttribute("default-layout-key", layoutKey); 281 } 282 } 283 284 public void setRenderable(Boolean renderable) { 285 String key = getDefaultLayoutKey(); 286 if (renderable == null) { 287 removeTemporaryAttribute("RENDER:" + key); 288 } else { 289 setTemporaryAttribute("RENDER:" + key, renderable); 290 } 291 } 292 293 public Boolean isRenderable() { 294 String key = getDefaultLayoutKey(); 295 Boolean bool = (Boolean )getTemporaryAttribute("RENDER:" + key); 296 if (bool != null) { 297 return bool; 298 } 299 return Boolean.TRUE; 300 } 301 302 305 public String getDefaultLayoutKey() { 306 String key = (String )this.getAttribute("default-layout-key"); 307 if ( key == null ) { 308 Configuration config = (Configuration)this.portalConfigurations.get(this.getPortalName()); 309 key = config.getAttribute("default-layout-key", "portal"); 310 if ( key != null ) { 311 this.setDefaultLayoutKey(key); 312 } 313 } 314 return key; 315 } 316 317 320 public List getSkinDescriptions() { 321 return (List )this.skins.get(this.getPortalName()); 322 } 323 324 327 public Map getObjectModel() { 328 return ContextHelper.getObjectModel(this.context); 329 } 330 } 331 | Popular Tags |