Skip to content

Commit e03a209

Browse files
committed
Fix the latest Sonar smells in the SMB module
1 parent 88d681f commit e03a209

File tree

2 files changed

+57
-107
lines changed

2 files changed

+57
-107
lines changed

spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java

Lines changed: 57 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import java.net.URL;
2626
import java.util.Arrays;
2727

28-
import org.apache.commons.logging.Log;
29-
import org.apache.commons.logging.LogFactory;
30-
28+
import org.springframework.core.log.LogAccessor;
3129
import org.springframework.integration.file.remote.session.Session;
3230
import org.springframework.util.Assert;
3331
import org.springframework.util.FileCopyUtils;
@@ -60,7 +58,7 @@
6058
*/
6159
public class SmbSession implements Session<SmbFile> {
6260

63-
private static final Log logger = LogFactory.getLog(SmbSession.class);
61+
private static final LogAccessor logger = new LogAccessor(SmbSession.class);
6462

6563
private static final String FILE_SEPARATOR = System.getProperty("file.separator");
6664

@@ -91,9 +89,6 @@ public class SmbSession implements Session<SmbFile> {
9189
public SmbSession(SmbShare _smbShare) {
9290
Assert.notNull(_smbShare, "smbShare must not be null");
9391
this.smbShare = _smbShare;
94-
if (logger.isDebugEnabled()) {
95-
logger.debug("New " + getClass().getName() + " created.");
96-
}
9792
}
9893

9994
/**
@@ -113,11 +108,11 @@ public boolean remove(String _path) throws IOException {
113108
removeFile.delete();
114109
removed = true;
115110
}
116-
if (!removed && logger.isInfoEnabled()) {
117-
logger.info("Could not remove non-existing resource [" + _path + "].");
111+
if (!removed) {
112+
logger.info(() -> "Could not remove non-existing resource [" + _path + "].");
118113
}
119-
else if (logger.isInfoEnabled()) {
120-
logger.info("Successfully removed resource [" + _path + "].");
114+
else {
115+
logger.info(() -> "Successfully removed resource [" + _path + "].");
121116
}
122117
return removed;
123118
}
@@ -131,34 +126,64 @@ else if (logger.isInfoEnabled()) {
131126
*/
132127
@Override
133128
public SmbFile[] list(String _path) throws IOException {
134-
SmbFile[] files = new SmbFile[0];
135129
try {
136130
SmbFile smbDir = createSmbDirectoryObject(_path);
137131
if (!smbDir.exists()) {
138-
if (logger.isWarnEnabled()) {
139-
logger.warn("Remote directory [" + _path + "] does not exist. Cannot list resources.");
140-
}
141-
return files;
132+
logger.warn(() -> "Remote directory [" + _path + "] does not exist. Cannot list resources.");
133+
return new SmbFile[0];
142134
}
143135
else if (!smbDir.isDirectory()) {
144136
throw new IOException("[" + _path + "] is not a directory. Cannot list resources.");
145137
}
146138

147-
files = smbDir.listFiles();
139+
SmbFile[] files = smbDir.listFiles();
140+
141+
logListedFiles(_path, files);
142+
143+
return files;
144+
}
145+
catch (SmbException _ex) {
146+
throw new IOException("Failed to list in [" + _path + "].", _ex);
147+
}
148+
}
149+
150+
/**
151+
* Return the contents of the specified SMB resource as an array of SmbFile filenames.
152+
* In case the remote resource does not exist, an empty array is returned.
153+
* @param _path path to a remote directory
154+
* @return array of SmbFile filenames
155+
* @throws IOException on error conditions returned by a CIFS server or if the remote resource is not a directory.
156+
*/
157+
@Override
158+
public String[] listNames(String _path) throws IOException {
159+
try {
160+
SmbFile smbDir = createSmbDirectoryObject(_path);
161+
if (!smbDir.exists()) {
162+
logger.warn(() -> "Remote directory [" + _path + "] does not exist. Cannot list resources.");
163+
return new String[0];
164+
}
165+
else if (!smbDir.isDirectory()) {
166+
throw new IOException("[" + _path + "] is not a directory. Cannot list resources.");
167+
}
168+
169+
String[] fileNames = smbDir.list();
170+
171+
logListedFiles(_path, fileNames);
172+
173+
return fileNames;
148174
}
149175
catch (SmbException _ex) {
150176
throw new IOException("Failed to list resources in [" + _path + "].", _ex);
151177
}
178+
}
152179

180+
private static void logListedFiles(String _path, Object[] files) {
153181
if (logger.isDebugEnabled()) {
154-
logger.debug("Successfully listed " + files.length + " resource(s) in [" + _path + "]"
155-
+ ": " + Arrays.toString(files));
182+
logger.debug("Successfully listed " + files.length + " in [" + _path + "]: " + Arrays.toString(files));
156183
}
157-
else if (logger.isInfoEnabled()) {
158-
logger.info("Successfully listed " + files.length + " resource(s) in [" + _path + "]" + ".");
184+
else {
185+
logger.info(() -> "Successfully listed " + files.length + " in [" + _path + "].");
159186
}
160-
161-
return files;
162187
}
163188

164189
/**
@@ -184,9 +209,7 @@ public void read(String _path, OutputStream _outputStream) throws IOException {
184209
throw new IOException("Failed to read resource [" + _path + "].", _ex);
185210
}
186211

187-
if (logger.isInfoEnabled()) {
188-
logger.info("Successfully read resource [" + _path + "].");
189-
}
212+
logger.info(() -> "Successfully read resource [" + _path + "].");
190213
}
191214

192215
/**
@@ -209,9 +232,7 @@ public void write(InputStream _inputStream, String _path) throws IOException {
209232
catch (SmbException _ex) {
210233
throw new IOException("Failed to write resource [" + _path + "].", _ex);
211234
}
212-
if (logger.isInfoEnabled()) {
213-
logger.info("Successfully wrote remote file [" + _path + "].");
214-
}
235+
logger.info(() -> "Successfully wrote remote file [" + _path + "].");
215236
}
216237

217238
/**
@@ -250,15 +271,11 @@ public boolean mkdir(String _path) throws IOException {
250271
SmbFile dir = createSmbDirectoryObject(_path);
251272
if (!dir.exists()) {
252273
dir.mkdirs();
253-
if (logger.isInfoEnabled()) {
254-
logger.info(
255-
"Successfully created remote directory [" + _path + "] in share [" + this.smbShare + "].");
256-
}
274+
logger.info(() ->
275+
"Successfully created remote directory [" + _path + "] in share [" + this.smbShare + "].");
257276
}
258277
else {
259-
if (logger.isInfoEnabled()) {
260-
logger.info("Remote directory [" + _path + "] exists in share [" + this.smbShare + "].");
261-
}
278+
logger.info(() -> "Remote directory [" + _path + "] exists in share [" + this.smbShare + "].");
262279
}
263280
return true;
264281
}
@@ -331,10 +348,7 @@ public void rename(String _pathFrom, String _pathTo) throws IOException {
331348
catch (SmbException _ex) {
332349
throw new IOException("Failed to rename [" + _pathFrom + "] to [" + _pathTo + "].", _ex);
333350
}
334-
if (logger.isInfoEnabled()) {
335-
logger.info("Successfully renamed remote resource [" + _pathFrom + "] to [" + _pathTo + "].");
336-
}
337-
351+
logger.info(() -> "Successfully renamed remote resource [" + _pathFrom + "] to [" + _pathTo + "].");
338352
}
339353

340354
@Override
@@ -350,15 +364,11 @@ public boolean rmdir(String directory) throws IOException {
350364
try {
351365
dir.delete();
352366
}
353-
catch (SmbException e) {
354-
if (logger.isWarnEnabled()) {
355-
logger.info("Failed to remove remote directory [" + directory + "]: " + e);
356-
}
367+
catch (SmbException ex) {
368+
logger.info(() -> "Failed to remove remote directory [" + directory + "]: " + ex);
357369
return false;
358370
}
359-
if (logger.isInfoEnabled()) {
360-
logger.info("Successfully removed remote directory [" + directory + "].");
361-
}
371+
logger.info(() -> "Successfully removed remote directory [" + directory + "].");
362372
return true;
363373
}
364374

@@ -480,43 +490,4 @@ public String getHostPort() {
480490
return url.getHost() + ":" + url.getPort();
481491
}
482492

483-
/**
484-
* Return the contents of the specified SMB resource as an array of SmbFile filenames.
485-
* In case the remote resource does not exist, an empty array is returned.
486-
* @param _path path to a remote directory
487-
* @return array of SmbFile filenames
488-
* @throws IOException on error conditions returned by a CIFS server or if the remote resource is not a directory.
489-
*/
490-
@Override
491-
public String[] listNames(String _path) throws IOException {
492-
String[] fileNames = new String[0];
493-
try {
494-
SmbFile smbDir = createSmbDirectoryObject(_path);
495-
if (!smbDir.exists()) {
496-
if (logger.isWarnEnabled()) {
497-
logger.warn("Remote directory [" + _path + "] does not exist. Cannot list resources.");
498-
}
499-
return fileNames;
500-
}
501-
else if (!smbDir.isDirectory()) {
502-
throw new IOException("[" + _path + "] is not a directory. Cannot list resources.");
503-
}
504-
505-
fileNames = smbDir.list();
506-
}
507-
catch (SmbException _ex) {
508-
throw new IOException("Failed to list resources in [" + _path + "].", _ex);
509-
}
510-
511-
if (logger.isDebugEnabled()) {
512-
logger.debug("Successfully listed " + fileNames.length + " resource(s) in [" + _path + "]"
513-
+ ": " + Arrays.toString(fileNames));
514-
}
515-
else if (logger.isInfoEnabled()) {
516-
logger.info("Successfully listed " + fileNames.length + " resource(s) in [" + _path + "]" + ".");
517-
}
518-
519-
return fileNames;
520-
}
521-
522493
}

spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,4 @@ public synchronized void close() {
139139
super.close();
140140
}
141141

142-
/**
143-
* Tests to see if two {@link SmbShare} objects are equal.
144-
* Relies on a super implementation.
145-
* @param other another {@link SmbShare} object to compare for equality.
146-
* @return equality result.
147-
*/
148-
@Override
149-
public boolean equals(Object other) {
150-
return super.equals(other);
151-
}
152-
153-
154-
/**
155-
* Return a cache code from the super class.
156-
* @return A hashcode for this share
157-
*/
158-
@Override
159-
public int hashCode() {
160-
return super.hashCode();
161-
}
162-
163142
}

0 commit comments

Comments
 (0)