简体   繁体   English

从GlassFish 4.1中的自定义JNDI资源返回值

[英]Returning a value from a custom JNDI resource in GlassFish 4.1

I have tried to implement this many ways, but this is the way that makes the most sense to me, and I am still unable to return anything from my resource. 我试图实现这么多方法,但这是对我最有意义的方式,我仍然无法从我的资源中返回任何内容。 I added the resource with the GlassFish admin GUI (essentially, i am trying to save username and passwords on the local server). 我使用GlassFish管理GUI添加了资源(实质上,我试图在本地服务器上保存用户名和密码)。

While I am getting a null pointer exception (NPE), please do not point me here, it doesn't help me at all. 虽然我得到一个空指针异常(NPE),请不要指向我这里,它根本没有帮助我。 What is a NullPointerException, and how do I fix it? 什么是NullPointerException,我该如何解决?

Here are my supporting classes... 这是我的支持班......

Creating the bean 创建bean

   @LocalBean
   @Stateless

public class JndiProperties {

    @Resource(name="jndiCreds")
    private Properties properties;

    public String getUser() {
        return properties.getProperty("UserName");
    }
    public String getPass() {
        return properties.getProperty("UserPass");
    }
}

This is my bean manager: 这是我的bean经理:

 @ManagedBean
 @ViewScoped
    public class GetCreds {
        @Inject
        private JndiProperties property;
        public String getUserName(){
            return property.getUser();
        }
        public String getPassword(){
            return property.getPass();
        }
    }

And this is how I call them 这就是我称之为的方式

GetCreds creds = new GetCreds();
String username = creds.getUserName();
String pass =  creds.getPassword();

I named the resource jndiCreds and have the names UserName and UserPass with the values containing respective data. 我将资源命名为jndiCreds,并将名称UserName和UserPass与包含相应数据的值相对应。

Here is the view from the GlassFish GUI: 以下是GlassFish GUI的视图:

在此输入图像描述

Have any idea WHY it won't return my requested information? 有任何想法为什么它不会返回我要求的信息? I AM receiving an NPE when I try to call the resource when I call either function from getCreds. 当我从getCreds调用任一函数时,当我尝试调用资源时,我收到了NPE。

Help would be appreciated; 帮助将不胜感激; I am very stuck. 我很困惑。

I decided to step away from trying to use a bean and just accessing it directly (although I am giving up some security here). 我决定放弃尝试使用bean并直接访问它(虽然我在这里放弃了一些安全性)。 I am trying to access the data in a contextual manner. 我试图以上下文方式访问数据。 BUT! 但! I still can not do it! 我还是做不到! Here is my NEW supporting class: 这是我的新支持课程:

public class JndiProperties {

    public Properties getProperties(String jndiName) {
        Properties properties = null;
        try {
            InitialContext context = new InitialContext();
            properties = (Properties) context.lookup(jndiName);
            context.close();
        }
        catch (NamingException e) {

            return null;
        }
        return properties;
    }

And this is how I grab the information: 这就是我获取信息的方式:

JndiProperties creds = new JndiProperties();

String username = creds.getProperties("jndiCreds").getProperty("UserName");
String pass =  creds.getProperties("jndiCreds").getProperty("UserPass");

String credentials = String.join(System.getProperty("line.separator"),
                                 "user=" + username,
                                 "password=" + pass);
System.out.print(credentials);

I am using the same resource shown above. 我使用上面显示的相同资源。 I am STILL ending up with null pointer... ANY help would be greatly appreciated.Feel free to answer what was wrong with my bean implementation also. 我仍然以空指针结束...非常感谢任何帮助。请随意回答我的bean实现的错误。

I have figured it out! 我已经弄清楚了!

What was happening is first, I was a complete idiot, and I was trying to test my program with JUnit (IT DOES NOT RUN ON THE SERVER!) 发生了什么事情,首先,我是一个完全白痴,我试图用JUnit测试我的程序(它不会在服务器上运行!)

Since the program wasn't being run on GlassFish, it couldn't access the resource. 由于该程序未在GlassFish上运行,因此无法访问该资源。

Secondly, (and most importantly) I was missing the appserver-rt.jar- very important. 其次,(最重要的是)我错过了appserver-rt.jar-非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM