1:
39:
40:
41: package ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57:
65: public class PacketProcessor
66: implements PrivilegedAction
67: {
68:
69: private JdwpConnection _connection;
70:
71:
72: private boolean _shutdown;
73:
74:
75: private CommandSet[] _sets;
76:
77:
78: private ByteArrayOutputStream _outputBytes;
79:
80:
81: private DataOutputStream _os;
82:
83:
89: public PacketProcessor (JdwpConnection con)
90: {
91: _connection = con;
92: _shutdown = false;
93:
94:
95: _sets = new CommandSet[JdwpConstants.CommandSet.MAXIMUM + 1];
96: _outputBytes = new ByteArrayOutputStream();
97: _os = new DataOutputStream (_outputBytes);
98:
99:
100: _sets[JdwpConstants.CommandSet.VirtualMachine.CS_VALUE] =
101: new VirtualMachineCommandSet();
102: _sets[JdwpConstants.CommandSet.ReferenceType.CS_VALUE] =
103: new ReferenceTypeCommandSet();
104: _sets[JdwpConstants.CommandSet.ClassType.CS_VALUE] =
105: new ClassTypeCommandSet();
106: _sets[JdwpConstants.CommandSet.ArrayType.CS_VALUE] =
107: new ArrayTypeCommandSet();
108: _sets[JdwpConstants.CommandSet.InterfaceType.CS_VALUE] =
109: new InterfaceTypeCommandSet();
110: _sets[JdwpConstants.CommandSet.Method.CS_VALUE] =
111: new MethodCommandSet();
112: _sets[JdwpConstants.CommandSet.Field.CS_VALUE] =
113: new FieldCommandSet();
114: _sets[JdwpConstants.CommandSet.ObjectReference.CS_VALUE] =
115: new ObjectReferenceCommandSet();
116: _sets[JdwpConstants.CommandSet.StringReference.CS_VALUE] =
117: new StringReferenceCommandSet();
118: _sets[JdwpConstants.CommandSet.ThreadReference.CS_VALUE] =
119: new ThreadReferenceCommandSet();
120: _sets[JdwpConstants.CommandSet.ThreadGroupReference.CS_VALUE] =
121: new ThreadGroupReferenceCommandSet();
122: _sets[JdwpConstants.CommandSet.ArrayReference.CS_VALUE] =
123: new ArrayReferenceCommandSet();
124: _sets[JdwpConstants.CommandSet.ClassLoaderReference.CS_VALUE] =
125: new ClassLoaderReferenceCommandSet();
126: _sets[JdwpConstants.CommandSet.EventRequest.CS_VALUE] =
127: new EventRequestCommandSet();
128: _sets[JdwpConstants.CommandSet.StackFrame.CS_VALUE] =
129: new StackFrameCommandSet();
130: _sets[JdwpConstants.CommandSet.ClassObjectReference.CS_VALUE] =
131: new ClassObjectReferenceCommandSet();
132: }
133:
134:
138: public Object run ()
139: {
140:
141:
142: Jdwp.getDefault().subcomponentInitialized ();
143:
144: try
145: {
146: while (!_shutdown)
147: {
148: _processOnePacket ();
149: }
150: }
151: catch (Exception ex)
152: {
153: ex.printStackTrace();
154: }
155:
156: Jdwp.getDefault().shutdown();
157: return null;
158: }
159:
160:
163: public void shutdown ()
164: {
165: _shutdown = true;
166: }
167:
168:
169:
170: private void _processOnePacket ()
171: throws IOException
172: {
173: JdwpPacket pkt = _connection.getPacket ();
174:
175: if (!(pkt instanceof JdwpCommandPacket))
176: {
177:
178:
179: return;
180: }
181:
182: if (pkt != null)
183: {
184: JdwpCommandPacket commandPkt = (JdwpCommandPacket) pkt;
185: JdwpReplyPacket reply = new JdwpReplyPacket(commandPkt);
186:
187:
188: _outputBytes.reset();
189:
190:
191: ByteBuffer bb = ByteBuffer.wrap(commandPkt.getData());
192: byte command = commandPkt.getCommand();
193: byte commandSet = commandPkt.getCommandSet();
194:
195: CommandSet set = null;
196: try
197: {
198:
199: if (commandSet > 0 && commandSet < _sets.length)
200: {
201: set = _sets[commandPkt.getCommandSet()];
202: }
203: if (set != null)
204: {
205: _shutdown = set.runCommand(bb, _os, command);
206: reply.setData(_outputBytes.toByteArray());
207: }
208: else
209: {
210:
211: reply.setErrorCode(JdwpConstants.Error.NOT_IMPLEMENTED);
212: }
213: }
214: catch (JdwpException ex)
215: {
216: reply.setErrorCode(ex.getErrorCode ());
217: }
218: _connection.sendPacket (reply);
219: }
220: }
221: }