001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.common.endpoints;
018
019 import javax.jbi.component.ComponentContext;
020 import javax.jbi.management.DeploymentException;
021 import javax.jbi.messaging.MessageExchange;
022 import javax.jbi.messaging.MessageExchange.Role;
023 import javax.jbi.servicedesc.ServiceEndpoint;
024 import javax.xml.namespace.QName;
025
026 import org.apache.servicemix.common.DefaultComponent;
027 import org.apache.servicemix.common.ExternalEndpoint;
028 import org.apache.servicemix.common.ServiceMixComponent;
029 import org.apache.servicemix.common.ServiceUnit;
030 import org.apache.servicemix.common.util.URIResolver;
031
032 public abstract class ConsumerEndpoint extends SimpleEndpoint {
033
034 private ServiceEndpoint activated;
035 private QName targetInterface;
036 private QName targetService;
037 private String targetEndpoint;
038 private QName targetOperation;
039 private String targetUri;
040
041 public ConsumerEndpoint() {
042 }
043
044 public ConsumerEndpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
045 super(serviceUnit, service, endpoint);
046 }
047
048 public ConsumerEndpoint(DefaultComponent component, ServiceEndpoint endpoint) {
049 super(component.getServiceUnit(), endpoint.getServiceName(), endpoint.getEndpointName());
050 }
051
052 public Role getRole() {
053 return Role.CONSUMER;
054 }
055
056 public synchronized void activate() throws Exception {
057 super.activate();
058 ServiceMixComponent component = getServiceUnit().getComponent();
059 ComponentContext ctx = component.getComponentContext();
060 activated = new ExternalEndpoint(component.getEPRElementName(), getLocationURI(), getService(),
061 getEndpoint(), getInterfaceName());
062 ctx.registerExternalEndpoint(activated);
063 }
064
065 public synchronized void deactivate() throws Exception {
066 ServiceMixComponent component = getServiceUnit().getComponent();
067 ComponentContext ctx = component.getComponentContext();
068 if (activated != null) {
069 ServiceEndpoint se = activated;
070 activated = null;
071 ctx.deregisterExternalEndpoint(se);
072 }
073 super.deactivate();
074 }
075
076 /**
077 * Return the URI identifying this external endpoint. This must be overriden so that endpoint resolution can work correctly.
078 *
079 * @return the URI identifying this external endpoint
080 */
081 public String getLocationURI() {
082 return null;
083 }
084
085 /**
086 * @return the targetEndpoint
087 */
088 public String getTargetEndpoint() {
089 return targetEndpoint;
090 }
091
092 /**
093 * Sets the endpoint name of the target endpoint.
094 *
095 * @param targetEndpoint a string specifiying the name of the target endpoint
096 * @org.apache.xbean.Property description="the name of the endpoint to which requests are sent"
097 */
098 public void setTargetEndpoint(String targetEndpoint) {
099 this.targetEndpoint = targetEndpoint;
100 }
101
102 /**
103 * @return the targetInterface
104 */
105 public QName getTargetInterface() {
106 return targetInterface;
107 }
108
109 /**
110 * Sets the name of the target interface.
111 *
112 * @param targetInterface a QName specifiying the name of the target interface
113 * @org.apache.xbean.Property description="the QName of the interface to which requests are sent"
114 */
115 public void setTargetInterface(QName targetInterface) {
116 this.targetInterface = targetInterface;
117 }
118
119 /**
120 * @return the targetService
121 */
122 public QName getTargetService() {
123 return targetService;
124 }
125
126 /**
127 * Sets the name of the target service.
128 *
129 * @param targetService a QName specifiying the name of the target interface
130 * @org.apache.xbean.Property description="the QName of the service to which requests are sent"
131 */
132 public void setTargetService(QName targetService) {
133 this.targetService = targetService;
134 }
135
136 /**
137 * @return the targetOperation
138 */
139 public QName getTargetOperation() {
140 return targetOperation;
141 }
142
143 /**
144 * Sets the name of the target operation.
145 *
146 * @param targetOperation a QName specifiying the name of the target operation
147 * @org.apache.xbean.Property description="the QName of the operation to which requests are sent"
148 */
149 public void setTargetOperation(QName targetOperation) {
150 this.targetOperation = targetOperation;
151 }
152
153 /**
154 * @return the targetUri
155 */
156 public String getTargetUri() {
157 return targetUri;
158 }
159
160 /**
161 * @param targetUri the targetUri to set
162 */
163 public void setTargetUri(String targetUri) {
164 this.targetUri = targetUri;
165 }
166
167 protected void configureExchangeTarget(MessageExchange exchange) {
168 if (targetUri != null) {
169 URIResolver.configureExchange(exchange, getContext(), targetUri);
170 }
171 if (exchange.getInterfaceName() == null && targetInterface != null) {
172 exchange.setInterfaceName(targetInterface);
173 }
174 if (exchange.getOperation() == null && targetOperation != null) {
175 exchange.setOperation(targetOperation);
176 }
177 if (exchange.getService() == null && targetService != null) {
178 exchange.setService(targetService);
179 if (targetEndpoint != null) {
180 ServiceEndpoint se = getContext().getEndpoint(targetService, targetEndpoint);
181 if (se != null) {
182 exchange.setEndpoint(se);
183 } else {
184 logger.warn("Target service (" + targetService + ") and endpoint (" + targetEndpoint + ")"
185 + " specified, but no matching endpoint found. Only the service will be used for routing.");
186 }
187 }
188 }
189 }
190
191 public void validate() throws DeploymentException {
192 super.validate();
193 if (targetInterface == null && targetService == null && targetUri == null) {
194 throw new DeploymentException("targetInterface, targetService or targetUri should be specified");
195 }
196 }
197 }