Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » context » support » [javadoc | source]
    1   /*
    2    * Copyright 2002-2008 the original author or authors.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   
   17   package org.springframework.context.support;
   18   
   19   import org.springframework.beans.factory.BeanNameAware;
   20   import org.springframework.beans.factory.InitializingBean;
   21   import org.springframework.context.ApplicationContext;
   22   import org.springframework.util.Assert;
   23   import org.springframework.util.StringUtils;
   24   import org.springframework.util.SystemPropertyUtils;
   25   
   26   /**
   27    * {@link AbstractRefreshableApplicationContext} subclass that adds common handling
   28    * of specified config locations. Serves as base class for XML-based application
   29    * context implementations such as {@link ClassPathXmlApplicationContext} and
   30    * {@link FileSystemXmlApplicationContext}, as well as
   31    * {@link org.springframework.web.context.support.XmlWebApplicationContext} and
   32    * {@link org.springframework.web.portlet.context.XmlPortletApplicationContext}.
   33    *
   34    * @author Juergen Hoeller
   35    * @since 2.5.2
   36    * @see #setConfigLocation
   37    * @see #setConfigLocations
   38    * @see #getDefaultConfigLocations
   39    */
   40   public abstract class AbstractRefreshableConfigApplicationContext extends AbstractRefreshableApplicationContext
   41   		implements BeanNameAware, InitializingBean {
   42   
   43   	private String[] configLocations;
   44   
   45   	private boolean setIdCalled = false;
   46   
   47   
   48   	/**
   49   	 * Create a new AbstractRefreshableConfigApplicationContext with no parent.
   50   	 */
   51   	public AbstractRefreshableConfigApplicationContext() {
   52   	}
   53   
   54   	/**
   55   	 * Create a new AbstractRefreshableConfigApplicationContext with the given parent context.
   56   	 * @param parent the parent context
   57   	 */
   58   	public AbstractRefreshableConfigApplicationContext(ApplicationContext parent) {
   59   		super(parent);
   60   	}
   61   
   62   
   63   	/**
   64   	 * Set the config locations for this application context in init-param style,
   65   	 * i.e. with distinct locations separated by commas, semicolons or whitespace.
   66   	 * <p>If not set, the implementation may use a default as appropriate.
   67   	 */
   68   	public void setConfigLocation(String location) {
   69   		setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS));
   70   	}
   71   
   72   	/**
   73   	 * Set the config locations for this application context.
   74   	 * <p>If not set, the implementation may use a default as appropriate.
   75   	 */
   76   	public void setConfigLocations(String[] locations) {
   77   		if (locations != null) {
   78   			Assert.noNullElements(locations, "Config locations must not be null");
   79   			this.configLocations = new String[locations.length];
   80   			for (int i = 0; i < locations.length; i++) {
   81   				this.configLocations[i] = resolvePath(locations[i]).trim();
   82   			}
   83   		}
   84   		else {
   85   			this.configLocations = null;
   86   		}
   87   	}
   88   
   89   	/**
   90   	 * Return an array of resource locations, referring to the XML bean definition
   91   	 * files that this context should be built with. Can also include location
   92   	 * patterns, which will get resolved via a ResourcePatternResolver.
   93   	 * <p>The default implementation returns <code>null</code>. Subclasses can override
   94   	 * this to provide a set of resource locations to load bean definitions from.
   95   	 * @return an array of resource locations, or <code>null</code> if none
   96   	 * @see #getResources
   97   	 * @see #getResourcePatternResolver
   98   	 */
   99   	protected String[] getConfigLocations() {
  100   		return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
  101   	}
  102   
  103   	/**
  104   	 * Return the default config locations to use, for the case where no
  105   	 * explicit config locations have been specified.
  106   	 * <p>The default implementation returns <code>null</code>,
  107   	 * requiring explicit config locations.
  108   	 * @return an array of default config locations, if any
  109   	 * @see #setConfigLocations
  110   	 */
  111   	protected String[] getDefaultConfigLocations() {
  112   		return null;
  113   	}
  114   
  115   	/**
  116   	 * Resolve the given path, replacing placeholders with corresponding
  117   	 * system property values if necessary. Applied to config locations.
  118   	 * @param path the original file path
  119   	 * @return the resolved file path
  120   	 * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
  121   	 */
  122   	protected String resolvePath(String path) {
  123   		return SystemPropertyUtils.resolvePlaceholders(path);
  124   	}
  125   
  126   
  127   	public void setId(String id) {
  128   		super.setId(id);
  129   		this.setIdCalled = true;
  130   	}
  131   
  132   	/**
  133   	 * Sets the id of this context to the bean name by default,
  134   	 * for cases where the context instance is itself defined as a bean.
  135   	 */
  136   	public void setBeanName(String name) {
  137   		if (!this.setIdCalled) {
  138   			super.setId(name);
  139   		}
  140   	}
  141   
  142   	/**
  143   	 * Triggers {@link #refresh()} if not refreshed in the concrete context's
  144   	 * constructor already.
  145   	 */
  146   	public void afterPropertiesSet() {
  147   		if (!isActive()) {
  148   			refresh();
  149   		}
  150   	}
  151   
  152   }

Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » context » support » [javadoc | source]