E.1 The com.mediamania.appserver package
This package includes classes that are described in Chapter 16 and Chapter 17 for using JDO
in an application server environment.
E.1.1 com.mediamania.appserver.CashierBean
1 package com.mediamania.appserver;
2
3 import javax.ejb.*;
4
5 import javax.naming.InitialContext;
6 import javax.naming.Context;
7 import javax.naming.NamingException;
8
9 import java.util.Iterator;
10 import java.util.Date;
11
12 import com.mediamania.store.StoreQueries;
13 import com.mediamania.store.Customer;
14 import com.mediamania.store.Purchase;
15 import com.mediamania.store.Rental;
16 import com.mediamania.store.RentalItem;
17 import com.mediamania.store.MediaItem;
18
19 import javax.jdo.PersistenceManager;
20 import javax.jdo.PersistenceManagerFactory;
21
22 public class CashierBean implements javax.ejb.SessionBean {
23 private javax.ejb.SessionContext context;
24 private PersistenceManagerFactory pmf;
25 private PersistenceManager pm;
26 private String pmfName = "java:comp/env/jdo/MediaManiaPMF";
27
28 /**
29 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
30 */
31 public void setSessionContext(javax.ejb.SessionContext aContext) {
32 context = aContext;
33 try {
34 Context ic = new InitialContext( );
35 pmf = (PersistenceManagerFactory)ic.lookup(pmfName);
36 } catch (NamingException ex) {
37 throw new EJBException("setSessionContext", ex);
38 }
39 }
40
41 public void ejbActivate( ) {
42 }
43 public void ejbPassivate( ) {
44 }
45 public void ejbRemove( ) {
46 }
47 public void ejbCreate( ) {
48 }
49
50 public void checkout(
51 final java.lang.String lastName,
52 final java.lang.String firstName,
53 final java.util.Collection rentals,
54 final java.util.Collection purchases)
55 throws java.rmi.RemoteException {
56 PersistenceManager pm = pmf.getPersistenceManager( );
57 Customer customer = StoreQueries.getCustomer(pm, firstName, lastName);
58 Iterator it = rentals.iterator( );
59 while (it.hasNext( )) {
60 RentalValueObject rvo = (RentalValueObject)it.next( );
61 RentalItem ri = StoreQueries.getRentalItem
62 (pm, rvo.serialNumber);
63 Rental rental = new Rental(customer, new Date( ), ri);
64 customer.addTransaction(rental);
65 customer.addRental(rental);
66 }
67 it = purchases.iterator( );
68 while (it.hasNext( )) {
69 PurchaseValueObject pvo = (PurchaseValueObject)it.next( );
70 MediaItem mediaItem = StoreQueries.getMediaItem(
71 pm, pvo.title, pvo.format);
72 Purchase purchase = new Purchase(customer, new Date( ), mediaItem);
73 customer.addTransaction(purchase);
74 }
75 pm.close( );
76 }
77 }
E.1.2 com.mediamania.appserver.JDOPlugIn
1 package com.mediamania.appserver;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5
6 import javax.jdo.PersistenceManagerFactory;
7 import javax.jdo.PersistenceManager;
8 import javax.jdo.JDOHelper;
9 import javax.jdo.Extent;
10
11 import java.util.Properties;
12 import java.util.Iterator;
13
14 import java.io.InputStream;
15 import java.io.IOException;
16
17 import javax.naming.Context;
18 import javax.naming.InitialContext;
19 import javax.naming.NamingException;
20
21 import org.apache.struts.action.ActionServlet;
22 import org.apache.struts.action.PlugIn;
23 import org.apache.struts.config.ModuleConfig;
24
25 public class JDOPlugIn implements PlugIn {
26 private ServletContext ctx;
27 private String name;
28 private String path;
29 private String jndiName;
30 public JDOPlugIn( ) {
31 }
32
33 public void setName(String name) {
34 this.name = name;
35 }
36
37 public void setPath(String path) {
38 this.path = path;
39 }
40
41 public void setJndiName(String jndiName) {
42 this.jndiName = jndiName;
43 }
44
45 public void init(ActionServlet servlet, ModuleConfig config)
46 throws ServletException {
47 ctx = servlet.getServletContext( );
48 if (name == null || name.length( ) == 0) {
49 throw new ServletException
50 ("You must specify name.");
51 }
52 try {
53 PersistenceManagerFactory pmf;
54 if (path != null) {
55 pmf = getPersistenceManagerFactoryFromPath(path);
56 } else if (jndiName != null) {
57 pmf = getPersistenceManagerFactoryFromJndi(jndiName);
58 } else {
59 throw new ServletException
60 ("You must specify either path or jndiName.");
61 }
62 ctx.setAttribute(name, pmf);
63 } catch (Exception ex) {
64 throw new ServletException(
65 "Unable to load PMF: name:" + name +
66 ", path: " + path +
67 ", jndiName: " + jndiName,
68 ex);
69 }
70 }
71
72 private PersistenceManagerFactory
73 getPersistenceManagerFactoryFromPath(String path)
74 throws IOException {
75 Properties props = new Properties( );
76 InputStream in = ctx.getResourceAsStream(path);
77 props.load(in);
78 return JDOHelper.getPersistenceManagerFactory(props);
79 }
80
81 private PersistenceManagerFactory
82 getPersistenceManagerFactoryFromJndi(String jndiName)
83 throws NamingException {
84 Context ic = new InitialContext( );
85 return (PersistenceManagerFactory) ic.lookup(jndiName);
86 }
87
88 public void destroy( ) {}
89 }
E.1.3 com.mediamania.appserver.LookupMovieAction
1 package com.mediamania.appserver;
2
3 import javax.servlet.ServletContext;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
6 import org.apache.struts.action.Action;
7 import org.apache.struts.action.ActionForm;
8 import org.apache.struts.action.ActionForward;
9 import org.apache.struts.action.ActionMapping;
10
11 import javax.jdo.PersistenceManagerFactory;
12 import javax.jdo.PersistenceManager;
13 import javax.jdo.JDOHelper;
14 import javax.jdo.Extent;
15 import javax.jdo.Transaction;
16 import javax.jdo.Query;
17 import javax.jdo.JDOException;
18
19 import java.util.Collection;
20 import java.util.Iterator;
21 import com.mediamania.content.Movie;
22
23 public class LookupMovieAction extends Action {
24 PersistenceManagerFactory pmf = null;
25 PersistenceManager pm = null;
26 public ActionForward execute(ActionMapping mapping,
27 ActionForm form,
28 HttpServletRequest request,
29 HttpServletResponse response)
30 throws Exception {
31 try {
32 ServletContext ctx = getServlet().getServletContext( );
33 pmf = (PersistenceManagerFactory)ctx.getAttribute("jdo.Movies");
34 pm = pmf.getPersistenceManager( );
35 Query q = pm.newQuery(Movie.class, "title == param1");
36 q.declareParameters ("String param1");
37 String movieName = request.getParameter("movieName");
38 Collection movies = (Collection)q.execute(movieName);
39 Movie movie = (Movie)movies.iterator().next( );
40 String description = movie.getDescription( );
41 ctx.setAttribute("movieDescription", description);
42 } catch (JDOException e) {
43 } finally {
44 if (pm != null) {
45 pm.close( );
46 }
47 pm = null;
48 }
49 return (mapping.findForward("success"));
50 }
51 }
E.1.4 com.mediamania.appserver.MediaValueObject
1 package com.mediamania.appserver;
2
3 import java.io.Serializable;
4
5 public class MediaValueObject implements Serializable {
6 public String title;
7 }
E.1.5 com.mediamania.appserver.MovieInfo
1 package com.mediamania.appserver;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5
6 import javax.jdo.PersistenceManagerFactory;
7 import javax.jdo.PersistenceManager;
8 import javax.jdo.JDOHelper;
9 import javax.jdo.Extent;
10 import javax.jdo.JDOException;
11
12 import java.util.Properties;
13 import java.util.Iterator;
14
15 import java.io.InputStream;
16 import java.io.IOException;
17
18 import javax.naming.Context;
19 import javax.naming.InitialContext;
20 import javax.naming.NamingException;
21
22 import com.mediamania.content.Movie;
23
24 public class MovieInfo extends HttpServlet {
25 PersistenceManagerFactory persistenceManagerFactory;
26 PersistenceManager pm;
27 public void init( ) throws ServletException {
28 try {
29 ServletContext ctx = getServletContext( );
30 InputStream in = ctx.getResourceAsStream("WEB-INF/pmf.properties");
31 Properties props = new Properties( );
32 props.load(in);
33 persistenceManagerFactory =
34 JDOHelper.getPersistenceManagerFactory(props);
35 } catch (IOException ex) {
36 throw new ServletException("Unable to load PMF properties.", ex);
37 } catch (JDOException ex) {
38 throw new ServletException("Unable to create PMF resource.", ex);
39 } catch (Exception ex) {
40 throw new ServletException("Unable to initialize.", ex);
41 }
42
43 }
44
45 /**
46 Destroys the servlet.
47 */
48 public void destroy( ) {
49 }
50
51 /** Processes requests for both HTTP <code>GET</code>
52 * and <code>POST</code> methods.
53 * @param request servlet request
54 * @param response servlet response
55 */
56 protected void processRequest(HttpServletRequest request,
57 HttpServletResponse response)
58 throws ServletException, java.io.IOException {
59 pm = persistenceManagerFactory.getPersistenceManager( );
60 response.setContentType("text/html");
61 java.io.PrintWriter out = response.getWriter( );
62 out.println("<html>");
63 out.println("<head>");
64 out.println("<title>Servlet</title>");
65 out.println("</head>");
66 out.println("<body>");
67 out.print(formatMovieInfo( ));
68 out.println("</body>");
69 out.println("</html>");
70 out.close( );
71 pm.close( );
72 }
73
74 protected String formatMovieInfo( ) {
75 StringBuffer result = new StringBuffer( );
76 Extent movies = pm.getExtent(Movie.class, true);
77 Iterator it = movies.iterator( );
78 while (it.hasNext( )) {
79 result.append("<P>");
80 Movie movie = (Movie)it.next( );
81 result.append(movie.getDescription( ));
82 }
83 return result.toString( );
84 }
85 /** Handles the HTTP <code>GET</code> method.
86 * @param request servlet request
87 * @param response servlet response
88 */
89 protected void doGet(HttpServletRequest request,
90 HttpServletResponse response)
91 throws ServletException, java.io.IOException {
92 processRequest(request, response);
93 }
94
95 /** Handles the HTTP <code>POST</code> method.
96 * @param request servlet request
97 * @param response servlet response
98 */
99 protected void doPost(HttpServletRequest request,
100 HttpServletResponse response)
101 throws ServletException, java.io.IOException {
102 processRequest(request, response);
103 }
104
105 /** Returns a short description of the servlet.
106 */
107 public String getServletInfo( ) {
108 return "Movie Information";
109 }
110
111 }
E.1.6 com.mediamania.appserver.PurchaseValueObject
1 package com.mediamania.appserver;
2
3 public class PurchaseValueObject extends MediaValueObject {
4 public String format;
5 }
E.1.7 com.mediamania.appserver.RentalValueObject
1 package com.mediamania.appserver;
2
3 public class RentalValueObject extends MediaValueObject {
4 public String serialNumber;
5 }
|