1 10 11 package org.mule.impl.endpoint; 12 13 import org.mule.MuleManager; 14 import org.mule.providers.service.ConnectorFactory; 15 import org.mule.umo.endpoint.MalformedEndpointException; 16 import org.mule.umo.endpoint.UMOEndpointURI; 17 import org.mule.util.PropertiesUtils; 18 19 import java.io.UnsupportedEncodingException ; 20 import java.net.URI ; 21 import java.net.URLDecoder ; 22 import java.util.Properties ; 23 24 31 32 public abstract class AbstractEndpointBuilder implements EndpointBuilder 33 { 34 protected String address; 35 protected String endpointName; 36 protected String connectorName; 37 protected String transformers; 38 protected String responseTransformers; 39 protected String userInfo; 40 41 protected int createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR; 42 43 public UMOEndpointURI build(URI uri) throws MalformedEndpointException 44 { 45 Properties props = getPropertiesForURI(uri); 46 String replaceAddress = null; 47 if (address != null) 51 { 52 replaceAddress = address; 53 setEndpoint(uri, props); 54 address = replaceAddress; 55 } 56 else 57 { 58 setEndpoint(uri, props); 59 } 60 61 UMOEndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers, 62 responseTransformers, createConnector, props, uri, userInfo); 63 address = null; 64 endpointName = null; 65 connectorName = null; 66 transformers = null; 67 responseTransformers = null; 68 createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR; 69 return ep; 70 } 71 72 protected abstract void setEndpoint(URI uri, Properties props) throws MalformedEndpointException; 73 74 protected Properties getPropertiesForURI(URI uri) throws MalformedEndpointException 75 { 76 Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery()); 77 78 String tempEndpointName = (String )properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_NAME); 79 if (tempEndpointName != null) 80 { 81 this.endpointName = tempEndpointName; 82 } 83 String endpoint = (String )properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_URI); 85 if (endpoint != null) 86 { 87 this.address = endpoint; 88 address = decode(address, uri); 89 } 90 91 String cnnName = (String )properties.get(UMOEndpointURI.PROPERTY_CONNECTOR_NAME); 92 if (cnnName != null) 93 { 94 this.connectorName = cnnName; 95 } 96 97 String create = (String )properties.get(UMOEndpointURI.PROPERTY_CREATE_CONNECTOR); 98 if (create != null) 99 { 100 if ("0".equals(create)) 101 { 102 this.createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR; 103 } 104 else if ("1".equals(create)) 105 { 106 this.createConnector = ConnectorFactory.ALWAYS_CREATE_CONNECTOR; 107 } 108 else if ("2".equals(create)) 109 { 110 this.createConnector = ConnectorFactory.NEVER_CREATE_CONNECTOR; 111 } 112 else if ("IF_NEEDED".equals(create)) 113 { 114 this.createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR; 115 } 116 else if ("ALWAYS".equals(create)) 117 { 118 this.createConnector = ConnectorFactory.ALWAYS_CREATE_CONNECTOR; 119 } 120 else if ("NEVER".equals(create)) 121 { 122 this.createConnector = ConnectorFactory.NEVER_CREATE_CONNECTOR; 123 } 124 else if (connectorName == null) 125 { 126 this.createConnector = ConnectorFactory.USE_CONNECTOR; 127 connectorName = create; 128 } 129 130 } 131 132 transformers = (String )properties.get(UMOEndpointURI.PROPERTY_TRANSFORMERS); 133 if (transformers != null) 134 { 135 transformers = transformers.replaceAll(" ", ","); 136 } 137 responseTransformers = (String )properties.get(UMOEndpointURI.PROPERTY_RESPONSE_TRANSFORMERS); 138 if (responseTransformers != null) 139 { 140 responseTransformers = responseTransformers.replaceAll(" ", ","); 141 } 142 userInfo = uri.getUserInfo(); 145 if (userInfo != null) 146 { 147 userInfo = decode(userInfo, uri); 148 } 149 return properties; 150 } 151 152 private String decode(String string, URI uri) throws MalformedEndpointException 153 { 154 try 155 { 156 return URLDecoder.decode(string, MuleManager.getConfiguration().getEncoding()); 157 } 158 catch (UnsupportedEncodingException e) 159 { 160 throw new MalformedEndpointException(uri.toString(), e); 161 } 162 } 163 } 164 | Popular Tags |