{"id":123,"date":"2009-12-01T11:38:57","date_gmt":"2009-12-01T11:38:57","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=123"},"modified":"2010-06-11T12:50:27","modified_gmt":"2010-06-11T12:50:27","slug":"ejb-referencingjndi-mapping-examples-in-jsfglassfish","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=123","title":{"rendered":"EJB referencing\/JNDI mapping examples in JSF\/Glassfish"},"content":{"rendered":"<p><strong><span style=\"text-decoration: underline;\">Update<\/span><\/strong><\/p>\n<p>I have posted some further notes on this <a title=\"http:\/\/salientsoft.co.uk\/?p=882\" href=\"http:\/\/salientsoft.co.uk\/?p=882\"><strong>here<\/strong><\/a><strong>. <\/strong>The notes are in raw form from an earlier\u00a0investigative email &#8211; much of it is covered here, but some points and links are not, and I do not have the time or inclination at present to rework everything into a single post!<\/p>\n<p>The following examples show typical scenarios which have been tested as working :-<\/p>\n<p><strong><span style=\"text-decoration: underline;\">1\/ Local EJB Interface injected into JSF managed bean via @EJB<\/span><\/strong><\/p>\n<p>Note that when implementing a dual interface as here, you can extend a single underlying interface. In this case, <strong>SimpleBeanLocal<\/strong> and <strong>SimpleBeanRemote<\/strong> both extend <strong>SimpleBean<\/strong>. However, when you do this, the implementation code must explicitly implemement both <strong>SimpleBeanLocal<\/strong> and <strong>SimpleBeanRemote<\/strong> as in this example. Implementing the superinterface <strong>SimpleBean<\/strong> on its own will not work. This is also explained in <strong><a title=\"http:\/\/salientsoft.co.uk\/?p=152\" href=\"http:\/\/salientsoft.co.uk\/?p=152\">this post<\/a><\/strong>.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">JSF managed bean code<\/span><\/strong><\/p>\n<pre>@EJB()\r\nprivate SimpleBean simpleBean;<\/pre>\n<p><strong><span style=\"text-decoration: underline;\"><strong>\u00a0<\/strong><\/span><\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\"><strong><span style=\"text-decoration: underline;\">EJB Implementation Code<\/span><\/strong><\/span><\/strong><\/p>\n<pre>@Stateless()\r\npublic class SimpleBeanImpl implements SimpleBeanLocal, SimpleBeanRemote {\r\n\r\n...class code here...\r\n\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">2\/ Local EJB Interface\u00a0looked up in JSF managed bean directly via JNDI\u00a0<\/span><\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">JSF managed bean code<\/span><\/strong><\/p>\n<pre>try {\r\n\u00a0\u00a0InitialContext ctx = new InitialContext();\r\n\r\n  \/* Note that the JNDI name consists of the Java EE Environment Naming Context (ENC) -\r\n    \"java:comp\/env\", followed by \"\/\", followed by the &lt;ejb-ref-name&gt; used in web.xml.\r\n    The extra \/Local is just to make the name different to the remote example.\r\n    Any name can be used providing web.xml matches, and multiple slashes can be present.\r\n  *\/\r\n\r\n\u00a0\u00a0simpleBean = (SimpleBean) ctx.lookup(\"java:comp\/env\/ejb\/JPAGlassFishIce\/Local\/SimpleBean\");\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0userList = simpleBean.fetchUsers();\r\n} catch (NamingException e1) {\r\n\u00a0\u00a0e1.printStackTrace();\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">web.xml<\/span><\/strong><\/p>\n<pre>&lt;!-- We define a reference for the bean here.\r\n     If we were using @EJB this would do it for us,\r\n     but in this case we are using an explicit JNDI lookup.\r\n--&gt;\r\n&lt;ejb-local-ref&gt;\r\n\u00a0\u00a0\u00a0 &lt;ejb-ref-name&gt;ejb\/JPAGlassFishIce\/Local\/SimpleBean&lt;\/ejb-ref-name&gt;\r\n\u00a0\u00a0\u00a0 &lt;ejb-ref-type&gt;Session&lt;\/ejb-ref-type&gt;\r\n\u00a0\u00a0\u00a0 &lt;local-home&gt;&lt;\/local-home&gt;\r\n\u00a0\u00a0\u00a0 &lt;local&gt;uk.co.salientsoft.jpaglassfishice.domain.SimpleBeanLocal&lt;\/local&gt;\r\n&lt;\/ejb-local-ref&gt;<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">EJB Implementation Code<\/span><\/strong><\/p>\n<pre>@Stateless()\r\npublic class SimpleBeanImpl implements SimpleBeanLocal, SimpleBeanRemote {\r\n\r\n...class code here...<\/pre>\n<p>}<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">3\/\u00a0Remote EJB Interface injected into JSF managed bean via @EJB<\/span><\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">JSF managed bean code<\/span><\/strong><\/p>\n<pre>@EJB(name=\"ejb\/JPAGlassFishIce\/SimpleBean\", beanInterface=SimpleBeanRemote.class)\r\nprivate SimpleBean simpleBean;<\/pre>\n<p><strong><span style=\"text-decoration: underline;\"><strong>\u00a0<\/strong><\/span><\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\"><strong><span style=\"text-decoration: underline;\">EJB Implementation Code<\/span><\/strong><\/span><\/strong><\/p>\n<pre>@Stateless(mappedName=\"ejb\/JPAGlassFishIceEJB\/SimpleBean\")\r\npublic class SimpleBeanImpl implements SimpleBeanLocal, SimpleBeanRemote {\r\n\r\n...class code here...\r\n\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">4\/\u00a0Remote EJB Interface\u00a0looked up in JSF managed bean directly via JNDI\u00a0<\/span><\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">JSF managed bean code<\/span><\/strong><\/p>\n<pre>try {\r\n\u00a0\u00a0InitialContext ctx = new InitialContext();\r\n\r\n  \/* Note that the JNDI name here matches the <strong>mappedName <\/strong>given in the\r\n     @Stateless annotation in the EJB implementation code.\r\n     <strong>mappedName<\/strong> is only relevant for remote interfaces.\r\n     It is not used for a local interface.\r\n  *\/\r\n\r\n\u00a0\u00a0simpleBean = (SimpleBean) ctx.lookup=(\"ejb\/JPAGlassFishIceEJB\/SimpleBean\");\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0userList = simpleBean.fetchUsers();\r\n} catch (NamingException e1) {\r\n\u00a0\u00a0e1.printStackTrace();\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">EJB Implementation Code<\/span><\/strong><\/p>\n<pre>@Stateless(mappedName=\"ejb\/JPAGlassFishIceEJB\/SimpleBean\")\r\npublic class SimpleBeanImpl implements SimpleBeanLocal, SimpleBeanRemote {\r\n\r\n...class code here...<\/pre>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update I have posted some further notes on this here. The notes are in raw form from an earlier\u00a0investigative email &#8211; much of it is covered here, but some points and links are not, and I do not have the time or inclination at present to rework everything into a single post! The following examples [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[18,10,19,15],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=123"}],"version-history":[{"count":29,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":125,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions\/125"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}