001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.testapp.serial;
031:
032: import java.io.FileInputStream;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.io.ObjectInputStream;
036: import java.io.ObjectOutputStream;
037: import java.util.Enumeration;
038:
039: import javax.servlet.http.HttpSession;
040:
041: import nextapp.echo2.app.ApplicationInstance;
042: import nextapp.echo2.app.Border;
043: import nextapp.echo2.app.Button;
044: import nextapp.echo2.app.Color;
045: import nextapp.echo2.app.Column;
046: import nextapp.echo2.app.ContentPane;
047: import nextapp.echo2.app.Extent;
048: import nextapp.echo2.app.Insets;
049: import nextapp.echo2.app.Label;
050: import nextapp.echo2.app.ListBox;
051: import nextapp.echo2.app.Window;
052: import nextapp.echo2.app.WindowPane;
053: import nextapp.echo2.app.event.ActionEvent;
054: import nextapp.echo2.app.event.ActionListener;
055: import nextapp.echo2.app.list.DefaultListModel;
056: import nextapp.echo2.testapp.interactive.InteractiveApp;
057: import nextapp.echo2.webcontainer.ContainerContext;
058: import nextapp.echo2.webrender.UserInstance;
059:
060: public class SerialApp extends ApplicationInstance {
061:
062: private ListBox listBox;
063: private Window mainWindow;
064:
065: private void doLoad() {
066: try {
067: String fileName = "session.data";
068: FileInputStream fis = new FileInputStream(fileName);
069: ObjectInputStream ois = new ObjectInputStream(fis);
070: String sessionKey = (String) ois.readObject();
071: UserInstance userInstance = (UserInstance) ois.readObject();
072: getSession().setAttribute(sessionKey, userInstance);
073: ois.close();
074: fis.close();
075: showDialog(false, sessionKey + " loaded successfully.");
076: } catch (ClassNotFoundException ex) {
077: showDialog(true, "Exception occurred: " + ex);
078: ex.printStackTrace();
079: } catch (IOException ex) {
080: showDialog(true, "Exception occurred: " + ex);
081: ex.printStackTrace();
082: }
083: }
084:
085: private void doRefresh() {
086: DefaultListModel listModel = (DefaultListModel) listBox
087: .getModel();
088: listModel.removeAll();
089: Enumeration enumeration = getSession().getAttributeNames();
090: while (enumeration.hasMoreElements()) {
091: String sessionKey = (String) enumeration.nextElement();
092: listModel.add(sessionKey);
093: }
094: }
095:
096: private void doStore() {
097: String sessionKey = (String) listBox.getSelectedValue();
098: UserInstance userInstance = (UserInstance) getSession()
099: .getAttribute(sessionKey);
100: if (userInstance == null) {
101: showDialog(true, "No instance selected.");
102: return;
103: }
104: try {
105: String fileName = "session.data";
106: FileOutputStream fos = new FileOutputStream(fileName);
107: ObjectOutputStream oos = new ObjectOutputStream(fos);
108: oos.writeObject(sessionKey);
109: oos.writeObject(userInstance);
110: oos.flush();
111: oos.close();
112: fos.close();
113: showDialog(false, sessionKey + " serialized successfully.");
114: } catch (IOException ex) {
115: showDialog(true, "Exception occurred: " + ex);
116: ex.printStackTrace();
117: }
118:
119: }
120:
121: private HttpSession getSession() {
122: ContainerContext containerContext = (ContainerContext) getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME);
123: return containerContext.getSession();
124: }
125:
126: /**
127: * @see nextapp.echo2.app.ApplicationInstance#init()
128: */
129: public Window init() {
130: if (InteractiveApp.LIVE_DEMO_SERVER) {
131: throw new RuntimeException(
132: "Serialization test disabled on live demo server.");
133: }
134:
135: mainWindow = new Window();
136: mainWindow
137: .setTitle("NextApp Echo2 Serialization Test Application");
138:
139: ContentPane content = new ContentPane();
140: mainWindow.setContent(content);
141:
142: Column mainColumn = new Column();
143: mainColumn.setBorder(new Border(new Extent(4), Color.BLUE,
144: Border.STYLE_OUTSET));
145: mainColumn.setInsets(new Insets(40));
146: mainColumn.setCellSpacing(new Extent(20));
147: content.add(mainColumn);
148:
149: Column serializeColumn = new Column();
150: mainColumn.add(serializeColumn);
151:
152: Button button;
153:
154: serializeColumn.add(new Label("Available Applications:"));
155:
156: listBox = new ListBox();
157: listBox.setWidth(new Extent(100, Extent.PERCENT));
158: serializeColumn.add(listBox);
159:
160: button = new Button("[ Refresh ]");
161: button.addActionListener(new ActionListener() {
162:
163: /**
164: * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
165: */
166: public void actionPerformed(ActionEvent e) {
167: doRefresh();
168: }
169: });
170: serializeColumn.add(button);
171:
172: button = new Button("[ Serialize ]");
173: button.addActionListener(new ActionListener() {
174:
175: /**
176: * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
177: */
178: public void actionPerformed(ActionEvent e) {
179: doStore();
180: }
181: });
182: serializeColumn.add(button);
183:
184: button = new Button("[ Load Serialized Application ]");
185: button.addActionListener(new ActionListener() {
186:
187: /**
188: * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
189: */
190: public void actionPerformed(ActionEvent e) {
191: doLoad();
192: }
193: });
194: mainColumn.add(button);
195:
196: return mainWindow;
197: }
198:
199: private void showDialog(boolean error, String message) {
200: WindowPane windowPane = new WindowPane();
201: windowPane.setModal(true);
202: windowPane.setTitle(error ? "Error" : "Status");
203: windowPane.setTitleBackground(error ? Color.RED : Color.GREEN);
204: windowPane.setInsets(new Insets(20));
205: windowPane.add(new Label(message));
206: mainWindow.getContent().add(windowPane);
207: }
208: }
|