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