/**
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.jboss.webbeans.environment.se.bootstrap;

import org.jboss.webbeans.bootstrap.PropertiesBasedBootstrap;
import org.jboss.webbeans.bootstrap.SimpleResourceLoader;
import org.jboss.webbeans.bootstrap.spi.EjbDiscovery;
import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
import org.jboss.webbeans.ejb.spi.EjbResolver;
import org.jboss.webbeans.environment.se.discovery.WebBeanDiscoveryImpl;
import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceLoader;
import org.jboss.webbeans.util.DeploymentProperties;

import java.util.HashSet;
import javax.transaction.Transaction;
import org.jboss.webbeans.bean.standard.InjectionPointBean;
import org.jboss.webbeans.bean.standard.ManagerBean;
import org.jboss.webbeans.bootstrap.BeanDeployer;

/**
 *
 * @author Peter Royle
 */
public class WebBeansBootstrap
        extends PropertiesBasedBootstrap
{

    WebBeanDiscovery wbDisc = new WebBeanDiscoveryImpl();
    ResourceLoader resourceLoader = new MockResourceLoader();
    NamingContext naming = new MockNaming();
    EjbResolver ejbResolver = new MockEjbResolver();
    EjbDiscovery ejbDiscovery = new MockEjbDiscovery( new HashSet<Class<?>>() );

    // The deployment properties
    private final DeploymentProperties deploymentProperties;

    public WebBeansBootstrap()
    {
        super();

        // Create a simpple resource loader based for initial loading
        ResourceLoader temporaryResourceLoader = new SimpleResourceLoader();
        this.deploymentProperties = new DeploymentProperties( temporaryResourceLoader );

        initManager( naming, ejbResolver, resourceLoader );
    }

    @Override
    public ResourceLoader getResourceLoader()
    {
        return resourceLoader;
    }

    @Override
    protected WebBeanDiscovery getWebBeanDiscovery()
    {
        return wbDisc;
    }

    @Override
    protected EjbDiscovery getEjbDiscovery()
    {
        return ejbDiscovery;
    }

    @Override
    protected DeploymentProperties getDeploymentProperties()
    {
        return this.deploymentProperties;
    }

    @Override
    protected void registerBeans( Iterable<Class<?>> classes )
    {
        BeanDeployer beanDeployer = new BeanDeployer( getManager() );
        beanDeployer.addClasses( classes );
        beanDeployer.addBean( ManagerBean.of( getManager() ) );
        beanDeployer.addBean( InjectionPointBean.of( getManager() ) );
        beanDeployer.addClass( Transaction.class );
        beanDeployer.deploy();
    }
}
