//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2006 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.
//
// 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.dao.jdbc;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.opennms.netmgt.dao.jdbc.alarm.AlarmFactory;
import org.opennms.netmgt.dao.jdbc.asset.AssetRecordFactory;
import org.opennms.netmgt.dao.jdbc.category.CategoryFactory;
import org.opennms.netmgt.dao.jdbc.distpoller.DistPollerFactory;
import org.opennms.netmgt.dao.jdbc.event.EventFactory;
import org.opennms.netmgt.dao.jdbc.ipif.IpInterfaceFactory;
import org.opennms.netmgt.dao.jdbc.monsvc.MonitoredServiceFactory;
import org.opennms.netmgt.dao.jdbc.node.NodeFactory;
import org.opennms.netmgt.dao.jdbc.notification.NotificationFactory;
import org.opennms.netmgt.dao.jdbc.snmpif.SnmpInterfaceFactory;
import org.opennms.netmgt.dao.jdbc.svctype.ServiceTypeFactory;
import org.opennms.netmgt.dao.jdbc.usernotification.UserNotificationFactory;

public class Cache {
	
    private static final class CacheMap extends ThreadLocal {
        protected Object initialValue() {
            return new HashMap();
        }
        
        public Map getMap() {
            return (Map)get();
        }
        
        public void put(Object key, Object val) {
            getMap().put(key, val);
        }
        
        public Object get(Object key) {
            return getMap().get(key);
        }
        
        public Object remove(Object key) {
            return getMap().remove(key);
        }
        
        public void clear() {
            getMap().clear();
        }
    }

    private static CacheMap s_cacheCache = new CacheMap();
    private static Map s_factories = new HashMap();
    
    static private Map getCache(Class clazz) {
        Map cache = (Map)s_cacheCache.get(clazz);
        if (cache == null) {
            cache = new HashMap();
            s_cacheCache.put(clazz, cache);
        }
        return cache;
    }
    
    static public void clear() {
        s_cacheCache.clear();
    }
    
    public static void registerFactory(Class clazz, Factory factory) {
    	s_factories.put(clazz, factory);
    }
    
    public static Factory findFactory(Class clazz) {
    	return (Factory)s_factories.get(clazz);
    }
    
    public static Object retrieve(Class clazz, Object key) {
    	return getCache(clazz).get(key);
    }
    
    public static void store(Class clazz, Object key, Object cached) {
    	getCache(clazz).put(key, cached);
    }
    
    public static Object obtain(Class clazz, Object key) {
    	return findFactory(clazz).get(clazz, key);
    }

	public static void registerFactories(DataSource dataSource) {
		AssetRecordFactory.register(dataSource);
		CategoryFactory.register(dataSource);
		DistPollerFactory.register(dataSource);
		EventFactory.register(dataSource);
		IpInterfaceFactory.register(dataSource);
		MonitoredServiceFactory.register(dataSource);
		NodeFactory.register(dataSource);
		ServiceTypeFactory.register(dataSource);
		SnmpInterfaceFactory.register(dataSource);
		EventFactory.register(dataSource);
		AlarmFactory.register(dataSource);
		NotificationFactory.register(dataSource);
		UserNotificationFactory.register(dataSource);
		
	}
    
}

@@ -3496,22 +3496,33 @@
  %7B%0A    %09
-return
+Factory factory =
  (Factor
@@ -3543,24 +3543,225 @@
 get(clazz);%0A
+    %09if (factory == null) %7B%0A    %09%09throw new IllegalArgumentException(%22Unable to find factory for class %22+clazz+%22 did you remember to register it in Cache.registerFactories?%22);%0A    %09%7D%0A%09%09return factory;%0A
     %7D%0A    %0A 

make the exception thrown when we failed to register more clear