001/*
002 * Copyright 2002-2012 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.scheduling.commonj;
018
019import commonj.work.Work;
020
021import org.springframework.scheduling.SchedulingAwareRunnable;
022import org.springframework.util.Assert;
023
024/**
025 * Simple Work adapter that delegates to a given Runnable.
026 *
027 * @author Juergen Hoeller
028 * @since 2.0
029 * @see commonj.work.Work
030 * @see java.lang.Runnable
031 */
032public class DelegatingWork implements Work {
033
034        private final Runnable delegate;
035
036
037        /**
038         * Create a new DelegatingWork.
039         * @param delegate the Runnable implementation to delegate to
040         * (may be a SchedulingAwareRunnable for extended support)
041         * @see org.springframework.scheduling.SchedulingAwareRunnable
042         * @see #isDaemon()
043         */
044        public DelegatingWork(Runnable delegate) {
045                Assert.notNull(delegate, "Delegate must not be null");
046                this.delegate = delegate;
047        }
048
049        /**
050         * Return the wrapped Runnable implementation.
051         */
052        public final Runnable getDelegate() {
053                return this.delegate;
054        }
055
056
057        /**
058         * Delegates execution to the underlying Runnable.
059         */
060        @Override
061        public void run() {
062                this.delegate.run();
063        }
064
065        /**
066         * This implementation delegates to
067         * {@link org.springframework.scheduling.SchedulingAwareRunnable#isLongLived()},
068         * if available.
069         */
070        @Override
071        public boolean isDaemon() {
072                return (this.delegate instanceof SchedulingAwareRunnable &&
073                                ((SchedulingAwareRunnable) this.delegate).isLongLived());
074        }
075
076        /**
077         * This implementation is empty, since we expect the Runnable
078         * to terminate based on some specific shutdown signal.
079         */
080        @Override
081        public void release() {
082        }
083
084}