Skip to content

Commit

Permalink
fix a number of sonar warnings in recent code
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jul 11, 2023
1 parent 58080b9 commit 07fabab
Show file tree
Hide file tree
Showing 36 changed files with 304 additions and 254 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2012-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
* Copyright (C) 2012-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand All @@ -28,6 +28,8 @@

package org.opennms.container.jaas;

import java.util.concurrent.atomic.AtomicReference;

import org.opennms.netmgt.config.GroupDao;
import org.opennms.netmgt.config.api.UserConfig;
import org.opennms.web.springframework.security.SpringSecurityUserDao;
Expand All @@ -37,55 +39,66 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JaasSupport {
private static final transient Logger LOG = LoggerFactory.getLogger(OpenNMSLoginModule.class);
public final class JaasSupport {
private static final Logger LOG = LoggerFactory.getLogger(JaasSupport.class);

private static AtomicReference<BundleContext> m_context = new AtomicReference<>();
private static AtomicReference<UserConfig> m_userConfig = new AtomicReference<>();
private static AtomicReference<GroupDao> m_groupDao = new AtomicReference<>();
private static AtomicReference<SpringSecurityUserDao> m_userDao = new AtomicReference<>();

private static transient volatile BundleContext m_context;
private static transient volatile UserConfig m_userConfig;
private static transient volatile GroupDao m_groupDao;
private static transient volatile SpringSecurityUserDao m_userDao;
private JaasSupport() {}

public static synchronized void setContext(final BundleContext context) {
m_userConfig = null;
m_groupDao = null;
m_userDao = null;
m_context = context;
m_context.set(context);
m_userConfig.set(null);
m_groupDao.set(null);
m_userDao.set(null);
}

public static synchronized BundleContext getContext() {
if (m_context == null) {
setContext(FrameworkUtil.getBundle(JaasSupport.class).getBundleContext());
}
return m_context;
final var context = m_context.get();
if (context != null) {
return context;
}
setContext(FrameworkUtil.getBundle(JaasSupport.class).getBundleContext());
return m_context.get();
}

public static UserConfig getUserConfig() {
if (m_userConfig == null) {
m_userConfig = getFromRegistry(UserConfig.class);
}
return m_userConfig;
final var userConfig = m_userConfig.get();
if (userConfig != null) {
return userConfig;
}
m_userConfig.set(getFromRegistry(UserConfig.class));
return m_userConfig.get();
}

public static SpringSecurityUserDao getSpringSecurityUserDao() {
if (m_userDao == null) {
m_userDao = getFromRegistry(SpringSecurityUserDao.class);
}
return m_userDao;
final var userDao = m_userDao.get();
if (userDao != null) {
return userDao;
}
m_userDao.set(getFromRegistry(SpringSecurityUserDao.class));
return m_userDao.get();
}

public static GroupDao getGroupDao() {
if (m_groupDao == null) {
m_groupDao = getFromRegistry(GroupDao.class);
}
return m_groupDao;
final var groupDao = m_groupDao.get();
if (groupDao != null) {
return groupDao;
}
m_groupDao.set(getFromRegistry(GroupDao.class));
return m_groupDao.get();
}

private static <T> T getFromRegistry(final Class<T> clazz) {
if (m_context == null) {
final var context = m_context.get();
if (context == null) {
LOG.warn("No bundle context. Unable to get class {} from the registry.", clazz);
return null;
}
final ServiceReference<T> ref = m_context.getServiceReference(clazz);
return m_context.getService(ref);
final ServiceReference<T> ref = context.getServiceReference(clazz);
return context.getService(ref);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2014-2017 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2017 The OpenNMS Group, Inc.
* Copyright (C) 2014-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -66,21 +66,25 @@
* }</pre></blockquote>
*/
@XmlAccessorType(XmlAccessType.NONE)
@SuppressWarnings("java:S2162")
public class JaxbListWrapper<T> implements Serializable, Iterable<T> {
private static final long serialVersionUID = 1L;

private List<T> m_objects = new ArrayList<>();
private transient List<T> m_objects = new ArrayList<>();
private Integer m_totalCount;
private Integer m_offset = 0;

public List<T> getObjects() {
return m_objects;
};
}

public void setObjects(final List<? extends T> objects) {
if (objects == m_objects) return;
if (m_objects.equals(objects)) {
return;
}
m_objects.clear();
m_objects.addAll(objects);
};
}

public JaxbListWrapper() {}
public JaxbListWrapper(final Collection<? extends T> objects) {
Expand All @@ -101,7 +105,7 @@ public boolean add(final T obj) {

@XmlAttribute(name="count")
public Integer getCount() {
if (m_objects.size() == 0) {
if (m_objects.isEmpty()) {
return null;
} else {
return m_objects.size();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2016 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2016 The OpenNMS Group, Inc.
* Copyright (C) 2016-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -33,6 +33,7 @@
import static org.opennms.core.ipc.common.kafka.KafkaSinkConstants.MESSAGEID_CACHE_CONFIG;
import static org.opennms.core.ipc.sink.api.Message.SINK_METRIC_CONSUMER_DOMAIN;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -90,6 +91,7 @@
import io.opentracing.util.GlobalTracer;

public class KafkaMessageConsumerManager extends AbstractMessageConsumerManager implements InitializingBean {
private static final Duration CONSUMER_POLL_DURATION = Duration.ofMillis(100);

private static final Logger LOG = LoggerFactory.getLogger(KafkaMessageConsumerManager.class);

Expand Down Expand Up @@ -160,7 +162,7 @@ public void run() {
try {
consumer.subscribe(Arrays.asList(topic));
while (!closed.get()) {
ConsumerRecords<String, byte[]> records = consumer.poll(100);
ConsumerRecords<String, byte[]> records = consumer.poll(CONSUMER_POLL_DURATION);
for (ConsumerRecord<String, byte[]> record : records) {
try {
// Parse sink message content from protobuf.
Expand All @@ -176,11 +178,13 @@ public void run() {
continue;
}
// Avoid duplicate chunks. discard if chunk is repeated.
if(currentChunkCache.getIfPresent(messageId) == null) {
Integer chunkNum = currentChunkCache.getIfPresent(messageId);
if (chunkNum == null) {
currentChunkCache.put(messageId, 0);
chunkNum = 0;
}
Integer chunkNum = currentChunkCache.getIfPresent(messageId);
if(chunkNum != null && chunkNum == sinkMessage.getCurrentChunkNumber()) {

if(chunkNum == sinkMessage.getCurrentChunkNumber()) {
continue;
}
ByteString byteString = largeMessageCache.getIfPresent(messageId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2019 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2019 The OpenNMS Group, Inc.
* Copyright (C) 2019-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -55,7 +55,9 @@

@Command(scope = "opennms", name = "metadata-test", description = "Test Meta-Data replacement")
@Service
@SuppressWarnings("java:S106")
public class MetaCommand implements Action {
private static final String MATCHER = "^.*([pP]assword|[sS]ecret).*$";

@Reference
private SessionUtils sessionUtils;
Expand All @@ -78,17 +80,15 @@ public class MetaCommand implements Action {
@Argument(index = 0, name = "expression", description = "Expression to use, e.g. '${context:key|fallback_context:fallback_key|default}'", required = false, multiValued = false)
private String expression;

private final String MATCHER = ".*([pP]assword|[sS]ecret).*";

void printScope(final Scope scope) {
final Map<String, Set<ContextKey>> grouped = scope.keys().stream()
.collect(Collectors.groupingBy(ContextKey::getContext, TreeMap::new, Collectors.toCollection(TreeSet::new)));

for (final Map.Entry<String, Set<ContextKey>> group : grouped.entrySet()) {
System.out.printf("%s:\n", group.getKey());
System.out.printf("%s:%n", group.getKey());
for (final ContextKey contextKey : group.getValue()) {
final boolean omitOutput = (SecureCredentialsVaultScope.CONTEXT.equals(group.getKey()) && SecureCredentialsVaultScope.PASSWORD.equals(contextKey.getKey())) || contextKey.getKey().matches(MATCHER);
System.out.printf(" %s='%s'\n", contextKey.getKey(), scope.get(contextKey).map(r -> String.format("%s @ %s", omitOutput ? "<output omitted>" : r.value, r.scopeName)).orElse(""));
System.out.printf(" %s='%s'%n", contextKey.getKey(), scope.get(contextKey).map(r -> String.format("%s @ %s", omitOutput ? "<output omitted>" : r.value, r.scopeName)).orElse(""));
}
}
}
Expand All @@ -100,7 +100,7 @@ public Object execute() throws Exception {
try {
final OnmsNode onmsNode = this.nodeDao.get(this.nodeRef);
if (onmsNode == null) {
System.out.printf("Cannot find node with ID/FS:FID=%s.\n", this.nodeRef);
System.out.printf("Cannot find node with ID/FS:FID=%s.%n", this.nodeRef);
return null;
}

Expand All @@ -109,28 +109,28 @@ public Object execute() throws Exception {
final Scope interfaceScope = this.entityScopeProvider.getScopeForInterface(onmsNode.getId(), this.interfaceAddress);
final Scope serviceScope = this.entityScopeProvider.getScopeForService(onmsNode.getId(), InetAddressUtils.getInetAddress(this.interfaceAddress), this.serviceName);

System.out.printf("---\nMeta-Data for node (id=%d)\n", onmsNode.getId());
System.out.printf("---%nMeta-Data for node (id=%d)%n", onmsNode.getId());
printScope(nodeScope);

if (this.interfaceAddress != null) {
System.out.printf("---\nMeta-Data for interface (ipAddress=%s):\n", this.interfaceAddress);
System.out.printf("---%nMeta-Data for interface (ipAddress=%s):%n", this.interfaceAddress);
printScope(interfaceScope);
}

if (this.serviceName != null) {
System.out.printf("---\nMeta-Data for service (name=%s):\n", this.serviceName);
System.out.printf("---%nMeta-Data for service (name=%s):%n", this.serviceName);
printScope(serviceScope);
}

System.out.printf("---\n");
System.out.printf("---%n");

if (!Strings.isNullOrEmpty(this.expression)) {
final Interpolator.Result result = Interpolator.interpolate(this.expression, new FallbackScope(nodeScope, interfaceScope, serviceScope));
System.out.printf("Input: '%s'\nOutput: '%s'\n", this.expression, result.output);
System.out.printf("Input: '%s'%nOutput: '%s'%n", this.expression, result.output);

System.out.printf("Details:\n");
System.out.printf("Details:%n");
for(final Interpolator.ResultPart resultPart : result.parts) {
System.out.printf(" Part: '%s' => match='%s', value='%s', scope='%s'\n", resultPart.input, resultPart.match, resultPart.value.value, resultPart.value.scopeName);
System.out.printf(" Part: '%s' => match='%s', value='%s', scope='%s'%n", resultPart.input, resultPart.match, resultPart.value.value, resultPart.value.scopeName);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2011-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
* Copyright (C) 2011-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -142,11 +142,11 @@ public void afterTestMethod(final TestContext testContext) throws Exception {
}

// Put the strategy class property back the way it was before the tests.
final String strategyClass = (String)testContext.getAttribute(STRATEGY_CLASS_KEY);
final Object strategyClass = testContext.getAttribute(STRATEGY_CLASS_KEY);
if (strategyClass == null) {
System.clearProperty(STRATEGY_CLASS_PROPERTY);
} else {
System.setProperty(STRATEGY_CLASS_PROPERTY, strategyClass);
System.setProperty(STRATEGY_CLASS_PROPERTY, (String)strategyClass);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2021 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2021 The OpenNMS Group, Inc.
* Copyright (C) 2021-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand All @@ -26,7 +26,6 @@
* http://www.opennms.com/
*******************************************************************************/


package liquibase.ext2.cm.change.types;

import java.util.List;
Expand Down Expand Up @@ -60,11 +59,13 @@ public NumberType(final List<ParsedNode> listOfAttributes) {
if (maxP < minP) {
throw new IllegalArgumentException(String.format("min=%s must not be bigger than max=%s", minP, maxP));
}
if (min.isPresent() && defaultValue < minP) {
throw new IllegalArgumentException(String.format("defaultValue=%s must not be smaller than min=%s", defaultValue, min.get()));
}
if (max.isPresent() && defaultValue > maxP) {
throw new IllegalArgumentException(String.format("defaultValue=%s must not be bigger than max=%s", defaultValue, max.get()));
if (defaultValue != null) {
if (min.isPresent() && defaultValue < minP) {
throw new IllegalArgumentException(String.format("defaultValue=%s must not be smaller than min=%s", defaultValue, min.get()));
}
if (max.isPresent() && defaultValue > maxP) {
throw new IllegalArgumentException(String.format("defaultValue=%s must not be bigger than max=%s", defaultValue, max.get()));
}
}
}

Expand Down
Loading

0 comments on commit 07fabab

Please sign in to comment.