Skip to content

Commit 0f7d399

Browse files
author
Arun Gupta
committed
Splitting the sample in three different ways to acquire BeanManager
1 parent 286280b commit 0f7d399

File tree

4 files changed

+288
-19
lines changed

4 files changed

+288
-19
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.cdisamples.beanmanager;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import java.util.Set;
45+
import javax.enterprise.inject.spi.Bean;
46+
import javax.enterprise.inject.spi.BeanManager;
47+
import javax.enterprise.inject.spi.CDI;
48+
import javax.servlet.ServletException;
49+
import javax.servlet.annotation.WebServlet;
50+
import javax.servlet.http.HttpServlet;
51+
import javax.servlet.http.HttpServletRequest;
52+
import javax.servlet.http.HttpServletResponse;
53+
54+
/**
55+
* @author Arun Gupta
56+
*/
57+
@WebServlet(urlPatterns = {"/TestServletCurrent"})
58+
public class TestServletCurrent extends HttpServlet {
59+
60+
/**
61+
* Processes requests for both HTTP
62+
* <code>GET</code> and
63+
* <code>POST</code> methods.
64+
*
65+
* @param request servlet request
66+
* @param response servlet response
67+
* @throws ServletException if a servlet-specific error occurs
68+
* @throws IOException if an I/O error occurs
69+
*/
70+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
71+
throws ServletException, IOException {
72+
response.setContentType("text/html;charset=UTF-8");
73+
try (PrintWriter out = response.getWriter()) {
74+
out.println("<!DOCTYPE html>");
75+
out.println("<html>");
76+
out.println("<head>");
77+
out.println("<title>BeanManager using CDI.current</title>");
78+
out.println("</head>");
79+
out.println("<body>");
80+
out.println("<h1>BeanManager using CDI.current</h1>");
81+
// Second way to get BeanManager
82+
BeanManager bm = CDI.current().getBeanManager();
83+
84+
Set<Bean<?>> beans = bm.getBeans(Greeting.class);
85+
for (Bean<?> b : beans) {
86+
out.println(b.getBeanClass().getName() + "<br>");
87+
}
88+
out.println("</body>");
89+
out.println("</html>");
90+
}
91+
}
92+
93+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
94+
/**
95+
* Handles the HTTP
96+
* <code>GET</code> method.
97+
*
98+
* @param request servlet request
99+
* @param response servlet response
100+
* @throws ServletException if a servlet-specific error occurs
101+
* @throws IOException if an I/O error occurs
102+
*/
103+
@Override
104+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
105+
throws ServletException, IOException {
106+
processRequest(request, response);
107+
}
108+
109+
/**
110+
* Handles the HTTP
111+
* <code>POST</code> method.
112+
*
113+
* @param request servlet request
114+
* @param response servlet response
115+
* @throws ServletException if a servlet-specific error occurs
116+
* @throws IOException if an I/O error occurs
117+
*/
118+
@Override
119+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
120+
throws ServletException, IOException {
121+
processRequest(request, response);
122+
}
123+
124+
/**
125+
* Returns a short description of the servlet.
126+
*
127+
* @return a String containing servlet description
128+
*/
129+
@Override
130+
public String getServletInfo() {
131+
return "Short description";
132+
}// </editor-fold>
133+
134+
}

cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServlet.java renamed to cdi/beanmanager/src/main/java/org/javaee7/cdisamples/beanmanager/TestServletInject.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
/**
5555
* @author Arun Gupta
5656
*/
57-
@WebServlet(name = "TestServlet", urlPatterns = {"/TestServlet"})
58-
public class TestServlet extends HttpServlet {
57+
@WebServlet(urlPatterns = {"/TestServletInject"})
58+
public class TestServletInject extends HttpServlet {
5959

6060
// First way to get BeanManager
6161
@Inject BeanManager bm;
@@ -77,22 +77,10 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
7777
out.println("<!DOCTYPE html>");
7878
out.println("<html>");
7979
out.println("<head>");
80-
out.println("<title>Servlet TestServlet</title>");
80+
out.println("<title>BeanManager using Injection</title>");
8181
out.println("</head>");
8282
out.println("<body>");
83-
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
84-
// Second way to get BeanManager
85-
// BeanManager bm = CDI.current().getBeanManager();
86-
87-
// Third way to get BeanManager
88-
// BeanManager bm = null;
89-
// try {
90-
// InitialContext context = new InitialContext();
91-
// bm = (BeanManager)context.lookup("java:comp/BeanManager");
92-
// } catch (NamingException | NullPointerException ex) {
93-
// Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
94-
// ex.printStackTrace(out);
95-
// }
83+
out.println("<h1>BeanManager using Injection</h1>");
9684
Set<Bean<?>> beans = bm.getBeans(Greeting.class);
9785
for (Bean<?> b : beans) {
9886
out.println(b.getBeanClass().getName() + "<br>");
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.cdisamples.beanmanager;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import java.util.Set;
45+
import java.util.logging.Level;
46+
import java.util.logging.Logger;
47+
import javax.enterprise.inject.spi.Bean;
48+
import javax.enterprise.inject.spi.BeanManager;
49+
import javax.naming.InitialContext;
50+
import javax.naming.NamingException;
51+
import javax.servlet.ServletException;
52+
import javax.servlet.annotation.WebServlet;
53+
import javax.servlet.http.HttpServlet;
54+
import javax.servlet.http.HttpServletRequest;
55+
import javax.servlet.http.HttpServletResponse;
56+
57+
/**
58+
* @author Arun Gupta
59+
*/
60+
@WebServlet(urlPatterns = {"/TestServletJNDI"})
61+
public class TestServletJNDI extends HttpServlet {
62+
63+
/**
64+
* Processes requests for both HTTP
65+
* <code>GET</code> and
66+
* <code>POST</code> methods.
67+
*
68+
* @param request servlet request
69+
* @param response servlet response
70+
* @throws ServletException if a servlet-specific error occurs
71+
* @throws IOException if an I/O error occurs
72+
*/
73+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
74+
throws ServletException, IOException {
75+
response.setContentType("text/html;charset=UTF-8");
76+
try (PrintWriter out = response.getWriter()) {
77+
out.println("<!DOCTYPE html>");
78+
out.println("<html>");
79+
out.println("<head>");
80+
out.println("<title>BeanManager using JNDI</title>");
81+
out.println("</head>");
82+
out.println("<body>");
83+
out.println("<h1>BeanManager using JNDI</h1>");
84+
// Third way to get BeanManager
85+
BeanManager bm = null;
86+
try {
87+
InitialContext context = new InitialContext();
88+
bm = (BeanManager)context.lookup("java:comp/BeanManager");
89+
} catch (NamingException | NullPointerException ex) {
90+
ex.printStackTrace(out);
91+
}
92+
Set<Bean<?>> beans = bm.getBeans(Greeting.class);
93+
for (Bean<?> b : beans) {
94+
out.println(b.getBeanClass().getName() + "<br>");
95+
}
96+
out.println("</body>");
97+
out.println("</html>");
98+
}
99+
}
100+
101+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
102+
/**
103+
* Handles the HTTP
104+
* <code>GET</code> method.
105+
*
106+
* @param request servlet request
107+
* @param response servlet response
108+
* @throws ServletException if a servlet-specific error occurs
109+
* @throws IOException if an I/O error occurs
110+
*/
111+
@Override
112+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
113+
throws ServletException, IOException {
114+
processRequest(request, response);
115+
}
116+
117+
/**
118+
* Handles the HTTP
119+
* <code>POST</code> method.
120+
*
121+
* @param request servlet request
122+
* @param response servlet response
123+
* @throws ServletException if a servlet-specific error occurs
124+
* @throws IOException if an I/O error occurs
125+
*/
126+
@Override
127+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
128+
throws ServletException, IOException {
129+
processRequest(request, response);
130+
}
131+
132+
/**
133+
* Returns a short description of the servlet.
134+
*
135+
* @return a String containing servlet description
136+
*/
137+
@Override
138+
public String getServletInfo() {
139+
return "Short description";
140+
}// </editor-fold>
141+
142+
}

cdi/beanmanager/src/main/webapp/index.jsp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@
4646
<html>
4747
<head>
4848
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
49-
<title>CDI BeanManager</title>
49+
<title>CDI : BeanManager</title>
5050
</head>
5151
<body>
52-
<h1>CDI BeanManager</h1>
52+
<h1>CDI : BeanManager</h1>
5353

54-
Show the <a href="${pageContext.request.contextPath}/TestServlet"/>list of beans</a>.<p/>
54+
Show the list of beans using BeanManager by:
55+
<ol>
56+
<li><a href="${pageContext.request.contextPath}/TestServletInject"/>injection</a>
57+
<li><a href="${pageContext.request.contextPath}/TestServletCurrent"/>CDI.current</a>
58+
<li><a href="${pageContext.request.contextPath}/TestServletJNDI"/>JNDI</a>
59+
</ol>
5560

5661
</body>
5762
</html>

0 commit comments

Comments
 (0)