1 16 package org.apache.cocoon.portal.coplet.impl; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.component.Component; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 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.ProcessingException; 35 import org.apache.cocoon.portal.PortalService; 36 import org.apache.cocoon.portal.aspect.AspectDataHandler; 37 import org.apache.cocoon.portal.aspect.AspectDataStore; 38 import org.apache.cocoon.portal.aspect.AspectDescription; 39 import org.apache.cocoon.portal.aspect.impl.DefaultAspectDataHandler; 40 import org.apache.cocoon.portal.aspect.impl.DefaultAspectDescription; 41 import org.apache.cocoon.portal.coplet.CopletData; 42 import org.apache.cocoon.portal.coplet.CopletFactory; 43 import org.apache.cocoon.portal.coplet.CopletInstanceData; 44 import org.apache.cocoon.portal.coplet.adapter.CopletAdapter; 45 46 53 public class DefaultCopletFactory 54 extends AbstractLogEnabled 55 implements Component, ThreadSafe, CopletFactory, Serviceable, Disposable, Configurable { 56 57 protected ServiceManager manager; 58 59 protected Map coplets = new HashMap (); 60 61 protected List descriptions = new ArrayList (); 62 63 protected ServiceSelector storeSelector; 64 65 protected static long idCounter = System.currentTimeMillis(); 66 67 70 public void prepare(CopletData copletData) 71 throws ProcessingException { 72 if ( copletData != null ) { 73 74 final String copletName = copletData.getName(); 75 if ( copletName == null ) { 76 throw new ProcessingException("CopletData "+copletData.getId()+" has no associated name."); 77 } 78 Object [] o = (Object []) this.coplets.get( copletName ); 79 80 if ( o == null ) { 81 throw new ProcessingException("CopletDescription with name " + copletName + " not found."); 82 } 83 DefaultCopletDescription copletDescription = (DefaultCopletDescription)o[0]; 84 85 copletData.setDescription( copletDescription ); 86 copletData.setAspectDataHandler((AspectDataHandler)o[1]); 87 88 } 89 } 90 91 94 public void prepare(CopletInstanceData copletInstanceData) 95 throws ProcessingException { 96 if ( copletInstanceData != null ) { 97 98 final String copletName = copletInstanceData.getName(); 99 if ( copletName == null ) { 100 throw new ProcessingException("CopletInstanceData "+copletInstanceData.getId()+" has no associated name."); 101 } 102 Object [] o = (Object []) this.coplets.get( copletName ); 103 104 if ( o == null ) { 105 throw new ProcessingException("CopletDescription with name " + copletName + " not found."); 106 } 107 DefaultCopletDescription copletDescription = (DefaultCopletDescription)o[0]; 108 109 copletInstanceData.setDescription( copletDescription ); 110 copletInstanceData.setAspectDataHandler((AspectDataHandler)o[2]); 111 112 } 113 } 114 115 116 119 public CopletInstanceData newInstance(CopletData copletData) 120 throws ProcessingException { 121 String name = copletData.getName(); 122 Object [] o = (Object []) this.coplets.get( name ); 123 124 if ( o == null ) { 125 throw new ProcessingException("CopletDescription with name " + name + " not found."); 126 } 127 DefaultCopletDescription copletDescription = (DefaultCopletDescription)o[0]; 128 129 CopletInstanceData instance = new CopletInstanceData(); 130 131 String id = null; 132 if ( copletDescription.createId() ) { 133 synchronized (this) { 134 id = copletData.getId() + '-' + idCounter; 135 idCounter += 1; 136 } 137 } 138 instance.initialize( name, id ); 139 140 instance.setDescription( copletDescription ); 141 instance.setAspectDataHandler((AspectDataHandler)o[2]); 142 instance.setCopletData(copletData); 143 144 final String adapterName = copletData.getCopletBaseData().getCopletAdapterName(); 146 CopletAdapter adapter = null; 147 ServiceSelector adapterSelector = null; 148 try { 149 adapterSelector = (ServiceSelector) this.manager.lookup( CopletAdapter.ROLE + "Selector"); 150 adapter = (CopletAdapter)adapterSelector.select( adapterName ); 151 adapter.init( instance ); 152 adapter.login( instance ); 153 } catch (ServiceException ce) { 154 throw new ProcessingException("Unable to lookup coplet adapter selector or adaptor.", ce); 155 } finally { 156 if ( adapterSelector != null) { 157 adapterSelector.release( adapter ); 158 } 159 this.manager.release( adapterSelector ); 160 } 161 162 PortalService service = null; 163 try { 164 service = (PortalService)this.manager.lookup(PortalService.ROLE); 165 service.getComponentManager().getProfileManager().register(instance); 166 } catch (ServiceException ce) { 167 throw new ProcessingException("Unable to lookup profile manager.", ce); 168 } finally { 169 this.manager.release( service ); 170 } 171 return instance; 172 } 173 174 177 public void service(ServiceManager manager) throws ServiceException { 178 this.manager = manager; 179 this.storeSelector = (ServiceSelector)this.manager.lookup( AspectDataStore.ROLE+"Selector" ); 180 } 181 182 185 public void dispose() { 186 if ( this.manager != null ) { 187 this.manager.release( this.storeSelector ); 188 this.storeSelector = null; 189 this.manager = null; 190 } 191 } 192 193 196 public void configure(Configuration configuration) 197 throws ConfigurationException { 198 final Configuration[] copletsConf = configuration.getChild("coplets").getChildren("coplet"); 199 if ( copletsConf != null ) { 200 for(int i=0; i < copletsConf.length; i++ ) { 201 DefaultCopletDescription desc = new DefaultCopletDescription(); 202 DefaultCopletDescription instanceDesc = new DefaultCopletDescription(); 203 final String name = copletsConf[i].getAttribute("name"); 204 205 if ( this.coplets.get(name) != null) { 207 throw new ConfigurationException("Coplet name must be unique. Double definition for " + name); 208 } 209 desc.setName(copletsConf[i].getAttribute("name")); 210 instanceDesc.setName(copletsConf[i].getAttribute("name")); 211 instanceDesc.setCreateId(copletsConf[i].getAttributeAsBoolean("create-id", true)); 212 213 Configuration[] aspectsConf = copletsConf[i].getChild("coplet-data-aspects").getChildren("aspect"); 215 if (aspectsConf != null) { 216 for(int m=0; m < aspectsConf.length; m++) { 217 AspectDescription adesc = DefaultAspectDescription.newInstance(aspectsConf[m]); 218 desc.addAspectDescription( adesc ); 219 } 220 } 221 222 aspectsConf = copletsConf[i].getChild("coplet-instance-data-aspects").getChildren("aspect"); 224 if (aspectsConf != null) { 225 for(int m=0; m < aspectsConf.length; m++) { 226 AspectDescription adesc = DefaultAspectDescription.newInstance(aspectsConf[m]); 227 instanceDesc.addAspectDescription( adesc ); 228 } 229 } 230 231 DefaultAspectDataHandler handler = new DefaultAspectDataHandler(desc, this.storeSelector); 232 DefaultAspectDataHandler instanceHandler = new DefaultAspectDataHandler(instanceDesc, this.storeSelector); 233 this.coplets.put(desc.getName(), new Object [] {desc, handler, instanceHandler}); 234 this.descriptions.add(desc); 235 } 236 } 237 } 238 239 242 public void remove(CopletInstanceData copletInstanceData) 243 throws ProcessingException { 244 if ( copletInstanceData != null ) { 245 final String adapterName = copletInstanceData.getCopletData().getCopletBaseData().getCopletAdapterName(); 247 CopletAdapter adapter = null; 248 ServiceSelector adapterSelector = null; 249 try { 250 adapterSelector = (ServiceSelector) this.manager.lookup( CopletAdapter.ROLE + "Selector"); 251 adapter = (CopletAdapter)adapterSelector.select( adapterName ); 252 adapter.logout( copletInstanceData ); 253 adapter.destroy( copletInstanceData ); 254 } catch (ServiceException ce) { 255 throw new ProcessingException("Unable to lookup coplet adapter selector or adaptor.", ce); 256 } finally { 257 if ( adapterSelector != null) { 258 adapterSelector.release( adapter ); 259 } 260 this.manager.release( adapterSelector ); 261 } 262 263 PortalService service = null; 264 try { 265 service = (PortalService)this.manager.lookup(PortalService.ROLE); 266 service.getComponentManager().getProfileManager().unregister(copletInstanceData); 267 } catch (ServiceException ce) { 268 throw new ProcessingException("Unable to lookup portal service.", ce); 269 } finally { 270 this.manager.release( service ); 271 } 272 } 273 } 274 275 } 276 | Popular Tags |