001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ExampleServlet.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.examples.ear;
025:
026: import java.io.IOException;
027: import java.io.PrintWriter;
028: import java.util.Collection;
029: import java.util.List;
030:
031: import javax.naming.Context;
032: import javax.naming.InitialContext;
033: import javax.servlet.ServletException;
034: import javax.servlet.http.HttpServlet;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: /**
039: * Defines a servlet that is accessing the two entities through a local session
040: * bean.
041: * @author Florent Benoit
042: */
043: public class ExampleServlet extends HttpServlet {
044:
045: /**
046: * Serializable class uid.
047: */
048: private static final long serialVersionUID = -3172627111841538912L;
049:
050: /**
051: * Called by the server (via the service method) to allow a servlet to
052: * handle a GET request.
053: * @param request an HttpServletRequest object that contains the request the
054: * client has made of the servlet
055: * @param response an HttpServletResponse object that contains the response
056: * the servlet sends to the client
057: * @throws IOException if an input or output error is detected when the
058: * servlet handles the GET request
059: * @throws ServletException if the request for the GET could not be handled
060: */
061: @Override
062: public void doGet(final HttpServletRequest request,
063: final HttpServletResponse response) throws IOException,
064: ServletException {
065:
066: response.setContentType("text/html");
067: PrintWriter out = response.getWriter();
068: out.println("<html>");
069: out.println("<head>");
070: out.println("<title>");
071: out
072: .println("Client of Local session bean - EAR example</title>");
073: out.println("</head>");
074: out.println("<body>");
075:
076: initAuthorBooks(out);
077: out.println("<br /><br />");
078: displayAuthors(out);
079:
080: out.println("</body>");
081: out.println("</html>");
082: out.close();
083: }
084:
085: /**
086: * Init list of authors/books.
087: * @param out the given writer
088: */
089: private void initAuthorBooks(final PrintWriter out) {
090: out.println("Initialize authors and their books...<br/>");
091:
092: List<Author> authors;
093: try {
094: authors = getBean().listOfAuthors();
095: } catch (Exception e) {
096: displayException(out, "Cannot Access to the bean", e);
097: return;
098: }
099:
100: if (authors != null && authors.size() > 0) {
101: out.println("Already initialized !<br/>");
102: return;
103: }
104:
105: try {
106: getBean().init();
107: } catch (Exception e) {
108: displayException(out,
109: "Cannot init list of authors with their books", e);
110: return;
111: }
112: }
113:
114: /**
115: * Display authors.
116: * @param out the given writer
117: */
118: private void displayAuthors(final PrintWriter out) {
119: out.println("Get authors");
120: out.println("<br /><br />");
121:
122: // Get list of Authors
123: List<Author> authors = null;
124: try {
125: authors = getBean().listOfAuthors();
126: } catch (Exception e) {
127: displayException(out,
128: "Cannot call listeOfAuthors on the bean", e);
129: return;
130: }
131:
132: // List for each author, the name of books
133: if (authors != null) {
134: for (Author author : authors) {
135: out.println("List of books with author '"
136: + author.getName() + "' :");
137: out.println("<ul>");
138: Collection<Book> books = author.getBooks();
139: if (books == null) {
140: out.println("<li>No book !</li>");
141: } else {
142: for (Book book : books) {
143: out.println("<li>Title '" + book.getTitle()
144: + "'.</li>");
145: }
146: }
147: out.println("</ul>");
148:
149: }
150: } else {
151: out.println("No author found !");
152: }
153:
154: }
155:
156: /**
157: * If there is an exception, print the exception.
158: * @param out the given writer
159: * @param errMsg the error message
160: * @param e the content of the exception
161: */
162: private void displayException(final PrintWriter out,
163: final String errMsg, final Exception e) {
164: out.println("<p>Exception : " + errMsg);
165: out.println("<pre>");
166: e.printStackTrace(out);
167: out.println("</pre></p>");
168: }
169:
170: /**
171: * Lookup the stateless bean and gets a reference on it.
172: * @return the stateless bean business interface.
173: * @throws Exception if the bean cannot be retrieved.
174: */
175: private StatelessLocal getBean() throws Exception {
176: Context initialContext = new InitialContext();
177: Object o = initialContext
178: .lookup("org.ow2.easybeans.examples.ear.StatelessBean"
179: + "_" + StatelessLocal.class.getName()
180: + "@Local");
181:
182: if (o instanceof StatelessLocal) {
183: StatelessLocal statelessBean = (StatelessLocal) o;
184: return statelessBean;
185: }
186: throw new Exception("Cannot cast object into StatelessLocal");
187:
188: }
189:
190: }
|