Source code: com/sample/tutorial/update/controller/servlet/UpdateTutorialServlet.java
1 /*
2 * UpdateTutorialServlet.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.sample.tutorial.update.controller.servlet;
11
12 import java.io.*;
13
14 import javax.servlet.*;
15 import javax.servlet.http.*;
16
17 import com.aendvari.cerberus.component.assembly.*;
18 import com.aendvari.cerberus.component.descriptor.*;
19 import com.aendvari.cerberus.component.descriptor.parser.*;
20 import com.aendvari.cerberus.component.directory.*;
21
22 import com.aendvari.common.model.*;
23 import com.aendvari.common.model.xalan.*;
24
25 import com.aendvari.common.util.*;
26
27 import com.aendvari.hermes.broker.*;
28 import com.aendvari.hermes.broker.http.*;
29
30 import com.aendvari.satyr.servlet.*;
31 import com.aendvari.satyr.servlet.gateway.HttpFormGateway.ContextModifier;
32 import com.aendvari.satyr.servlet.gateway.tethys.*;
33
34 import com.aendvari.tethys.tag.*;
35
36 import java.util.*;
37
38 /**
39 * Main servlet for the update tutorial.
40 *
41 * @author Trevor Milne
42 *
43 */
44
45 public class UpdateTutorialServlet extends HttpServlet
46 {
47 protected final String Broker = "tutorial-update/broker";
48
49 /**
50 * Process an HTTP "GET" request.
51 *
52 * @param request The servlet request being processed.
53 * @param response The servlet response being created.
54 *
55 * @exception IOException Thrown if an input/output error occurs.
56 * @exception ServletException Thrown if a servlet exception occurs.
57 *
58 */
59
60 public void doGet(HttpServletRequest request, HttpServletResponse response)
61 throws ServletException, IOException
62 {
63 ModelTree modelTree = null;
64
65 // retrieve model tree from session, if any
66 try
67 {
68 modelTree = ServletModelUtil.getModelTree(request.getSession());
69 }
70 catch (ServletModelUtil.NoModelException exception)
71 {
72 // create application view model
73
74 // determine the location of the application model
75 String path = getServletContext().getRealPath("model.xml");
76
77 // create a model tree instance
78 modelTree = new XalanModelTree();
79
80 // load the model from the stream
81 try
82 {
83 modelTree.loadFromFile(path);
84 }
85 catch (ModelParserException parseException)
86 {
87 throw new ServletException(parseException);
88 }
89
90 // place the model into the client's session
91 ServletModelUtil.setModelTree(request.getSession(), modelTree);
92 }
93
94 // show the main screen
95
96 // get the message broker
97 MessageBroker broker = (MessageBroker)getServletConfig().getServletContext().getAttribute(Broker);
98
99 // create a context for the connection
100 HttpMessageBrokerContext brokerContext = new HttpMessageBrokerContext();
101
102 brokerContext.setServletConfig(getServletConfig());
103 brokerContext.setRequest(request);
104 brokerContext.setResponse(response);
105 brokerContext.setSession(request.getSession());
106
107 // place model into broker context for components to use
108 brokerContext.getProperties().setObject("model", modelTree);
109
110 // create a connection
111 MessageBrokerConnection connection = broker.createConnection();
112
113 // set the context into the connection
114 connection.setContext(brokerContext);
115
116 // tell login view to display
117 connection.publish(
118 connection.getTopic("main.show"), connection.createMessage());
119 }
120
121 /**
122 * Process an HTTP "POST" request.
123 *
124 * @param request The servlet request being processed.
125 * @param response The servlet response being created.
126 *
127 * @exception IOException Thrown if an input/output error occurs.
128 * @exception ServletException Thrown if a servlet exception occurs.
129 *
130 */
131
132 public void doPost(HttpServletRequest request, HttpServletResponse response)
133 throws ServletException, IOException
134 {
135 try
136 {
137 // retrieve the model from the session
138 final ModelTree modelTree = ServletModelUtil.getModelTree(request.getSession());
139
140 // retrieve the message broker from the servlet context
141 MessageBroker broker = (MessageBroker)getServletConfig().getServletContext().getAttribute(Broker);
142
143 // process the HTTP post
144 MessageGateway.process(broker, getServletConfig(), request, response,
145 null,
146 // use an anonymous class to extend the gateway
147 new ContextModifier() {
148 public void modifyContext(
149 MessageBrokerContext context,
150 ServletConfig servletConfig,
151 HttpServletRequest request, HttpServletResponse response)
152 throws ServletException
153 {
154 // place model into broker context for components to use
155 context.getProperties().setObject("model", modelTree);
156 }
157 },
158 null);
159 }
160 // if no model tree, then there is no session, start the application
161 catch (ServletModelUtil.NoModelException exception)
162 {
163 doGet(request, response);
164 }
165 }
166
167 /**
168 * Initialize the servlet.
169 *
170 * @exception ServletException Thrown if a servlet exception occurs.
171 *
172 */
173
174 public void init()
175 throws ServletException
176 {
177 // create the MessageBroker
178 MessageBroker broker = new MessageBroker();
179 getServletConfig().getServletContext().setAttribute(Broker, broker);
180
181 // assemble components
182 try
183 {
184 // retrieve component configuration
185 String configuration = getServletConfig().getInitParameter("configuration");
186
187 // get assembly descriptor stream
188 InputStream input = ResourceLoader.getResourceAsStream(configuration);
189
190 // parse the descriptor
191 AssemblyParser parser = new AssemblyParser();
192 AssemblyDescriptor descriptor = parser.parse(input);
193
194 // create connection to broker
195 MessageBrokerConnection connection = broker.createConnection();
196
197 // create output objects
198 AssemblyContext context = new AssemblyContext(broker);
199 ComponentDirectory directory = new ComponentDirectory();
200
201 // assemble components
202 ComponentAssembler assembler = new ComponentAssembler(context, descriptor, directory);
203 assembler.assemble();
204 }
205 catch (Exception exception)
206 {
207 exception.printStackTrace(System.err);
208 throw new ServletException(exception);
209 }
210
211 System.out.println("UpdateTutorialServlet initialized");
212 }
213 }
214