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.jbi.framework;
018
019 import java.beans.PropertyChangeListener;
020 import java.net.URI;
021
022 import javax.jbi.messaging.MessageExchange;
023 import javax.jbi.messaging.NormalizedMessage;
024 import javax.management.JMException;
025 import javax.management.MBeanAttributeInfo;
026 import javax.management.MBeanOperationInfo;
027 import javax.xml.namespace.QName;
028 import javax.xml.transform.TransformerException;
029
030 import org.apache.commons.logging.Log;
031 import org.apache.commons.logging.LogFactory;
032 import org.apache.servicemix.client.ServiceMixClient;
033 import org.apache.servicemix.jbi.FaultException;
034 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
035 import org.apache.servicemix.jbi.jaxp.StringSource;
036 import org.apache.servicemix.jbi.management.AttributeInfoHelper;
037 import org.apache.servicemix.jbi.management.MBeanInfoProvider;
038 import org.apache.servicemix.jbi.management.OperationInfoHelper;
039 import org.apache.servicemix.jbi.servicedesc.AbstractServiceEndpoint;
040 import org.apache.servicemix.jbi.servicedesc.ExternalEndpoint;
041 import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
042 import org.apache.servicemix.jbi.servicedesc.LinkedEndpoint;
043 import org.apache.servicemix.jbi.util.DOMUtil;
044 import org.apache.servicemix.jbi.util.QNameUtil;
045
046 public class Endpoint implements EndpointMBean, MBeanInfoProvider {
047
048 private static final Log LOG = LogFactory.getLog(Endpoint.class);
049
050 private AbstractServiceEndpoint endpoint;
051 private Registry registry;
052
053 public Endpoint(AbstractServiceEndpoint endpoint, Registry registry) {
054 this.endpoint = endpoint;
055 this.registry = registry;
056 }
057
058 public String getEndpointName() {
059 return endpoint.getEndpointName();
060 }
061
062 public QName[] getInterfaces() {
063 return endpoint.getInterfaces();
064 }
065
066 public QName getServiceName() {
067 return endpoint.getServiceName();
068 }
069
070 public String loadReference() {
071 try {
072 return DOMUtil.asIndentedXML(endpoint.getAsReference(null));
073 } catch (TransformerException e) {
074 return null;
075 }
076 }
077
078 public String loadWSDL() {
079 try {
080 return DOMUtil.asXML(registry.getEndpointDescriptor(endpoint));
081 } catch (Exception e) {
082 return null;
083 }
084 }
085
086 public String getComponentName() {
087 if (endpoint.getComponentNameSpace() != null) {
088 return endpoint.getComponentNameSpace().getName();
089 } else {
090 return null;
091 }
092 }
093
094 public MBeanAttributeInfo[] getAttributeInfos() throws JMException {
095 AttributeInfoHelper helper = new AttributeInfoHelper();
096 helper.addAttribute(getObjectToManage(), "endpointName", "name of the endpoint");
097 helper.addAttribute(getObjectToManage(), "serviceName", "name of the service");
098 helper.addAttribute(getObjectToManage(), "componentName", "component name of the service unit");
099 helper.addAttribute(getObjectToManage(), "interfaces", "interfaces implemented by this endpoint");
100 return helper.getAttributeInfos();
101 }
102
103 public MBeanOperationInfo[] getOperationInfos() throws JMException {
104 OperationInfoHelper helper = new OperationInfoHelper();
105 helper.addOperation(getObjectToManage(), "loadReference", "retrieve the endpoint reference");
106 helper.addOperation(getObjectToManage(), "loadWSDL", "retrieve the wsdl description of this endpoint");
107 helper.addOperation(getObjectToManage(), "send", "send a simple message exchange to test this endpoint");
108 return helper.getOperationInfos();
109 }
110
111 public Object getObjectToManage() {
112 return this;
113 }
114
115 public String getName() {
116 return endpoint.getServiceName() + endpoint.getEndpointName();
117 }
118
119 public String getType() {
120 return "Endpoint";
121 }
122
123 public String getSubType() {
124 if (endpoint instanceof InternalEndpoint) {
125 return "Internal";
126 } else if (endpoint instanceof LinkedEndpoint) {
127 return "Linked";
128 } else if (endpoint instanceof ExternalEndpoint) {
129 return "External";
130 }
131 return null;
132 }
133
134 public String getDescription() {
135 return null;
136 }
137
138 public void setPropertyChangeListener(PropertyChangeListener l) {
139 }
140
141 protected AbstractServiceEndpoint getEndpoint() {
142 return endpoint;
143 }
144
145 public String send(String content, String operation, String mep) {
146 ServiceMixClient client = null;
147 try {
148 client = registry.getContainer().getClientFactory().createClient();
149 MessageExchange me = client.getExchangeFactory().createExchange(URI.create(mep));
150 NormalizedMessage nm = me.createMessage();
151 me.setMessage(nm, "in");
152 nm.setContent(new StringSource(content));
153 me.setEndpoint(endpoint);
154 if (operation != null) {
155 me.setOperation(QNameUtil.parse(operation));
156 }
157 client.sendSync(me);
158 if (me.getError() != null) {
159 throw me.getError();
160 } else if (me.getFault() != null) {
161 throw FaultException.newInstance(me);
162 } else if (me.getMessage("out") != null) {
163 return new SourceTransformer().contentToString(me.getMessage("out"));
164 }
165 return null;
166 } catch (Exception e) {
167 LOG.debug("Error proces test exchange", e);
168 throw new RuntimeException(e);
169 } finally {
170 if (client != null) {
171 try {
172 client.close();
173 } catch (Exception e) {
174 // ignore
175 }
176 }
177 }
178 }
179
180 }