/*
 * Copyright 2010-2012 Ning, Inc.
 * Copyright 2014-2017 Groupon, Inc
 * Copyright 2014-2017 The Billing Project, LLC
 *
 * The Billing Project licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.util.glue;

import java.net.URISyntaxException;
import java.util.Set;

import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import javax.inject.Inject;
import javax.inject.Provider;

import org.killbill.billing.util.cache.BaseCacheLoader;
import org.killbill.billing.util.config.definition.EhCacheConfig;

import com.codahale.metrics.MetricRegistry;

// EhCache specific provider
public class Eh107CacheManagerProvider extends CacheProviderBase implements Provider<CacheManager> {

    private final Set<BaseCacheLoader> cacheLoaders;

    @Inject
    public Eh107CacheManagerProvider(final MetricRegistry metricRegistry,
                                     final EhCacheConfig cacheConfig,
                                     final Set<BaseCacheLoader> cacheLoaders) {
        super(metricRegistry, cacheConfig);
        this.cacheLoaders = cacheLoaders;
    }

    @Override
    public CacheManager get() {
        // JSR-107 registration, required for JMX integration
        final CachingProvider cachingProvider = Caching.getCachingProvider();

        final CacheManager cacheManager;
        try {
            cacheManager = cachingProvider.getCacheManager(xmlConfigurationURL.toURI(), getClass().getClassLoader());
        } catch (final URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }

        for (final BaseCacheLoader<?, ?> cacheLoader : cacheLoaders) {
            createCache(cacheManager,
                        cacheLoader.getCacheType().getCacheName(),
                        cacheLoader.getCacheType().getKeyType(),
                        cacheLoader.getCacheType().getValueType());
        }

        return cacheManager;
    }
}

@@ -1097,16 +1097,73 @@
 eConfig;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1332,16 +1332,116 @@
 ger%3E %7B%0A%0A
+    private static final Logger logger = LoggerFactory.getLogger(Eh107CacheManagerProvider.class);%0A%0A
     priv
@@ -2006,30 +2006,24 @@
 );%0A%0A        
-final 
 CacheManager
@@ -2192,25 +2192,23 @@
  (final 
-URISyntax
+Runtime
 Exceptio
@@ -2230,44 +2230,340 @@
     
-throw new IllegalArgumentException(e
+logger.error(%22Unable to read ehcache.xml, using default configuration%22, e);%0A            cacheManager = cachingProvider.getCacheManager();%0A        %7D catch (final URISyntaxException e) %7B%0A            logger.error(%22Unable to read ehcache.xml, using default configuration%22, e);%0A            cacheManager = cachingProvider.getCacheManager(
 );%0A 

handle gracefully errors in Eh107CacheManagerProvider