无法在Java程序中创建初始上下文

时间:2018-08-31 12:04:09

标签: java java-deployment-toolkit noinitialcontextexception

take(1)


MockLocalContext是扩展javax.naming.InitialContext类的类。 这是在创建InitialContext对象时出错。

我正在获取以下堆栈跟踪:

public static void setupInitialContext() {
    try {
        System.out.println("Loading Initial Context");
        NamingManager.setInitialContextFactoryBuilder(new InitialContextFactoryBuilder() {

            @Override
            public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment)
                    throws NamingException {
                return new InitialContextFactory() {
                    @Override
                    public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
                        InitialContext context = null;

                        try {
                            context = new MockLocalContext();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                        return context;
                    }

                };
            }

        });
        System.out.println("Loaded Initial Context");
    } catch (NamingException ne) {
        ne.printStackTrace();
    }
}

    public static void main(String[] args) throws NamingException {
    setupInitialContext();
    InitialContext context = new InitialContext();
    context.lookup(DS_ORACLE);
}

它适用于内容相同的许多行,我只是对其进行了调整以适合此处。谢谢!

2 个答案:

答案 0 :(得分:1)

看起来像您的MockLocalContext启动了InitialContext,它试图获取默认的初始上下文,该默认初始上下文是MockLocalContext,它启动了MockLocalContext,后者启动了InitialContext,依此类推,依此类推。等等。

答案 1 :(得分:0)

class MyContext extends InitialContext {

private Hashtable store;

public MyContext(Hashtable store) throws NamingException {
    super(true);
    this.store = store;
}

@Override
public void bind(String name, Object obj) {
    store.put(name, obj);
}

@Override
public Object lookup(String name) {
    return store.get(name);
}    

}

public class FooMain {

private static final int    ANSWER    = 42;
private static final String JNDI_NAME = "/config/theAnswerToEverything";

public static void main (String args[]) throws NamingException {
    Context context = new InitialContext();
    putInContext(                    JNDI_NAME, ANSWER);

    int answer = retrieveFromContext(JNDI_NAME);
    Assert.assertEquals(ANSWER, answer);
    System.out.printf("%d\n", answer);
}


private static void putInContext(final String key, final Object value) throws NamingException {
    Context context = new InitialContext();
    context.bind(key, value);
}

private static int retrieveFromContext(final String key) throws NamingException {
    Context context = new InitialContext();
    return (int) context.lookup(key);
}}


public class MyContextFactory implements InitialContextFactory {

private static Hashtable store = new Hashtable();

/*
  Do not confuse [store] with [environment]. They serve different
  purposes.
 */

@Override
public Context getInitialContext(Hashtable environment) throws NamingException {
    return new MyContext(store);
}}

this code is used to remove error while creating an InitialContext the code goes recursively...

在扩展InitialContext时,构造函数应该为true。