//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified 
// and included code are below.
//
// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
//
// Modifications:
//
// 2003 Jan 31: Cleaned up some unused imports.
//
// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.                                                            
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//       
// For more information contact: 
//      OpenNMS Licensing       <license@opennms.org>
//      http://www.opennms.org/
//      http://www.opennms.com/
//
// Tab Size = 8
//

package org.opennms.netmgt.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.opennms.netmgt.ConfigFileConstants;
import org.opennms.netmgt.config.surveillanceViews.SurveillanceViewConfiguration;
import org.opennms.netmgt.config.surveillanceViews.View;

public class SurveillanceViewsFactory {
    /** The singleton instance. */
    private static SurveillanceViewsFactory m_instance;

    private static boolean m_loadedFromFile = false;

    /** Boolean indicating if the init() method has been called. */
    protected boolean initialized = false;

    /** Timestamp of the viewDisplay file, used to know when to reload from disk. */
    protected static long m_lastModified;

    /** Map of view objects by name. */
    protected static Map<String,View> m_viewsMap;

    private static SurveillanceViewConfiguration m_config;

    public SurveillanceViewsFactory(String configFile) throws MarshalException, ValidationException, IOException {
        
        InputStreamReader rdr = new InputStreamReader(new FileInputStream(configFile));
        
        try {
            initialize(rdr);
        } finally {
            rdr.close();
        }
        
    }

    public SurveillanceViewsFactory(Reader rdr) throws MarshalException, ValidationException {
        initialize(rdr);
    }

    private void initialize(Reader rdr) throws MarshalException, ValidationException {        
        m_config = (SurveillanceViewConfiguration) Unmarshaller.unmarshal(SurveillanceViewConfiguration.class, rdr);

        m_viewsMap = new HashMap<String, View>();
        Collection viewList = m_config.getViews().getViewCollection();
        Iterator i = viewList.iterator();
        while (i.hasNext()) {
            View view = (View) i.next();
            m_viewsMap.put(view.getName(), view);
        }
    }

    /** Be sure to call this method before calling getInstance(). */
    public static synchronized void init() throws IOException, FileNotFoundException, MarshalException, ValidationException {
        if (m_instance == null) {
            File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.SURVEILLANCE_VIEWS_FILE_NAME);
            m_instance = new SurveillanceViewsFactory(cfgFile.getPath());
            m_lastModified = cfgFile.lastModified();
            m_loadedFromFile = true;

        }
    }

    /**
     * Singleton static call to get the only instance that should exist for the
     * ViewsDisplayFactory
     * 
     * @return the single views display factory instance
     * @throws IllegalStateException
     *             if init has not been called
     */
    public static synchronized SurveillanceViewsFactory getInstance() {
        if (m_instance == null) {
            throw new IllegalStateException("You must call ViewDisplay.init() before calling getInstance().");
        }

        return m_instance;
    }
    
    public static synchronized void setInstance(SurveillanceViewsFactory instance) {
        m_instance = instance;
        m_loadedFromFile = false;
    }

    public synchronized void reload() throws IOException, FileNotFoundException, MarshalException, ValidationException {
        m_instance = null;
        init();
    }

    /** Can't be null */
    public View getView(String viewName) throws IOException, MarshalException, ValidationException {
        if (viewName == null) {
            throw new IllegalArgumentException("Cannot take null parameters.");
        }

        this.updateFromFile();

        View view = (View) m_viewsMap.get(viewName);

        return view;
    }

    /**
     * Reload the viewsdisplay.xml file if it has been changed since we last
     * read it.
     */
    protected void updateFromFile() throws IOException, MarshalException, ValidationException {
        if (m_loadedFromFile) {
            File surveillanceViewsFile = ConfigFileConstants.getFile(ConfigFileConstants.SURVEILLANCE_VIEWS_FILE_NAME);
            if (m_lastModified != surveillanceViewsFile.lastModified()) {
                this.reload();
            }
        }
    }

    public synchronized static SurveillanceViewConfiguration getConfig() {
        return m_config;
    }

    public synchronized static void setConfig(SurveillanceViewConfiguration m_config) {
        SurveillanceViewsFactory.m_config = m_config;
    }

    public synchronized static Map<String, View> getViewsMap() {
        return m_viewsMap;
    }

    public synchronized static void setViewsMap(Map<String, View> map) {
        m_viewsMap = map;
    }
}

@@ -1859,16 +1859,50 @@
 l.Map;%0A%0A
+import org.apache.log4j.Category;%0A
 import o
@@ -2025,32 +2025,78 @@
 ationException;%0A
+import org.opennms.core.utils.ThreadCategory;%0A
 import org.openn
@@ -3391,24 +3391,25 @@
 eption %7B
+%0A
         
 %0A       
@@ -3392,32 +3392,100 @@
 ption %7B%0A        
+log().debug(%22initialize: initializing surveillance views factory.%22);
 %0A        m_confi
@@ -6587,10 +6587,95 @@
 ;%0A    %7D%0A
+    %0A    private Category log() %7B%0A        return ThreadCategory.getInstance();%0A    %7D%0A
 %7D%0A

update example surveillance-views.xml working on NodeDaoHibernate bugs (in progress) add log to surveillance views factory