1   package eu.fbk.dkm.pikes.tintopclient;
2   
3   import org.apache.log4j.Logger;
4   
5   import java.io.IOException;
6   import java.net.URL;
7   
8   /**
9    * Created by alessio on 25/02/15.
10   */
11  
12  public class TintopServer {
13  
14      static Logger logger = Logger.getLogger(TintopServer.class.getName());
15  
16      private String protocol;
17      private String host;
18      private String path;
19      private int port;
20  
21      private int id;
22  
23      public TintopServer(URL url) {
24          this(url, (int) (Math.random() * 32000 + 1));
25      }
26  
27      public TintopServer(URL url, int id) {
28          this.protocol = url.getProtocol();
29          this.host = url.getHost();
30          this.port = url.getPort();
31          this.path = url.getPath();
32          this.id = id;
33      }
34  
35      public TintopServer(String line) throws IOException {
36          String[] parts = line.split("\\s+");
37          if (parts.length != 4) {
38              throw new IOException("Invalid line");
39          }
40          this.protocol = parts[0].trim();
41          this.host = parts[1].trim();
42          this.port = Integer.parseInt(parts[2].trim());
43          this.path = parts[3].trim();
44      }
45  
46      public TintopServer(String protocol, String host, String path, int port) {
47          this.protocol = protocol;
48          this.host = host;
49          this.path = path;
50          this.port = port;
51      }
52  
53      public int getId() {
54          return id;
55      }
56  
57      public void setId(int id) {
58          this.id = id;
59      }
60  
61      public String getProtocol() {
62          return protocol;
63      }
64  
65      public void setProtocol(String protocol) {
66          this.protocol = protocol;
67      }
68  
69      public String getHost() {
70          return host;
71      }
72  
73      public void setHost(String host) {
74          this.host = host;
75      }
76  
77      public String getPath() {
78          return path;
79      }
80  
81      public void setPath(String path) {
82          this.path = path;
83      }
84  
85      public int getPort() {
86          return port;
87      }
88  
89      public void setPort(int port) {
90          this.port = port;
91      }
92  
93      public String getShortName() {
94          return String.format("%s://%s:%d%s", protocol, host, port, path);
95  
96      }
97  
98      @Override
99      public String toString() {
100         return "TintopServer{" +
101                 "protocol='" + protocol + '\'' +
102                 ", host='" + host + '\'' +
103                 ", path='" + path + '\'' +
104                 ", port=" + port +
105                 '}';
106     }
107 }