Skip to content

Commit

Permalink
Replace checkstyle with PMD
Browse files Browse the repository at this point in the history
Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
  • Loading branch information
bestbeforetoday committed Nov 18, 2024
1 parent d5aa6c5 commit cdfa714
Show file tree
Hide file tree
Showing 86 changed files with 1,092 additions and 1,120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void setUp() throws Exception {
}

@Test
public void TestInstall() {
public void testInstall() {

InvokeHelper helper = InvokeHelper.newHelper("baregradlecc", "sachannel");
String text = helper.invoke("org1", "whoami");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void setUp() throws Exception {
}

@Test
public void TestLedgers() {
public void testLedgers() {
InvokeHelper helper = InvokeHelper.newHelper("ledgercc", "sachannel");

String text = helper.invoke("org1", "accessLedgers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void setUp() throws Exception {
}

@Test
public void TestLedger() {
public void testLedger() {

InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel");
String text = helper.invoke("org1", "putBulkStates");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void setUp() throws Exception {
}

@Test
public void RunSBE_pub_setget() {
public void runSBE_pub_setget() {
final String mode = "pub";

final InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel");
Expand Down Expand Up @@ -85,7 +85,7 @@ public void RunSBE_pub_setget() {
}

@Test
public void RunSBE_priv() {
public void runSBE_priv() {
final String mode = "priv";

final InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
import org.hyperleder.fabric.shim.integration.util.Bash.BashBuilder;

public final class FabricState {

private static FabricState state;

private static final Map<String, Boolean> channelStarted = new HashMap<>();

// sempaphore to protect access
private static final Semaphore flag = new Semaphore(1);

public static FabricState getState() {
public static synchronized FabricState getState() {
if (state == null) {
state = new FabricState();
}
Expand Down
18 changes: 6 additions & 12 deletions fabric-chaincode-shim/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ plugins {
id 'maven-publish'
id 'jacoco'
id 'signing'
id 'checkstyle'
id 'pmd'
}

checkstyle {
toolVersion '10.18.1'
configFile file("../ci/checkstyle/checkstyle.xml")
configProperties = [root_dir: file("..") ]
}
checkstyleMain {
source ='src/main/java'
}
checkstyleMain.exclude("**/ChaincodeServerProperties.**")
checkstyleTest {
source ='src/test/java'
pmd {
toolVersion = '7.7.0'
ruleSetFiles = files('../pmd-ruleset.xml')
ruleSets = [] // explicitly set to empty to avoid using the default configuration
ignoreFailures = false
}

configurations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected Logger(final String name) {
super(name, null);

// ensure that the parent logger is set
this.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
super.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
}

/**
Expand All @@ -45,9 +45,9 @@ public void debug(final String msg) {
*/
public static Logger getLogger(final Class<?> class1) {
// important to add the logger to the log manager
final Logger l = Logger.getLogger(class1.getName());
LogManager.getLogManager().addLogger(l);
return l;
final Logger result = Logger.getLogger(class1.getName());
LogManager.getLogManager().addLogger(result);
return result;
}

/** @param message */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.LogManager;

Expand Down Expand Up @@ -50,7 +51,7 @@ public static String formatError(final Throwable throwable) {
final Throwable cause = throwable.getCause();
if (cause != null) {
buffer.append(".. caused by ..").append(System.lineSeparator());
buffer.append(Logging.formatError(cause));
buffer.append(formatError(cause));
}

return buffer.toString();
Expand All @@ -67,11 +68,11 @@ public static void setLogLevel(final String newLevel) {
final LogManager logManager = LogManager.getLogManager();
// slightly cumbersome approach - but the loggers don't have a 'get children'
// so find those that have the correct stem.
final ArrayList<String> allLoggers = Collections.list(logManager.getLoggerNames());
final List<String> allLoggers = Collections.list(logManager.getLoggerNames());
allLoggers.add("org.hyperledger");
allLoggers.stream()
.filter(name -> name.startsWith("org.hyperledger"))
.map(name -> logManager.getLogger(name))
.map(logManager::getLogger)
.forEach(logger -> {
if (logger != null) {
logger.setLevel(l);
Expand All @@ -81,7 +82,7 @@ public static void setLogLevel(final String newLevel) {

private static Level mapLevel(final String level) {
if (level != null) {
switch (level.toUpperCase().trim()) {
switch (level.toUpperCase(Locale.getDefault()).trim()) {
case "ERROR":
case "CRITICAL":
return Level.SEVERE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
* and attributes. Such information is useful in enforcing access control by the chaincode.
*/
public final class ClientIdentity {
private static Logger logger = Logger.getLogger(ContractRouter.class.getName());
private static final Logger LOGGER = Logger.getLogger(ContractRouter.class.getName());

private final String mspId;
private final X509Certificate cert;
private Map<String, String> attrs;
private final Map<String, String> attrs;
private final String id;
// special OID used by Fabric to save attributes in x.509 certificates
private static final String FABRIC_CERT_ATTR_OID = "1.2.3.4.5.6.7.8.1";
Expand All @@ -47,7 +47,7 @@ public final class ClientIdentity {
* @throws JSONException
* @throws IOException
*/
public ClientIdentity(final ChaincodeStub stub) throws CertificateException, JSONException, IOException {
public ClientIdentity(final ChaincodeStub stub) throws CertificateException, IOException {
final byte[] signingId = stub.getCreator();

// Create a Serialized Identity protobuf
Expand All @@ -60,11 +60,12 @@ public ClientIdentity(final ChaincodeStub stub) throws CertificateException, JSO
CertificateFactory.getInstance("X509").generateCertificate(new ByteArrayInputStream(idBytes));
this.cert = cert;

this.attrs = new HashMap<String, String>();
// Get the extension where the identity attributes are stored
final byte[] extensionValue = cert.getExtensionValue(FABRIC_CERT_ATTR_OID);
if (extensionValue != null) {
this.attrs = parseAttributes(extensionValue);
} else {
this.attrs = new HashMap<>();
}

// Populate identity
Expand Down Expand Up @@ -100,7 +101,7 @@ public String getMSPID() {
*/
private Map<String, String> parseAttributes(final byte[] extensionValue) throws IOException {

final Map<String, String> attrMap = new HashMap<String, String>();
final Map<String, String> attrMap = new HashMap<>();

// Create ASN1InputStream from extensionValue
try (ByteArrayInputStream inStream = new ByteArrayInputStream(extensionValue);
Expand All @@ -126,7 +127,7 @@ private Map<String, String> parseAttributes(final byte[] extensionValue) throws
} catch (final JSONException error) {
// creating a JSON object failed
// decoded extensionValue is not a string containing JSON
logger.error(() -> logger.formatError(error));
LOGGER.error(() -> LOGGER.formatError(error));
// return empty map
}
return attrMap;
Expand All @@ -142,11 +143,7 @@ private Map<String, String> parseAttributes(final byte[] extensionValue) throws
* @return {String | null} Value of the attribute or null if the invoking identity does not possess the attribute.
*/
public String getAttributeValue(final String attrName) {
if (this.attrs.containsKey(attrName)) {
return this.attrs.get(attrName);
} else {
return null;
}
return this.attrs.getOrDefault(attrName, null);
}

/**
Expand All @@ -160,11 +157,7 @@ public String getAttributeValue(final String attrName) {
* expected value. Otherwise, returns false.
*/
public boolean assertAttributeValue(final String attrName, final String attrValue) {
if (!this.attrs.containsKey(attrName)) {
return false;
} else {
return attrValue.equals(this.attrs.get(attrName));
}
return this.attrs.containsKey(attrName) && attrValue.equals(this.attrs.get(attrName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@

/** Factory to create {@link Context} from {@link ChaincodeStub} by wrapping stub with dynamic proxy. */
public final class ContextFactory {
private static ContextFactory cf;
private static final ContextFactory INSTANCE = new ContextFactory();

/** @return ContextFactory */
public static synchronized ContextFactory getInstance() {
if (cf == null) {
cf = new ContextFactory();
}
return cf;
public static ContextFactory getInstance() {
return INSTANCE;
}

/**
* @param stub
* @return Context
*/
public Context createContext(final ChaincodeStub stub) {
final Context newContext = new Context(stub);
return newContext;
return new Context(stub);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ default void unknownTransaction(final Context ctx) {
*
* @param ctx the context as created by {@link #createContext(ChaincodeStub)}.
*/
default void beforeTransaction(final Context ctx) {}
default void beforeTransaction(final Context ctx) {
// Nothing by default
}

/**
* Invoked once after each transaction.
Expand All @@ -86,5 +88,7 @@ default void beforeTransaction(final Context ctx) {}
* @param result The object returned from the transaction function if any. As this is a Java object and therefore
* pass-by-reference it is possible to modify this object.
*/
default void afterTransaction(final Context ctx, final Object result) {}
default void afterTransaction(final Context ctx, final Object result) {
// Nothing by default
}
}
Loading

0 comments on commit cdfa714

Please sign in to comment.