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 |
/** Determines whether this server is on the local host. */ |
83 |
public boolean |
84 |
isLocal() { |
85 |
if(getAddress() == null) return false; |
86 |
if("127.0.0.1".equals(getAddress())) return true; |
87 |
if("localhost".equals(getAddress())) return true; |
88 |
return false; |
89 |
} |
90 |
|
91 |
/** |
92 |
* Reads and sets the the server information provided by <code>node</code>. |
93 |
* @param node The node providing the server information. |
94 |
* @throws IllegalArgumentException If an error occurs while |
95 |
* reading the server information. |
96 |
*/ |
97 |
public void |
98 |
readObject(Node node) { |
99 |
if( |
100 |
node.getNodeType() != Node.ELEMENT_NODE || |
101 |
!(node.getNodeName().equals("server")) |
102 |
) { |
103 |
throw new IllegalArgumentException("Not a server node!"); |
104 |
} |
105 |
|
106 |
NamedNodeMap nnm = node.getAttributes(); |
107 |
Node n = nnm.getNamedItem("name"); |
108 |
if(n == null) { |
109 |
throw new IllegalArgumentException("The server name is undefined!"); |
110 |
} |
111 |
DOMUtils.validateTextContent(n); |
112 |
setName(n.getFirstChild().getNodeValue()); |
113 |
|
114 |
|
115 |
String s = null; |
116 |
NodeList nl = node.getChildNodes(); |
117 |
|
118 |
for(int i = 0; i < nl.getLength(); i++) { |
119 |
node = nl.item(i); |
120 |
if(node.getNodeType() != Node.ELEMENT_NODE) continue; |
121 |
|
122 |
s = node.getNodeName(); |
123 |
if(s.equals("description")) { |
124 |
if(node.hasChildNodes()) { |
125 |
DOMUtils.validateTextContent(node); |
126 |
setDescription(node.getFirstChild().getNodeValue()); |
127 |
} |
128 |
} else if(s.equals("address")) { |
129 |
if(node.hasChildNodes()) { |
130 |
DOMUtils.validateTextContent(node); |
131 |
setAddress(node.getFirstChild().getNodeValue()); |
132 |
} |
133 |
} else if(s.equals("port")) { |
134 |
if(node.hasChildNodes()) { |
135 |
DOMUtils.validateTextContent(node); |
136 |
String port = node.getFirstChild().getNodeValue(); |
137 |
int p; |
138 |
try { p = Integer.parseInt(port); } |
139 |
catch(Exception e) { |
140 |
throw new IllegalArgumentException("Invalid port!"); |
141 |
} |
142 |
|
143 |
setPort(p); |
144 |
} |
145 |
} else { // Unknown content |
146 |
CC.getLogger().info ("Unknown field: " + s); |
147 |
} |
148 |
} |
149 |
} |
150 |
|
151 |
/** |
152 |
* Writes the content of this server info to the |
153 |
* specified node of document <code>doc</code>. |
154 |
* @param doc The document containing <code>node</code>. |
155 |
* @param node Specifies the node where the content of this server info |
156 |
* should be written. |
157 |
*/ |
158 |
public void |
159 |
writeObject(Document doc, Node node) { |
160 |
Element el = doc.createElement("server"); |
161 |
el.setAttribute("name", getName()); |
162 |
node.appendChild(el); |
163 |
|
164 |
node = el; |
165 |
|
166 |
el = doc.createElement("description"); |
167 |
el.appendChild(doc.createTextNode(getDescription())); |
168 |
node.appendChild(el); |
169 |
|
170 |
el = doc.createElement("address"); |
171 |
el.appendChild(doc.createTextNode(getAddress())); |
172 |
node.appendChild(el); |
173 |
|
174 |
el = doc.createElement("port"); |
175 |
el.appendChild(doc.createTextNode(String.valueOf(getPort()))); |
176 |
node.appendChild(el); |
177 |
} |
178 |
|
179 |
public String |
180 |
toString() { return getName(); } |
181 |
} |