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
081: .getEndpointDescriptor(endpoint));
082: } catch (Exception e) {
083: return null;
084: }
085: }
086:
087: public String getComponentName() {
088: if (endpoint.getComponentNameSpace() != null) {
089: return endpoint.getComponentNameSpace().getName();
090: } else {
091: return null;
092: }
093: }
094:
095: public MBeanAttributeInfo[] getAttributeInfos() throws JMException {
096: AttributeInfoHelper helper = new AttributeInfoHelper();
097: helper.addAttribute(getObjectToManage(), "endpointName",
098: "name of the endpoint");
099: helper.addAttribute(getObjectToManage(), "serviceName",
100: "name of the service");
101: helper.addAttribute(getObjectToManage(), "componentName",
102: "component name of the service unit");
103: helper.addAttribute(getObjectToManage(), "interfaces",
104: "interfaces implemented by this endpoint");
105: return helper.getAttributeInfos();
106: }
107:
108: public MBeanOperationInfo[] getOperationInfos() throws JMException {
109: OperationInfoHelper helper = new OperationInfoHelper();
110: helper.addOperation(getObjectToManage(), "loadReference",
111: "retrieve the endpoint reference");
112: helper.addOperation(getObjectToManage(), "loadWSDL",
113: "retrieve the wsdl description of this endpoint");
114: helper.addOperation(getObjectToManage(), "send",
115: "send a simple message exchange to test this endpoint");
116: return helper.getOperationInfos();
117: }
118:
119: public Object getObjectToManage() {
120: return this ;
121: }
122:
123: public String getName() {
124: return endpoint.getServiceName() + endpoint.getEndpointName();
125: }
126:
127: public String getType() {
128: return "Endpoint";
129: }
130:
131: public String getSubType() {
132: if (endpoint instanceof InternalEndpoint) {
133: return "Internal";
134: } else if (endpoint instanceof LinkedEndpoint) {
135: return "Linked";
136: } else if (endpoint instanceof ExternalEndpoint) {
137: return "External";
138: }
139: return null;
140: }
141:
142: public String getDescription() {
143: return null;
144: }
145:
146: public void setPropertyChangeListener(PropertyChangeListener l) {
147: }
148:
149: protected AbstractServiceEndpoint getEndpoint() {
150: return endpoint;
151: }
152:
153: public String send(String content, String operation, String mep) {
154: ServiceMixClient client = null;
155: try {
156: client = registry.getContainer().getClientFactory()
157: .createClient();
158: MessageExchange me = client.getExchangeFactory()
159: .createExchange(URI.create(mep));
160: NormalizedMessage nm = me.createMessage();
161: me.setMessage(nm, "in");
162: nm.setContent(new StringSource(content));
163: me.setEndpoint(endpoint);
164: if (operation != null) {
165: me.setOperation(QNameUtil.parse(operation));
166: }
167: client.sendSync(me);
168: if (me.getError() != null) {
169: throw me.getError();
170: } else if (me.getFault() != null) {
171: throw FaultException.newInstance(me);
172: } else if (me.getMessage("out") != null) {
173: return new SourceTransformer().contentToString(me
174: .getMessage("out"));
175: }
176: return null;
177: } catch (Exception e) {
178: LOG.debug("Error proces test exchange", e);
179: throw new RuntimeException(e);
180: } finally {
181: if (client != null) {
182: try {
183: client.close();
184: } catch (Exception e) {
185: // ignore
186: }
187: }
188: }
189: }
190:
191: }
|