001: /*
002: * $Id: EndpointService.java 11311 2008-03-10 20:15:57Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.management.mbean;
012:
013: import org.mule.api.endpoint.ImmutableEndpoint;
014: import org.mule.api.endpoint.InboundEndpoint;
015: import org.mule.api.endpoint.OutboundEndpoint;
016: import org.mule.api.transport.MessageReceiver;
017: import org.mule.config.i18n.CoreMessages;
018: import org.mule.util.ObjectNameHelper;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: /**
024: * The EndpointServiceMBean allows you to check the confiugration of an endpoint and
025: * conect/disconnect endpoints manually.
026: */
027: public class EndpointService implements EndpointServiceMBean {
028:
029: /**
030: * logger used by this class
031: */
032: protected transient Log logger = LogFactory.getLog(getClass());
033:
034: private ImmutableEndpoint endpoint;
035: private MessageReceiver receiver;
036: private String name;
037: private String componentName;
038:
039: public EndpointService(ImmutableEndpoint endpoint) {
040: this .endpoint = endpoint;
041: init();
042: }
043:
044: public EndpointService(MessageReceiver receiver) {
045: if (receiver == null) {
046: throw new IllegalArgumentException(CoreMessages
047: .objectIsNull("Receiver").getMessage());
048: }
049: this .endpoint = receiver.getEndpoint();
050: this .receiver = receiver;
051: this .componentName = receiver.getService().getName();
052: init();
053: }
054:
055: private void init() {
056: if (endpoint == null) {
057: throw new IllegalArgumentException(CoreMessages
058: .objectIsNull("Endpoint").getMessage());
059: }
060: if (receiver == null && endpoint instanceof InboundEndpoint) {
061: throw new IllegalArgumentException(
062: "Recevier is null for Endpoint MBean but the endpoint itself is a receiving endpoint");
063: }
064:
065: name = ObjectNameHelper.getEndpointName(endpoint
066: .getEndpointURI());
067: }
068:
069: public String getAddress() {
070: return endpoint.getEndpointURI().getAddress();
071: }
072:
073: public String getName() {
074: return name;
075: }
076:
077: public boolean isConnected() {
078: return receiver == null || receiver.isConnected();
079: }
080:
081: public void connect() throws Exception {
082: if (receiver != null && !receiver.isConnected()) {
083: receiver.connect();
084: } else if (logger.isDebugEnabled()) {
085: logger.debug("Endpoint is already connected");
086: }
087: }
088:
089: public void disconnect() throws Exception {
090: if (receiver != null && receiver.isConnected()) {
091: receiver.disconnect();
092: } else if (logger.isDebugEnabled()) {
093: logger.debug("Endpoint is already disconnected");
094: }
095: }
096:
097: public boolean isInbound() {
098: return endpoint instanceof InboundEndpoint;
099: }
100:
101: public boolean isOutbound() {
102: return endpoint instanceof OutboundEndpoint;
103: }
104:
105: public boolean isSynchronous() {
106: return endpoint.isSynchronous();
107: }
108:
109: public String getComponentName() {
110: return componentName;
111: }
112:
113: public void setComponentName(String componentName) {
114: this.componentName = componentName;
115: }
116:
117: }
|