标签: initial-context

如何使用默认构造函数伪造InitialContext

所有,

我试图在一些古老的java代码中进行一些单元测试(没有接口,没有抽象等)

这是一个使用ServletContext的servlet(我假设它是由Tomcat设置的),并且它在web.xml/context.xml文件中设置了数据库信息.现在,我已经弄清楚如何制作一个假的ServletContext,但代码有

 InitialContext _ic = new InitialContext();
Run Code Online (Sandbox Code Playgroud)

到处都是(所以更换它是不可行的).我需要找到一种方法来使默认的InitialContext()能够在_ic.lookup(val)不抛出异常的情况下完成.

我假设有一些方法可以加载context.xml,但是这个魔法是如何工作的,我正在画一个空白.有人有主意吗?

java unit-testing initial-context

14
推荐指数
3
解决办法
2万
查看次数

在Weblogic 10.xx上使用InitialContext查找EJB

你能告诉我如何在Weblogic上查找EJB吗?
我有以下bean:

@Stateless
@EJB(name = "DataAccess", beanInterface = DataAccessLocal.class)
public class DataAccess implements DataAccessLocal {
    ...
}
Run Code Online (Sandbox Code Playgroud)

我在其他类中需要这个bean,它不是托管内容的一部分(只是简单的类),所以我想它应该这样做:

DataAccessLocal dataAccess = DataAccessLocal.class.cast((new InitialContext()).lookup("%SOME_JNDI_NAME%"));
Run Code Online (Sandbox Code Playgroud)

问题是在Weblogic 10.xx AS的情况下应该使用%SOME_JNDI_NAME%?
任何帮助将不胜感激.

java lookup ejb weblogic initial-context

6
推荐指数
1
解决办法
1万
查看次数

如何在Spring-JUnit测试中设置JNDI查找?

我正在使用Maven 3.0.3,Spring 3.1.0.RELEASE和JUnit 4.8.1.如何在容器外部创建JNDI功能(在我的例子中是JBoss)?我认为Spring的jndiTemplate可以解决这个问题(来自我的testApplicationContext.xml文件):

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate" lazy-init="true">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
                <prop key="java.naming.provider.url">localhost:1099</prop>
            </props>
        </property>
</bean>

...

<!-- Can't change the below code -->
<bean class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean" lazy-init="true" singleton="true" id="skillManager">
    <property name="jndiName">
        <value>customLibSkillManager</value>
    </property>
    <property name="businessInterface">
        <value>customLibx.skills.customLibSkillManager_IF</value>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

