]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/ProCoreServerReference.java
Merge branch 'master' into private/eclipse-4.7
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / ProCoreServerReference.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package fi.vtt.simantics.procore;
13
14 import java.io.IOException;
15 import java.net.InetSocketAddress;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19
20 import org.simantics.db.ServerReference;
21 import org.simantics.db.common.utils.Logger;
22
23 /**
24  * @author Toni Kalajainen
25  */
26 public final class ProCoreServerReference implements ServerReference {
27     public final Path dbFolder; // ProCoreServer is identified by database folder.
28     public final InetSocketAddress socketAddress; // deprecated
29     public final String dbid; // deprecated
30
31     public ProCoreServerReference() {
32     dbFolder = Paths.get("/"); // Assuming we don't have write rights to root.
33     dbid = "";
34     socketAddress = InetSocketAddress.createUnresolved("127.0.0.0", 0);
35     }
36     public ProCoreServerReference(Path dbFolder) {
37         if (null == dbFolder) // Null means that caller does not care what the value is.
38             dbFolder = Paths.get("/"); // Assuming we don't have write rights to root.
39         this.dbFolder = dbFolder;
40         dbid = "";
41         socketAddress = InetSocketAddress.createUnresolved("127.0.0.0", 0);
42     }
43 //    public ProCoreServerReference(ServerAddress address) throws InternalException {
44 //        Path dbFolder = Paths.get(address.getDbid()); // Maybe it would be better to let address resolve folder?
45 //        if (Files.isDirectory(dbFolder))
46 //            throw new ProCoreException("Server address does not contain valid database folder. address=" + address);
47 //        if (null == dbFolder) // Null means that caller does not care what the value is.
48 //            dbFolder = Paths.get("/"); // Assuming we don't have write rights to root.
49 //        this.dbFolder = dbFolder;
50 //        dbid = "";
51 //        socketAddress = InetSocketAddress.createUnresolved("127.0.0.0", 0);
52 //    }
53     public ProCoreServerReference(Path dbFolder, String hostAndPort) {
54         this.dbFolder = dbFolder;
55         String[] split = hostAndPort.split(":");
56         if (split.length != 2)
57             throw new IllegalArgumentException("address does not contain a port, missing ':' character");
58         this.socketAddress = InetSocketAddress.createUnresolved(split[0], Integer.parseInt(split[1]));
59         this.dbid = null;
60     }
61
62     public ProCoreServerReference(Path dbFolder, String host, int port) {
63         this.dbFolder = dbFolder;
64         this.socketAddress = new InetSocketAddress(host, port);
65         this.dbid = null;
66     }
67
68     public ProCoreServerReference(Path dbFolder, String host, int port, String dbid) {
69         this.dbFolder = dbFolder;
70         this.socketAddress = InetSocketAddress.createUnresolved(host, port);
71         this.dbid = dbid;
72     }
73
74     public ProCoreServerReference(Path dbFolder, InetSocketAddress socketAddress) {
75         this.dbFolder = dbFolder;
76         this.socketAddress = socketAddress;
77         this.dbid = null;
78     }
79
80     public ProCoreServerReference(Path dbFolder, InetSocketAddress socketAddress, String dbid) {
81         this.dbFolder = dbFolder;
82         this.socketAddress = socketAddress;
83         this.dbid = dbid;
84     }
85
86     @Override
87     public int hashCode() {
88         return dbFolder.hashCode();
89     }
90
91     @Override
92     public boolean equals(Object other) {
93         if (this == other) return true;
94         if (other == null || !(getClass().equals(other.getClass()))) return false;
95         ProCoreServerReference r = (ProCoreServerReference) other;
96         try {
97             return Files.isSameFile(dbFolder, r.dbFolder);
98         } catch (IOException e) {
99             Logger.defaultLogError("Failed to compare db folders. f1=" + dbFolder + " f2=" + r.dbFolder, e);
100             return false;
101         }
102     }
103
104     @Override
105     public String toString() {
106         return dbFolder.toString();
107     }
108     @Override
109     public Path getDBFolder() {
110         return dbFolder;
111     }
112
113 }