001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.util.Properties;
021import javax.naming.Context;
022import javax.naming.InitialContext;
023import javax.naming.NamingException;
024
025public class InitialContextUtils {
026    
027    private InitialContextUtils() {
028        super();
029    }
030    
031    private static class InitialContextUtilsHolder {
032        static InitialContextUtils instance = new InitialContextUtils();
033    }
034    
035    public static InitialContextUtils getInstance() {
036        return InitialContextUtilsHolder.instance;
037    }
038
039    public InitialContext getInitialContext()
040            throws NamingException {
041        var env = new Properties();
042        var is = InitialContextUtils.class.getClassLoader().getResourceAsStream("echothree-jndi.properties");
043        var loaded = false;
044
045        if(is != null) {
046            try {
047                env.load(is);
048                loaded = true;
049            } catch (IOException ioe) {
050                // Fall through, leave loaded = false.
051            }
052        }
053
054        if(!loaded) {
055            env.put(Context.PROVIDER_URL, "remote+http://127.0.0.1:8080");
056        }
057
058        env.put(Context.INITIAL_CONTEXT_FACTORY, org.wildfly.naming.client.WildFlyInitialContextFactory.class.getName());
059
060        return new InitialContext(env);
061    }
062
063}