/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., 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.weld.resources;

import ch.qos.cal10n.IMessageConveyor;
import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.manager.BeanManagerImpl;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.spi.ObjectFactory;
import java.util.Hashtable;
import java.util.Map.Entry;

import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BeanManagerMessage.CANNOT_LOCATE_BEAN_MANAGER;

public class ManagerObjectFactory implements ObjectFactory {
    private final String contextId;

    public ManagerObjectFactory(String contextId) {
        this.contextId = contextId;
    }

    // Exception messages
    private static final IMessageConveyor messageConveyor = loggerFactory().getMessageConveyor();

    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
        if (Container.available(contextId)) {
            for (Entry<BeanDeploymentArchive, BeanManagerImpl> entry : Container.instance(contextId).beanDeploymentArchives().entrySet()) {
                if (entry.getKey().getId().equals("flat")) {
                    return entry.getValue().getCurrent();
                }
            }
            throw new NamingException(messageConveyor.getMessage(CANNOT_LOCATE_BEAN_MANAGER));
        } else {
            throw new NamingException(messageConveyor.getMessage(CANNOT_LOCATE_BEAN_MANAGER));
        }
    }

}

@@ -815,71 +815,65 @@
 ort 
-ch.qos.cal10n.IMessageConveyor;%0Aimport org.jboss.weld.Container
+static org.jboss.weld.logging.LoggerFactory.loggerFactory
 ;%0Aim
@@ -869,32 +869,39 @@
 Factory;%0Aimport 
+static 
 org.jboss.weld.b
@@ -903,90 +903,127 @@
 eld.
-bootstrap.spi.BeanDeploymentArchive;%0Aimport org.jboss.weld.manager.BeanManagerImpl
+logging.messages.BeanManagerMessage.CANNOT_LOCATE_BEAN_MANAGER;%0A%0Aimport java.util.Hashtable;%0Aimport java.util.Map.Entry
 ;%0A%0Ai
@@ -1156,72 +1156,120 @@
 ry;%0A
+%0A
 import 
-java.util.Hashtable;%0Aimport java.util.Map.Entry;%0A
+org.jboss.weld.Container;%0Aimport org.jboss.weld.bootstrap.api.helpers.RegistrySingletonProvider;
 %0Aimport 
 stat
@@ -1256,39 +1256,32 @@
 rovider;%0Aimport 
-static 
 org.jboss.weld.l
@@ -1283,43 +1283,43 @@
 eld.
-logging.LoggerFactory.loggerFactory
+bootstrap.spi.BeanDeploymentArchive
 ;%0Aim
@@ -1315,39 +1315,32 @@
 Archive;%0Aimport 
-static 
 org.jboss.weld.l
@@ -1338,32 +1338,23 @@
 ss.weld.
-logging.messages
+manager
 .BeanMan
@@ -1361,42 +1361,52 @@
 ager
-Message.CANNOT_LOCATE_BEAN_MANAGER
+Impl;%0A%0Aimport ch.qos.cal10n.IMessageConveyor
 ;%0A%0Ap
@@ -1502,16 +1502,116 @@
 extId;%0A%0A
+    public ManagerObjectFactory() %7B%0A        this(RegistrySingletonProvider.STATIC_INSTANCE);%0A    %7D%0A%0A
     publ

Fix failing servlet tests