1 16 package org.apache.cocoon.portal.aspect.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.service.ServiceException; 24 import org.apache.avalon.framework.service.ServiceSelector; 25 import org.apache.cocoon.portal.aspect.AspectDataHandler; 26 import org.apache.cocoon.portal.aspect.AspectDataStore; 27 import org.apache.cocoon.portal.aspect.AspectDescription; 28 import org.apache.cocoon.portal.aspect.Aspectalizable; 29 import org.apache.cocoon.portal.aspect.AspectalizableDescription; 30 31 37 public class DefaultAspectDataHandler 38 implements AspectDataHandler { 39 40 protected AspectalizableDescription description; 41 42 protected ServiceSelector storeSelector; 43 44 47 public DefaultAspectDataHandler(AspectalizableDescription desc, 48 ServiceSelector storeSelector) { 49 this.description = desc; 50 this.storeSelector = storeSelector; 51 } 52 53 56 public Object getAspectData(Aspectalizable owner, String aspectName) { 57 AspectDescription aspectDesc = this.description.getAspectDescription( aspectName ); 59 if ( aspectDesc == null ) return null; 60 61 AspectDataStore store = null; 63 Object data = null; 64 try { 65 store = (AspectDataStore)this.storeSelector.select(aspectDesc.getStoreName()); 66 data = store.getAspectData(owner, aspectName); 67 68 if ( data == null && aspectDesc.isAutoCreate() ) { 69 data = AspectUtil.createNewInstance(aspectDesc); 70 store.setAspectData( owner, aspectName, data ); 71 } 72 73 } catch (ServiceException ce) { 74 throw new CascadingRuntimeException("Unable to lookup aspect data store " + aspectDesc.getStoreName(), ce); 75 } finally { 76 this.storeSelector.release( store ); 77 } 78 79 return data; 80 } 81 82 85 public Map getAspectDatas(Aspectalizable owner) { 86 AspectDatasHashMap datas = new AspectDatasHashMap(owner, this); 87 Iterator iter = this.description.getAspectDescriptions().iterator(); 88 while ( iter.hasNext() ) { 89 AspectDescription current = (AspectDescription)iter.next(); 90 Object data = this.getAspectData(owner, current.getName()); 91 if ( data != null ) { 92 datas.put( current.getName(), data ); 93 } 94 } 95 datas.initialize(); 96 return datas; 97 } 98 99 102 public Map getPersistentAspectDatas(Aspectalizable owner) { 103 Map datas = new HashMap (); 104 Iterator iter = this.description.getAspectDescriptions().iterator(); 105 while ( iter.hasNext() ) { 106 AspectDescription current = (AspectDescription)iter.next(); 107 108 AspectDataStore store = null; 110 Object data = null; 111 try { 112 store = (AspectDataStore)this.storeSelector.select(current.getStoreName()); 113 if ( store.isPersistent() ) { 114 data = store.getAspectData(owner, current.getName()); 115 116 if ( data == null && current.isAutoCreate() ) { 117 data = AspectUtil.createNewInstance(current); 118 store.setAspectData( owner, current.getName(), data ); 119 } 120 121 if ( data != null ) { 122 datas.put( current.getName(), data ); 123 } 124 } 125 126 } catch (ServiceException ce) { 127 throw new CascadingRuntimeException("Unable to lookup aspect data store " + current.getStoreName(), ce); 128 } finally { 129 this.storeSelector.release( store ); 130 } 131 132 } 133 return datas; 134 } 135 136 139 public void setAspectData(Aspectalizable owner, 140 String aspectName, 141 Object data) { 142 AspectDescription aspectDesc = this.description.getAspectDescription( aspectName ); 144 if ( aspectDesc == null ) return; 145 146 AspectDataStore store = null; 148 try { 149 store = (AspectDataStore)this.storeSelector.select(aspectDesc.getStoreName()); 150 store.setAspectData(owner, aspectName, AspectUtil.convert(aspectDesc, data)); 151 } catch (ServiceException ce) { 152 throw new CascadingRuntimeException("Unable to lookup aspect data store " + aspectDesc.getStoreName(), ce); 153 } finally { 154 this.storeSelector.release( store ); 155 } 156 } 157 158 161 public boolean isAspectSupported(String aspectName) { 162 return (this.description.getAspectDescription(aspectName) != null); 163 } 164 } 165 166 final class AspectDatasHashMap extends HashMap { 167 168 protected AspectDataHandler handler; 169 protected Aspectalizable owner; 170 protected boolean init = false; 171 172 public AspectDatasHashMap(Aspectalizable owner, AspectDataHandler handler) { 173 this.handler = handler; 174 this.owner = owner; 175 } 176 177 180 public Object put(Object key, Object value) { 181 if ( this.init ) { 182 this.handler.setAspectData(this.owner, key.toString(), value); 183 value = this.handler.getAspectData(this.owner, key.toString()); 184 } 185 return super.put(key, value); 186 } 187 188 public void initialize() { 189 this.init = true; 190 } 191 } 192 | Popular Tags |