但是当我运行JUnit测试时,我得到了这个异常......

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'skillsValidationManager' defined in class path resource [_module_config/managers/proxies.remote.COMMON.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, …
Run Code Online (Sandbox Code Playgroud)

junit spring jndi initial-context

6
推荐指数
0
解决办法
6827
查看次数

以编程方式为Spring创建JNDI DataSource

我有一个基于Spring的基于Web的应用程序,它具有使用JNDI定义的数据源,我正在尝试创建一个独立的应用程序来使用bean.如何在独立应用程序中以编程方式创建JNDI条目和数据库属性?

<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/MyDS" />
</bean>

    public static void main(String[] args) {

      // this throws an error since the JNDI lookup fails - can I programmatically define the database properties here?

    ClassPathXmlApplicationContext ctx = new  ClassPathXmlApplicationContext("applicationContext.xml");

    UserService userService = ctx.getBean(UserService.class);
    User user = userService.findUserById("jdoe");

    System.out.println("display name: " + user.getDisplayName());
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我尝试过类似的东西,但现在收到错误"javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名"

public static void main(String[] args) {
    setupJNDI();

    ClassPathXmlApplicationContext ctx = new  ClassPathXmlApplicationContext("applicationContext.xml");

    UserService userService = ctx.getBean(UserService.class);
    User user = userService.findUserById("jdoe");

    System.out.println("display name: " + user.getDisplayName()); …
Run Code Online (Sandbox Code Playgroud)

java spring jndi mocking initial-context

6
推荐指数
1
解决办法
1万
查看次数

如何在Storm jar中加载jndi绑定?

我们有一个项目正在使用Storm,因此我们的代码必须打包在一个jar中.我们以前用作com.sun.jndi.fscontext.RefFSContextFactory我们的InitialContextFactory实现jndicontext从系统配置目录中的文件加载绑定classpath(工作正常).但是,当尝试使用此工厂从jar中加载上下文时,我们得到以下内容:

javax.naming.InvalidNameException: unknown protocol: jar
     at com.sun.jndi.fscontext.FScontextFactory.getFileNameFromURLSTring(FSContextFactory.java:139)
     at com.sun.jndi.fscontext.RefFSContextFactory.createContext(RefFSContextFactory.java:31)
Run Code Online (Sandbox Code Playgroud)

这是由于工厂尝试从以下URL加载jdni上下文:

"罐子:文件:/mount/storm-dir/data/storm.jar/jndicontext"

这是一个有效的URL,但工厂不了解如何打开jar.有没有实现javax.naming.spi.InitialContextFactory呢?或者有一种方法可以解决这个问题,并添加一个配置目录到Storm的classpath

java jndi initial-context apache-storm

6
推荐指数
0
解决办法
315
查看次数

EJB查找的初始Context属性值

我正在学习EJB 3.0的基础知识.我已设法获取并运行示例代码.现在我正在逐行分析,以获得深入的知识.但我被困在几行,其中有查找以找到所需的bean.

任何人都可以用简单的语言向我解释以下几行的含义和需要吗?

Properties properties = new Properties();
properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs", "org.jboss.naming rg.jnp.interfaces");
properties.setProperty(Context.PROVIDER_URL, "localhost:1099");

IniialContext context = null;
SamleEjbRemote cl = null;
try {
    context = new InitialContext(properties);
    cl = (SampleEjbRemote) context.lookup("SampleEjbBean/remote");
} catch (NamingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)


属性中使用的每个'key'和'value'的确切含义是什么?

其余的是将'属性'放在初始上下文实例中.我对上述内容有一个非常含糊的想法,但我想非常清楚地澄清它.如果有人能指出我对上述内容的任何链接或见解,我会很高兴.

提前致谢.

java jboss jndi initial-context ejb-3.0

5
推荐指数
1
解决办法
2万
查看次数

关于weblogic 11g中InitialContext和副本感知存根缓存的问题

我们有一个集群,并部署了一些无状态ejb会话bean.目前我们只在客户端代码中缓存了InitialContext对象,我有几个问题:

  1. 在当前情况下,如果我们调用lookup()来获取副本感知存根,哪个服务器将返回存根对象,我们获取InitialContext的同一服务器,或者每次调用查找方法时它将平衡到其他服务器?
  2. 我们应该只缓存存根吗?它是线程安全的吗?如果是,存根如何处理来自客户端线程的并发请求?相似还是顺序?
  3. 还有一个问题,当我们调用新的InitialContext()时,如果服务器无法访问,它将需要很长时间才能返回超时异常,我们如何为这种情况设置超时?

caching ejb weblogic initial-context

5
推荐指数
1
解决办法
553
查看次数

在纯 java 应用程序中从 context.xml 创建 InitialContext

正如问题所暗示的,我有一个简单的java应用程序(又名“简单的主”),需要初始化一个hibernate连接,其信息位于context.xml.

经过大量搜索和一些技巧后,得出以下结论:

public static void main(String[] args) {
    //JNDI provider is needed and RMI registry has one...
    try {
        java.rmi.registry.LocateRegistry.createRegistry(1099);
        System.out.println("RMI registry ready.");
    } catch (Exception e) {
        System.out.println("Exception starting RMI registry:");
        e.printStackTrace();
    }
    new InitialContext().readContextXml("context.xml");//of course there is nothing like that. But is there an equivalent?
    String result = thatWillConnectWithHibernate();
    System.out.println(result);
}

private static String thatWillConnectWithHibernate() {
    //does stuff
}
Run Code Online (Sandbox Code Playgroud)

那么有没有一种简单的方法可以从 xml 文件创建一个 InitialContext 呢?我对手动解析文件不感兴趣,我可以自己做。

java jndi initial-context

5
推荐指数
0
解决办法
297
查看次数

如何在远程WebSphere上进行JNDI查找期间解决sun / io / MalformedInputException

我使用WebSphere 8.5托管我的应用程序,并在应用程序服务器上配置了一些JDBC资源。我还使用瘦客户机运行时库开发了一个客户机应用程序。以以下方式执行JNDI查找时:

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "corbaloc:iiop:serv:2809");
Context initialContext = new InitialContext(env);
DataSource ds = (DataSource) initialContext.lookup("cell/node/servers/server/MYDB");
Run Code Online (Sandbox Code Playgroud)

引发以下异常:

java.lang.NoClassDefFoundError: sun/io/MalformedInputException
at com.ibm.rmi.iiop.CDRReader.getTcsCConverter(CDRReader.java:451)
    at com.ibm.rmi.iiop.CDRReader.readStringOrIndirection(CDRReader.java:532)
    at com.ibm.rmi.iiop.CDRReader.read_string(CDRReader.java:518)
    at com.ibm.rmi.IOR.read(IOR.java:337)
    at com.ibm.rmi.iiop.Connection._locate(Connection.java:531)
    at com.ibm.rmi.iiop.Connection.locate(Connection.java:490)
    at com.ibm.rmi.iiop.GIOPImpl.locate(GIOPImpl.java:229)
    at com.ibm.rmi.corba.Corbaloc.locateUsingINS(Corbaloc.java:307)
    at com.ibm.rmi.corba.Corbaloc.resolve(Corbaloc.java:378)
    at com.ibm.rmi.corba.ORB.objectURLToObject(ORB.java:3796)
    at com.ibm.CORBA.iiop.ORB.objectURLToObject(ORB.java:3263)
    at com.ibm.rmi.corba.ORB.string_to_object(ORB.java:3694)
    at com.ibm.ws.naming.util.WsnInitCtxFactory.stringToObject(WsnInitCtxFactory.java:1645)
    at com.ibm.ws.naming.util.WsnInitCtxFactory.getWsnNameService(WsnInitCtxFactory.java:1502)
    at com.ibm.ws.naming.util.WsnInitCtxFactory.getRootContextFromServer(WsnInitCtxFactory.java:1040)
    at com.ibm.ws.naming.util.WsnInitCtxFactory.getRootJndiContext(WsnInitCtxFactory.java:962)
    at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:614)
    at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:128)
    at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:765)
    at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:164)
    at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at ch.bit.easy.wf.util.Database.getConnection(Database.java:74)
    at ch.bit.easy.wf.ops.test.TestDBAccess.testSuccessfullyLimitedQuery(TestDBAccess.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source) …
