Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5b2e8b3b8baf56371f880028c199d9c8be82b6e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.jsdt.debug.internal.chrome.transport;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.wst.jsdt.debug.transport.packet.Request;

/**
 * Request packet
 * 
 * @since 1.0
 */
public class RequestPacketImpl extends PacketImpl implements Request {

	/**
	 * The type of this packet
	 */
	public static final String REQUEST = "request"; //$NON-NLS-1$
	private static int next_seq = 0;
	private final String command;
	private final int seq;
	private Map args = Collections.synchronizedMap(new HashMap(4));
	
	/**
	 * Constructor
	 * 
	 * @param command
	 */
	public RequestPacketImpl(String command) {
		super(REQUEST);
		if(command == null) {
			throw new IllegalArgumentException(Messages.cannot_create_request_null_command);
		}
		this.command = command;
		this.seq = ++next_seq;
	}
	
	/**
	 * Constructor
	 * @param json
	 */
	public RequestPacketImpl(Map json) {
		super(json);
		this.command = (String) json.get(Attributes.COMMAND);
		if(command == null) {
			throw new IllegalArgumentException(Messages.no_command_in_request_json);
		}
		this.seq = ((Number)json.get(Attributes.SEQ)).intValue();
		Map pargs = (Map) json.get(Attributes.ARGUMENTS);
		if(pargs != null) {
			args.putAll(pargs);
		}
	}

	/**
	 * Sets the given argument in the JSON map.
	 * 
	 * @param key the key for the attribute, <code>null</code> is not accepted
	 * @param argument the value for the argument, <code>null</code> is not accepted
	 */
	public void setArgument(String key, Object argument) {
		if(key == null) {
			throw new IllegalArgumentException("The argument key cannot be null"); //$NON-NLS-1$
		}
		if(argument == null) {
			throw new IllegalArgumentException("A null argument is not allowed"); //$NON-NLS-1$
		}
		args.put(key, argument);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.debug.transport.packet.Request#getCommand()
	 */
	public String getCommand() {
		return command;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.debug.transport.packet.Request#getSequence()
	 */
	public int getSequence() {
		return seq;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.debug.transport.packet.Request#getArguments()
	 */
	public Map getArguments() {
		return args;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.debug.internal.crossfire.transport.CFPacket#toJSON()
	 */
	public Map toJSON() {
		Map json = super.toJSON();
		json.put(Attributes.SEQ, new Integer(seq));
		json.put(Attributes.COMMAND, command);
		if(!args.isEmpty()) {
			json.put(Attributes.ARGUMENTS, args);
		}
		return json;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		Object json = toJSON();
		buffer.append("RequestPacketImpl: "); //$NON-NLS-1$
		JSON.writeValue(json, buffer);
		return buffer.toString();
	}
}

Back to the top