/*
 * Copyright 2011 Red Hat inc. and third party contributors as noted
 * by the author tags.
 * 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 com.redhat.ceylon.cmr.api;

import com.redhat.ceylon.cmr.spi.ContentTransformer;
import com.redhat.ceylon.cmr.spi.MergeStrategy;

import java.io.File;
import java.lang.reflect.Constructor;

/**
 * RepositoryManager builder API.
 *
 * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
 */
public class RepositoryManagerBuilder {
    private final static String DEFAULT_DELEGATE = "com.redhat.ceylon.cmr.impl.RepositoryManagerBuilderImpl";
    private RepositoryManagerBuilder delegate;

    protected RepositoryManagerBuilder() {
    }

    public RepositoryManagerBuilder(Logger log) {
        try {
            Class<?> clazz = getClass().getClassLoader().loadClass(DEFAULT_DELEGATE);
            Constructor ctor = clazz.getConstructor(Logger.class);
            delegate = (RepositoryManagerBuilder) ctor.newInstance(log);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    public RepositoryManagerBuilder(File mainRepository, Logger log) {
        try {
            Class<?> clazz = getClass().getClassLoader().loadClass(DEFAULT_DELEGATE);
            Constructor ctor = clazz.getConstructor(File.class, Logger.class);
            delegate = (RepositoryManagerBuilder) ctor.newInstance(mainRepository, log);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    protected RepositoryManagerBuilder getDelegate() {
        if (delegate == null)
            throw new IllegalArgumentException("Missing method impl / override!");
        return delegate;
    }

    public RepositoryBuilder repositoryBuilder() {
        return getDelegate().repositoryBuilder();
    }

    public RepositoryManagerBuilder mergeStrategy(MergeStrategy strategy) {
        getDelegate().mergeStrategy(strategy);
        return this;
    }

    public RepositoryManagerBuilder contentTransformer(ContentTransformer transformer) {
        getDelegate().contentTransformer(transformer);
        return this;
    }

    public RepositoryManagerBuilder cacheContent() {
        getDelegate().cacheContent();
        return this;
    }

    /**
     * It prepends ${ceylon.home} directory to roots, if it exists.
     *
     * @return this
     */
    public RepositoryManagerBuilder addCeylonHome() {
        getDelegate().addCeylonHome();
        return this;
    }

    /**
     * It prepends ./modules directory to roots, if it exists.
     *
     * @return this
     */
    public RepositoryManagerBuilder addModules() {
        getDelegate().addModules();
        return this;
    }

    /**
     * It appends http://modules.ceylon-lang.org to roots, if it exists.
     *
     * @return this
     */
    public RepositoryManagerBuilder addModulesCeylonLangOrg() {
        getDelegate().addModulesCeylonLangOrg();
        return this;
    }

    public RepositoryManagerBuilder prependRepository(Repository external) {
        getDelegate().prependRepository(external);
        return this;
    }

    public RepositoryManagerBuilder appendRepository(Repository external) {
        getDelegate().appendRepository(external);
        return this;
    }

    public RepositoryManager buildRepository() {
        return getDelegate().buildRepository();
    }
}

@@ -1202,37 +1202,56 @@
 ) %7B%0A    %7D%0A%0A    p
-ublic
+rotected Class%3C? extends
  RepositoryManag
@@ -1263,36 +1263,143 @@
 lder
-(Logger log) %7B%0A        try %7B
+%3E getDelegateClass() throws Exception %7B%0A        ClassLoader classLoader = getClass().getClassLoader();%0A        if (classLoader == null)
 %0A   
@@ -1411,118 +1411,348 @@
     
-C
+c
 lass
-%3C?%3E clazz = getClass().getClassLoader().loadClass(DEFAULT_DELEGATE);%0A            Constructor ctor = clazz
+Loader = ClassLoader.getSystemClassLoader();%0A%0A        //noinspection unchecked%0A        return (Class%3C? extends RepositoryManagerBuilder%3E) classLoader.loadClass(DEFAULT_DELEGATE);%0A    %7D%0A%0A    public RepositoryManagerBuilder(Logger log) %7B%0A        try %7B%0A            Constructor%3C? extends RepositoryManagerBuilder%3E ctor = getDelegateClass()
 .get
@@ -1804,35 +1804,8 @@
 te =
- (RepositoryManagerBuilder)
  cto
@@ -2026,117 +2026,80 @@
    C
-lass%3C?%3E clazz = getClass().getClassLoader().loadClass(DEFAULT_DELEGATE);%0A            Constructor ctor = clazz
+onstructor%3C? extends RepositoryManagerBuilder%3E ctor = getDelegateClass()
 .get
@@ -2163,35 +2163,8 @@
 te =
- (RepositoryManagerBuilder)
  cto

Use sys CL, if class' is null.