KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > serial > SerialApp


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.serial;
31
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.ObjectInputStream JavaDoc;
36 import java.io.ObjectOutputStream JavaDoc;
37 import java.util.Enumeration JavaDoc;
38
39 import javax.servlet.http.HttpSession JavaDoc;
40
41 import nextapp.echo2.app.ApplicationInstance;
42 import nextapp.echo2.app.Border;
43 import nextapp.echo2.app.Button;
44 import nextapp.echo2.app.Color;
45 import nextapp.echo2.app.Column;
46 import nextapp.echo2.app.ContentPane;
47 import nextapp.echo2.app.Extent;
48 import nextapp.echo2.app.Insets;
49 import nextapp.echo2.app.Label;
50 import nextapp.echo2.app.ListBox;
51 import nextapp.echo2.app.Window;
52 import nextapp.echo2.app.WindowPane;
53 import nextapp.echo2.app.event.ActionEvent;
54 import nextapp.echo2.app.event.ActionListener;
55 import nextapp.echo2.app.list.DefaultListModel;
56 import nextapp.echo2.testapp.interactive.InteractiveApp;
57 import nextapp.echo2.webcontainer.ContainerContext;
58 import nextapp.echo2.webrender.UserInstance;
59
60 public class SerialApp extends ApplicationInstance {
61     
62     private ListBox listBox;
63     private Window mainWindow;
64     
65     private void doLoad() {
66         try {
67             String JavaDoc fileName = "session.data";
68             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fileName);
69             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
70             String JavaDoc sessionKey = (String JavaDoc) ois.readObject();
71             UserInstance userInstance = (UserInstance) ois.readObject();
72             getSession().setAttribute(sessionKey, userInstance);
73             ois.close();
74             fis.close();
75             showDialog(false, sessionKey + " loaded successfully.");
76         } catch (ClassNotFoundException JavaDoc ex) {
77             showDialog(true, "Exception occurred: " + ex);
78             ex.printStackTrace();
79         } catch (IOException JavaDoc ex) {
80             showDialog(true, "Exception occurred: " + ex);
81             ex.printStackTrace();
82         }
83     }
84
85     private void doRefresh() {
86         DefaultListModel listModel = (DefaultListModel) listBox.getModel();
87         listModel.removeAll();
88         Enumeration JavaDoc enumeration = getSession().getAttributeNames();
89         while (enumeration.hasMoreElements()) {
90             String JavaDoc sessionKey = (String JavaDoc) enumeration.nextElement();
91             listModel.add(sessionKey);
92         }
93     }
94
95     private void doStore() {
96         String JavaDoc sessionKey = (String JavaDoc) listBox.getSelectedValue();
97         UserInstance userInstance = (UserInstance) getSession().getAttribute(sessionKey);
98         if (userInstance == null) {
99             showDialog(true, "No instance selected.");
100             return;
101         }
102         try {
103             String JavaDoc fileName = "session.data";
104             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fileName);
105             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
106             oos.writeObject(sessionKey);
107             oos.writeObject(userInstance);
108             oos.flush();
109             oos.close();
110             fos.close();
111             showDialog(false, sessionKey + " serialized successfully.");
112         } catch (IOException JavaDoc ex) {
113             showDialog(true, "Exception occurred: " + ex);
114             ex.printStackTrace();
115         }
116         
117     }
118     
119     private HttpSession JavaDoc getSession() {
120         ContainerContext containerContext = (ContainerContext) getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME);
121         return containerContext.getSession();
122     }
123
124     /**
125      * @see nextapp.echo2.app.ApplicationInstance#init()
126      */

127     public Window init() {
128         if (InteractiveApp.LIVE_DEMO_SERVER) {
129             throw new RuntimeException JavaDoc("Serialization test disabled on live demo server.");
130         }
131         
132         mainWindow = new Window();
133         mainWindow.setTitle("NextApp Echo2 Serialization Test Application");
134         
135         ContentPane content = new ContentPane();
136         mainWindow.setContent(content);
137         
138         Column mainColumn = new Column();
139         mainColumn.setBorder(new Border(new Extent(4), Color.BLUE, Border.STYLE_OUTSET));
140         mainColumn.setInsets(new Insets(40));
141         mainColumn.setCellSpacing(new Extent(20));
142         content.add(mainColumn);
143         
144         Column serializeColumn = new Column();
145         mainColumn.add(serializeColumn);
146         
147         Button button;
148
149         serializeColumn.add(new Label("Available Applications:"));
150         
151         listBox = new ListBox();
152         listBox.setWidth(new Extent(100, Extent.PERCENT));
153         serializeColumn.add(listBox);
154         
155         button = new Button("[ Refresh ]");
156         button.addActionListener(new ActionListener() {
157             
158             /**
159              * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
160              */

161             public void actionPerformed(ActionEvent e) {
162                 doRefresh();
163             }
164         });
165         serializeColumn.add(button);
166         
167         button = new Button("[ Serialize ]");
168         button.addActionListener(new ActionListener() {
169             
170             /**
171              * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
172              */

173             public void actionPerformed(ActionEvent e) {
174                 doStore();
175             }
176         });
177         serializeColumn.add(button);
178         
179         button = new Button("[ Load Serialized Application ]");
180         button.addActionListener(new ActionListener() {
181             
182             /**
183              * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
184              */

185             public void actionPerformed(ActionEvent e) {
186                 doLoad();
187             }
188         });
189         mainColumn.add(button);
190         
191         return mainWindow;
192     }
193
194     private void showDialog(boolean error, String JavaDoc message) {
195          WindowPane windowPane = new WindowPane();
196          windowPane.setModal(true);
197          windowPane.setTitle(error ? "Error" : "Status");
198          windowPane.setTitleBackground(error ? Color.RED : Color.GREEN);
199          windowPane.setInsets(new Insets(20));
200          windowPane.add(new Label(message));
201          mainWindow.getContent().add(windowPane);
202     }
203 }
204
Popular Tags