IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
Topic
  • 20 replies
  • Latest Post - ‏2018-12-06T18:27:39Z by david_hay
Friendy
Friendy
17 Posts

Pinned topic Resource Environment Provider in EJB-jar xml

‏2015-07-28T09:00:43Z |

I am following this old document (but with websphere 8.5 and ejb 3.0) ,  I want to know the XML source code for the last part (section 2 - 6) of this ejb application
http://www.ibm.com/developerworks/websphere/library/techarticles/0611_totapally/0611_totapally.html#sec2-6

 

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
  <display-name>
TestEJB3.0

</display-name>
</ejb-jar>

  <resource-env-ref>
   <description>
   </description>
   <resource-env-ref-name>MyConstants</resource-env-ref-name>
   <resource-env-ref-type>com.ibm.acme.ree.lib.Config</resource-env-ref-type>
   </resource-env-ref>

I am using this but this is not a valid deployment descriptor, can anyone help in this.

also I dont see the "show all option" while deploying the EAR file,
can someone tell steps for EJB application, I think the given ones are for WAR only

Updated on 2015-07-31T08:55:12Z at 2015-07-31T08:55:12Z by Friendy
  • BrianSPaskin
    BrianSPaskin
    6818 Posts
    ACCEPTED ANSWER

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T11:27:20Z  
    • Friendy
    • ‏2015-07-29T11:16:04Z

    Hi, I don't see where you are trying to reference the name in the code at all.  Never lookup the jdbc driver directly.  What you want to do is use @Resoiurce and inject the datasource or whatever.  You give it a name and then during deploy it will ask for the binding information.  

    http://docs.oracle.com/javaee/5/tutorial/doc/bncjk.html

    Regards,

    Brian

  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-28T13:16:19Z  

    Hi, Your resource-env-ref must be in the <ejb-jar> tag.  Your current XML is invalid.  From there you can use the ejb bindings from MyConstants to map them to the real value in the App Server or just deploy the App and bind them then.

    Regards,

    Brian

  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T07:18:07Z  

    can you give me the exact XML i must use, the "resource-env-ref" should be under some thing like enterprise bean or session or something else

    Updated on 2015-07-29T07:54:21Z at 2015-07-29T07:54:21Z by Friendy
  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T08:22:36Z  
    • Friendy
    • ‏2015-07-29T07:18:07Z

    can you give me the exact XML i must use, the "resource-env-ref" should be under some thing like enterprise bean or session or something else

    Hi, just move the entire resource-env-ref stanza into the the ejb-jar stanza.  It does not need to be anywhere special, just in the ejb-jar stanza.

    <ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
            <display-name>TestEJB3.0</display-name>
            <resource-env-ref>
                    <description/>
                    <resource-env-ref-name>MyConstants</resource-env-ref-name>
                    <resource-env-ref-type>com.ibm.acme.ree.lib.Config</resource-env-ref-type>
            </resource-env-ref>
    </ejb-jar>
    

    Regards,

    Brian

  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T08:29:21Z  

    Hi, just move the entire resource-env-ref stanza into the the ejb-jar stanza.  It does not need to be anywhere special, just in the ejb-jar stanza.

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr"><ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1"> <display-name>TestEJB3.0</display-name> <resource-env-ref> <description/> <resource-env-ref-name>MyConstants</resource-env-ref-name> <resource-env-ref-type>com.ibm.acme.ree.lib.Config</resource-env-ref-type> </resource-env-ref> </ejb-jar> </pre>

    Regards,

    Brian

    no this XML is not working in websphere, when deploying it will not show the option for Resource environment mapping , also the xml editor shows this while inserting it directly under ejb-jar that

    cvc-complex-type.2.4.a: Invalid content was found starting with element 'resource-env-ref'. One of '{"http://
     java.sun.com/xml/ns/javaee":display-name, "http://java.sun.com/xml/ns/javaee":icon, "http://java.sun.com/
     xml/ns/javaee":enterprise-beans, "http://java.sun.com/xml/ns/javaee":interceptors, "http://java.sun.com/xml/ns/
     javaee":relationships, "http://java.sun.com/xml/ns/javaee":assembly-descriptor, "http://java.sun.com/xml/ns/
     javaee":ejb-client-jar}' is expected.

    I already tried it putting directly before posting this question .

    however if I put it under "<entrprise bean><session>" etc, I see that option, since this is not a bean there must be someway to declare it in xml :(

    this is the error websphere sysErr.log will give in deploying


    java.lang.IllegalStateException: Parent Translator (EJBJarTranslator(ejb-jar,1357922054)) did not find a Child Translator for "resource-env-ref".
     

     

    Updated on 2015-07-29T08:47:14Z at 2015-07-29T08:47:14Z by Friendy
  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T10:47:12Z  
    • Friendy
    • ‏2015-07-29T08:29:21Z

    no this XML is not working in websphere, when deploying it will not show the option for Resource environment mapping , also the xml editor shows this while inserting it directly under ejb-jar that

    cvc-complex-type.2.4.a: Invalid content was found starting with element 'resource-env-ref'. One of '{"http://
     java.sun.com/xml/ns/javaee":display-name, "http://java.sun.com/xml/ns/javaee":icon, "http://java.sun.com/
     xml/ns/javaee":enterprise-beans, "http://java.sun.com/xml/ns/javaee":interceptors, "http://java.sun.com/xml/ns/
     javaee":relationships, "http://java.sun.com/xml/ns/javaee":assembly-descriptor, "http://java.sun.com/xml/ns/
     javaee":ejb-client-jar}' is expected.

    I already tried it putting directly before posting this question .

    however if I put it under "<entrprise bean><session>" etc, I see that option, since this is not a bean there must be someway to declare it in xml :(

    this is the error websphere sysErr.log will give in deploying


    java.lang.IllegalStateException: Parent Translator (EJBJarTranslator(ejb-jar,1357922054)) did not find a Child Translator for "resource-env-ref".
     

     

    Hi, please attach your ear file.  If you are using EJB 3,0 then you don't need to have this here at all.  What you need to do is have the it bound in the ibm bindings file or not at all and do it at deploy time.

    Regards,

    Brian

    Updated on 2015-07-29T10:54:26Z at 2015-07-29T10:54:26Z by BrianSPaskin
  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T10:54:16Z  

    Hi, please attach your ear file.  If you are using EJB 3,0 then you don't need to have this here at all.  What you need to do is have the it bound in the ibm bindings file or not at all and do it at deploy time.

    Regards,

    Brian

    Thank you so much for taking out time to guide me on this, I am new to WebSphere community but I am an active participant in other communities over here.
    EJBDbAppEAR.earView Details

     

    I know it would be a silly thing which i am missing out there.

  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T10:58:25Z  
    • Friendy
    • ‏2015-07-29T10:54:16Z

    Thank you so much for taking out time to guide me on this, I am new to WebSphere community but I am an active participant in other communities over here.
    EJBDbAppEAR.earView Details

     

    I know it would be a silly thing which i am missing out there.

    I am receiving a permission denied trying to download the file. Can you please try to upload it again?

    Regards,

    Brian

  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T11:05:26Z  

    I am receiving a permission denied trying to download the file. Can you please try to upload it again?

    Regards,

    Brian

    EJBDbAppEAR.earView Details

     

    oh this new community site allows us to give permission to for sharing, I just gave now :)

    Updated on 2015-07-29T11:08:06Z at 2015-07-29T11:08:06Z by Friendy
  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T11:11:06Z  
    • Friendy
    • ‏2015-07-29T11:05:26Z

    EJBDbAppEAR.earView Details

     

    oh this new community site allows us to give permission to for sharing, I just gave now :)

    Ok, one last thing.  Please include the .java source code in the ear file.


    Thanks,

    Brian

  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T11:16:04Z  

    Ok, one last thing.  Please include the .java source code in the ear file.


    Thanks,

    Brian

    EJBDbAppEAR.earView Details

  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T11:27:20Z  
    • Friendy
    • ‏2015-07-29T11:16:04Z

    Hi, I don't see where you are trying to reference the name in the code at all.  Never lookup the jdbc driver directly.  What you want to do is use @Resoiurce and inject the datasource or whatever.  You give it a name and then during deploy it will ask for the binding information.  

    http://docs.oracle.com/javaee/5/tutorial/doc/bncjk.html

    Regards,

    Brian

  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T11:31:34Z  

    Hi, I don't see where you are trying to reference the name in the code at all.  Never lookup the jdbc driver directly.  What you want to do is use @Resoiurce and inject the datasource or whatever.  You give it a name and then during deploy it will ask for the binding information.  

    http://docs.oracle.com/javaee/5/tutorial/doc/bncjk.html

    Regards,

    Brian

    Hi Brian,

    Thanks for reply but

    the question here is deploying the EAR file,

    the code is not updated to use custom properties,you can Ignore it for time being,

    as I already have properties like username,password defined in WAS,

    once I am able to deploy the EAR file, I will update it with code to read it through lookup and deploy again. :)

    why it should give error in that structure of EAR jar :(

    Updated on 2015-07-29T11:34:10Z at 2015-07-29T11:34:10Z by Friendy
  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T11:43:52Z  
    This reply was deleted by Friendy 2015-07-29T11:44:23Z.

    Hi, the code in the sample you referenced can be replaced from

    try {
            Context ctx = new InitialContext();
            Object object = 
    ctx.lookup("java:comp/env/MyConstants");
            Config config = (Config) config;
            String myVariable = config.getProperty("MyVariable");
    

    to 

    @Resource(name="MyConstants")
    private Config config;
    ...
    config.getProperty("MyVariable")
    

    During deploy it will ask to map MyConstants to a something in WAS.

    Regards,

    Brian

  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T11:53:47Z  

    Hi, the code in the sample you referenced can be replaced from

    <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">try { Context ctx = new InitialContext(); Object object = ctx.lookup("java:comp/env/MyConstants"); Config config = (Config) config; String myVariable = config.getProperty("MyVariable"); </pre>

    to 

    <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">@Resource(name="MyConstants") private Config config; ... config.getProperty("MyVariable") </pre>

    During deploy it will ask to map MyConstants to a something in WAS.

    Regards,

    Brian

    I am using this now ,

    but still not able to get that option in websphere,
    the application gets deployed but not able to start due to same error which is in ear-jar file.
    Can you arrange a sample very small EAR file which I can take reference to , just with one property .

    However,
    I still feel and believe deployment is independent of java files it will just refer to XML for that .
    so problem is not in lookup but getting the resource mapping at time of deployment.

     

    Thanks

    Updated on 2015-07-29T12:04:12Z at 2015-07-29T12:04:12Z by Friendy
  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-29T18:09:21Z  
    • Friendy
    • ‏2015-07-29T11:53:47Z

    I am using this now ,

    but still not able to get that option in websphere,
    the application gets deployed but not able to start due to same error which is in ear-jar file.
    Can you arrange a sample very small EAR file which I can take reference to , just with one property .

    However,
    I still feel and believe deployment is independent of java files it will just refer to XML for that .
    so problem is not in lookup but getting the resource mapping at time of deployment.

     

    Thanks

    Hi, you are incorrect saying that it requires XML for the lookup. That is not the case in JEE5 and above if you are using the @Resource  to inject the resource.  You can use the ejjb bindings file to take the name and bind it to WAS, which is XML, but it is not required, unless you want to do this to force the binding during deploy.  I would never do what that article is stating today.  That was written for J2EE 1.4 and WASv6, both which have not been supported in years and the article is 9 years old.  

    Post your full code and I will take a look at it when I have time.  

    Regards,

    Brian

     

     

  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-30T05:53:34Z  

    Hi, you are incorrect saying that it requires XML for the lookup. That is not the case in JEE5 and above if you are using the @Resource  to inject the resource.  You can use the ejjb bindings file to take the name and bind it to WAS, which is XML, but it is not required, unless you want to do this to force the binding during deploy.  I would never do what that article is stating today.  That was written for J2EE 1.4 and WASv6, both which have not been supported in years and the article is 9 years old.  

    Post your full code and I will take a look at it when I have time.  

    Regards,

    Brian

     

     

    Thank you for your guidance,

     

    My problem has been resolved by using the @Resource parameters, it is a easy way to handle resources.

    Thanks a lot.

    Updated on 2015-07-30T07:17:55Z at 2015-07-30T07:17:55Z by Friendy
  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-30T21:27:53Z  
    • Friendy
    • ‏2015-07-30T05:53:34Z

    Thank you for your guidance,

     

    My problem has been resolved by using the @Resource parameters, it is a easy way to handle resources.

    Thanks a lot.

    Hi, I rewrote the application and the shared lib.  I changed the jndi name to "resource/test" and I am looking for the property "url".  I have no web or ejb xml files.

    package com.ibm.dw.lib;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class Config {
            private Map<String, String> attributes = null;
            
            public Config() {
                    attributes = new HashMap<String, String>(10);
            }
            
            protected void setAttribute(String attributeName, String attributeValue) {
                    attributes.put(attributeName, attributeValue);
            }
            
            public Object getAttribute(String attributeName) {
                    return attributes.get(attributeName);
            }
    }
    
    package com.ibm.dw.lib;
    
    import java.util.Enumeration;
    import java.util.Hashtable;
    
    import javax.naming.Context;
    import javax.naming.Name;
    import javax.naming.RefAddr;
    import javax.naming.Reference;
    import javax.naming.spi.ObjectFactory;
    
    public class ConfigFactory implements ObjectFactory {
            private static Config config = null;
            
            @Override
            public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
                    if (null == config) {
                            config = new Config();
                            Reference ref = (Reference) obj;
                            Enumeration<RefAddr> addrs = ref.getAll();
                            RefAddr addr = null;
                            String entryName = null;
                            String value = null;
                            while (addrs.hasMoreElements()) {
                                    addr = (RefAddr) addrs.nextElement();
                                    entryName = addr.getType();
                                    value = (String) addr.getContent();
                                    config.setAttribute(entryName, value);
                            }
                    }
                    return config;
            }
    }
    
    package com.ibm.dw.controller;
    
    import java.io.IOException;
    
    import javax.annotation.Resource;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.ibm.dw.lib.Config;
    
    @WebServlet("/")
    public class GetResources extends HttpServlet {
            private static final long serialVersionUID = 1L;
    
            @Resource(lookup="resource/test")
            private Config config;
            
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    response.getWriter().println(config.getAttribute("url"));
            }
    }
    

    Regards,

    Brian

  • Friendy
    Friendy
    17 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-31T05:55:25Z  

    Hi, I rewrote the application and the shared lib.  I changed the jndi name to "resource/test" and I am looking for the property "url".  I have no web or ejb xml files.

    <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">package com.ibm.dw.lib; import java.util.HashMap; import java.util.Map; public class Config { private Map<String, String> attributes = null; public Config() { attributes = new HashMap<String, String>(10); } protected void setAttribute(String attributeName, String attributeValue) { attributes.put(attributeName, attributeValue); } public Object getAttribute(String attributeName) { return attributes.get(attributeName); } } </pre> <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">package com.ibm.dw.lib; import java.util.Enumeration; import java.util.Hashtable; import javax.naming.Context; import javax.naming.Name; import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; public class ConfigFactory implements ObjectFactory { private static Config config = null; @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception { if (null == config) { config = new Config(); Reference ref = (Reference) obj; Enumeration<RefAddr> addrs = ref.getAll(); RefAddr addr = null; String entryName = null; String value = null; while (addrs.hasMoreElements()) { addr = (RefAddr) addrs.nextElement(); entryName = addr.getType(); value = (String) addr.getContent(); config.setAttribute(entryName, value); } } return config; } } </pre> <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">package com.ibm.dw.controller; import java.io.IOException; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ibm.dw.lib.Config; @WebServlet("/") public class GetResources extends HttpServlet { private static final long serialVersionUID = 1L; @Resource(lookup="resource/test") private Config config; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(config.getAttribute("url")); } } </pre>

    Regards,

    Brian

    Thanks Brian, my issue was resolved still its a good learning example, I really appreciate your time for this :)

  • BrianSPaskin
    BrianSPaskin
    6818 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2015-07-31T08:38:25Z  
    • Friendy
    • ‏2015-07-31T05:55:25Z

    Thanks Brian, my issue was resolved still its a good learning example, I really appreciate your time for this :)

    Please mark this thread is answered.

    Brian

  • david_hay
    david_hay
    2 Posts

    Re: Resource Environment Provider in EJB-jar xml

    ‏2018-12-06T18:27:39Z  

    Hi, I rewrote the application and the shared lib.  I changed the jndi name to "resource/test" and I am looking for the property "url".  I have no web or ejb xml files.

    <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">package com.ibm.dw.lib; import java.util.HashMap; import java.util.Map; public class Config { private Map<String, String> attributes = null; public Config() { attributes = new HashMap<String, String>(10); } protected void setAttribute(String attributeName, String attributeValue) { attributes.put(attributeName, attributeValue); } public Object getAttribute(String attributeName) { return attributes.get(attributeName); } } </pre> <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">package com.ibm.dw.lib; import java.util.Enumeration; import java.util.Hashtable; import javax.naming.Context; import javax.naming.Name; import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; public class ConfigFactory implements ObjectFactory { private static Config config = null; @Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception { if (null == config) { config = new Config(); Reference ref = (Reference) obj; Enumeration<RefAddr> addrs = ref.getAll(); RefAddr addr = null; String entryName = null; String value = null; while (addrs.hasMoreElements()) { addr = (RefAddr) addrs.nextElement(); entryName = addr.getType(); value = (String) addr.getContent(); config.setAttribute(entryName, value); } } return config; } } </pre> <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">package com.ibm.dw.controller; import java.io.IOException; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ibm.dw.lib.Config; @WebServlet("/") public class GetResources extends HttpServlet { private static final long serialVersionUID = 1L; @Resource(lookup="resource/test") private Config config; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(config.getAttribute("url")); } } </pre>

    Regards,

    Brian

    Three years on, and this sage advice saved me, albeit with WAS ND 9.0.0.7 :-)

    Thanks Brian, you rock