KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > examples > startup > Init


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.examples.startup;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 import javax.servlet.ServletContextEvent JavaDoc;
23 import javax.servlet.ServletContextListener JavaDoc;
24
25 import org.apache.taglibs.standard.examples.beans.Customers;
26
27 /**
28  * Initialization class. Builds all the data structures
29  * used in the "examples" webapp.
30  *
31  * @author Pierre Delisle
32  * @version $Revision: 1.4 $ $Date: 2004/08/16 21:38:27 $
33  */

34 public class Init implements ServletContextListener JavaDoc {
35     
36     //*********************************************************************
37
// ServletContextListener methods
38

39     // recovers the one context parameter we need
40
public void contextInitialized(ServletContextEvent JavaDoc sce) {
41         //p("contextInitialized");
42
init(sce);
43     }
44     
45     public void contextDestroyed(ServletContextEvent JavaDoc sce) {
46         //p("contextInitialized");
47
}
48     
49     //*********************************************************************
50
// Initializations
51

52     private void init(ServletContextEvent JavaDoc sce) {
53         /*
54          * Customers
55          */

56         Customers.create("Richard", "Maurice", "5/15/35",
57         "123 Chemin Royal", "Appt. #301",
58         "Montreal", "QC", "H3J 9R9", "Canada");
59         Customers.create("Mikita", "Stan", "12/25/47",
60         "45 Fisher Blvd", "Suite 203",
61         "Chicago", "IL", "65982", "USA", "(320)876-9784", null);
62         Customers.create("Gilbert", "Rod", "3/11/51",
63         "123 Main Street", "",
64         "New-York City", "NY", "19432", "USA");
65         Customers.create("Howe", "Gordie", "7/25/46",
66         "7654 Wings Street", "",
67         "Detroit", "MG", "07685", "USA", "(465)675-0761", "(465)879-9802");
68         Customers.create("Sawchuk", "Terrie", "11/05/46",
69         "12 Maple Leafs Avenue", "",
70         "Toronto", "ON", "M5C 1Z1", "Canada");
71         sce.getServletContext().setAttribute("customers", Customers.findAll());
72
73     /**
74      * Array of primitives (int)
75      */

76     int[] intArray = new int[] {10, 20, 30, 40, 50};
77         sce.getServletContext().setAttribute("intArray", intArray);
78
79     /**
80      * Array of Objects (String)
81      */

82     String JavaDoc[] stringArray = new String JavaDoc[] {
83         "A first string",
84         "La deuxieme string",
85         "Ella troisiemo stringo",
86     };
87         sce.getServletContext().setAttribute("stringArray", stringArray);
88
89     /**
90         * String-keyed Map
91         */

92         Hashtable JavaDoc stringMap = new Hashtable JavaDoc();
93         sce.getServletContext().setAttribute("stringMap", stringMap);
94         stringMap.put("one", "uno");
95         stringMap.put("two", "dos");
96         stringMap.put("three", "tres");
97         stringMap.put("four", "cuatro");
98         stringMap.put("five", "cinco");
99         stringMap.put("six", "seis");
100         stringMap.put("seven", "siete");
101         stringMap.put("eight", "ocho");
102         stringMap.put("nine", "nueve");
103         stringMap.put("ten", "diez");
104
105         /**
106          * Integer-keyed Map
107      */

108     // we use a Hashtable so we can get an Enumeration easily, below
109
Hashtable JavaDoc numberMap = new Hashtable JavaDoc();
110     sce.getServletContext().setAttribute("numberMap", numberMap);
111     numberMap.put(new Integer JavaDoc(1), "uno");
112     numberMap.put(new Integer JavaDoc(2), "dos");
113     numberMap.put(new Integer JavaDoc(3), "tres");
114     numberMap.put(new Integer JavaDoc(4), "cuatro");
115     numberMap.put(new Integer JavaDoc(5), "cinco");
116     numberMap.put(new Integer JavaDoc(6), "seis");
117     numberMap.put(new Integer JavaDoc(7), "siete");
118     numberMap.put(new Integer JavaDoc(8), "ocho");
119     numberMap.put(new Integer JavaDoc(9), "nueve");
120     numberMap.put(new Integer JavaDoc(10), "diez");
121
122     /**
123      * Enumeration
124      */

125     Enumeration JavaDoc enum_ = numberMap.keys();
126     // don't use 'enum' for attribute name because it is a
127
// reserved word in EcmaScript.
128
sce.getServletContext().setAttribute("enumeration", enum_);
129
130     /**
131      * Message arguments for parametric replacement
132      */

133     Object JavaDoc[] serverInfoArgs =
134         new Object JavaDoc[] {
135         sce.getServletContext().getServerInfo(),
136         System.getProperty("java.version")
137         };
138     sce.getServletContext().setAttribute("serverInfoArgs", serverInfoArgs);
139     }
140     
141     //*********************************************************************
142
// Initializations
143

144     private void p(String JavaDoc s) {
145         System.out.println("[Init] " + s);
146     }
147 }
148
Popular Tags