From e8e0da7cae7aae29ef145f2d91e39a93c4336b92 Mon Sep 17 00:00:00 2001 From: Johann Latocha Date: Tue, 31 Jul 2012 17:26:09 +0200 Subject: jdom -> simple xml --- src/main/java/org/openslx/Test.java | 66 -------------- src/main/java/org/openslx/dnbd3/DNBD3Client.java | 29 ------ src/main/java/org/openslx/dnbd3/DNBD3Image.java | 91 ------------------- src/main/java/org/openslx/dnbd3/DNBD3Server.java | 96 ++++---------------- src/main/java/org/openslx/dnbd3/Test.java | 63 +++++++++++++ src/main/java/org/openslx/dnbd3/xml/Client.java | 33 +++++++ src/main/java/org/openslx/dnbd3/xml/Image.java | 111 +++++++++++++++++++++++ src/main/java/org/openslx/dnbd3/xml/Info.java | 34 +++++++ 8 files changed, 259 insertions(+), 264 deletions(-) delete mode 100644 src/main/java/org/openslx/Test.java delete mode 100644 src/main/java/org/openslx/dnbd3/DNBD3Client.java delete mode 100644 src/main/java/org/openslx/dnbd3/DNBD3Image.java create mode 100644 src/main/java/org/openslx/dnbd3/Test.java create mode 100644 src/main/java/org/openslx/dnbd3/xml/Client.java create mode 100644 src/main/java/org/openslx/dnbd3/xml/Image.java create mode 100644 src/main/java/org/openslx/dnbd3/xml/Info.java (limited to 'src/main') diff --git a/src/main/java/org/openslx/Test.java b/src/main/java/org/openslx/Test.java deleted file mode 100644 index 111e9f2..0000000 --- a/src/main/java/org/openslx/Test.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.openslx; - -import java.io.IOException; -import java.util.List; - -import org.openslx.dnbd3.DNBD3Client; -import org.openslx.dnbd3.DNBD3Exception; -import org.openslx.dnbd3.DNBD3Image; -import org.openslx.dnbd3.DNBD3Server; - -public class Test { - - public static void main(String[] args) throws IOException { - - try { - DNBD3Server server = new DNBD3Server(); - - String group = "Test"; - int vid = 10; - int rid = 0; - String path = "/home/jjl/Data/ISOs/Ubuntu/ubuntu-10.04.3-desktop-i386.iso"; - String servers = "132.230.4.29;132.230.4.220;10.1.1.1;10.1.1.2"; - String cache = "/tmp/image.iso.cache"; - DNBD3Image image = new DNBD3Image(group, vid, rid, path, servers, cache); - server.addImage(image); - - System.out.println("Exported images (group, atime, vid, rid, path, servers, cache):"); - System.out.println("==============================================================="); - List images = server.getImages(); - for (DNBD3Image i : images) { - System.out.print(i.getGroup() + "\t"); - System.out.print(i.getAtime() + "\t"); - System.out.print(i.getVid() + "\t"); - System.out.print(i.getRid() + "\t"); - System.out.print(i.getPath() + "\t"); - System.out.print(i.getServers() + "\t"); - System.out.print(i.getCache()); - System.out.println(); - } - System.out.println(); - System.out.println("Number images: " + images.size()); - System.out.println(); - - System.out.println("Connected clients (ip, file)"); - System.out.println("============================"); - List clients = server.getClients(); - for (DNBD3Client client : clients) { - System.out.print(client.getIp() + "\t"); - System.out.print(client.getImage()); - System.out.println(); - } - System.out.println(); - System.out.println("Number clients: " + clients.size()); - System.out.println(); - - - // server.doShutdown(); - // server.doReload(); - - } catch (DNBD3Exception e) { - System.out.println(e); - - } - } - -} diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Client.java b/src/main/java/org/openslx/dnbd3/DNBD3Client.java deleted file mode 100644 index 6608bc8..0000000 --- a/src/main/java/org/openslx/dnbd3/DNBD3Client.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.openslx.dnbd3; - -public class DNBD3Client { - - private String ip; - private String image; - - public DNBD3Client(String ip, String image) { - this.ip = ip; - this.image = image; - } - - public String getIp() { - return ip; - } - - public void setIp(String ip) { - this.ip = ip; - } - - public String getImage() { - return image; - } - - public void setImage(String image) { - this.image = image; - } - -} diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Image.java b/src/main/java/org/openslx/dnbd3/DNBD3Image.java deleted file mode 100644 index 98d8fc8..0000000 --- a/src/main/java/org/openslx/dnbd3/DNBD3Image.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.openslx.dnbd3; - -public class DNBD3Image { - - private String group; - private String atime; - private int vid; - private int rid; - private String path; - private String servers; - private String cache; - - public DNBD3Image(String group, String atime, int vid, int rid, String path, String servers, String cache) { - this.group = group; - this.atime = atime; - this.vid = vid; - this.rid = rid; - this.path = path; - this.servers = servers; - this.cache = cache; - } - - public DNBD3Image(String group, int vid, int rid, String path, String servers, String cache) { - this(group, "01.01.70 01:00:00", vid, rid, path, servers, cache); - } - - public DNBD3Image(String group, int vid, int rid, String path, String servers) { - this(group, "01.01.70 01:00:00", vid, rid, path, servers, ""); - } - - public DNBD3Image(String group, int vid, String path, String servers) { - this(group, "01.01.70 01:00:00", vid, 0, path, servers, ""); - } - - public String getGroup() { - return group; - } - - public void setGroup(String group) { - this.group = group; - } - - public String getAtime() { - return atime; - } - - public void setAtime(String atime) { - this.atime = atime; - } - - public int getVid() { - return vid; - } - - public void setVid(int vid) { - this.vid = vid; - } - - public int getRid() { - return rid; - } - - public void setRid(int rid) { - this.rid = rid; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public String getServers() { - return servers; - } - - public void setServers(String servers) { - this.servers = servers; - } - - public String getCache() { - return cache; - } - - public void setCache(String cache) { - this.cache = cache; - } - -} diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Server.java b/src/main/java/org/openslx/dnbd3/DNBD3Server.java index 36646b5..1ac2e73 100644 --- a/src/main/java/org/openslx/dnbd3/DNBD3Server.java +++ b/src/main/java/org/openslx/dnbd3/DNBD3Server.java @@ -1,23 +1,17 @@ package org.openslx.dnbd3; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.nio.ByteBuffer; -import java.util.ArrayList; import java.util.List; -import org.jdom2.Attribute; -import org.jdom2.Document; -import org.jdom2.Element; -import org.jdom2.JDOMException; -import org.jdom2.filter.Filters; -import org.jdom2.input.SAXBuilder; -import org.jdom2.output.Format; -import org.jdom2.output.XMLOutputter; -import org.jdom2.xpath.XPathExpression; -import org.jdom2.xpath.XPathFactory; +import org.openslx.dnbd3.xml.Client; +import org.openslx.dnbd3.xml.Image; +import org.openslx.dnbd3.xml.Info; +import org.simpleframework.xml.core.Persister; public class DNBD3Server { @@ -43,88 +37,34 @@ public class DNBD3Server { sock.close(); } - public void addImage(DNBD3Image image) throws IOException, DNBD3Exception { - - Element rootNode = new Element("dnbd3-server"); - Element imageNode = new Element("image"); - imageNode.setAttribute(new Attribute("group", image.getGroup())); - imageNode.setAttribute(new Attribute("atime", image.getAtime())); - imageNode.setAttribute(new Attribute("vid", String.valueOf(image.getVid()))); - imageNode.setAttribute(new Attribute("rid", String.valueOf(image.getRid()))); - imageNode.setAttribute(new Attribute("file", image.getPath())); - imageNode.setAttribute(new Attribute("servers", image.getServers())); - imageNode.setAttribute(new Attribute("cache_file", image.getCache())); - rootNode.addContent(imageNode); - Document doc = new Document(rootNode); + public void addImage(Image image) throws Exception { + Info info = new Info(); + info.getImages().add(image); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + new Persister().write(info, os); Socket sock = new Socket(HOST, PORT); - OutputStream os = sock.getOutputStream(); - XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); - sendHeader(sock, new DNBD3Header(CMDCONFIG, outputter.outputString(doc).length(), 0)); - outputter.output(doc, os); + sendHeader(sock, new DNBD3Header(CMDCONFIG, os.size(), 0)); + sock.getOutputStream().write(os.toByteArray()); receiveHeader(sock); sock.close(); } - public List getImages() throws IOException, DNBD3Exception { - List images = new ArrayList(); + public List getImages() throws Exception { Socket sock = new Socket(HOST, PORT); sendHeader(sock, new DNBD3Header(CMDINFO, 0, 0)); receiveHeader(sock); - - try { - String group, atime, path, servers, cache; - int vid, rid; - InputStream is = sock.getInputStream(); - SAXBuilder builder = new SAXBuilder(); - Document document = (Document) builder.build(is); - XPathFactory xpfac = XPathFactory.instance(); - XPathExpression xp; - xp = xpfac.compile("//dnbd3-server/images/image", Filters.element()); - for (Element e : xp.evaluate(document)) { - group = e.getAttributeValue("group"); - atime = e.getAttributeValue("atime"); - vid = Integer.parseInt(e.getAttributeValue("vid")); - rid = Integer.parseInt(e.getAttributeValue("rid")); - path = e.getAttributeValue("file"); - servers = e.getAttributeValue("servers"); - cache = e.getAttributeValue("cache_file"); - images.add(new DNBD3Image(group, atime, vid, rid, path, servers, cache)); - } - - } catch (JDOMException ex) { - ex.printStackTrace(); - } - + Info info = new Persister().read(Info.class, sock.getInputStream()); sock.close(); - return images; + return info.getImages(); } - public List getClients() throws IOException, DNBD3Exception { - List clients = new ArrayList(); + public List getClients() throws Exception { Socket sock = new Socket(HOST, PORT); sendHeader(sock, new DNBD3Header(CMDINFO, 0, 0)); receiveHeader(sock); - - try { - String ip, image; - InputStream is = sock.getInputStream(); - SAXBuilder builder = new SAXBuilder(); - Document document = (Document) builder.build(is); - XPathFactory xpfac = XPathFactory.instance(); - XPathExpression xp; - xp = xpfac.compile("//dnbd3-server/clients/client", Filters.element()); - for (Element e : xp.evaluate(document)) { - ip = e.getAttributeValue("ip"); - image = e.getAttributeValue("file"); - clients.add(new DNBD3Client(ip, image)); - } - - } catch (JDOMException ex) { - ex.printStackTrace(); - } - + Info info = new Persister().read(Info.class, sock.getInputStream()); sock.close(); - return clients; + return info.getClients(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/main/java/org/openslx/dnbd3/Test.java b/src/main/java/org/openslx/dnbd3/Test.java new file mode 100644 index 0000000..c94348c --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/Test.java @@ -0,0 +1,63 @@ +package org.openslx.dnbd3; + +import java.util.List; + +import org.openslx.dnbd3.xml.Client; +import org.openslx.dnbd3.xml.Image; + +public class Test { + + public static void main(String[] args) throws Exception { + + try { + DNBD3Server server = new DNBD3Server(); + + String group = "Test 11.11"; + int vid = 10; + int rid = 0; + String path = "/home/jjl/Data/ISOs/Ubuntu/ubuntu-10.04.3-desktop-i386.iso"; + String servers = "132.230.4.29;132.230.4.220;10.1.1.1"; + String cache = "/tmp/image.iso.cache"; + Image image = new Image(group, vid, rid, path, servers, cache); + server.addImage(image); + + server.doReload(); + + System.out.println("Exported images (group, atime, vid, rid, path, servers, cache):"); + System.out.println("==============================================================="); + List images = server.getImages(); + for (Image i : images) { + System.out.print(i.getGroup() + "\t"); + System.out.print(i.getAtime() + "\t"); + System.out.print(i.getVid() + "\t"); + System.out.print(i.getRid() + "\t"); + System.out.print(i.getFile() + "\t"); + System.out.print(i.getServers() + "\t"); + System.out.print(i.getCache()); + System.out.println(); + } + System.out.println(); + System.out.println("Number images: " + images.size()); + System.out.println(); + + System.out.println("Connected clients (ip, file)"); + System.out.println("============================"); + List clients = server.getClients(); + for (Client client : clients) { + System.out.print(client.getIp() + "\t"); + System.out.print(client.getFile()); + System.out.println(); + } + System.out.println(); + System.out.println("Number clients: " + clients.size()); + System.out.println(); + + // server.doShutdown(); + + } catch (DNBD3Exception e) { + System.out.println(e); + + } + } + +} diff --git a/src/main/java/org/openslx/dnbd3/xml/Client.java b/src/main/java/org/openslx/dnbd3/xml/Client.java new file mode 100644 index 0000000..8cc7879 --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/xml/Client.java @@ -0,0 +1,33 @@ +package org.openslx.dnbd3.xml; + +import org.simpleframework.xml.Attribute; + +public class Client { + + @Attribute + private String ip; + @Attribute + private String file; + + public Client(@Attribute(name="ip") String ip, @Attribute(name="file") String file) { + this.ip = ip; + this.file = file; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + +} diff --git a/src/main/java/org/openslx/dnbd3/xml/Image.java b/src/main/java/org/openslx/dnbd3/xml/Image.java new file mode 100644 index 0000000..84ad9d9 --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/xml/Image.java @@ -0,0 +1,111 @@ +package org.openslx.dnbd3.xml; + +import org.simpleframework.xml.Attribute; + +public class Image { + + @Attribute + private String group; + @Attribute + private String atime; + @Attribute + private int vid; + @Attribute + private int rid; + @Attribute + private String file; + @Attribute + private String servers; + @Attribute + private String cache; + + public Image(@Attribute(name = "group") String group, @Attribute(name = "atime") String atime, + @Attribute(name = "vid") int vid, @Attribute(name = "rid") int rid, @Attribute(name = "file") String file, + @Attribute(name = "servers") String servers, @Attribute(name = "cache") String cache) { + + this.group = group; + this.atime = atime; + this.vid = vid; + this.rid = rid; + this.file = file; + this.servers = servers; + this.cache = cache; + } + + public Image(@Attribute(name = "group") String group, @Attribute(name = "vid") int vid, + @Attribute(name = "rid") int rid, @Attribute(name = "file") String file, + @Attribute(name = "servers") String servers, @Attribute(name = "cache") String cache) { + + this(group, "01.01.70 01:00:00", vid, rid, file, servers, cache); + } + + public Image(@Attribute(name = "group") String group, @Attribute(name = "vid") int vid, + @Attribute(name = "rid") int rid, @Attribute(name = "file") String file, + @Attribute(name = "servers") String servers) { + + this(group, "01.01.70 01:00:00", vid, rid, file, servers, ""); + } + + public Image(@Attribute(name = "group") String group, @Attribute(name = "vid") int vid, + @Attribute(name = "file") String file, @Attribute(name = "servers") String servers) { + + this(group, "01.01.70 01:00:00", vid, 0, file, servers, ""); + } + + public String getGroup() { + return group; + } + + public void setGroup(String group) { + this.group = group; + } + + public String getAtime() { + return atime; + } + + public void setAtime(String atime) { + this.atime = atime; + } + + public int getVid() { + return vid; + } + + public void setVid(int vid) { + this.vid = vid; + } + + public int getRid() { + return rid; + } + + public void setRid(int rid) { + this.rid = rid; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + + public String getServers() { + return servers; + } + + public void setServers(String servers) { + this.servers = servers; + } + + public String getCache() { + return cache; + } + + public void setCache(String cache) { + this.cache = cache; + } + +} diff --git a/src/main/java/org/openslx/dnbd3/xml/Info.java b/src/main/java/org/openslx/dnbd3/xml/Info.java new file mode 100644 index 0000000..7b817ac --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/xml/Info.java @@ -0,0 +1,34 @@ +package org.openslx.dnbd3.xml; + +import java.util.ArrayList; +import java.util.List; + +import org.simpleframework.xml.ElementList; +import org.simpleframework.xml.Root; + +@Root +public class Info { + + @ElementList + private List images = new ArrayList(); + + @ElementList + private List clients = new ArrayList(); + + public List getImages() { + return images; + } + + public void setImages(List images) { + this.images = images; + } + + public List getClients() { + return clients; + } + + public void setClients(List clients) { + this.clients = clients; + } + +} -- cgit v1.2.3-55-g7522