Skip to content

Commit 047fcf6

Browse files
committed
RANGER-5519: introduce ranger-common-utils module
1 parent 2345c1d commit 047fcf6

52 files changed

Lines changed: 153 additions & 603 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

agents-common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@
133133
<artifactId>ranger-authz-api</artifactId>
134134
<version>${project.version}</version>
135135
</dependency>
136+
<dependency>
137+
<groupId>org.apache.ranger</groupId>
138+
<artifactId>ranger-common-utils</artifactId>
139+
<version>${project.version}</version>
140+
</dependency>
136141
<dependency>
137142
<groupId>org.apache.ranger</groupId>
138143
<artifactId>ranger-plugin-classloader</artifactId>

agents-common/src/main/java/org/apache/ranger/admin/client/RangerAdminRESTClient.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public ServicePolicies getServicePoliciesIfUpdated(final long lastKnownVersion,
179179

180180
ret = null;
181181
} else if (response.getStatus() == HttpStatus.SC_OK) {
182-
ret = JsonUtilsV2.readResponse(response, ServicePolicies.class);
182+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), ServicePolicies.class);
183183
} else if (response.getStatus() == HttpStatus.SC_NOT_FOUND) {
184184
ret = null;
185185

@@ -257,7 +257,7 @@ public RangerRoles getRolesIfUpdated(final long lastKnownRoleVersion, final long
257257

258258
ret = null;
259259
} else if (response.getStatus() == HttpStatus.SC_OK) {
260-
ret = JsonUtilsV2.readResponse(response, RangerRoles.class);
260+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), RangerRoles.class);
261261
} else if (response.getStatus() == HttpStatus.SC_NOT_FOUND) {
262262
ret = null;
263263

@@ -327,7 +327,7 @@ public RangerRole createRole(final RangerRole request) throws Exception {
327327
} else if (response == null) {
328328
throw new Exception("unknown error during createRole. roleName=" + request.getName());
329329
} else {
330-
ret = JsonUtilsV2.readResponse(response, RangerRole.class);
330+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), RangerRole.class);
331331
}
332332

