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.io.Serializable;
020:
021: import javax.jbi.JBIException;
022: import javax.management.JMException;
023: import javax.management.MBeanOperationInfo;
024: import javax.naming.NamingException;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.servicemix.client.DefaultServiceMixClient;
029: import org.apache.servicemix.client.ServiceMixClient;
030: import org.apache.servicemix.jbi.management.BaseSystemService;
031: import org.apache.servicemix.jbi.management.OperationInfoHelper;
032:
033: /**
034: * @author <a href="mailto:gnodet [at] apache.org">Guillaume Nodet</a>
035: */
036: public class ClientFactory extends BaseSystemService implements
037: ClientFactoryMBean, Serializable {
038:
039: private static final Log LOG = LogFactory
040: .getLog(ClientFactory.class);
041:
042: private String jndiName = DEFAULT_JNDI_NAME;
043:
044: private boolean isFactoryJNDIregistered;
045:
046: public ClientFactory() {
047: }
048:
049: /**
050: * @return the jndiName
051: */
052: public String getJndiName() {
053: return jndiName;
054: }
055:
056: /**
057: * @param jndiName the jndiName to set
058: */
059: public void setJndiName(String jndiName) {
060: this .jndiName = jndiName;
061: }
062:
063: public ServiceMixClient createClient() throws JBIException {
064: return new DefaultServiceMixClient(getContainer());
065: }
066:
067: protected Class getServiceMBean() {
068: return ClientFactoryMBean.class;
069: }
070:
071: public String getDescription() {
072: return "Client Factory Service";
073: }
074:
075: public MBeanOperationInfo[] getOperationInfos() throws JMException {
076: OperationInfoHelper helper = new OperationInfoHelper();
077: helper.addOperation(getObjectToManage(), "createClient", 0,
078: "create a new client");
079: return OperationInfoHelper.join(super .getOperationInfos(),
080: helper.getOperationInfos());
081: }
082:
083: /**
084: * Start the item.
085: *
086: * @exception javax.jbi.JBIException if the item fails to start.
087: */
088: public void start() throws javax.jbi.JBIException {
089: try {
090: getContainer().getNamingContext().bind(jndiName, this );
091: isFactoryJNDIregistered = true;
092: } catch (NamingException e) {
093: LOG.warn("Cound not start ClientFactory: " + e);
094: if (LOG.isDebugEnabled()) {
095: LOG.debug("Could not start ClientFactory", e);
096: }
097: isFactoryJNDIregistered = false;
098: }
099: super .start();
100: }
101:
102: /**
103: * Stop the item. This suspends current messaging activities.
104: *
105: * @exception javax.jbi.JBIException if the item fails to stop.
106: */
107: public void stop() throws javax.jbi.JBIException {
108: super .stop();
109: try {
110: if (isFactoryJNDIregistered) {
111: getContainer().getNamingContext().unbind(jndiName);
112: }
113: } catch (NamingException e) {
114: LOG.warn("Cound not stop ClientFactory: " + e);
115: if (LOG.isDebugEnabled()) {
116: LOG.debug("Could not stop ClientFactory", e);
117: }
118: }
119: }
120:
121: }
|