1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler; |
24 |
|
25 |
import org.w3c.dom.Document; |
26 |
import org.w3c.dom.Element; |
27 |
import org.w3c.dom.NamedNodeMap; |
28 |
import org.w3c.dom.Node; |
29 |
import org.w3c.dom.NodeList; |
30 |
|
31 |
|
32 |
/** |
33 |
* |
34 |
* @author Grigor Iliev |
35 |
*/ |
36 |
public class Server extends Resource { |
37 |
private String address = "127.0.0.1"; |
38 |
private int port = 8888; |
39 |
|
40 |
/** |
41 |
* Creates a new instance of <code>Server</code> |
42 |
*/ |
43 |
public |
44 |
Server() { |
45 |
|
46 |
} |
47 |
|
48 |
/** |
49 |
* Gets the address of the server. |
50 |
* @return The address of the server. |
51 |
*/ |
52 |
public String |
53 |
getAddress() { return address; } |
54 |
|
55 |
/** |
56 |
* Sets the address of the server. |
57 |
* @param name The new address of the server. |
58 |
*/ |
59 |
public void |
60 |
setAddress(String name) { |
61 |
this.address = address; |
62 |
fireChangeEvent(); |
63 |
} |
64 |
|
65 |
/** |
66 |
* Gets the port to which to connect. |
67 |
* @return The port to which to connect. |
68 |
*/ |
69 |
public int |
70 |
getPort() { return port; } |
71 |
|
72 |
/** |
73 |
* Sets the port to which to connect. |
74 |
* @param name The new port to which to connect. |
75 |
*/ |
76 |
public void |
77 |
setPort(int port) { |
78 |
this.port = port; |
79 |
fireChangeEvent(); |
80 |
} |
81 |
|
82 |
/** |
83 |
* Reads and sets the the server information provided by <code>node</code>. |
84 |
* @param node The node providing the server information. |
85 |
* @throws IllegalArgumentException If an error occurs while |
86 |
* reading the server information. |
87 |
*/ |
88 |
public void |
89 |
readObject(Node node) { |
90 |
if( |
91 |
node.getNodeType() != Node.ELEMENT_NODE || |
92 |
!(node.getNodeName().equals("server")) |
93 |
) { |
94 |
throw new IllegalArgumentException("Not a server node!"); |
95 |
} |
96 |
|
97 |
NamedNodeMap nnm = node.getAttributes(); |
98 |
Node n = nnm.getNamedItem("name"); |
99 |
if(n == null) { |
100 |
throw new IllegalArgumentException("The server name is undefined!"); |
101 |
} |
102 |
DOMUtils.validateTextContent(n); |
103 |
setName(n.getFirstChild().getNodeValue()); |
104 |
|
105 |
|
106 |
String s = null; |
107 |
NodeList nl = node.getChildNodes(); |
108 |
|
109 |
for(int i = 0; i < nl.getLength(); i++) { |
110 |
node = nl.item(i); |
111 |
if(node.getNodeType() != Node.ELEMENT_NODE) continue; |
112 |
|
113 |
s = node.getNodeName(); |
114 |
if(s.equals("description")) { |
115 |
if(node.hasChildNodes()) { |
116 |
DOMUtils.validateTextContent(node); |
117 |
setDescription(node.getFirstChild().getNodeValue()); |
118 |
} |
119 |
} else if(s.equals("address")) { |
120 |
if(node.hasChildNodes()) { |
121 |
DOMUtils.validateTextContent(node); |
122 |
setAddress(node.getFirstChild().getNodeValue()); |
123 |
} |
124 |
} else if(s.equals("port")) { |
125 |
if(node.hasChildNodes()) { |
126 |
DOMUtils.validateTextContent(node); |
127 |
String port = node.getFirstChild().getNodeValue(); |
128 |
int p; |
129 |
try { p = Integer.parseInt(port); } |
130 |
catch(Exception e) { |
131 |
throw new IllegalArgumentException("Invalid port!"); |
132 |
} |
133 |
|
134 |
setPort(p); |
135 |
} |
136 |
} else { // Unknown content |
137 |
CC.getLogger().info ("Unknown field: " + s); |
138 |
} |
139 |
} |
140 |
} |
141 |
|
142 |
/** |
143 |
* Writes the content of this server info to the |
144 |
* specified node of document <code>doc</code>. |
145 |
* @param doc The document containing <code>node</code>. |
146 |
* @param node Specifies the node where the content of this server info |
147 |
* should be written. |
148 |
*/ |
149 |
public void |
150 |
writeObject(Document doc, Node node) { |
151 |
Element el = doc.createElement("server"); |
152 |
el.setAttribute("name", getName()); |
153 |
node.appendChild(el); |
154 |
|
155 |
node = el; |
156 |
|
157 |
el = doc.createElement("description"); |
158 |
el.appendChild(doc.createTextNode(getDescription())); |
159 |
node.appendChild(el); |
160 |
|
161 |
el = doc.createElement("address"); |
162 |
el.appendChild(doc.createTextNode(getAddress())); |
163 |
node.appendChild(el); |
164 |
|
165 |
el = doc.createElement("port"); |
166 |
el.appendChild(doc.createTextNode(String.valueOf(getPort()))); |
167 |
node.appendChild(el); |
168 |
} |
169 |
|
170 |
public String |
171 |
toString() { return getName(); } |
172 |
} |