333333
LOG.debug("<== RangerAdminRESTClient.createRole({})", request);
@@ -430,7 +430,7 @@ public List<String> getAllRoles(final String execUser) throws Exception {
430430

431431
throw new Exception("HTTP " + response.getStatus() + " Error: " + resp.getMessage());
432432
} else {
433-
ret = JsonUtilsV2.readResponse(response, TYPE_LIST_STRING);
433+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), TYPE_LIST_STRING);
434434
}
435435
} else {
436436
throw new Exception("unknown error during getAllRoles.");
@@ -482,7 +482,7 @@ public List<String> getUserRoles(final String execUser) throws Exception {
482482

483483
throw new Exception("HTTP " + response.getStatus() + " Error: " + resp.getMessage());
484484
} else {
485-
ret = JsonUtilsV2.readResponse(response, TYPE_LIST_STRING);
485+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), TYPE_LIST_STRING);
486486
}
487487
} else {
488488
throw new Exception("unknown error during getUserRoles. execUser=" + execUser);
@@ -538,7 +538,7 @@ public RangerRole getRole(final String execUser, final String roleName) throws E
538538

539539
throw new Exception("HTTP " + response.getStatus() + " Error: " + resp.getMessage());
540540
} else {
541-
ret = JsonUtilsV2.readResponse(response, RangerRole.class);
541+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), RangerRole.class);
542542
}
543543
} else {
544544
throw new Exception("unknown error during getPrincipalsForRole. roleName=" + roleName);
@@ -791,7 +791,7 @@ public ServiceTags getServiceTagsIfUpdated(final long lastKnownVersion, final lo
791791

792792
ret = null;
793793
} else if (response.getStatus() == HttpStatus.SC_OK) {
794-
ret = JsonUtilsV2.readResponse(response, ServiceTags.class);
794+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), ServiceTags.class);
795795
} else if (response.getStatus() == HttpStatus.SC_NOT_FOUND) {
796796
ret = null;
797797

@@ -851,7 +851,7 @@ public List<String> getTagTypes(String pattern) throws Exception {
851851
List<String> ret;
852852

853853
if (response != null && response.getStatus() == HttpStatus.SC_OK) {
854-
ret = JsonUtilsV2.readResponse(response, TYPE_LIST_STRING);
854+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), TYPE_LIST_STRING);
855855
} else {
856856
RESTResponse resp = RESTResponse.fromClientResponse(response);
857857

@@ -919,7 +919,7 @@ public RangerUserStore getUserStoreIfUpdated(long lastKnownUserStoreVersion, lon
919919

920920
ret = null;
921921
} else if (response.getStatus() == HttpStatus.SC_OK) {
922-
ret = JsonUtilsV2.readResponse(response, RangerUserStore.class);
922+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), RangerUserStore.class);
923923
} else if (response.getStatus() == HttpStatus.SC_NOT_FOUND) {
924924
ret = null;
925925

@@ -996,7 +996,7 @@ public ServiceGdsInfo getGdsInfoIfUpdated(long lastKnownVersion, long lastActiva
996996
LOG.debug("No change in GdsInfo: secureMode={}, response={}, serviceName={}, lastKnownGdsVersion={}, lastActivationTimeInMillis={}",
997997
isSecureMode, resp, serviceName, lastKnownVersion, lastActivationTimeInMillis);
998998
} else if (response.getStatus() == HttpStatus.SC_OK) {
999-
ret = JsonUtilsV2.readResponse(response, ServiceGdsInfo.class);
999+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), ServiceGdsInfo.class);
10001000
} else if (response.getStatus() == HttpStatus.SC_NOT_FOUND) {
10011001
ret = null;
10021002

agents-common/src/main/java/org/apache/ranger/plugin/contextenricher/RangerUserStoreRefresher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ private RangerUserStore getUserStoreIfUpdated(long lastKnownUserStoreVersion, lo
392392

393393
ret = null;
394394
} else if (response.getStatus() == SC_OK) {
395-
ret = JsonUtilsV2.readResponse(response, RangerUserStore.class);
395+
ret = JsonUtilsV2.jsonToObj(response.getEntity(String.class), RangerUserStore.class);
396396
} else if (response.getStatus() == SC_NOT_FOUND) {
397397
ret = null;
398398

audit-server/server/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@
279279
</exclusion>
280280
</exclusions>
281281
</dependency>
282+
<dependency>
283+
<groupId>org.apache.ranger</groupId>
284+
<artifactId>ranger-common-utils</artifactId>
285+
<version>${project.version}</version>
286+
</dependency>
282287
<dependency>
283288
<groupId>org.apache.ranger</groupId>
284289
<artifactId>ranger-plugins-common</artifactId>

common-utils/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<parent>
21+
<groupId>org.apache.ranger</groupId>
22+
<artifactId>ranger</artifactId>
23+
<version>3.0.0-SNAPSHOT</version>
24+
</parent>
25+
26+
<artifactId>ranger-common-utils</artifactId>
27+
28+
<properties>
29+
<maven.compiler.source>8</maven.compiler.source>
30+
<maven.compiler.target>8</maven.compiler.target>
31+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32+
</properties>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>com.fasterxml.jackson.core</groupId>
37+
<artifactId>jackson-core</artifactId>
38+
<version>${fasterxml.jackson.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.fasterxml.jackson.core</groupId>
42+
<artifactId>jackson-databind</artifactId>
43+
<version>${fasterxml.jackson.databind.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.apache.commons</groupId>
47+
<artifactId>commons-lang3</artifactId>
48+
<version>${commons.lang3.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.slf4j</groupId>
52+
<artifactId>slf4j-api</artifactId>
53+
<version>${slf4j.version}</version>
54+
</dependency>
55+
56+
<!-- Test -->
57+
<dependency>
58+
<groupId>org.junit.jupiter</groupId>
59+
<artifactId>junit-jupiter</artifactId>
60+
<version>${junit.jupiter.version}</version>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
</project>

agents-common/src/main/java/org/apache/ranger/plugin/util/AutoClosableLock.java renamed to common-utils/src/main/java/org/apache/ranger/plugin/util/AutoClosableLock.java

File renamed without changes.

agents-common/src/main/java/org/apache/ranger/plugin/util/JavaScriptEdits.java renamed to common-utils/src/main/java/org/apache/ranger/plugin/util/JavaScriptEdits.java

File renamed without changes.

agents-common/src/main/java/org/apache/ranger/plugin/util/JsonUtilsV2.java renamed to common-utils/src/main/java/org/apache/ranger/plugin/util/JsonUtilsV2.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import com.fasterxml.jackson.core.type.TypeReference;
2323
import com.fasterxml.jackson.databind.ObjectMapper;
24-
import com.sun.jersey.api.client.ClientResponse;
2524

2625
import java.io.Reader;
2726
import java.io.Serializable;
@@ -84,16 +83,4 @@ public static <T> T readValue(Reader reader, Class<T> tClass) throws Exception {
8483
public static String nonSerializableObjToJson(Object obj) throws Exception {
8584
return getMapper().writeValueAsString(obj);
8685
}
87-
88-
public static <T> T readResponse(ClientResponse response, Class<T> cls) throws Exception {
89-
String jsonStr = response.getEntity(String.class);
90-
91-
return jsonToObj(jsonStr, cls);
92-
}
93-
94-
public static <T> T readResponse(ClientResponse response, TypeReference<T> cls) throws Exception {
95-
String jsonStr = response.getEntity(String.class);
96-
97-
return jsonToObj(jsonStr, cls);
98-
}
9986
}

agents-common/src/main/java/org/apache/ranger/plugin/util/PerfDataRecorder.java renamed to common-utils/src/main/java/org/apache/ranger/plugin/util/PerfDataRecorder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.apache.ranger.plugin.util;
2121

22-
import org.apache.commons.collections.CollectionUtils;
23-
import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableMap;
2422
import org.slf4j.Logger;
2523
import org.slf4j.LoggerFactory;
2624

@@ -41,7 +39,7 @@ public class PerfDataRecorder {
4139
private RangerReadWriteLock lock;
4240

4341
private PerfDataRecorder(List<String> names) {
44-
if (CollectionUtils.isNotEmpty(names)) {
42+
if (names != null) {
4543
for (String name : names) {
4644
// Create structure
4745
perfStatistics.put(name, new PerfStatistic());
@@ -98,9 +96,9 @@ public static void recordStatistic(String tag, long cpuTime, long elapsedTime) {
9896

9997
public static Map<String, PerfStatistic> exposeStatistics() {
10098
if (instance != null) {
101-
return ImmutableMap.copyOf(instance.perfStatistics);
99+
return Collections.unmodifiableMap(new HashMap<>(instance.perfStatistics));
102100
}
103-
return ImmutableMap.of();
101+
return Collections.emptyMap();
104102
}
105103

106104
private void dumpStatistics() {

agents-common/src/main/java/org/apache/ranger/plugin/util/RangerCache.java renamed to common-utils/src/main/java/org/apache/ranger/plugin/util/RangerCache.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717

1818
package org.apache.ranger.plugin.util;
1919

20-
import org.apache.ranger.plugin.util.AutoClosableLock.AutoClosableTryLock;
2120
import org.slf4j.Logger;
2221
import org.slf4j.LoggerFactory;
2322

24-
import javax.annotation.Nonnull;
25-
2623
import java.util.HashSet;
2724
import java.util.Map;
2825
import java.util.Set;
@@ -177,7 +174,7 @@ private ThreadFactory createThreadFactory() {
177174
private final AtomicInteger number = new AtomicInteger(1);
178175

179176
@Override
180-
public Thread newThread(@Nonnull Runnable r) {
177+
public Thread newThread(Runnable r) {
181178
Thread t = new Thread(r, namePrefix + number.getAndIncrement());
182179

183180
if (!t.isDaemon()) {
@@ -303,7 +300,7 @@ private void refreshIfNeeded(long timeoutMs, Object context) {
303300
if (needsRefresh()) {
304301
long startTime = System.currentTimeMillis();
305302

306-
try (AutoClosableTryLock tryLock = new AutoClosableTryLock(lock, timeoutMs, TimeUnit.MILLISECONDS)) {
303+
try (AutoClosableLock.AutoClosableTryLock tryLock = new AutoClosableLock.AutoClosableTryLock(lock, timeoutMs, TimeUnit.MILLISECONDS)) {
307304
if (tryLock.isLocked()) {
308305
if (needsRefresh()) {
309306
Future<?> future = this.refresher;

0 commit comments

Comments
 (0)