1 15 package org.apache.tapestry.contrib.inspector; 16 17 import java.io.ByteArrayOutputStream ; 18 import java.io.CharArrayWriter ; 19 import java.io.IOException ; 20 import java.io.ObjectOutputStream ; 21 import java.io.OutputStream ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.tapestry.BaseComponent; 25 import org.apache.tapestry.IMarkupWriter; 26 import org.apache.tapestry.IRender; 27 import org.apache.tapestry.IRequestCycle; 28 import org.apache.tapestry.Tapestry; 29 import org.apache.tapestry.event.PageDetachListener; 30 import org.apache.tapestry.event.PageEvent; 31 import org.apache.tapestry.util.io.BinaryDumpOutputStream; 32 33 42 43 public abstract class ShowEngine extends BaseComponent implements PageDetachListener 44 { 45 private byte[] serializedEngine; 46 47 public void pageDetached(PageEvent event) 48 { 49 serializedEngine = null; 50 } 51 52 59 60 public String getEngineClassName() 61 { 62 return getPage().getEngine().getClass().getName(); 63 } 64 65 private byte[] getSerializedEngine() 66 { 67 if (serializedEngine == null) 68 buildSerializedEngine(); 69 70 return serializedEngine; 71 } 72 73 private void buildSerializedEngine() 74 { 75 ByteArrayOutputStream bos = null; 76 ObjectOutputStream oos = null; 77 78 try 79 { 80 bos = new ByteArrayOutputStream (); 81 oos = new ObjectOutputStream (bos); 82 83 85 oos.writeObject(getPage().getEngine()); 86 87 89 serializedEngine = bos.toByteArray(); 90 } 91 catch (IOException ex) 92 { 93 throw new ApplicationRuntimeException( 94 Tapestry.getMessage("ShowEngine.could-not-serialize"), 95 ex); 96 } 97 finally 98 { 99 close(oos); 100 close(bos); 101 } 102 103 } 106 107 private void close(OutputStream stream) 108 { 109 if (stream == null) 110 return; 111 112 try 113 { 114 stream.close(); 115 } 116 catch (IOException ex) 117 { 118 } 120 } 121 122 public int getEngineByteCount() 123 { 124 return getSerializedEngine().length; 125 } 126 127 public IRender getEngineDumpDelegate() 128 { 129 return new IRender() 130 { 131 public void render(IMarkupWriter writer, IRequestCycle cycle) 132 { 133 dumpSerializedEngine(writer); 134 } 135 }; 136 } 137 138 private void dumpSerializedEngine(IMarkupWriter responseWriter) 139 { 140 CharArrayWriter writer = null; 141 BinaryDumpOutputStream bos = null; 142 143 try 144 { 145 149 writer = new CharArrayWriter (); 150 151 bos = new BinaryDumpOutputStream(writer); 152 bos.setBytesPerLine(32); 153 154 bos.write(getSerializedEngine()); 155 bos.close(); 156 157 responseWriter.print(writer.toString()); 158 } 159 catch (IOException ex) 160 { 161 } 163 finally 164 { 165 if (bos != null) 166 { 167 try 168 { 169 bos.close(); 170 } 171 catch (IOException ex) 172 { 173 } 175 } 176 177 if (writer != null) 178 { 179 writer.reset(); 180 writer.close(); 181 } 182 } 183 } 184 185 } | Popular Tags |