1 10 11 package org.mule.providers; 12 13 import org.mule.MuleManager; 14 import org.mule.config.i18n.Message; 15 import org.mule.config.i18n.Messages; 16 import org.mule.providers.service.ConnectorFactory; 17 import org.mule.providers.service.ConnectorServiceDescriptor; 18 import org.mule.providers.service.ConnectorServiceException; 19 import org.mule.umo.MessagingException; 20 import org.mule.umo.UMOComponent; 21 import org.mule.umo.endpoint.UMOEndpoint; 22 import org.mule.umo.endpoint.UMOEndpointURI; 23 import org.mule.umo.lifecycle.InitialisationException; 24 import org.mule.umo.provider.UMOMessageAdapter; 25 import org.mule.umo.provider.UMOMessageReceiver; 26 import org.mule.umo.provider.UMOStreamMessageAdapter; 27 import org.mule.util.BeanUtils; 28 import org.mule.util.ObjectNameHelper; 29 import org.mule.util.PropertiesUtils; 30 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 37 47 48 public abstract class AbstractServiceEnabledConnector extends AbstractConnector 49 { 50 53 protected ConnectorServiceDescriptor serviceDescriptor; 54 55 protected Properties serviceOverrides; 56 57 public void doInitialise() throws InitialisationException 58 { 59 initFromServiceDescriptor(); 60 } 61 62 public void initialiseFromUrl(UMOEndpointURI endpointUri) throws InitialisationException 63 { 64 if (!supportsProtocol(endpointUri.getFullScheme())) 65 { 66 throw new InitialisationException(new Message(Messages.SCHEME_X_NOT_COMPATIBLE_WITH_CONNECTOR_X, 67 endpointUri.getFullScheme(), getClass().getName()), this); 68 } 69 Properties props = new Properties (); 70 props.putAll(endpointUri.getParams()); 71 if (endpointUri.getUserInfo() != null) 73 { 74 props.setProperty("username", endpointUri.getUsername()); 75 String passwd = endpointUri.getPassword(); 76 if (passwd != null) 77 { 78 props.setProperty("password", passwd); 79 } 80 } 81 String host = endpointUri.getHost(); 82 if (host != null) 83 { 84 props.setProperty("hostname", host); 85 props.setProperty("host", host); 86 } 87 if (endpointUri.getPort() > -1) 88 { 89 props.setProperty("port", String.valueOf(endpointUri.getPort())); 90 } 91 92 BeanUtils.populateWithoutFail(this, props, true); 93 94 setName(ObjectNameHelper.getConnectorName(this)); 95 } 96 97 protected synchronized void initFromServiceDescriptor() throws InitialisationException 98 { 99 try 100 { 101 serviceDescriptor = ConnectorFactory.getServiceDescriptor(getProtocol().toLowerCase(), 102 serviceOverrides); 103 104 if (serviceDescriptor.getDispatcherFactory() != null) 105 { 106 logger.info("Loading DispatcherFactory: " + serviceDescriptor.getDispatcherFactory()); 107 dispatcherFactory = serviceDescriptor.createDispatcherFactory(); 108 } 109 110 defaultInboundTransformer = serviceDescriptor.createInboundTransformer(); 111 defaultOutboundTransformer = serviceDescriptor.createOutboundTransformer(); 112 defaultResponseTransformer = serviceDescriptor.createResponseTransformer(); 113 114 sessionHandler = serviceDescriptor.createSessionHandler(); 115 Map props = new HashMap (); 122 PropertiesUtils.getPropertiesWithPrefix(MuleManager.getInstance().getProperties(), 123 getProtocol().toLowerCase(), props); 124 if (props.size() > 0) 125 { 126 props = PropertiesUtils.removeNamespaces(props); 127 org.mule.util.BeanUtils.populateWithoutFail(this, props, true); 128 } 129 } 130 catch (Exception e) 131 { 132 throw new InitialisationException(e, this); 133 } 134 } 135 136 protected ConnectorServiceDescriptor getServiceDescriptor() 137 { 138 if (serviceDescriptor == null) 139 { 140 throw new IllegalStateException ("This connector has not yet been initialised: " + name); 141 } 142 return serviceDescriptor; 143 } 144 145 public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception 146 { 147 return getServiceDescriptor().createMessageReceiver(this, component, endpoint); 148 } 149 150 161 public UMOMessageAdapter getMessageAdapter(Object message) throws MessagingException 162 { 163 try 164 { 165 return serviceDescriptor.createMessageAdapter(message); 166 } 167 catch (ConnectorServiceException e) 168 { 169 throw new MessagingException(new Message(Messages.FAILED_TO_CREATE_X, "Message Adapter"), 170 message, e); 171 } 172 } 173 174 public UMOStreamMessageAdapter getStreamMessageAdapter(InputStream in, OutputStream out) 175 throws MessagingException 176 { 177 try 178 { 179 return serviceDescriptor.createStreamMessageAdapter(in, out); 180 } 181 catch (ConnectorServiceException e) 182 { 183 throw new MessagingException(new Message(Messages.FAILED_TO_CREATE_X, "Stream Message Adapter"), 184 in, e); 185 } 186 } 187 188 public Map getServiceOverrides() 189 { 190 return serviceOverrides; 191 } 192 193 public void setServiceOverrides(Map serviceOverrides) 194 { 195 this.serviceOverrides = new Properties (); 196 this.serviceOverrides.putAll(serviceOverrides); 197 } 198 } 199 | Popular Tags |