/*
 * Copyright(c) 2016-2017 IBM, Red Hat, and others.
 *
 * Licensed 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 io.microprofile.showcase.bootstrap;

import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

/**
 * @author Heiko Braun
 * @since 15/09/16
 */
@ApplicationScoped
public class BootstrapDataProducer {

    @Produces
    public BootstrapData load() {
        final URL schedule = Thread.currentThread().getContextClassLoader().getResource("schedule.json");
        assert schedule !=null : "Failed to load 'schedule.json'";

        final URL speaker = Thread.currentThread().getContextClassLoader().getResource("speaker.json");
        assert speaker !=null : "Failed to load 'speaker.json'";

        final Parser parser = new Parser();
        final BootstrapData data = parser.parse(schedule, speaker);

        Logger.getLogger(BootstrapData.class.getName()).log(Level.INFO, "Schedule contains "+data.getSessions().size() + " sessions");

        return data;
    }

}


@@ -667,15 +667,73 @@
 ava.
-net.URL
+io.IOException;%0Aimport java.net.URL;%0Aimport java.util.Enumeration
 ;%0Aim
@@ -1046,36 +1046,55 @@
 d() 
-%7B%0A        final URL schedule
+throws IOException %7B%0A        ClassLoader loader
  = T
@@ -1130,32 +1130,63 @@
 extClassLoader()
+;%0A        URL schedule = loader
 .getResource(%22sc
@@ -1208,23 +1208,138 @@
         
-assert 
+if (schedule == null) %7B%0A            schedule = BootstrapDataProducer.class.getResource(%22/schedule.json%22);%0A            if (
 schedule
@@ -1339,25 +1339,75 @@
 chedule 
-!
 =
+= 
 null
- : 
+) %7B%0A                throw new IllegalStateException(
 %22Failed 
@@ -1434,85 +1434,174 @@
 on'%22
+)
 ;%0A
-%0A        final URL speaker = Thread.currentThread().getContextClassLoader()
+            %7D%0A        %7D%0A%0A        URL speaker = loader.getResource(%22speaker.json%22);%0A        if (speaker == null) %7B%0A            speaker = BootstrapDataProducer.class
 .get
@@ -1602,32 +1602,33 @@
 ss.getResource(%22
+/
 speaker.json%22);%0A
@@ -1639,23 +1639,24 @@
     
-assert 
+    if (
 speaker 
 !=nu
@@ -1655,17 +1655,67 @@
 ker 
-!
 =
+= 
 null
- : 
+) %7B%0A                throw new IllegalStateException(
 %22Fai
@@ -1741,17 +1741,42 @@
 r.json'%22
-;
+);%0A            %7D%0A        %7D
 %0A%0A      

Make the loading of the app data more tolerant to the class loading by falling back to the BootstrapDataProducer.class.getResource(String) method if TCCL fails.