KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > inspector > ShowEngine


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.inspector;
16
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.CharArrayWriter JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
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 /**
34  * Component of the {@link Inspector} page used to display
35  * the properties of the {@link org.apache.tapestry.IEngine} as well as a serialized view of it.
36  * Also, the {@link org.apache.tapestry.request.RequestContext} is dumped out.
37  *
38  *
39  * @author Howard Lewis Ship
40  *
41  **/

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     /**
53      * Workaround for OGNL limitation --- OGNL can't dereference
54      * past class instances.
55      *
56      * @since 2.2
57      *
58      **/

59
60     public String JavaDoc 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 JavaDoc bos = null;
76         ObjectOutputStream JavaDoc oos = null;
77
78         try
79         {
80             bos = new ByteArrayOutputStream JavaDoc();
81             oos = new ObjectOutputStream JavaDoc(bos);
82
83             // Write the application object to the stream.
84

85             oos.writeObject(getPage().getEngine());
86
87             // Extract the application as an array of bytes.
88

89             serializedEngine = bos.toByteArray();
90         }
91         catch (IOException JavaDoc 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         // It would be nice to deserialize the application object now, but in
104
// practice, that fails due to class loader problems.
105
}
106
107     private void close(OutputStream JavaDoc stream)
108     {
109         if (stream == null)
110             return;
111
112         try
113         {
114             stream.close();
115         }
116         catch (IOException JavaDoc ex)
117         {
118             // Ignore.
119
}
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 JavaDoc writer = null;
141         BinaryDumpOutputStream bos = null;
142
143         try
144         {
145             // Because IReponseWriter doesn't implement the
146
// java.io.Writer interface, we have to buffer this
147
// stuff then pack it in all at once. Kind of a waste!
148

149             writer = new CharArrayWriter JavaDoc();
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 JavaDoc ex)
160         {
161             // Ignore.
162
}
163         finally
164         {
165             if (bos != null)
166             {
167                 try
168                 {
169                     bos.close();
170                 }
171                 catch (IOException JavaDoc ex)
172                 {
173                     // Ignore.
174
}
175             }
176
177             if (writer != null)
178             {
179                 writer.reset();
180                 writer.close();
181             }
182         }
183     }
184
185 }
Popular Tags