1 22 package org.objectweb.petals.component.common.basic; 23 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 27 import javax.jbi.JBIException; 28 import javax.jbi.component.Bootstrap; 29 import javax.jbi.component.Component; 30 import javax.jbi.component.ComponentContext; 31 import javax.jbi.component.ComponentLifeCycle; 32 import javax.jbi.component.InstallationContext; 33 import javax.jbi.component.ServiceUnitManager; 34 import javax.jbi.management.DeploymentException; 35 import javax.jbi.messaging.DeliveryChannel; 36 import javax.jbi.messaging.ExchangeStatus; 37 import javax.jbi.messaging.Fault; 38 import javax.jbi.messaging.MessageExchange; 39 import javax.jbi.messaging.MessagingException; 40 import javax.jbi.messaging.NormalizedMessage; 41 import javax.jbi.servicedesc.ServiceEndpoint; 42 import javax.management.ObjectName ; 43 import javax.xml.transform.Source ; 44 45 import org.objectweb.petals.component.common.listener.IMessageExchangeProcessor; 46 import org.objectweb.petals.component.common.listener.MessageExchangeListener; 47 import org.objectweb.petals.component.common.util.ComponentLogger; 48 import org.objectweb.petals.component.common.util.SourceHelper; 49 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.DocumentFragment ; 52 53 60 public abstract class AbstractBasicComponent implements Component, 61 ComponentLifeCycle, Bootstrap, IMessageExchangeProcessor { 62 63 66 private DeliveryChannel channel; 67 68 71 private ComponentContext context; 72 73 76 protected Logger log; 77 78 81 protected AbstractServiceUnitManager serviceUnitManager; 82 83 86 private MessageExchangeListener listener; 87 88 91 public AbstractBasicComponent() { 92 } 93 94 102 public AbstractBasicComponent(DeliveryChannel channel, Logger log) { 103 this.channel = channel; 104 this.log = log; 105 } 106 107 112 public void cleanUp() throws JBIException { 113 114 } 115 116 125 protected DeliveryChannel getDeliveryChannel() throws MessagingException, 126 JBIException { 127 if (channel == null) { 128 channel = getComponentContext().getDeliveryChannel(); 129 } 130 return channel; 131 } 132 133 140 protected ComponentContext getComponentContext() throws JBIException { 141 if (context == null) { 142 throw new JBIException("Component not initilized."); 143 } 144 return context; 145 } 146 147 152 public ObjectName getExtensionMBeanName() { 153 return null; 154 } 155 156 161 public ComponentLifeCycle getLifeCycle() { 162 return this; 163 } 164 165 170 public Document getServiceDescription(ServiceEndpoint endpoint) { 171 return serviceUnitManager.getServiceDescription(endpoint); 172 } 173 174 179 public ServiceUnitManager getServiceUnitManager() { 180 return serviceUnitManager; 181 } 182 183 188 public void init(ComponentContext ctx) throws JBIException { 189 this.context = ctx; 190 Logger log = context.getLogger("", null); 191 this.log = new ComponentLogger(log, log.getName(), log 192 .getResourceBundleName(), context.getComponentName()); 193 channel = ctx.getDeliveryChannel(); 194 serviceUnitManager = createServiceUnitManager(); 195 log.log(Level.INFO, "init"); 196 } 197 198 203 public void init(InstallationContext installCtx) throws JBIException { 204 205 } 206 207 213 public boolean isExchangeWithConsumerOkay(ServiceEndpoint endpoint, 214 MessageExchange exchange) { 215 return true; 216 } 217 218 224 public boolean isExchangeWithProviderOkay(ServiceEndpoint endpoint, 225 MessageExchange exchange) { 226 return true; 227 } 228 229 234 public void onInstall() throws JBIException { 235 236 } 237 238 243 public void onUninstall() throws JBIException { 244 245 } 246 247 250 abstract public boolean process(MessageExchange exchange) throws Exception ; 251 252 257 public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) { 258 return null; 259 } 260 261 266 public void shutDown() throws JBIException { 267 log.log(Level.INFO, "shutdown"); 268 listener = null; 269 } 270 271 276 public void start() throws JBIException { 277 log.log(Level.INFO, "start"); 278 listener = createMessageExchangeListener(this); 279 listener.listen(); 280 } 281 282 287 public void stop() throws JBIException { 288 log.log(Level.INFO, "stop"); 289 this.listener.terminate(); 290 } 291 292 302 protected boolean ackFaultReception(MessageExchange ex) throws Exception { 303 boolean result = false; 304 305 if (ex.getFault() != null) { 306 ex.setStatus(ExchangeStatus.DONE); 307 308 result = true; 309 } 310 return result; 311 } 312 313 325 protected Fault createFault(Exception e, MessageExchange exchange) 326 throws MessagingException { 327 Fault f = exchange.createFault(); 328 String faultString = SourceHelper.createSoapFault(e, null); 329 Source content = SourceHelper.createSource(faultString); 330 f.setContent(content); 331 return f; 332 } 333 334 341 protected MessageExchangeListener createMessageExchangeListener( 342 AbstractBasicComponent processor) { 343 return new MessageExchangeListener(channel, processor, 344 MessageExchangeListener.IgnoredStatus.DONE_AND_ERROR_IGNORED, 345 0, context.getComponentName()); 346 } 347 348 356 protected NormalizedMessage createNormalizedMessage(MessageExchange exchange) 357 throws MessagingException { 358 return exchange.createMessage(); 359 } 360 361 367 protected AbstractServiceUnitManager createServiceUnitManager() 368 throws DeploymentException { 369 AbstractServiceUnitManager sum = new SimpleServiceUnitManager(context, 370 log); 371 return sum; 372 } 373 374 380 protected void logError(Throwable e) { 381 if (log != null) 382 log.log(Level.SEVERE, e.getMessage(), e); 383 else 384 e.printStackTrace(); 385 } 386 } 387 | Popular Tags |