/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2005 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:
 *
 * 2007 Jul 31: Format. - dj@opennms.org
 * 2007 Jul 31: Use CastorUtils.unmarshal, Java 5 for loops, and don't set
 *              m_commands until we've finished populating the hash. - dj@opennms.org
 *
 * 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/
 */
package org.opennms.netmgt.config;

import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.opennms.netmgt.config.notificationCommands.Command;
import org.opennms.netmgt.config.notificationCommands.NotificationCommands;
import org.opennms.netmgt.dao.castor.CastorUtils;

/**
 * @author david hustace <david@opennms.org>
 */

public abstract class NotificationCommandManager {
    /**
     * List of all configuration notification commands.  parseXml must be called to populate this.
     */
    private Map<String, Command> m_commands;

    /**
     * 
     */
    protected InputStream configIn;

    /**
     * Populate the internal list of notification commands from an XML file.
     *  
     * @param reader contains the XML file to be parsed
     * @throws MarshalException
     * @throws ValidationException
     */
    protected void parseXML(Reader reader) throws MarshalException, ValidationException {
        NotificationCommands config = CastorUtils.unmarshal(NotificationCommands.class, reader);

        Map<String, Command> commands = new HashMap<String, Command>();
        for (Command curCommand : getCommandsFromConfig(config)) {
            commands.put(curCommand.getName(), curCommand);
        }

        m_commands = commands;
    }

    @SuppressWarnings("unchecked")
    private List<Command> getCommandsFromConfig(NotificationCommands config) {
        return config.getCommandCollection();
    }

    /**
     * Gets a notification command for a particular command name.
     */
    public Command getCommand(String name) {
        return m_commands.get(name);
    }

    /**
     * Gets all configured notification commands.
     */
    public Map<String, Command> getCommands() {
        return m_commands;
    }
}

@@ -1692,16 +1692,46 @@
 Reader;%0A
+import java.util.Collections;%0A
 import j
@@ -1794,16 +1794,50 @@
 l.Map;%0A%0A
+import org.apache.log4j.Category;%0A
 import o
@@ -1917,32 +1917,78 @@
 ationException;%0A
+import org.opennms.core.utils.ThreadCategory;%0A
 import org.openn
@@ -2180,15 +2180,15 @@
 hor 
-d
+D
 avid 
-h
+H
 usta
@@ -2845,58 +2845,15 @@
 g = 
-CastorUtils.unmarshal(NotificationCommands.class, 
+getXML(
 read
@@ -3013,262 +3013,898 @@
     
-commands.put(curCommand.getName(), curCommand);%0A        %7D%0A%0A        m_commands = commands;%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    private List%3CCommand%3E getCommandsFromConfig(NotificationCommands config) %7B%0A        return config.getCommandCollection(
+if (curCommand != null && curCommand.getName() != null) %7B%0A                commands.put(curCommand.getName(), curCommand);%0A            %7D else %7B%0A                log().warn(%22invalid notification command: %22 + curCommand);%0A            %7D%0A        %7D%0A%0A        m_commands = commands;%0A    %7D%0A%0A    @SuppressWarnings(%22deprecation%22)%0A    private NotificationCommands getXML(Reader reader)%0A            throws MarshalException, ValidationException %7B%0A        return CastorUtils.unmarshal(NotificationCommands.class, reader);%0A    %7D%0A%0A    private List%3CCommand%3E getCommandsFromConfig(NotificationCommands config) %7B%0A        if (config == null) %7B%0A            log().warn(%22no notification commands found%22);%0A            return Collections.emptyList();%0A        %7D%0A        return config.getCommandCollection();%0A    %7D%0A%0A    private Category log() %7B%0A        return ThreadCategory.getInstance(NotificationCommandManager.class
 );%0A 

refactor notification command manager to avoid NPEs, bug #1526