001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.common.control;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.Properties;
022import javax.naming.Context;
023import javax.naming.InitialContext;
024import javax.naming.NamingException;
025
026public class InitialContextUtils {
027    
028    private InitialContextUtils() {
029        super();
030    }
031    
032    private static class InitialContextUtilsHolder {
033        static InitialContextUtils instance = new InitialContextUtils();
034    }
035    
036    public static InitialContextUtils getInstance() {
037        return InitialContextUtilsHolder.instance;
038    }
039
040    public InitialContext getInitialContext()
041            throws NamingException {
042        Properties env = new Properties();
043        InputStream is = InitialContextUtils.class.getClassLoader().getResourceAsStream("echothree-jndi.properties");
044        boolean loaded = false;
045
046        if(is != null) {
047            try {
048                env.load(is);
049                loaded = true;
050            } catch (IOException ioe) {
051                // Fall through, leave loaded = false.
052            }
053        }
054
055        if(!loaded) {
056            env.put(Context.PROVIDER_URL, "remote+http://127.0.0.1:8080");
057        }
058
059        env.put(Context.INITIAL_CONTEXT_FACTORY, org.wildfly.naming.client.WildFlyInitialContextFactory.class.getName());
060
061        return new InitialContext(env);
062    }
063
064}