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    package org.picocontainer.gems.jndi;
010    
011    import javax.naming.InitialContext;
012    import javax.naming.NamingException;
013    
014    import org.picocontainer.ComponentAdapter;
015    import org.picocontainer.behaviors.Stored;
016    
017    /**
018     * exposes component to JNDI basically does same thing as cached, but uses JNDI
019     * reference instead. Maybe Cached shall be refactored? as there is little new
020     * functionality.
021     * 
022     * @author Konstantin Pribluda
023     * 
024     */
025    @SuppressWarnings("serial")
026    public class JNDIExposed<T> extends Stored<T> {
027    
028    
029            /**
030             * construct reference itself using vanilla initial context.
031             * JNDI name is stringified component key
032             * @param delegate
033             *            delegate adapter
034    
035             * @throws NamingException
036             */
037            public JNDIExposed(final ComponentAdapter<T> delegate) throws NamingException {
038                    super(delegate, new JNDIObjectReference<Stored.Instance<T>>(delegate.getComponentKey()
039                                    .toString(), new InitialContext()));
040            }
041    
042            /**
043             * create with provided reference
044             * 
045             * @param delegate
046             * @param instanceReference
047             */
048            public JNDIExposed(final ComponentAdapter<T> delegate,
049                            final JNDIObjectReference<Stored.Instance<T>> instanceReference) {
050                    super(delegate, instanceReference);
051            }
052    
053            /**
054             * create adapter with desired name
055             * @param delegate
056             * @param name
057             * @throws NamingException
058             */
059            public JNDIExposed(final ComponentAdapter<T> delegate, final String name) throws NamingException {
060                    super(delegate, new JNDIObjectReference<Stored.Instance<T>>(name, new InitialContext()));
061            }
062            
063            @Override
064            public String toString() {
065                    return "JNDI" + getDelegate().toString();
066            }
067    }