Skip to content

Commit f6d1349

Browse files
author
Henning Schmiedehausen
committed
remove some of the more weird runtime exception suppression code
1 parent 8904163 commit f6d1349

21 files changed

+54
-139
lines changed

Diff for: src/main/java/org/productivity/java/syslog4j/Syslog.java

+9-59
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.productivity.java.syslog4j.util.OSDetectUtility;
1919
import org.productivity.java.syslog4j.util.SyslogUtility;
2020

21+
import com.google.common.base.Preconditions;
2122
import com.google.common.collect.Maps;
2223

2324
/**
@@ -45,8 +46,6 @@
4546
*/
4647
public final class Syslog
4748
{
48-
private static boolean SUPPRESS_RUNTIME_EXCEPTIONS = false;
49-
5049
protected static final Map<String, SyslogIF> instances = Maps.newHashMap();
5150

5251
/**
@@ -64,42 +63,6 @@ private Syslog() {
6463
//
6564
}
6665

67-
/**
68-
* @return Returns the current version identifier for Syslog4j.
69-
*/
70-
public static final String getVersion() {
71-
return Syslog4jVersion.VERSION;
72-
}
73-
74-
/**
75-
* @param suppress - true to suppress throwing SyslogRuntimeException in many methods of this class, false to throw exceptions (default)
76-
*/
77-
public static void setSuppressRuntimeExceptions(boolean suppress) {
78-
SUPPRESS_RUNTIME_EXCEPTIONS = suppress;
79-
}
80-
81-
/**
82-
* @return Returns whether or not to suppress throwing SyslogRuntimeException in many methods of this class.
83-
*/
84-
public static boolean getSuppressRuntimeExceptions() {
85-
return SUPPRESS_RUNTIME_EXCEPTIONS;
86-
}
87-
88-
/**
89-
* Throws SyslogRuntimeException unless it has been suppressed via setSuppressRuntimeException(boolean).
90-
*
91-
* @param message
92-
* @throws SyslogRuntimeException
93-
*/
94-
private static void throwRuntimeException(String message) throws SyslogRuntimeException {
95-
if (SUPPRESS_RUNTIME_EXCEPTIONS) {
96-
return;
97-
98-
} else {
99-
throw new SyslogRuntimeException(message.toString());
100-
}
101-
}
102-
10366
/**
10467
* Use getInstance(protocol) as the starting point for Syslog4j.
10568
*
@@ -130,8 +93,7 @@ public static final SyslogIF getInstance(String protocol) throws SyslogRuntimeEx
13093
}
13194
}
13295

133-
throwRuntimeException(message.toString());
134-
return null;
96+
throw new SyslogRuntimeException(message.toString());
13597
}
13698
}
13799

@@ -153,27 +115,17 @@ public static final SyslogIF getInstance(String protocol) throws SyslogRuntimeEx
153115
* @return Returns an instance of SyslogIF.
154116
* @throws SyslogRuntimeException
155117
*/
156-
public static final SyslogIF createInstance(String protocol, SyslogConfigIF config) throws SyslogRuntimeException {
157-
158-
if (StringUtils.isBlank(protocol)) {
159-
throwRuntimeException("Instance protocol cannot be null or empty");
160-
return null;
161-
}
162-
163-
if (config == null) {
164-
throwRuntimeException("SyslogConfig cannot be null");
165-
return null;
166-
}
118+
public static final SyslogIF createInstance(String protocol, SyslogConfigIF config) throws SyslogRuntimeException
119+
{
120+
Preconditions.checkArgument(!StringUtils.isBlank(protocol), "Instance protocol cannot be null or empty");
121+
Preconditions.checkArgument(config != null, "SyslogConfig cannot be null");
167122

168123
String syslogProtocol = protocol.toLowerCase();
169124

170125
SyslogIF syslog = null;
171126

172127
synchronized(instances) {
173-
if (instances.containsKey(syslogProtocol)) {
174-
throwRuntimeException("Syslog protocol \"" + protocol + "\" already defined");
175-
return null;
176-
}
128+
Preconditions.checkState(!instances.containsKey(syslogProtocol), "Syslog protocol \"%s\" already defined", protocol);
177129

178130
try {
179131
Class<? extends SyslogIF> syslogClass = config.getSyslogClass();
@@ -295,8 +247,7 @@ public synchronized static final void destroyInstance(String protocol) throws Sy
295247
}
296248

297249
} else {
298-
throwRuntimeException("Cannot destroy protocol \"" + protocol + "\" instance; call shutdown instead");
299-
return;
250+
throw new SyslogRuntimeException("Cannot destroy protocol \"%s\" instance; call shutdown instead", protocol);
300251
}
301252
}
302253

@@ -323,8 +274,7 @@ public synchronized static final void destroyInstance(SyslogIF syslog) throws Sy
323274
}
324275

325276
} else {
326-
throwRuntimeException("Cannot destroy protocol \"" + protocol + "\" instance; call shutdown instead");
327-
return;
277+
throw new SyslogRuntimeException("Cannot destroy protocol \"%s\" instance; call shutdown instead", protocol);
328278
}
329279
}
330280

