001/*
002 * Copyright 2002-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jca.work.jboss;
018
019import java.lang.reflect.Method;
020import javax.management.MBeanServerConnection;
021import javax.management.MBeanServerInvocationHandler;
022import javax.management.ObjectName;
023import javax.naming.InitialContext;
024import javax.resource.spi.work.WorkManager;
025
026import org.springframework.util.Assert;
027
028/**
029 * Utility class for obtaining the JBoss JCA WorkManager,
030 * typically for use in web applications.
031 *
032 * @author Juergen Hoeller
033 * @since 2.5.2
034 * @deprecated as of Spring 4.0, since there are no fully supported versions
035 * of JBoss that this class works with anymore
036 */
037@Deprecated
038public abstract class JBossWorkManagerUtils {
039
040        private static final String JBOSS_WORK_MANAGER_MBEAN_CLASS_NAME = "org.jboss.resource.work.JBossWorkManagerMBean";
041
042        private static final String MBEAN_SERVER_CONNECTION_JNDI_NAME = "jmx/invoker/RMIAdaptor";
043
044        private static final String DEFAULT_WORK_MANAGER_MBEAN_NAME = "jboss.jca:service=WorkManager";
045
046
047        /**
048         * Obtain the default JBoss JCA WorkManager through a JMX lookup
049         * for the default JBossWorkManagerMBean.
050         * @see org.jboss.resource.work.JBossWorkManagerMBean
051         */
052        public static WorkManager getWorkManager() {
053                return getWorkManager(DEFAULT_WORK_MANAGER_MBEAN_NAME);
054        }
055
056        /**
057         * Obtain the default JBoss JCA WorkManager through a JMX lookup
058         * for the JBossWorkManagerMBean.
059         * @param mbeanName the JMX object name to use
060         * @see org.jboss.resource.work.JBossWorkManagerMBean
061         */
062        public static WorkManager getWorkManager(String mbeanName) {
063                Assert.hasLength(mbeanName, "JBossWorkManagerMBean name must not be empty");
064                try {
065                        Class<?> mbeanClass = JBossWorkManagerUtils.class.getClassLoader().loadClass(JBOSS_WORK_MANAGER_MBEAN_CLASS_NAME);
066                        InitialContext jndiContext = new InitialContext();
067                        MBeanServerConnection mconn = (MBeanServerConnection) jndiContext.lookup(MBEAN_SERVER_CONNECTION_JNDI_NAME);
068                        ObjectName objectName = ObjectName.getInstance(mbeanName);
069                        Object workManagerMBean = MBeanServerInvocationHandler.newProxyInstance(mconn, objectName, mbeanClass, false);
070                        Method getInstanceMethod = workManagerMBean.getClass().getMethod("getInstance");
071                        return (WorkManager) getInstanceMethod.invoke(workManagerMBean);
072                }
073                catch (Exception ex) {
074                        throw new IllegalStateException(
075                                        "Could not initialize JBossWorkManagerTaskExecutor because JBoss API is not available", ex);
076                }
077        }
078
079}