1 16 package org.apache.cocoon.portal.profile.impl; 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.apache.avalon.framework.CascadingRuntimeException; 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.cocoon.portal.PortalService; 31 import org.apache.cocoon.portal.coplet.CopletData; 32 import org.apache.cocoon.portal.coplet.CopletFactory; 33 import org.apache.cocoon.portal.coplet.CopletInstanceData; 34 import org.apache.cocoon.portal.layout.CompositeLayout; 35 import org.apache.cocoon.portal.layout.Item; 36 import org.apache.cocoon.portal.layout.Layout; 37 import org.apache.cocoon.portal.layout.LayoutFactory; 38 import org.apache.cocoon.portal.profile.PortalUser; 39 import org.apache.cocoon.portal.profile.ProfileLS; 40 import org.apache.commons.collections.map.LinkedMap; 41 import org.apache.commons.collections.map.StaticBucketMap; 42 import org.apache.excalibur.source.SourceValidity; 43 44 52 public class StaticProfileManager 53 extends AbstractProfileManager 54 implements Configurable { 55 56 protected String profilesPath; 57 58 protected StaticBucketMap copletInstanceDataManagers = new StaticBucketMap(); 59 protected StaticBucketMap copletDataManagers = new StaticBucketMap(); 60 61 protected static final String LAYOUTKEY_PREFIX = StaticProfileManager.class.getName() + "/Layout/"; 62 63 protected PortalUser portalUser = new StaticPortalUser(); 64 65 68 public Layout getPortalLayout(String layoutKey, String layoutID) { 69 PortalService service = null; 70 ProfileLS adapter = null; 71 try { 72 service = (PortalService) this.manager.lookup(PortalService.ROLE); 73 74 if (layoutKey == null) { 75 layoutKey = service.getDefaultLayoutKey(); 76 } 77 78 String serviceKey = LAYOUTKEY_PREFIX + layoutKey; 79 Object [] objects = (Object []) service.getAttribute(serviceKey); 80 81 int valid = SourceValidity.INVALID; 83 SourceValidity sourceValidity = null; 84 if (objects != null) { 85 sourceValidity = (SourceValidity) objects[1]; 86 valid = sourceValidity.isValid(); 87 Layout layout = null; 88 if (valid == SourceValidity.VALID) 89 layout = (Layout) ((Map ) objects[0]).get(layoutID); 90 if (layout != null) 91 return layout; 92 } 93 94 CopletInstanceDataManager copletInstanceDataManager = getCopletInstanceDataManager(service); 95 96 Map parameters = new HashMap (); 97 parameters.put("profiletype", "layout"); 98 parameters.put("objectmap", copletInstanceDataManager.getCopletInstanceData()); 99 100 Map map = new LinkedMap(); 101 map.put("base", this.profilesPath); 102 map.put("portalname", service.getPortalName()); 103 map.put("profile", "layout"); 104 map.put("groupKey", layoutKey); 105 106 adapter = (ProfileLS) this.manager.lookup(ProfileLS.ROLE); 107 SourceValidity newValidity = adapter.getValidity(map, parameters); 108 if (valid == SourceValidity.UNKNOWN) { 109 if (sourceValidity.isValid(newValidity) == SourceValidity.VALID) { 110 return (Layout) ((Map ) objects[0]).get(layoutID); 111 } 112 } 113 114 Layout layout = (Layout) adapter.loadProfile(map, parameters); 116 Map layouts = new HashMap (); 117 118 layouts.put(null, layout); cacheLayouts(layouts, layout); 120 121 LayoutFactory factory = service.getComponentManager().getLayoutFactory(); 122 factory.prepareLayout(layout); 123 124 if (newValidity != null) { 126 objects = new Object [] { layouts, newValidity }; 127 service.setAttribute(serviceKey, objects); 128 } 129 130 return (Layout) layouts.get(layoutID); 131 } catch (Exception ce) { 132 throw new CascadingRuntimeException("Unable to get layout.", ce); 133 } finally { 134 this.manager.release(service); 135 this.manager.release(adapter); 136 } 137 } 138 139 143 private void cacheLayouts(Map layoutMap, Layout layout) { 144 if (layout != null) { 145 if (layout.getId() != null) { 146 String layoutId = layout.getId(); 147 layoutMap.put(layoutId, layout); 148 } 149 if (layout instanceof CompositeLayout) { 150 CompositeLayout cl = (CompositeLayout) layout; 152 Iterator i = cl.getItems().iterator(); 153 while (i.hasNext()) { 154 Item current = (Item) i.next(); 155 this.cacheLayouts(layoutMap, current.getLayout()); 156 } 157 } 158 } 159 160 } 161 162 private CopletDataManager getCopletDataManager(PortalService service) 163 throws Exception { 164 final String portalName = service.getPortalName(); 165 this.getCopletInstanceDataManager(service); 167 return (CopletDataManager)this.copletDataManagers.get(portalName); 168 } 169 170 private CopletInstanceDataManager getCopletInstanceDataManager(PortalService service) 171 throws Exception { 172 String portalName = service.getPortalName(); 173 CopletInstanceDataManager copletInstanceDataManager = 174 (CopletInstanceDataManager) this.copletInstanceDataManagers.get(portalName); 175 if (copletInstanceDataManager != null) { 176 return copletInstanceDataManager; 177 } 178 179 ProfileLS adapter = null; 180 try { 181 adapter = (ProfileLS) this.manager.lookup(ProfileLS.ROLE); 182 183 Map parameters = new HashMap (); 184 parameters.put("profiletype", "copletbasedata"); 185 parameters.put("objectmap", null); 186 187 Map map = new LinkedMap(); 188 map.put("base", this.profilesPath); 189 map.put("portalname", service.getPortalName()); 190 map.put("profile", "coplet"); 191 map.put("name", "basedata"); 192 CopletBaseDataManager copletBaseDataManager = (CopletBaseDataManager) adapter.loadProfile(map, parameters); 193 194 parameters.clear(); 196 parameters.put("profiletype", "copletdata"); 197 parameters.put("objectmap", copletBaseDataManager.getCopletBaseData()); 198 199 map.clear(); 200 map.put("base", this.profilesPath); 201 map.put("portalname", service.getPortalName()); 202 map.put("profile", "coplet"); 203 map.put("name", "data"); 204 CopletDataManager copletDataManager = (CopletDataManager) adapter.loadProfile(map, parameters); 205 206 parameters.clear(); 208 parameters.put("profiletype", "copletinstancedata"); 209 parameters.put("objectmap", copletDataManager.getCopletData()); 210 211 map.clear(); 212 map.put("base", this.profilesPath); 213 map.put("portalname", service.getPortalName()); 214 map.put("profile", "coplet"); 215 map.put("name", "instancedata"); 216 copletInstanceDataManager = (CopletInstanceDataManager) adapter.loadProfile(map, parameters); 217 218 CopletFactory copletFactory = service.getComponentManager().getCopletFactory(); 219 Iterator iterator = copletDataManager.getCopletData().values().iterator(); 220 while (iterator.hasNext()) { 221 CopletData cd = (CopletData) iterator.next(); 222 copletFactory.prepare(cd); 223 } 224 iterator = copletInstanceDataManager.getCopletInstanceData().values().iterator(); 225 while (iterator.hasNext()){ 226 CopletInstanceData cid = (CopletInstanceData) iterator.next(); 227 copletFactory.prepare(cid); 228 } 229 230 this.copletInstanceDataManagers.put(portalName, copletInstanceDataManager); 232 this.copletDataManagers.put(portalName, copletDataManager); 233 return copletInstanceDataManager; 234 235 } finally { 236 this.manager.release(service); 237 this.manager.release(adapter); 238 } 239 } 240 241 244 public CopletInstanceData getCopletInstanceData(String copletID) { 245 PortalService service = null; 246 try { 247 service = (PortalService) this.manager.lookup(PortalService.ROLE); 248 return getCopletInstanceDataManager(service).getCopletInstanceData(copletID); 249 } catch (Exception e) { 250 throw new CascadingRuntimeException("Error in getCopletInstanceData", e); 251 } finally { 252 this.manager.release(service); 253 } 254 } 255 256 259 public CopletData getCopletData(String copletDataId) { 260 PortalService service = null; 261 try { 262 service = (PortalService) this.manager.lookup(PortalService.ROLE); 263 264 Iterator i = getCopletInstanceDataManager(service).getCopletInstanceData().values().iterator(); 265 boolean found = false; 266 CopletInstanceData current = null; 267 while ( !found && i.hasNext() ) { 268 current = (CopletInstanceData)i.next(); 269 found = current.getCopletData().getId().equals(copletDataId); 270 } 271 if ( found ) { 272 return current.getCopletData(); 273 } 274 return null; 275 } catch (Exception e) { 276 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 277 } finally { 278 this.manager.release(service); 279 } 280 } 281 282 285 public List getCopletInstanceData(CopletData data) { 286 List coplets = new ArrayList (); 287 PortalService service = null; 288 try { 289 service = (PortalService) this.manager.lookup(PortalService.ROLE); 290 Iterator iter = getCopletInstanceDataManager(service).getCopletInstanceData().values().iterator(); 291 while (iter.hasNext()){ 292 CopletInstanceData current = (CopletInstanceData) iter.next(); 293 if (current.getCopletData().equals(data)) { 294 coplets.add(current); 295 } 296 } 297 return coplets; 298 } catch (Exception e) { 299 throw new CascadingRuntimeException("Error in getCopletInstanceData", e); 300 } finally { 301 this.manager.release(service); 302 } 303 } 304 305 308 public void register(CopletInstanceData coplet) { 309 } 311 312 315 public void unregister(CopletInstanceData coplet) { 316 } 318 319 322 public void register(Layout layout) { 323 } 325 326 329 public void unregister(Layout layout) { 330 } 332 333 336 public void configure(Configuration configuration) 337 throws ConfigurationException { 338 Configuration child = configuration.getChild("profiles-path"); 339 this.profilesPath = child.getValue("cocoon:/profiles"); 340 } 341 342 345 public Collection getCopletDatas() { 346 PortalService service = null; 347 try { 348 service = (PortalService) this.manager.lookup(PortalService.ROLE); 349 return this.getCopletDataManager(service).getCopletData().values(); 350 } catch (Exception e) { 351 throw new CascadingRuntimeException("Error in getCopletDatas.", e); 352 } finally { 353 this.manager.release(service); 354 } 355 } 356 357 360 public Collection getCopletInstanceDatas() { 361 PortalService service = null; 362 try { 363 service = (PortalService) this.manager.lookup(PortalService.ROLE); 364 return this.getCopletInstanceDataManager(service).getCopletInstanceData().values(); 365 } catch (Exception e) { 366 throw new CascadingRuntimeException("Error in getCopletInstanceDatas.", e); 367 } finally { 368 this.manager.release(service); 369 } 370 } 371 372 373 376 public void storeProfile(Layout rootLayout, String layoutKey) { 377 throw new RuntimeException ("The static profile manager does not support the storeProfile() method."); 378 } 379 380 383 public PortalUser getUser() { 384 return this.portalUser; 385 } 386 387 protected static final class StaticPortalUser implements PortalUser, Serializable { 388 389 392 public String getGroup() { 393 return null; 394 } 395 396 399 public String getUserName() { 400 return "static"; 401 } 402 403 406 public boolean isUserInRole(String role) { 407 return false; 408 } 409 } 410 } 411 | Popular Tags |