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 java.io.Serializable; |
26 |
|
27 |
import org.w3c.dom.Document; |
28 |
import org.w3c.dom.Element; |
29 |
import org.w3c.dom.NamedNodeMap; |
30 |
import org.w3c.dom.Node; |
31 |
import org.w3c.dom.NodeList; |
32 |
|
33 |
|
34 |
/** |
35 |
* |
36 |
* @author Grigor Iliev |
37 |
*/ |
38 |
public class Server extends Resource implements Serializable { |
39 |
private String address = "127.0.0.1"; |
40 |
private int port = 8888; |
41 |
|
42 |
/** |
43 |
* Creates a new instance of <code>Server</code> |
44 |
*/ |
45 |
public |
46 |
Server() { |
47 |
|
48 |
} |
49 |
|
50 |
/** |
51 |
* Gets the address of the server. |
52 |
* @return The address of the server. |
53 |
*/ |
54 |
public String |
55 |
getAddress() { return address; } |
56 |
|
57 |
/** |
58 |
* Sets the address of the server. |
59 |
* @param name The new address of the server. |
60 |
*/ |
61 |
public void |
62 |
setAddress(String address) { |
63 |
this.address = address; |
64 |
fireChangeEvent(); |
65 |
} |
66 |
|
67 |
/** |
68 |
* Gets the port to which to connect. |
69 |
* @return The port to which to connect. |
70 |
*/ |
71 |
public int |
72 |
getPort() { return port; } |
73 |
|
74 |
/** |
75 |
* Sets the port to which to connect. |
76 |
* @param name The new port to which to connect. |
77 |
*/ |
78 |
public void |
79 |
setPort(int port) { |
80 |
this.port = port; |
81 |
fireChangeEvent(); |
82 |
} |
83 |
|
84 |
/** Determines whether this server is on the local host. */ |
85 |
public boolean |
86 |
isLocal() { |
87 |
if(getAddress() == null) return false; |
88 |
if("127.0.0.1".equals(getAddress())) return true; |
89 |
if("localhost".equalsIgnoreCase(getAddress())) return true; |
90 |
return false; |
91 |
} |
92 |
|
93 |
/** |
94 |
* Reads and sets the the server information provided by <code>node</code>. |
95 |
* @param node The node providing the server information. |
96 |
* @throws IllegalArgumentException If an error occurs while |
97 |
* reading the server information. |
98 |
*/ |
99 |
public void |
100 |
readObject(Node node) { |
101 |
if( |
102 |
node.getNodeType() != Node.ELEMENT_NODE || |
103 |
!(node.getNodeName().equals("server")) |
104 |
) { |
105 |
throw new IllegalArgumentException("Not a server node!"); |
106 |
} |
107 |
|
108 |
NamedNodeMap nnm = node.getAttributes(); |
109 |
Node n = nnm.getNamedItem("name"); |
110 |
if(n == null) { |
111 |
throw new IllegalArgumentException("The server name is undefined!"); |
112 |
} |
113 |
DOMUtils.validateTextAttr(n); |
114 |
setName(n.getNodeValue()); |
115 |
|
116 |
|
117 |
String s = null; |
118 |
NodeList nl = node.getChildNodes(); |
119 |
|
120 |
for(int i = 0; i < nl.getLength(); i++) { |
121 |
node = nl.item(i); |
122 |
if(node.getNodeType() != Node.ELEMENT_NODE) continue; |
123 |
|
124 |
s = node.getNodeName(); |
125 |
if(s.equals("description")) { |
126 |
if(node.hasChildNodes()) { |
127 |
DOMUtils.validateTextContent(node); |
128 |
setDescription(node.getFirstChild().getNodeValue()); |
129 |
} |
130 |
} else if(s.equals("address")) { |
131 |
if(node.hasChildNodes()) { |
132 |
DOMUtils.validateTextContent(node); |
133 |
setAddress(node.getFirstChild().getNodeValue()); |
134 |
} |
135 |
} else if(s.equals("port")) { |
136 |
if(node.hasChildNodes()) { |
137 |
DOMUtils.validateTextContent(node); |
138 |
String port = node.getFirstChild().getNodeValue(); |
139 |
int p; |
140 |
try { p = Integer.parseInt(port); } |
141 |
catch(Exception e) { |
142 |
throw new IllegalArgumentException("Invalid port!"); |
143 |
} |
144 |
|
145 |
setPort(p); |
146 |
} |
147 |
} else { // Unknown content |
148 |
CC.getLogger().info ("Unknown field: " + s); |
149 |
} |
150 |
} |
151 |
} |
152 |
|
153 |
/** |
154 |
* Writes the content of this server info to the |
155 |
* specified node of document <code>doc</code>. |
156 |
* @param doc The document containing <code>node</code>. |
157 |
* @param node Specifies the node where the content of this server info |
158 |
* should be written. |
159 |
*/ |
160 |
public void |
161 |
writeObject(Document doc, Node node) { |
162 |
Element el = doc.createElement("server"); |
163 |
el.setAttribute("name", getName()); |
164 |
node.appendChild(el); |
165 |
|
166 |
node = el; |
167 |
|
168 |
el = doc.createElement("description"); |
169 |
el.appendChild(doc.createTextNode(getDescription())); |
170 |
node.appendChild(el); |
171 |
|
172 |
el = doc.createElement("address"); |
173 |
el.appendChild(doc.createTextNode(getAddress())); |
174 |
node.appendChild(el); |
175 |
|
176 |
el = doc.createElement("port"); |
177 |
el.appendChild(doc.createTextNode(String.valueOf(getPort()))); |
178 |
node.appendChild(el); |
179 |
} |
180 |
|
181 |
@Override |
182 |
public String |
183 |
toString() { return getName(); } |
184 |
} |