001/*
002 * Copyright 2002-2016 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.quartz;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.net.URL;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.quartz.spi.ClassLoadHelper;
026
027import org.springframework.core.io.DefaultResourceLoader;
028import org.springframework.core.io.Resource;
029import org.springframework.core.io.ResourceLoader;
030
031/**
032 * Wrapper that adapts from the Quartz {@link ClassLoadHelper} interface
033 * onto Spring's {@link ResourceLoader} interface. Used by default when
034 * the SchedulerFactoryBean runs in a Spring ApplicationContext.
035 *
036 * @author Juergen Hoeller
037 * @since 2.5.5
038 * @see SchedulerFactoryBean#setApplicationContext
039 */
040public class ResourceLoaderClassLoadHelper implements ClassLoadHelper {
041
042        protected static final Log logger = LogFactory.getLog(ResourceLoaderClassLoadHelper.class);
043
044        private ResourceLoader resourceLoader;
045
046
047        /**
048         * Create a new ResourceLoaderClassLoadHelper for the default
049         * ResourceLoader.
050         * @see SchedulerFactoryBean#getConfigTimeResourceLoader()
051         */
052        public ResourceLoaderClassLoadHelper() {
053        }
054
055        /**
056         * Create a new ResourceLoaderClassLoadHelper for the given ResourceLoader.
057         * @param resourceLoader the ResourceLoader to delegate to
058         */
059        public ResourceLoaderClassLoadHelper(ResourceLoader resourceLoader) {
060                this.resourceLoader = resourceLoader;
061        }
062
063
064        @Override
065        public void initialize() {
066                if (this.resourceLoader == null) {
067                        this.resourceLoader = SchedulerFactoryBean.getConfigTimeResourceLoader();
068                        if (this.resourceLoader == null) {
069                                this.resourceLoader = new DefaultResourceLoader();
070                        }
071                }
072        }
073
074        @Override
075        public Class<?> loadClass(String name) throws ClassNotFoundException {
076                return this.resourceLoader.getClassLoader().loadClass(name);
077        }
078
079        @SuppressWarnings("unchecked")
080        public <T> Class<? extends T> loadClass(String name, Class<T> clazz) throws ClassNotFoundException {
081                return (Class<? extends T>) loadClass(name);
082        }
083
084        @Override
085        public URL getResource(String name) {
086                Resource resource = this.resourceLoader.getResource(name);
087                if (resource.exists()) {
088                        try {
089                                return resource.getURL();
090                        }
091                        catch (IOException ex) {
092                                if (logger.isWarnEnabled()) {
093                                        logger.warn("Could not load " + resource);
094                                }
095                                return null;
096                        }
097                }
098                else {
099                        return getClassLoader().getResource(name);
100                }
101        }
102
103        @Override
104        public InputStream getResourceAsStream(String name) {
105                Resource resource = this.resourceLoader.getResource(name);
106                if (resource.exists()) {
107                        try {
108                                return resource.getInputStream();
109                        }
110                        catch (IOException ex) {
111                                if (logger.isWarnEnabled()) {
112                                        logger.warn("Could not load " + resource);
113                                }
114                                return null;
115                        }
116                }
117                else {
118                        return getClassLoader().getResourceAsStream(name);
119                }
120        }
121
122        @Override
123        public ClassLoader getClassLoader() {
124                return this.resourceLoader.getClassLoader();
125        }
126
127}