1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package fi.vtt.simantics.procore;
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;
20 import org.simantics.db.ServerReference;
21 import org.simantics.db.common.utils.Logger;
24 * @author Toni Kalajainen
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
31 public ProCoreServerReference() {
32 dbFolder = Paths.get("/"); // Assuming we don't have write rights to root.
34 socketAddress = InetSocketAddress.createUnresolved("127.0.0.0", 0);
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;
41 socketAddress = InetSocketAddress.createUnresolved("127.0.0.0", 0);
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;
51 // socketAddress = InetSocketAddress.createUnresolved("127.0.0.0", 0);
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]));
62 public ProCoreServerReference(Path dbFolder, String host, int port) {
63 this.dbFolder = dbFolder;
64 this.socketAddress = new InetSocketAddress(host, port);
68 public ProCoreServerReference(Path dbFolder, String host, int port, String dbid) {
69 this.dbFolder = dbFolder;
70 this.socketAddress = InetSocketAddress.createUnresolved(host, port);
74 public ProCoreServerReference(Path dbFolder, InetSocketAddress socketAddress) {
75 this.dbFolder = dbFolder;
76 this.socketAddress = socketAddress;
80 public ProCoreServerReference(Path dbFolder, InetSocketAddress socketAddress, String dbid) {
81 this.dbFolder = dbFolder;
82 this.socketAddress = socketAddress;
87 public int hashCode() {
88 return dbFolder.hashCode();
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;
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);
105 public String toString() {
106 return dbFolder.toString();
109 public Path getDBFolder() {