    001    /*****************************************************************************
    002     * Copyright (c) PicoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     *****************************************************************************/
    009
    010    package org.picocontainer.gems.jndi;
    011
    012    import java.util.Set;
    013
    014    import javax.management.MBeanServer;
    015
    016    import org.picocontainer.ComponentAdapter;
    017    import org.picocontainer.PicoContainer;
    018    import org.picocontainer.visitors.TraversalCheckingVisitor;
    019
    020    /**
    021     * traverse pico container and expose components to JNDI on
    022     * sight of JNDIExposed
    023     * @author k.pribluda
    024     */
    025    public class JNDIContainerVisitor extends TraversalCheckingVisitor {
    026
    027            private PicoContainer container;
    028
    029            /**
    030             * in case component adapter is JNDIExposed, poke it gently and
    031             * it will create component and register it to JNDI if not already
    032             * done.
    033             */
    034            @Override
    035            public void visitComponentAdapter(final ComponentAdapter componentAdapter)
    036            {
    037                    super.visitComponentAdapter(componentAdapter);
    038
    039                    if(componentAdapter instanceof JNDIExposed) {
    040                            componentAdapter.getComponentInstance(container,null);
    041                    }
    042
    043            }
    044
    045            /**
    046         * Provides the PicoContainer, that can resolve the components to register as MBean.
    047         * @see org.picocontainer.PicoVisitor#visitContainer(org.picocontainer.PicoContainer)
    048         */
    049        @Override
    050            public boolean visitContainer(final PicoContainer pico) {
    051            super.visitContainer(pico);
    052            container = pico;
    053            return CONTINUE_TRAVERSAL;
    054        }
    055
    056        /**
    057         * Entry point for the visitor traversal.
    058         * @return Returns a {@link Set} with all ObjectInstance instances retrieved from the {@link MBeanServer} for the
    059         *         registered MBeans.
    060         * @see org.picocontainer.visitors.AbstractPicoVisitor#traverse(java.lang.Object)
    061         */
    062        @Override
    063            public Object traverse(final Object node) {
    064            super.traverse(node);
    065            container = null;
    066            return null;
    067        }
    068
    069    }