001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Copyright (c) 2010 The JOMC Project
005     *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
006     *   All rights reserved.
007     *
008     *   Redistribution and use in source and binary forms, with or without
009     *   modification, are permitted provided that the following conditions
010     *   are met:
011     *
012     *     o Redistributions of source code must retain the above copyright
013     *       notice, this list of conditions and the following disclaimer.
014     *
015     *     o Redistributions in binary form must reproduce the above copyright
016     *       notice, this list of conditions and the following disclaimer in
017     *       the documentation and/or other materials provided with the
018     *       distribution.
019     *
020     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     *
032     *   $Id: javaURLContextFactory.java 2270 2010-06-29 13:25:34Z schulte2005 $
033     *
034     */
035    // </editor-fold>
036    // SECTION-END
037    package org.jomc.standalone.ri.naming.java;
038    
039    import java.util.Hashtable;
040    import javax.naming.CompositeName;
041    import javax.naming.Context;
042    import javax.naming.Name;
043    import javax.naming.spi.ObjectFactory;
044    import org.jomc.standalone.ri.naming.StandaloneContext;
045    
046    // SECTION-START[Documentation]
047    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
048    /**
049     * Standalone context factory for the ''java'' URL scheme.
050     * <p><b>Specifications</b><ul>
051     * <li>{@code 'javax.naming.spi.ObjectFactory'} {@code (javax.naming.spi.ObjectFactory)} {@code Multiton}</li>
052     * </ul></p>
053     *
054     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
055     * @version $Id: javaURLContextFactory.java 2270 2010-06-29 13:25:34Z schulte2005 $
056     */
057    // </editor-fold>
058    // SECTION-END
059    // SECTION-START[Annotations]
060    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
061    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
062    // </editor-fold>
063    // SECTION-END
064    public class javaURLContextFactory implements ObjectFactory
065    {
066        // SECTION-START[ObjectFactory]
067    
068        private static Context javaContext;
069    
070        public Object getObjectInstance( final Object obj, final Name name, final Context nameCtx,
071                                         final Hashtable environment ) throws Exception
072        {
073            synchronized ( javaURLContextFactory.class )
074            {
075                if ( javaContext == null )
076                {
077                    javaContext = new StandaloneContext();
078                    javaContext.getEnvironment().putAll( environment );
079                }
080    
081                if ( obj == null )
082                {
083                    return javaContext;
084                }
085                if ( obj instanceof String && ( (String) obj ).startsWith( "java:" ) )
086                {
087                    return javaContext.lookup( new CompositeName( obj.toString() ) );
088                }
089                if ( obj instanceof String[] && ( (String[]) obj ).length > 0 )
090                {
091                    final String url = ( (String[]) obj )[0];
092                    if ( url.startsWith( "java:" ) )
093                    {
094                        return javaContext.lookup( new CompositeName( ( (String[]) obj )[0] ) );
095                    }
096                }
097    
098                return null;
099            }
100        }
101    
102        // SECTION-END
103        // SECTION-START[javaURLContextFactory]
104        // SECTION-END
105        // SECTION-START[Constructors]
106        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
107    
108        /** Creates a new {@code javaURLContextFactory} instance. */
109        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
110        public javaURLContextFactory()
111        {
112            // SECTION-START[Default Constructor]
113            super();
114            // SECTION-END
115        }
116        // </editor-fold>
117        // SECTION-END
118        // SECTION-START[Dependencies]
119        // SECTION-END
120        // SECTION-START[Properties]
121        // SECTION-END
122        // SECTION-START[Messages]
123        // SECTION-END
124    }