1 29 30 package nextapp.echo2.testapp.serial; 31 32 import java.io.FileInputStream ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.io.ObjectInputStream ; 36 import java.io.ObjectOutputStream ; 37 import java.util.Enumeration ; 38 39 import javax.servlet.http.HttpSession ; 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 fileName = "session.data"; 68 FileInputStream fis = new FileInputStream (fileName); 69 ObjectInputStream ois = new ObjectInputStream (fis); 70 String sessionKey = (String ) 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 ex) { 77 showDialog(true, "Exception occurred: " + ex); 78 ex.printStackTrace(); 79 } catch (IOException 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 enumeration = getSession().getAttributeNames(); 89 while (enumeration.hasMoreElements()) { 90 String sessionKey = (String ) enumeration.nextElement(); 91 listModel.add(sessionKey); 92 } 93 } 94 95 private void doStore() { 96 String sessionKey = (String ) 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 fileName = "session.data"; 104 FileOutputStream fos = new FileOutputStream (fileName); 105 ObjectOutputStream oos = new ObjectOutputStream (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 ex) { 113 showDialog(true, "Exception occurred: " + ex); 114 ex.printStackTrace(); 115 } 116 117 } 118 119 private HttpSession getSession() { 120 ContainerContext containerContext = (ContainerContext) getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME); 121 return containerContext.getSession(); 122 } 123 124 127 public Window init() { 128 if (InteractiveApp.LIVE_DEMO_SERVER) { 129 throw new RuntimeException ("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 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 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 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 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 |