/*
 * This software is distributed under following license based on modified BSD
 * style license.
 * ----------------------------------------------------------------------
 * 
 * Copyright 2009 The Nimbus2 Project. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE NIMBUS PROJECT ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
 * NO EVENT SHALL THE NIMBUS PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of the Nimbus2 Project.
 */
package jp.ossc.nimbus.service.context;

import java.util.*;

import jp.ossc.nimbus.core.*;

/**
 * デフォルトコンテキスト。<p>
 * 任意の定数値や変数値などを、キーに関連付けて格納し、提供する。<br>
 * 以下に、サービス定義例を示す。<br>
 * <pre>
 * &lt;?xml version="1.0" encoding="Shift_JIS"?&gt;
 * 
 * &lt;nimbus&gt;
 *     
 *     &lt;manager name="Sample"&gt;
 *         
 *         &lt;service name="Context"
 *                  code="jp.ossc.nimbus.service.context.DefaultContextService"&gt;
 *             &lt;attribute name="HOME_PATH"&gt;/home&lt;/attribute&gt;
 *             &lt;attribute name="DOMAIN"&gt;nimbus2.ossc.jp&lt;/attribute&gt;
 *         &lt;/service&gt;
 *         
 *     &lt;/manager&gt;
 *     
 * &lt;/nimbus&gt;
 * </pre>
 *
 * @author M.Takata
 */
public class DefaultContextService<K, V> extends ServiceBase
 implements Context<K, V>, DefaultContextServiceMBean<K, V>, java.io.Serializable{
    
    private static final long serialVersionUID = 920050666611019516L;
    
    /**
     * コンテキスト情報を格納するマップ。<p>
     */
    protected Map<K, V> context;
    
    /**
     * サービスの生成処理を行う。<p>
     * インスタンス変数を初期化する。<br>
     *
     * @exception Exception サービスの生成処理に失敗した場合
     */
    @Override
    public void createService() throws Exception{
        context = Collections.synchronizedMap(new HashMap<K, V>());
    }
    
    /**
     * サービスの停止処理を行う。<p>
     *
     * @exception Exception サービスの停止処理に失敗した場合
     */
    @Override
    public void stopService() throws Exception{
        context.clear();
    }
    
    /**
     * サービスの破棄処理を行う。<p>
     * インスタンス変数を破棄する。<br>
     *
     * @exception Exception サービスの破棄処理に失敗した場合
     */
    @Override
    public void destroyService() throws Exception{
        context = null;
    }
    
    // ContextのJavaDoc
    @Override
    public V get(Object key){
        V val = context.get(key);
        if(val == null){
            return null;
        }else if(val instanceof ServiceName){
            final ServiceName name = (ServiceName)val;
            try{
                val = ServiceManagerFactory.<V>getServiceObject(name);
            }catch(ServiceNotFoundException e){
                val = null;
            }
        }
        return val;
    }
    
    // ContextのJavaDoc
    @Override
    public V put(K key, V value) {
        return context.put(key,value);
    }
    
    // ContextのJavaDoc
    @Override
    public Set<K> keySet(){
        return context.keySet();
    }
    
    // ContextのJavaDoc
    @Override
    public Collection<V> values() {
        return context.values();
    }
    
    // ContextのJavaDoc
    @Override
    public V remove(Object key) {
        return context.remove(key);
    }
    
    // ContextのJavaDoc
    @Override
    public void clear() {
        context.clear() ;
    }
    
    // ContextのJavaDoc
    @Override
    public boolean isEmpty() {
        return context.isEmpty();
    }
    
    // ContextのJavaDoc
    @Override
    public boolean containsKey(Object key) {
        return context.containsKey(key);
    }
    
    // ContextのJavaDoc
    @Override
    public boolean containsValue(Object value) {
        return context.containsValue(value);
    }
    
    // ContextのJavaDoc
    @Override
    public Set<Map.Entry<K,V>> entrySet(){
        return context.entrySet();
    }
    
    // ContextのJavaDoc
    @Override
    public int size(){
        return context.size();
    }
    
    // ContextのJavaDoc
    @Override
    public void putAll(Map<? extends K,? extends V> t){
        context.putAll(t);
    }
    
    /**
     * コンテキスト情報の複製を保持するハッシュテーブルを生成する。<p>
     *
     * @return コンテキスト情報の複製を保持するハッシュテーブル
     */
    public Hashtable<K, V> cloneHash(){
        return new Hashtable<K, V>(context);
    }
 }
