KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > consumerwebsite > AdventureComponentManager


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

37
38 package com.sun.j2ee.blueprints.consumerwebsite;
39
40 // j2ee imports
41
import javax.servlet.ServletContext JavaDoc;
42 import javax.servlet.http.*;
43
44 import java.beans.Beans JavaDoc;
45
46 import com.sun.j2ee.blueprints.util.tracer.Debug;
47
48 // WAF imports
49
import com.sun.j2ee.blueprints.waf.controller.web.WebComponentManager;
50
51 // customer component imports
52
import com.sun.j2ee.blueprints.customer.CustomerFacade;
53
54 // signon component
55
import com.sun.j2ee.blueprints.signon.SignOnFacade;
56
57 // Catalog imports
58
import com.sun.j2ee.blueprints.catalog.CatalogFacade;
59
60 /**
61  * This class manages components used in the web tier
62  *
63  */

64 public class AdventureComponentManager extends WebComponentManager
65               implements HttpSessionListener {
66             
67     public AdventureComponentManager () {
68     }
69     
70     /**
71      *
72      * Initialize the component manager
73      *
74      */

75     public void init(HttpSession session) {
76         session.setAttribute(AdventureKeys.COMPONENT_MANAGER, this);
77         // intialize the adventure package
78
getCart(session);
79     }
80     /**
81      *
82      * Initialize what needs initialization
83      *
84      */

85     public void sessionCreated(HttpSessionEvent se) {
86         se.getSession().setAttribute(AdventureKeys.COMPONENT_MANAGER, this);
87         se.getSession().setAttribute(AdventureKeys.CART,
88                                                    getCart(se.getSession()));
89     }
90     
91     public void sessionDestroyed(HttpSessionEvent se) {
92         // initialize the adventure package
93
try {
94       getCart(se.getSession());
95   } catch (Exception JavaDoc e) {
96       // swallow the session listener exception for now
97
}
98     }
99     
100     public Cart getCart(HttpSession session) {
101         Cart cart = (Cart)session.getAttribute(AdventureKeys.CART);
102         if (cart == null) {
103            try {
104                 cart = (Cart)Beans.instantiate(this.getClass().getClassLoader(),
105                                                             "com.sun.j2ee.blueprints.consumerwebsite.Cart");
106                 session.setAttribute(AdventureKeys.CART, cart);
107             } catch (Exception JavaDoc ex) {
108                 ex.printStackTrace();
109                 Debug.print("Error instanciating Cart object: " + ex);
110             }
111         }
112         return cart;
113     }
114     
115     /**
116      * Keep only one copy of the Catalog Facade around
117      * in the context to provide optimized creation of DAOs for
118      * accessing signon data.
119      */

120     public CatalogFacade getCatalogFacade (HttpSession session) {
121         ServletContext JavaDoc context = session.getServletContext();
122         CatalogFacade catalogFacade = null;
123         if (context.getAttribute(AdventureKeys.CATALOG_FACADE) != null) {
124             catalogFacade = (CatalogFacade)context.getAttribute(AdventureKeys.CATALOG_FACADE);
125         } else {
126             catalogFacade = new CatalogFacade();
127             context.setAttribute(AdventureKeys.CATALOG_FACADE, catalogFacade);
128         }
129         return catalogFacade;
130     }
131     
132     /**
133      * Keep only one copy of the SignOn Facade around
134      * in the context to provide optimized creation of DAOs for
135      * accessing signon data.
136      */

137     public SignOnFacade getSignOnFacade (HttpSession session) {
138         ServletContext JavaDoc context = session.getServletContext();
139         SignOnFacade signOnFacade = null;
140         if (context.getAttribute(AdventureKeys.SIGN_ON_FACADE) != null) {
141             signOnFacade = (SignOnFacade)context.getAttribute(AdventureKeys.SIGN_ON_FACADE);
142         } else {
143             signOnFacade = new SignOnFacade();
144             context.setAttribute(AdventureKeys.SIGN_ON_FACADE, signOnFacade);
145         }
146         return signOnFacade;
147     }
148     
149     /**
150      * Keep only one copy of the Customer Facade around
151      * in the context to provide optimized creation of DAOs for
152      * accessing customer data.
153      */

154     public CustomerFacade getCustomerFacade (HttpSession session) {
155         ServletContext JavaDoc context = session.getServletContext();
156         CustomerFacade CustomerFacade = null;
157         if (context.getAttribute(AdventureKeys.CUSTOMER_FACADE) != null) {
158             CustomerFacade = (CustomerFacade)context.getAttribute(AdventureKeys.CUSTOMER_FACADE);
159         } else {
160             CustomerFacade = new CustomerFacade();
161             context.setAttribute(AdventureKeys.CUSTOMER_FACADE, CustomerFacade);
162         }
163         return CustomerFacade;
164     }
165 }
166
167
168
Popular Tags