/
SymmetricUtils.java
214 lines (189 loc) · 9.11 KB
/
SymmetricUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.util;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.jumpmind.symmetric.Version;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.util.AppUtils;
import org.jumpmind.util.CollectionUtils;
import org.jumpmind.util.FormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final public class SymmetricUtils {
protected static final Logger log = LoggerFactory.getLogger(SymmetricUtils.class);
protected static boolean isJava7 = true;
protected static Method copyMethod;
protected static Method fileMethod;
protected static Object optionArray;
protected static boolean isNoticeLogged;
private SymmetricUtils() {
}
public static String quote(ISymmetricDialect symmetricDialect, String name) {
String quote = symmetricDialect.getPlatform().getDatabaseInfo().getDelimiterToken();
if (StringUtils.isNotBlank(quote)) {
return quote + name + quote;
} else {
return name;
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void copyFile(File source, File target) throws IOException {
if (isJava7) {
try {
if (copyMethod == null) {
Class filesClass = Class.forName("java.nio.file.Files");
Class pathClass = Class.forName("java.nio.file.Path");
Class optionArrayClass = Class.forName("[Ljava.nio.file.CopyOption;");
Class optionClass = Class.forName("java.nio.file.CopyOption");
Class standardOptionClass = Class.forName("java.nio.file.StandardCopyOption");
copyMethod = filesClass.getMethod("copy", new Class[] { pathClass, pathClass, optionArrayClass });
fileMethod = File.class.getMethod("toPath", (Class[]) null);
optionArray = Array.newInstance(optionClass, 1);
Array.set(optionArray, 0, Enum.valueOf(standardOptionClass, "REPLACE_EXISTING"));
}
Object sourcePath = fileMethod.invoke(source, (Object[]) null);
Object targetPath = fileMethod.invoke(target, (Object[]) null);
copyMethod.invoke(null, new Object[] { sourcePath, targetPath, optionArray });
return;
} catch (Exception e) {
isJava7 = false;
}
}
FileUtils.copyFile(source, target);
}
public static final void replaceSystemAndEnvironmentVariables(Properties properties) {
Set<Object> keys = new HashSet<Object>(properties.keySet());
Map<String, String> env = System.getenv();
Map<String, String> systemProperties = CollectionUtils.toMap(System.getProperties());
for (Object object : keys) {
String value = properties.getProperty((String)object);
if (isNotBlank(value)) {
value = FormatUtils.replaceTokens(value, env, true);
value = FormatUtils.replaceTokens(value, systemProperties, true);
if (value.contains("hostName")) {
value = FormatUtils.replace("hostName", AppUtils.getHostName(), value);
}
if (value.contains("portNumber")) {
value = FormatUtils.replace("portNumber", AppUtils.getPortNumber(), value);
}
if (value.contains("protocol")) {
value = FormatUtils.replace("protocol", AppUtils.getProtocol(), value);
}
if (value.contains("ipAddress")) {
value = FormatUtils.replace("ipAddress", AppUtils.getIpAddress(), value);
}
if (value.contains("engineName")) {
value = FormatUtils.replace("engineName", properties.getProperty(ParameterConstants.ENGINE_NAME, ""), value);
}
if (value.contains("nodeGroupId")) {
value = FormatUtils.replace("nodeGroupId", properties.getProperty(ParameterConstants.NODE_GROUP_ID, ""), value);
}
if (value.contains("externalId")) {
value = FormatUtils.replace("externalId", properties.getProperty(ParameterConstants.EXTERNAL_ID, ""), value);
}
if (value.contains("syncUrl")) {
value = FormatUtils.replace("syncUrl", properties.getProperty(ParameterConstants.SYNC_URL, ""), value);
}
if (value.contains("registrationUrl")) {
value = FormatUtils.replace("registrationUrl", properties.getProperty(ParameterConstants.REGISTRATION_URL, ""), value);
}
properties.put(object, value);
}
}
}
public static String replaceNodeVariables(Node sourceNode, Node targetNode, String str) {
if (sourceNode != null) {
str = FormatUtils.replace("sourceNodeId", sourceNode.getNodeId(), str);
str = FormatUtils.replace("sourceExternalId", sourceNode.getExternalId(), str);
str = FormatUtils.replace("sourceNodeGroupId", sourceNode.getNodeGroupId(), str);
}
if (targetNode != null) {
str = FormatUtils.replace("targetNodeId", targetNode.getNodeGroupId(), str);
str = FormatUtils.replace("targetExternalId", targetNode.getExternalId(), str);
str = FormatUtils.replace("targetNodeGroupId", targetNode.getNodeGroupId(), str);
}
return str;
}
public static void logNotices() {
synchronized (SymmetricUtils.class) {
if (isNoticeLogged) {
return;
}
isNoticeLogged = true;
}
String notices = null;
try {
notices = String.format("%n%s%n", IOUtils.toString(Thread.currentThread().getContextClassLoader().getResource("symmetricds.asciiart"), Charset.defaultCharset()));
notices = notices.replaceAll("\n", String.format("%n"));
} catch (Exception ex) {
notices = String.format("SymmetricDS Start%n");
}
String buildTime = Long.toString(Version.getBuildTime());
String year = null;
if (buildTime.length() >= 4) {
year = buildTime.substring(0, 4);
} else {
year = new SimpleDateFormat("yyyy").format(new Date());
}
int pad = 65;
notices += String.format(
"+" + StringUtils.repeat("-", pad) + "+%n" +
"|" + StringUtils.rightPad(" Copyright (C) 2007-" + year + " JumpMind, Inc.", pad) + "|%n" +
"|" + StringUtils.repeat(" ", pad) + "|%n");
InputStream in = null;
try {
in = AppUtils.class.getResourceAsStream("/symmetric-console-default.properties");
if (in != null) {
in.close();
}
} catch (Exception e) {
}
if (in != null) {
notices += String.format(
"|" + StringUtils.rightPad(" Licensed under one or more agreements from JumpMind, Inc.", pad) + "|%n" +
"|" + StringUtils.rightPad(" See doc/license.html", pad) + "|%n");
} else {
notices += String.format(
"|" + StringUtils.rightPad(" Licensed under the GNU General Public License version 3.", pad) + "|%n" +
"|" + StringUtils.rightPad(" This software comes with ABSOLUTELY NO WARRANTY.", pad) + "|%n" +
"|" + StringUtils.rightPad(" See http://www.gnu.org/licenses/gpl.html", pad) + "|%n");
}
notices += "+" + StringUtils.repeat("-", pad) + "+";
log.info(notices);
}
}