19
19
package com .tc .server ;
20
20
21
21
import java .io .FileNotFoundException ;
22
+ import java .io .IOException ;
22
23
import java .nio .file .Files ;
23
24
import java .nio .file .Path ;
24
25
import java .nio .file .Paths ;
26
+ import java .util .NoSuchElementException ;
27
+ import java .util .Optional ;
25
28
26
29
/**
27
30
* Knows how to find certain directories. You should try <em>everything</em> you can to avoid using this class; using it
@@ -46,15 +49,14 @@ public class Directories {
46
49
/**
47
50
*/
48
51
public static final String TC_SERVER_LIB_PROPERTY_NAME = "tc.server-lib" ;
52
+ /**
53
+ */
54
+ public static final String TC_SERVER_JAR_PROPERTY_NAME = "tc.server-jar" ;
49
55
/**
50
56
* The property "tc.install-root.ignore-checks", which is used for testing to ignore checks for the installation root
51
57
* directory.
52
58
*/
53
59
public static final String TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME = "tc.install-root.ignore-checks" ;
54
- /**
55
- * Relative location for default configuration file under Terracotta installation directory
56
- */
57
- public static final String DEFAULT_CONFIG_FILE_LOCATION = "conf/tc-config.xml" ;
58
60
59
61
/**
60
62
* Get installation root directory.
@@ -64,7 +66,7 @@ public class Directories {
64
66
* @throws FileNotFoundException If {@link #TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME} has not been set,
65
67
* this exception may be thrown if the installation root directory is not a directory
66
68
*/
67
- static Path getInstallationRoot () throws FileNotFoundException {
69
+ public static Path getInstallationRoot () throws FileNotFoundException {
68
70
boolean ignoreCheck = Boolean .getBoolean (TC_INSTALL_ROOT_IGNORE_CHECKS_PROPERTY_NAME );
69
71
if (ignoreCheck ) {
70
72
return Paths .get (System .getProperty ("user.dir" ));
@@ -87,36 +89,58 @@ static Path getInstallationRoot() throws FileNotFoundException {
87
89
}
88
90
}
89
91
90
- public static Path getDefaultConfigFile () throws FileNotFoundException {
91
- return getInstallationRoot ().resolve (DEFAULT_CONFIG_FILE_LOCATION );
92
+ public static Path getServerJar () throws FileNotFoundException {
93
+ String jar = System .getProperty (TC_SERVER_JAR_PROPERTY_NAME );
94
+ if (jar == null ) {
95
+ jar = "tc-server" ;
96
+ }
97
+ Path jarFile = searchForServerJar (getServerLibFolder (), jar );
98
+ if (jarFile == null ) {
99
+ jarFile = searchForServerJar (getInstallationRoot (), jar );
100
+ }
101
+
102
+ return jarFile ;
103
+ }
104
+
105
+ private static Path searchForServerJar (Path directory , String name ) {
106
+ try {
107
+ Optional <Path > p = Files .list (directory ).filter (f ->f .getFileName ().toString ().startsWith (name )).findFirst ();
108
+ Path option = p .get ();
109
+ if (Files .exists (option ) && Files .isRegularFile (option )) {
110
+ return option ;
111
+ }
112
+ return null ;
113
+ } catch (IOException | NoSuchElementException io ) {
114
+ return null ;
115
+ }
92
116
}
93
117
94
118
public static Path getServerLibFolder () throws FileNotFoundException {
95
- String installRoot = System . getProperty ( TC_INSTALL_ROOT_PROPERTY_NAME , System . getProperty ( "user.dir" ) );
96
- String serverLib = System .getProperty (TC_PLUGINS_LIB_PROPERTY_NAME , "lib" );
97
- Path f = Paths . get ( installRoot , serverLib );
119
+ Path installRoot = getInstallationRoot ( );
120
+ String serverLib = System .getProperty (TC_SERVER_LIB_PROPERTY_NAME , "lib" );
121
+ Path f = installRoot . resolve ( serverLib );
98
122
if (!Files .isDirectory (f )) {
99
123
throw new FileNotFoundException ("server library folder at " + f .toAbsolutePath () + " is not valid" );
100
124
}
101
125
return f ;
102
126
}
103
127
104
128
public static Path getServerPluginsApiDir () throws FileNotFoundException {
105
- String installRoot = System . getProperty ( TC_INSTALL_ROOT_PROPERTY_NAME , System . getProperty ( "user.dir" ) );
129
+ Path installRoot = getInstallationRoot ( );
106
130
String pluginsRoot = System .getProperty (TC_PLUGINS_ROOT_PROPERTY_NAME , "plugins" );
107
131
String pluginsApi = System .getProperty (TC_PLUGINS_API_PROPERTY_NAME , "api" );
108
- Path f = Paths . get ( installRoot , pluginsRoot , pluginsApi );
132
+ Path f = installRoot . resolve ( pluginsRoot ). resolve ( pluginsApi );
109
133
if (!Files .isDirectory (f )) {
110
134
throw new FileNotFoundException ("server plugins api folder at " + f .toAbsolutePath () + " is not valid" );
111
135
}
112
136
return f ;
113
137
}
114
138
115
139
public static Path getServerPluginsLibDir () throws FileNotFoundException {
116
- String installRoot = System . getProperty ( TC_INSTALL_ROOT_PROPERTY_NAME , System . getProperty ( "user.dir" ) );
140
+ Path installRoot = getInstallationRoot ( );
117
141
String pluginsRoot = System .getProperty (TC_PLUGINS_ROOT_PROPERTY_NAME , "plugins" );
118
142
String pluginsLib = System .getProperty (TC_PLUGINS_LIB_PROPERTY_NAME , "lib" );
119
- Path f = Paths . get ( installRoot , pluginsRoot , pluginsLib );
143
+ Path f = installRoot . resolve ( pluginsRoot ). resolve ( pluginsLib );
120
144
if (!Files .isDirectory (f )) {
121
145
throw new FileNotFoundException ("server plugins implementations folder at " + f .toAbsolutePath () + " is not valid" );
122
146
}
0 commit comments