Run Code Online (Sandbox Code Playgroud)

java websphere jndi initial-context

4
推荐指数
1
解决办法
6406
查看次数

如何为独立客户端设置Glassfish 3.1.2的JNDI?

我尝试从独立的swing客户端(在客户端计算机上的单独JVM中运行)连接到Glassfish服务器.

我目前使用Netbeans的以下设置,一切正常:

System.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
System.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
System.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
System.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.3");
System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext context = new InitialContext();
Run Code Online (Sandbox Code Playgroud)

但是当我尝试通过键入"java -jar client.jar"从控制台启动编译的客户端时,我收到以下错误:

D:\workspace\gf-client\dist>java -jar gf-client.jar
17.08.2012 11:07:38 ch.client.core.ServerContext getInitialContext SCHWERWIEGEND: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory]
        at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
        at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.init(Unknown Source)
        at javax.naming.InitialContext.<init>(Unknown Source)
        at ch.lawsuite.core.ServerContext.getInitialContext(ServerContext.java:2 7)
        at ch.client.core.remote.Facades.initialize(Facades.java:68)
        at ch.client.core.Client.main(Client.java:57) Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialIni tContextFactory
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown …
Run Code Online (Sandbox Code Playgroud)

java glassfish initial-context java-ee-6

2
推荐指数
1
解决办法
2万
查看次数