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.glassfish;
018
019import java.lang.reflect.Method;
020import javax.resource.spi.work.WorkManager;
021
022import org.springframework.jca.work.WorkManagerTaskExecutor;
023import org.springframework.util.ReflectionUtils;
024
025/**
026 * Spring TaskExecutor adapter for the GlassFish JCA WorkManager.
027 * Can be defined in web applications to make a TaskExecutor reference
028 * available, talking to the GlassFish WorkManager (thread pool) underneath.
029 *
030 * <p>This is the GlassFish equivalent of the CommonJ
031 * {@link org.springframework.scheduling.commonj.WorkManagerTaskExecutor}
032 * adapter for WebLogic and WebSphere.
033 *
034 * <p>Note: On GlassFish 4 and higher, a
035 * {@link org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor}
036 * should be preferred, following JSR-236 support in Java EE 7.
037 *
038 * @author Juergen Hoeller
039 * @since 2.5.2
040 */
041public class GlassFishWorkManagerTaskExecutor extends WorkManagerTaskExecutor {
042
043        private static final String WORK_MANAGER_FACTORY_CLASS = "com.sun.enterprise.connectors.work.WorkManagerFactory";
044
045        private final Method getWorkManagerMethod;
046
047
048        public GlassFishWorkManagerTaskExecutor() {
049                try {
050                        Class<?> wmf = getClass().getClassLoader().loadClass(WORK_MANAGER_FACTORY_CLASS);
051                        this.getWorkManagerMethod = wmf.getMethod("getWorkManager", String.class);
052                }
053                catch (Exception ex) {
054                        throw new IllegalStateException(
055                                        "Could not initialize GlassFishWorkManagerTaskExecutor because GlassFish API is not available", ex);
056                }
057        }
058
059        /**
060         * Identify a specific GlassFish thread pool to talk to.
061         * <p>The thread pool name matches the resource adapter name
062         * in default RAR deployment scenarios.
063         */
064        public void setThreadPoolName(String threadPoolName) {
065                WorkManager wm = (WorkManager) ReflectionUtils.invokeMethod(this.getWorkManagerMethod, null, threadPoolName);
066                if (wm == null) {
067                        throw new IllegalArgumentException("Specified thread pool name '" + threadPoolName +
068                                        "' does not correspond to an actual pool definition in GlassFish. Check your configuration!");
069                }
070                setWorkManager(wm);
071        }
072
073        /**
074         * Obtains GlassFish's default thread pool.
075         */
076        @Override
077        protected WorkManager getDefaultWorkManager() {
078                return (WorkManager) ReflectionUtils.invokeMethod(this.getWorkManagerMethod, null, new Object[] {null});
079        }
080
081}