/*
 * Copyright 2016 Red Hat, Inc. and/or its affiliates.
 *
 * 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 org.optaplanner.core.config;

import org.kie.api.runtime.KieContainer;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.config.solver.SolverConfig;

/**
 * Provided to a {@link SolverConfig#buildSolver(SolverConfigContext)}
 * to inject a reference to containers, .
 */
public class SolverConfigContext {

    private ClassLoader classLoader;
    private KieContainer kieContainer;

    public SolverConfigContext() {
    }

    public SolverConfigContext(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public SolverConfigContext(KieContainer kieContainer) {
        this.kieContainer = kieContainer;
    }

    /**
     * @return sometimes null, the {@link ClassLoader} to use for loading all resources and {@link Class}es,
     *      null to use the default {@link ClassLoader}
     */
    public ClassLoader getClassLoader() {
        return classLoader;
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public KieContainer getKieContainer() {
        return kieContainer;
    }

    public void setKieContainer(KieContainer kieContainer) {
        this.kieContainer = kieContainer;
    }

    // ************************************************************************
    // Complex methods
    // ************************************************************************

    public ClassLoader determineActualClassLoader() {
        if (classLoader != null) {
            return classLoader;
        } else if (kieContainer != null) {
            return kieContainer.getClassLoader();
        }
        return getClass().getClassLoader();
    }

    public void validate() {
        if (classLoader != null && kieContainer != null) {
            throw new IllegalStateException("The classLoader (" + classLoader + ") and kieContainer (" + kieContainer
                    + ") cannot both be configured because the " + KieContainer.class.getSimpleName()
                    + " already has a " + ClassLoader.class.getSimpleName() + ".");
        }
    }

}

@@ -1303,16 +1303,206 @@
     /**%0A
+     * Used to work with a %7B@link ClassLoader%7D provided by OSGi, JBoss modules, etc, but not %7B@link KieContainer%7D.%0A     * %3Cp/%3E%0A     * Must be null if %7B@link #getKieContainer()%7D is not null.%0A
      * @
@@ -1618,19 +1618,73 @@
     
-null to use
+if this is null and %7B@link #getKieContainer()%7D is also null, then
  the
@@ -1711,16 +1711,24 @@
 sLoader%7D
+ is used
 %0A     */
@@ -1902,32 +1902,226 @@
 sLoader;%0A    %7D%0A%0A
+    /**%0A     * Must be null if %7B@link #getClassLoader()%7D is not null.%0A     * @return sometimes null, affects the %7B@link ClassLoader%7D to use for loading all resources and %7B@link Class%7Des%0A     */%0A
     public KieCo

improve SolverConfigContext javadocs (thanks to Edson's feedback)