Diff for: src/main/java/org/productivity/java/syslog4j/Syslog4jVersion.java

-16
This file was deleted.

Diff for: src/main/java/org/productivity/java/syslog4j/SyslogCharSetIF.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.productivity.java.syslog4j;
22

3-
import java.io.Serializable;
43
import java.nio.charset.Charset;
54

65
/**
@@ -14,7 +13,7 @@
1413
* @author &lt;[email protected]&gt;
1514
* @version $Id: SyslogCharSetIF.java,v 1.3 2008/11/07 15:15:41 cvs Exp $
1615
*/
17-
public interface SyslogCharSetIF extends Serializable
16+
public interface SyslogCharSetIF
1817
{
1918
Charset getCharSet();
2019

Diff for: src/main/java/org/productivity/java/syslog4j/SyslogMain.java

-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ public static void main(String[] args, boolean shutdown) throws Exception {
126126
if (CALL_SYSTEM_EXIT_ON_FAILURE) { System.exit(1); } else { return; }
127127
}
128128

129-
if (!options.quiet) {
130-
System.out.println("Syslog " + Syslog.getVersion());
131-
}
132-
133129
if (!Syslog.exists(options.protocol)) {
134130
usage("Protocol \"" + options.protocol + "\" not supported");
135131
if (CALL_SYSTEM_EXIT_ON_FAILURE) { System.exit(1); } else { return; }

Diff for: src/main/java/org/productivity/java/syslog4j/SyslogMessageIF.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.productivity.java.syslog4j;
22

3-
import java.io.Serializable;
43

54
/**
65
* SyslogMessageIF provides a common interface for all Syslog4j event implementations.
@@ -12,6 +11,6 @@
1211
* @author &lt;[email protected]&gt;
1312
* @version $Id: SyslogMessageIF.java,v 1.1 2008/11/10 04:38:37 cvs Exp $
1413
*/
15-
public interface SyslogMessageIF extends Serializable {
14+
public interface SyslogMessageIF {
1615
public String createMessage();
1716
}

Diff for: src/main/java/org/productivity/java/syslog4j/SyslogMessageProcessorIF.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.productivity.java.syslog4j;
22

3-
import java.io.Serializable;
43

54
/**
65
* SyslogMessageProcessorIF provides an extensible interface for writing custom
@@ -13,7 +12,7 @@
1312
* @author &lt;[email protected]&gt;
1413
* @version $Id: SyslogMessageProcessorIF.java,v 1.4 2010/11/28 04:15:18 cvs Exp $
1514
*/
16-
public interface SyslogMessageProcessorIF extends Serializable {
15+
public interface SyslogMessageProcessorIF {
1716
public String createSyslogHeader(SyslogFacility facility, SyslogLevel level, String localName, boolean sendLocalTimestamp, boolean sendLocalName);
1817

1918
public byte[] createPacketData(byte[] header, byte[] message, int start, int length);

Diff for: src/main/java/org/productivity/java/syslog4j/SyslogPoolConfigIF.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.productivity.java.syslog4j;
22

3-
import java.io.Serializable;
43

54
/**
65
* SyslogPoolConfigIF is an interface which provides configuration support
@@ -13,7 +12,7 @@
1312
* @author &lt;[email protected]&gt;
1413
* @version $Id: SyslogPoolConfigIF.java,v 1.2 2009/03/29 17:38:58 cvs Exp $
1514
*/
16-
public interface SyslogPoolConfigIF extends Serializable {
15+
public interface SyslogPoolConfigIF {
1716
public int getMaxActive();
1817
public void setMaxActive(int maxActive);
1918

Diff for: src/main/java/org/productivity/java/syslog4j/SyslogRuntimeException.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
* @author &lt;[email protected]&gt;
1212
* @version $Id: SyslogRuntimeException.java,v 1.3 2008/11/13 14:48:36 cvs Exp $
1313
*/
14-
public class SyslogRuntimeException extends RuntimeException {
15-
public SyslogRuntimeException(String arg0) {
16-
super(arg0);
14+
public class SyslogRuntimeException extends RuntimeException
15+
{
16+
public SyslogRuntimeException(String format, Object ... args) {
17+
super(String.format(format, args));
1718
}
1819

19-
public SyslogRuntimeException(Throwable arg0) {
20-
super(arg0);
20+
public SyslogRuntimeException(Throwable t) {
21+
super(t);
22+
}
23+
24+
public SyslogRuntimeException(Throwable t, String format, Object ... args) {
25+
super(String.format(format, args), t);
2126
}
2227
}

Diff for: src/main/java/org/productivity/java/syslog4j/impl/AbstractSyslogWriter.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.productivity.java.syslog4j.impl;
22

3-
import java.io.Serializable;
43
import java.util.List;
54

65
import org.productivity.java.syslog4j.SyslogLevel;
@@ -23,7 +22,7 @@
2322
* @author &lt;[email protected]&gt;
2423
* @version $Id: AbstractSyslogWriter.java,v 1.9 2010/10/25 03:50:25 cvs Exp $
2524
*/
26-
public abstract class AbstractSyslogWriter implements Runnable, Serializable {
25+
public abstract class AbstractSyslogWriter implements Runnable {
2726
protected AbstractSyslog syslog = null;
2827

2928
protected List<byte []> queuedMessages = null;

Diff for: src/main/java/org/productivity/java/syslog4j/impl/backlog/NullSyslogBackLogHandler.java

+4
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@
1818
public class NullSyslogBackLogHandler implements SyslogBackLogHandlerIF {
1919
public static final NullSyslogBackLogHandler INSTANCE = new NullSyslogBackLogHandler();
2020

21+
@Override
2122
public void initialize() {
2223
//
2324
}
2425

26+
@Override
2527
public void down(SyslogIF syslog, String reason) {
2628
//
2729
}
2830

31+
@Override
2932
public void up(SyslogIF syslog) {
3033
//
3134
}
3235

36+
@Override
3337
public void log(SyslogIF syslog, SyslogLevel level, String message, String reason) {
3438
//
3539
}

Diff for: src/main/java/org/productivity/java/syslog4j/impl/backlog/Syslog4jBackLogHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void initialize() throws SyslogRuntimeException {
4545

4646
public void log(SyslogIF syslog, SyslogLevel level, String message, String reason) throws SyslogRuntimeException {
4747
if (this.syslog.getProtocol().equals(syslog.getProtocol())) {
48-
throw new SyslogRuntimeException("Ignoring this log entry since the backLog protocol \"" + this.syslog.getProtocol() + "\" is the same as the main protocol");
48+
throw new SyslogRuntimeException("Ignoring this log entry since the backLog protocol \"%s\" is the same as the main protocol", this.syslog.getProtocol());
4949
}
5050

5151
String combinedMessage = combine(syslog,level,message,reason);

Diff for: src/main/java/org/productivity/java/syslog4j/impl/message/modifier/checksum/ChecksumSyslogMessageModifier.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ChecksumSyslogMessageModifierConfig getConfig() {
5252

5353
protected void continuousCheckForVerify() {
5454
if (this.config.isContinuous()) {
55-
throw new SyslogRuntimeException(this.getClass().getName() + ".verify(..) does not work with isContinuous() returning true");
55+
throw new SyslogRuntimeException("%s.verify(..) does not work with isContinuous() returning true", this.getClass().getName());
5656
}
5757

5858
}

Diff for: src/main/java/org/productivity/java/syslog4j/impl/unix/UnixSyslogConfig.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public int getPort() {
3939
}
4040

4141
public void setHost(String host) throws SyslogRuntimeException {
42-
throw new SyslogRuntimeException("Host not appropriate for class \"" + this.getClass().getName() + "\"");
42+
throw new SyslogRuntimeException("Host not appropriate for class \"%s\"", this.getClass().getName());
4343
}
4444

4545
public void setPort(int port) throws SyslogRuntimeException {
46-
throw new SyslogRuntimeException("Port not appropriate for class \"" + this.getClass().getName() + "\"");
46+
throw new SyslogRuntimeException("Port not appropriate for class \"%s\"", this.getClass().getName());
4747
}
4848

4949
public String getLibrary() {

Diff for: src/main/java/org/productivity/java/syslog4j/impl/unix/socket/UnixSocketSyslogConfig.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public int getPort() {
6464
}
6565

6666
public void setHost(String host) throws SyslogRuntimeException {
67-
throw new SyslogRuntimeException("Host not appropriate for class \"" + this.getClass().getName() + "\"");
67+
throw new SyslogRuntimeException("Host not appropriate for class \"%s\"", this.getClass().getName());
6868
}
6969

7070
public void setPort(int port) throws SyslogRuntimeException {
71-
throw new SyslogRuntimeException("Port not appropriate for class \"" + this.getClass().getName() + "\"");
71+
throw new SyslogRuntimeException("Port not appropriate for class \"%s\"", this.getClass().getName());
7272
}
7373

7474
public String getLibrary() {
@@ -97,7 +97,7 @@ public void setType(int type) {
9797

9898
public void setType(String type) {
9999
if (type == null) {
100-
throw new SyslogRuntimeException("Type cannot be null for class \"" + this.getClass().getName() + "\"");
100+
throw new SyslogRuntimeException("Type cannot be null for class \"%s\"", this.getClass().getName());
101101
}
102102

103103
if ("SOCK_STREAM".equalsIgnoreCase(StringUtils.trimToEmpty(type))) {
@@ -107,7 +107,7 @@ public void setType(String type) {
107107
this.type = SOCK_DGRAM;
108108

109109
} else {
110-
throw new SyslogRuntimeException("Type must be \"SOCK_STREAM\" or \"SOCK_DGRAM\" for class \"" + this.getClass().getName() + "\"");
110+
throw new SyslogRuntimeException("Type must be \"SOCK_STREAM\" or \"SOCK_DGRAM\" for class \"%s\"", this.getClass().getName());
111111
}
112112
}
113113

@@ -121,14 +121,14 @@ public void setFamily(short family) {
121121

122122
public void setFamily(String family) {
123123
if (family == null) {
124-
throw new SyslogRuntimeException("Family cannot be null for class \"" + this.getClass().getName() + "\"");
124+
throw new SyslogRuntimeException("Family cannot be null for class \"%s\"", this.getClass().getName());
125125
}
126126

127127
if ("AF_UNIX".equalsIgnoreCase(StringUtils.trimToEmpty(family))) {
128128
this.family = AF_UNIX;
129129

130130
} else {
131-
throw new SyslogRuntimeException("Family must be \"AF_UNIX\" for class \"" + this.getClass().getName() + "\"");
131+
throw new SyslogRuntimeException("Family must be \"AF_UNIX\" for class \"%s\"", this.getClass().getName());
132132
}
133133
}
134134

Diff for: src/main/java/org/productivity/java/syslog4j/server/SyslogServer.java

-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.Set;
99

1010
import org.apache.commons.lang3.StringUtils;
11-
import org.productivity.java.syslog4j.Syslog4jVersion;
1211
import org.productivity.java.syslog4j.SyslogConstants;
1312
import org.productivity.java.syslog4j.SyslogRuntimeException;
1413
import org.productivity.java.syslog4j.server.impl.net.tcp.TCPNetSyslogServerConfig;
@@ -40,14 +39,6 @@ private SyslogServer() {
4039
//
4140
}
4241

43-
/**
44-
* @return Returns the current version identifier for Syslog4j.
45-
*/
46-
public static final String getVersion() {
47-
return Syslog4jVersion.VERSION;
48-
}
49-
50-
5142
/**
5243
* @param suppress - true to suppress throwing SyslogRuntimeException in many methods of this class, false to throw exceptions (default)
5344
*/
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.productivity.java.syslog4j.server;
22

3-
import java.io.Serializable;
43

5-
public abstract interface SyslogServerEventHandlerIF extends Serializable {
4+
public abstract interface SyslogServerEventHandlerIF {
65
public void initialize(SyslogServerIF syslogServer);
76
public void destroy(SyslogServerIF syslogServer);
87
}

0 commit comments

Comments
 (0)