SinaburocastPropertiesTool.java 4.68 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * 独自に作成したVelocityToolを置くパッケージ.
 */
package jp.agentec.sinaburocast.common.util.velocity;

import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.tools.generic.ValueParser;

Kim Gyeongeun committed
15 16
import jp.agentec.sinaburocast.common.util.PropertyUtil;

Kim Gyeongeun committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/**
 * SinaburoCast.properties用VelocityTools.
 *
 * applicationスコープに置き、起動時に一度だけ設定ファイルを読んでapplicationコンテキストへ反映させるツール
 *
 * @author arima
 *
 */
public class SinaburocastPropertiesTool {
	private static final Logger __LOG = Logger.getLogger(SinaburocastPropertiesTool.class);

	public interface PropertyKey {
		String POPULATION_PREFIX = "view.";
		String CSSPATH = "cssPath";
		String JSPATH = "jsPath";
		String IMAGESPATH = "imagesPath";
		String LOGOPATH = "logoPath";
		String SWFPATH = "swfPath";
	}

	protected ServletContext _application;

	protected String _propertiesFileName;
	protected String _propertiesDfFileName;
	protected Properties _props;
	protected Properties _propsDf;

	protected String _defaultCssSubDirName;
	protected String _defaultJsSubDirName;
	protected String _defaultImagesSubDirName;
	protected String _defaultLogoSubDirName;
	protected String _defaultSwfSubDirName;

	/**
	 * Initializes this tool.
	 *
	 * @param obj the current ViewContext
	 * @throws IllegalArgumentException if the param is not a ViewContext
	 */
	public void init(Object obj) {
		if (! (obj instanceof ServletContext)) {
			throw new IllegalArgumentException("Tool can only be initialized with a ServletContext");
		}

		_application = (ServletContext) obj;

		populateProperties();
		populateDefaultValues();
	}

    /**
     * Looks for configuration values in the given params.
     * @since VelocityTools 1.4
     */
    public void configure(Map<?, ?> params)
    {
    	ValueParser values = new ValueParser(params);
    	_propertiesFileName = values.getString("filename", "sinaburocast");
    	_propertiesDfFileName = values.getString("filename", "sinaburocast_df");
    	_defaultCssSubDirName = values.getString("defaultCssSubDir", "css");
    	_defaultJsSubDirName = values.getString("defaultJsSubDir", "js");
    	_defaultImagesSubDirName = values.getString("defaultImagesSubDir", "images");
    	_defaultLogoSubDirName = values.getString("defaultLogoSubDir", "logo");
    	_defaultSwfSubDirName = values.getString("defaultSwfSubDir", "swf");
    }

	/**
	 * 設定ファイルを展開・設定する
	 * @param context
	 */
	private void populateProperties() {
		_props = PropertyUtil.getProperties(_propertiesFileName, false);
		_propsDf = PropertyUtil.getProperties(_propertiesDfFileName, false);
		if (_props == null || _propsDf == null) {
			throw new ResourceNotFoundException("Can't find configuration (" + _propertiesFileName + ")" );
		}

		final int prefixLength = PropertyKey.POPULATION_PREFIX.length();
		for (String key : _props.stringPropertyNames()) {
			if (key.startsWith(PropertyKey.POPULATION_PREFIX)) {
				_application.setAttribute(key.substring(prefixLength), _props.getProperty(key));
			}
		}
		for (String key : _propsDf.stringPropertyNames()) {
			if (key.startsWith(PropertyKey.POPULATION_PREFIX)) {
				_application.setAttribute(key.substring(prefixLength), _propsDf.getProperty(key));
			}
		}

	}

	/**
	 * いくつかの要素について、設定ファイルになかった場合のデフォルト値を設定する
	 */
	private void populateDefaultValues() {
Kim Gyeongeun committed
112
		String contextPath = _application.getContextPath(); 
Kim Gyeongeun committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
		if (contextPath == null)	contextPath = "";

		if (_application.getAttribute(PropertyKey.CSSPATH) == null) {
			_application.setAttribute(PropertyKey.CSSPATH, contextPath + '/' + _defaultCssSubDirName);
		}
		if (_application.getAttribute(PropertyKey.JSPATH) == null) {
			_application.setAttribute(PropertyKey.JSPATH, contextPath + '/' + _defaultJsSubDirName);
		}
		if (_application.getAttribute(PropertyKey.IMAGESPATH) == null) {
			_application.setAttribute(PropertyKey.IMAGESPATH, contextPath + '/' + _defaultImagesSubDirName);
		}
		if (_application.getAttribute(PropertyKey.LOGOPATH) == null) {
			_application.setAttribute(PropertyKey.LOGOPATH, contextPath + '/' + _defaultLogoSubDirName);
		}
		if (_application.getAttribute(PropertyKey.SWFPATH) == null) {
			_application.setAttribute(PropertyKey.SWFPATH, contextPath + '/' + _defaultSwfSubDirName);
		}
	}

	/**
	 * プロパティを返す.
	 *
	 * 名前が POPULATION_PREFIX で始まらないプロパティはこちらから取得できる
	 *
	 * @param key
	 * @return
	 */
	public String get(String key) {
		return PropertyUtil.getProperty(key);
	}
}