Skip to content

Commit eca2573

Browse files
committed
Update RequestCache.java
1 parent 1a6d946 commit eca2573

1 file changed

Lines changed: 69 additions & 67 deletions

File tree

authorizer/src/main/java/org/nifiopa/nifiopa/RequestCache.java

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,113 +13,115 @@
1313
import org.apache.nifi.authorization.AuthorizerConfigurationContext;
1414
import org.apache.nifi.authorization.exception.AuthorizationAccessException;
1515
import org.apache.nifi.authorization.exception.AuthorizerCreationException;
16-
import org.apache.nifi.components.PropertyValue;
1716
import org.slf4j.Logger;
1817
import org.slf4j.LoggerFactory;
1918

2019
public class RequestCache {
21-
20+
2221
private static final Logger logger = LoggerFactory.getLogger(RequestCache.class);
2322

2423
private Cache<RequestKey, AuthorizationResult> cache;
25-
26-
private final String PROP_CACHE_TIME_SECS = "CACHE_TIME_SECS";
27-
private int CACHE_TIME_SECS = 30;
28-
29-
private final String PROP_CACHE_MAX_ENTRY_COUNT = "CACHE_MAX_ENTRY_COUNT";
30-
private int CACHE_MAX_ENTRY_COUNT = 0;
31-
24+
25+
private final String CACHE_TIME_SECS_PROPNAME = "CACHE_TIME_SECS";
26+
private int CACHE_TIME_SECS;
27+
private final String CACHE_TIME_SECS_DEFAULT = "30";
28+
29+
private final String CACHE_MAX_ENTRY_COUNT_PROPNAME = "CACHE_MAX_ENTRY_COUNT";
30+
private int CACHE_MAX_ENTRY_COUNT;
31+
private final String CACHE_MAX_ENTRY_COUNT_DEFAULT = "0";
32+
3233
void putCachedResult(AuthorizationRequest request, AuthorizationResult result) throws AuthorizationAccessException {
33-
if(request == null | result == null)
34+
if (request == null | result == null)
3435
return;
35-
36+
3637
cache.put(new RequestKey(request), result);
3738
}
38-
39+
3940
AuthorizationResult getCachedResult(AuthorizationRequest request) throws AuthorizationAccessException {
4041
return cache.getIfPresent(new RequestKey(request));
4142
}
42-
43+
4344
void clear() {
4445
cache.invalidateAll();
4546
}
4647

4748
public void initialize(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
4849
initializePropertys(configurationContext);
49-
50+
5051
cache = Caffeine.newBuilder()
5152
.expireAfterWrite(CACHE_TIME_SECS, TimeUnit.SECONDS)
5253
.maximumSize(CACHE_MAX_ENTRY_COUNT)
5354
.build();
5455
}
55-
56+
5657
void initializePropertys(AuthorizerConfigurationContext configurationContext) {
5758
try {
5859
/* Initialize property of maximum cache time after write */
59-
PropertyValue cacheTimeProp = configurationContext.getProperty(PROP_CACHE_TIME_SECS);
60-
if(cacheTimeProp.isSet())
61-
CACHE_TIME_SECS = cacheTimeProp.asInteger();
62-
63-
}catch(NumberFormatException nfe) {
64-
String message = MessageFormat.format("An error occured while trying to initialze the Cache property {0}: {1}", PROP_CACHE_TIME_SECS, nfe.getStackTrace().toString());
65-
logger.error(message);
66-
throw new AuthorizerCreationException(message);
67-
}
68-
69-
/* Initialize property of maximum entries in the cache at one time*/
70-
try {
71-
PropertyValue cacheMaxEntryProp = configurationContext.getProperty(PROP_CACHE_MAX_ENTRY_COUNT);
72-
if(cacheMaxEntryProp.isSet())
73-
CACHE_MAX_ENTRY_COUNT = cacheMaxEntryProp.asInteger();
74-
75-
}catch(NumberFormatException nfe) {
76-
String message = MessageFormat.format("An error occured while trying to initialze the Cache property {0}: {1}", PROP_CACHE_MAX_ENTRY_COUNT, nfe.getStackTrace().toString());
77-
logger.error(message);
78-
throw new AuthorizerCreationException(message);
60+
String cacheTimeProp = ConfigLoader.getProperty(CACHE_TIME_SECS_PROPNAME, CACHE_TIME_SECS_DEFAULT);
61+
if (cacheTimeProp != null)
62+
CACHE_TIME_SECS = Integer.parseInt(cacheTimeProp);
63+
} catch (NumberFormatException nfe) {
64+
String message = MessageFormat.format(
65+
"An error occured while trying to initialze the Cache property {0}: {1}", CACHE_TIME_SECS_PROPNAME,
66+
nfe.getStackTrace().toString());
67+
logger.error(message);
68+
throw new AuthorizerCreationException(message);
69+
}
70+
71+
/* Initialize property of maximum entries in the cache at one time */
72+
try {
73+
String cacheMaxEntryProp = ConfigLoader.getProperty(CACHE_MAX_ENTRY_COUNT_PROPNAME, CACHE_MAX_ENTRY_COUNT_DEFAULT);
74+
if (cacheMaxEntryProp != null)
75+
CACHE_MAX_ENTRY_COUNT = Integer.parseInt(cacheMaxEntryProp);
76+
} catch (NumberFormatException nfe) {
77+
String message = MessageFormat.format(
78+
"An error occured while trying to initialze the Cache property {0}: {1}",
79+
CACHE_MAX_ENTRY_COUNT_PROPNAME, nfe.getStackTrace().toString());
80+
logger.error(message);
81+
throw new AuthorizerCreationException(message);
7982
}
8083
}
81-
84+
8285
class RequestKey {
83-
86+
8487
private int hashCode;
85-
88+
8689
public RequestKey(AuthorizationRequest request) {
8790
hashCode = Objects.hash(
88-
89-
Optional.ofNullable(request.getIdentity())
90-
.map(Object::hashCode)
91-
.orElse(0),
92-
93-
Optional.ofNullable(request.getGroups())
94-
.map(Object::hashCode)
95-
.orElse(0),
96-
97-
request.getAction().toString().hashCode(),
98-
99-
Optional.ofNullable(request.getRequestedResource())
100-
.map(res -> res.getIdentifier() != null ? res.getIdentifier().hashCode() : 0)
101-
.orElse(0),
102-
103-
Optional.ofNullable(request.getResource())
104-
.map(res -> res.getIdentifier() != null ? res.getIdentifier().hashCode() : 0)
105-
.orElse(0),
106-
107-
Optional.ofNullable(request.getResourceContext())
108-
.map(Object::hashCode)
109-
.orElse(0),
110-
111-
Optional.ofNullable(request.getUserContext())
112-
.map(Object::hashCode)
113-
.orElse(0)
114-
);
91+
92+
Optional.ofNullable(request.getIdentity())
93+
.map(Object::hashCode)
94+
.orElse(0),
95+
96+
Optional.ofNullable(request.getGroups())
97+
.map(Object::hashCode)
98+
.orElse(0),
99+
100+
request.getAction().toString().hashCode(),
101+
102+
Optional.ofNullable(request.getRequestedResource())
103+
.map(res -> res.getIdentifier() != null ? res.getIdentifier().hashCode() : 0)
104+
.orElse(0),
105+
106+
Optional.ofNullable(request.getResource())
107+
.map(res -> res.getIdentifier() != null ? res.getIdentifier().hashCode() : 0)
108+
.orElse(0),
109+
110+
Optional.ofNullable(request.getResourceContext())
111+
.map(Object::hashCode)
112+
.orElse(0),
113+
114+
Optional.ofNullable(request.getUserContext())
115+
.map(Object::hashCode)
116+
.orElse(0));
115117

116118
}
117-
119+
118120
@Override
119121
public int hashCode() {
120122
return this.hashCode;
121123
}
122-
124+
123125
@Override
124126
public boolean equals(Object obj) {
125127
return obj.hashCode() == hashCode;

0 commit comments

Comments
 (0)