    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.io.Serializable;
    013    import java.lang.reflect.Type;
    014
    015    import javax.naming.NamingException;
    016
    017    import org.picocontainer.ComponentAdapter;
    018    import org.picocontainer.PicoCompositionException;
    019    import org.picocontainer.PicoContainer;
    020    import org.picocontainer.PicoVisitor;
    021
    022    /**
    023     * represents dependency provided via JNDI. This dependency is not
    024     * to be managed by container at all, so there is no lifecycle, no
    025     * monitoring etc.
    026     * @author Konstantin Pribluda
    027     *
    028     */
    029    @SuppressWarnings("serial")
    030    public class JNDIProvided<T> implements ComponentAdapter<T> , Serializable {
    031
    032
    033            JNDIObjectReference<T> jndiReference;
    034
    035             Object componentKey;
    036
    037            /**
    038             * create adapter with specified key and reference
    039             * @param componentKey component key
    040             * @param reference JNDI reference storing component
    041             */
    042            public JNDIProvided(final Object componentKey,final JNDIObjectReference<T> reference) {
    043                    this.componentKey = componentKey;
    044                    this.jndiReference = reference;
    045            }
    046
    047            /**
    048             * create adapter with JNDI reference. referenced object class will be
    049             * takes as key
    050             * @param reference JNDI reference storing component
    051             */
    052            public JNDIProvided(final JNDIObjectReference<T> reference) {
    053                    this(reference.get().getClass(),reference);
    054            }
    055
    056            /**
    057             * create adapter based on JNDI name. I leave this unchecked because
    058             * type is really not known at this time
    059             * @param jndiName name to be used
    060             * @throws NamingException will be thrown if something goes
    061             * wrong in JNDI
    062             */
    063            @SuppressWarnings("unchecked")
    064            public JNDIProvided(final String jndiName) throws NamingException {
    065                    this(new JNDIObjectReference(jndiName));
    066            }
    067
    068            public Object getComponentKey() {
    069                    return componentKey;
    070            }
    071
    072            @SuppressWarnings("unchecked")
    073            public Class getComponentImplementation() {
    074                    return jndiReference.get().getClass();
    075            }
    076
    077        public T getComponentInstance(final PicoContainer container) throws PicoCompositionException {
    078            return getComponentInstance(container, null);
    079        }
    080
    081        /**
    082             * retrieve instance out of JNDI
    083             */
    084            public T getComponentInstance(final PicoContainer container, final Type into)
    085                            throws PicoCompositionException {
    086                    return  jndiReference.get();
    087            }
    088
    089            /**
    090             * we have nothing to verify here
    091             */
    092            public void verify(final PicoContainer container) throws PicoCompositionException {
    093            }
    094
    095            /**
    096             * as there is no puprose of proceeding further down,
    097             * we do nothing here
    098             */
    099            public void accept(final PicoVisitor visitor) {
    100            }
    101
    102        public ComponentAdapter<T> getDelegate() {
    103            return null;
    104        }
    105
    106        public <U extends ComponentAdapter> U findAdapterOfType(final Class<U> adapterType) {
    107            return null;
    108        }
    109
    110        public String getDescriptor() {
    111            return "JNDI(" + jndiReference.getName() + ")";
    112        }
    113
    114    }