1 16 package org.apache.cocoon.portal.wsrp.consumer; 17 18 import java.util.Hashtable ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.logger.LogEnabled; 26 import org.apache.avalon.framework.logger.Logger; 27 import org.apache.cocoon.portal.wsrp.adapter.WSRPAdapter; 28 import org.apache.wsrp4j.consumer.ConsumerEnvironment; 29 import org.apache.wsrp4j.consumer.Producer; 30 import org.apache.wsrp4j.consumer.driver.GenericProducerRegistryImpl; 31 import org.apache.wsrp4j.consumer.driver.ProducerImpl; 32 import org.apache.wsrp4j.exception.WSRPException; 33 34 44 public class ProducerRegistryImpl 45 extends GenericProducerRegistryImpl 46 implements LogEnabled, Configurable, RequiresConsumerEnvironment, RequiresWSRPAdapter { 47 48 49 protected Logger logger; 50 51 52 protected ConsumerEnvironment environment; 53 54 55 protected Map descriptions = new Hashtable (); 56 57 58 protected boolean initialized = false; 59 60 61 protected WSRPAdapter adapter; 62 63 66 public void setConsumerEnvironment(ConsumerEnvironment env) { 67 this.environment = env; 68 } 69 70 73 public void setWSRPAdapter(WSRPAdapter adapter) { 74 this.adapter = adapter; 75 } 76 77 80 public void configure(Configuration c) throws ConfigurationException { 81 if ( c != null ) { 82 Configuration config = c.getChild("producers", true); 83 Configuration[] children = config.getChildren("producer"); 85 for(int i=0; i<children.length; i++) { 86 final Configuration current = children[i]; 87 final ProducerDescription desc = ProducerDescription.fromConfiguration(current, this.environment); 88 this.descriptions.put(desc.getId(), desc); 89 } 90 } 91 } 92 93 96 public void enableLogging(Logger newLogger) { 97 this.logger = newLogger; 98 } 99 100 104 protected void checkInitialized() { 105 if ( !this.initialized ) { 106 synchronized (this) { 107 if (! this.initialized ) { 108 this.initialized = true; 109 try { 110 this.configure(this.adapter.getWsrpConfiguration()); 111 } catch (ConfigurationException ce) { 112 this.logger.error("Unable to read wsrp configuration.", ce); 113 } 114 } 115 } 116 } 117 } 118 119 125 public boolean addProducer(ProducerDescription desc) { 126 this.checkInitialized(); 127 try { 128 final Producer producer = new ProducerImpl(desc.getId(), 129 desc.getMarkupInterfaceUrl(), 130 desc.getServiceDescriptionInterfaceUrl(), 131 desc.getRegistrationInterfaceUrl(), 132 desc.getPortletManagementInterfaceUrl(), 133 desc.getRegistrationData()); 134 producer.setName(desc.getName()); 135 producer.setDescription(desc.getDescription()); 136 this.addProducer(producer); 137 return true; 138 } catch (WSRPException we) { 139 this.logger.error("Unable to add wsrp producer: " + desc.getId() 140 + " - Continuing without configured producer.", we); 141 return false; 142 } 143 } 144 145 148 public void addProducer(Producer producer) { 149 this.checkInitialized(); 150 this.descriptions.remove(producer.getID()); 152 super.addProducer(producer); 153 } 154 155 158 public boolean existsProducer(String id) { 159 this.checkInitialized(); 160 if ( this.descriptions.containsKey(id) ) { 161 return true; 162 } 163 return super.existsProducer(id); 164 } 165 166 169 public Iterator getAllProducers() { 170 this.checkInitialized(); 171 if ( this.descriptions.size() > 0 ) { 173 final Iterator i = this.descriptions.values().iterator(); 174 while ( i.hasNext() ) { 175 final ProducerDescription desc = (ProducerDescription)i.next(); 176 this.addProducer(desc); 177 } 178 this.descriptions.clear(); 179 } 180 return super.getAllProducers(); 181 } 182 183 186 public Producer getProducer(String id) { 187 this.checkInitialized(); 188 ProducerDescription desc = (ProducerDescription)this.descriptions.remove(id); 190 if ( desc != null ) { 191 this.addProducer(desc); 192 } 193 return super.getProducer(id); 194 } 195 196 199 public void removeAllProducers() { 200 this.checkInitialized(); 201 this.descriptions.clear(); 202 super.removeAllProducers(); 203 } 204 205 208 public Producer removeProducer(String id) { 209 this.checkInitialized(); 210 if ( this.descriptions.containsKey(id) ) { 214 this.getProducer(id); 215 } 216 return super.removeProducer(id); 217 } 218 } 219 | Popular Tags |