Skip to content

Commit

Permalink
There is no reason to declare never thrown ISOException
Browse files Browse the repository at this point in the history
In ISOMsg methods set and unset declare never thrown ISOException.
This leads to the introduction of many lines of boilerplate code at
calling that methods.
This change removes that unnececary declarations. As a side effect it
can cause not compiling code. It may be necessary to remove unnecesary
handling of ISOException (As a result simplifying that calls).
  • Loading branch information
demsey committed Apr 6, 2016
1 parent f91eaa2 commit 5ce639e
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 157 deletions.
32 changes: 14 additions & 18 deletions jpos/src/main/java/org/jpos/core/CardHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,20 @@ public CardHolder (ISOMsg m)
throws InvalidCardException
{
super();
try {
if (m.hasField(35))
parseTrack2 ((String) m.getValue(35));
else if (m.hasField(2)) {
setPAN ((String) m.getValue(2));
if (m.hasField (14))
setEXP ((String) m.getValue(14));
} else {
throw new InvalidCardException("required fields not present");
}
if (m.hasField(45)) {
setTrack1((String) m.getValue(45));
}
if (m.hasField(55)) {
setSecurityCode (m.getString(55));
}
} catch (ISOException e) {
throw new InvalidCardException();
if (m.hasField(35))
parseTrack2((String) m.getValue(35));
else if (m.hasField(2)) {
setPAN((String) m.getValue(2));
if (m.hasField(14))
setEXP((String) m.getValue(14));
} else {
throw new InvalidCardException("required fields not present");
}
if (m.hasField(45)) {
setTrack1((String) m.getValue(45));
}
if (m.hasField(55)) {
setSecurityCode(m.getString(55));
}
}

Expand Down
146 changes: 77 additions & 69 deletions jpos/src/main/java/org/jpos/iso/ISOMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,20 @@ public void set (ISOComponent c) throws ISOException {
dirty = true;
}
}
/**
* Creates an ISOField associated with fldno within this ISOMsg
* @param fldno field number
* @param value field value
* @throws ISOException on error
*/
public void set(int fldno, String value) throws ISOException {
if (value != null) {

/**
* Creates an ISOField associated with fldno within this ISOMsg.
*
* @param fldno field number
* @param value field value
*/
public void set(int fldno, String value) {
if (value == null) {
unset(fldno);
return;
}

try {
if (!(packager instanceof ISOBasePackager)) {
// No packager is available, we can't tell what the field
// might be, so treat as a String!
Expand All @@ -235,18 +241,16 @@ public void set(int fldno, String value) throws ISOException {
set(new ISOField(fldno, value));
}
}
}
else
unset(fldno);
} catch (ISOException ex) {}; //NOPMD: never happens for the given arguments of set methods
}

/**
* Creates an ISOField associated with fldno within this ISOMsg
* @param fpath dot-separated field path (i.e. 63.2)
* @param value field value
* @throws ISOException on error
*/
public void set (String fpath, String value) throws ISOException {
/**
* Creates an ISOField associated with fldno within this ISOMsg.
*
* @param fpath dot-separated field path (i.e. 63.2)
* @param value field value
*/
public void set(String fpath, String value) {
StringTokenizer st = new StringTokenizer (fpath, ".");
ISOMsg m = this;
for (;;) {
Expand All @@ -256,18 +260,20 @@ public void set (String fpath, String value) throws ISOException {
if (obj instanceof ISOMsg)
m = (ISOMsg) obj;
else
/*
/**
* we need to go deeper, however, if the value == null then
* there is nothing to do (unset) at the lower levels, so break now and save some processing.
*/
if (value == null) {
break;
} else {
// We have a value to set, so adding a level to hold it is sensible.
m.set (m = new ISOMsg (fldno));
try {
// We have a value to set, so adding a level to hold it is sensible.
m.set(m = new ISOMsg (fldno));
} catch (ISOException ex) {} //NOPMD: never happens for the given arguments of set methods
}
} else {
m.set (fldno, value);
m.set(fldno, value);
break;
}
}
Expand Down Expand Up @@ -305,15 +311,14 @@ public void set (String fpath, ISOComponent c) throws ISOException {
}
}
}


/**
* Creates an ISOField associated with fldno within this ISOMsg
* @param fpath dot-separated field path (i.e. 63.2)
* @param value binary field value
* @throws ISOException on error
*/
public void set (String fpath, byte[] value) throws ISOException {

/**
* Creates an ISOField associated with fldno within this ISOMsg.
*
* @param fpath dot-separated field path (i.e. 63.2)
* @param value binary field value
*/
public void set(String fpath, byte[] value) {
StringTokenizer st = new StringTokenizer (fpath, ".");
ISOMsg m = this;
for (;;) {
Expand All @@ -323,24 +328,31 @@ public void set (String fpath, byte[] value) throws ISOException {
if (obj instanceof ISOMsg)
m = (ISOMsg) obj;
else
m.set (m = new ISOMsg (fldno));
try {
m.set(m = new ISOMsg (fldno));
} catch (ISOException ex) {} //NOPMD: never happens for the given arguments of set methods
} else {
m.set (fldno, value);
m.set(fldno, value);
break;
}
}
}
/**
* Creates an ISOBinaryField associated with fldno within this ISOMsg
* @param fldno field number
* @param value field value
* @throws ISOException on error
*/
public void set (int fldno, byte[] value) throws ISOException {
if (value != null)
set (new ISOBinaryField (fldno, value));
else
unset (fldno);

/**
* Creates an ISOBinaryField associated with fldno within this ISOMsg.
*
* @param fldno field number
* @param value field value
*/
public void set(int fldno, byte[] value) {
if (value == null) {
unset(fldno);
return;
}

try {
set(new ISOBinaryField(fldno, value));
} catch (ISOException ex) {}; //NOPMD: never happens for the given arguments of set methods
}


Expand All @@ -361,12 +373,13 @@ public void unset (int[] flds) {
for (int fld : flds)
unset(fld);
}

/**
* Unset a field referenced by a fpath if it exists, otherwise ignore.
*
* @param fpath dot-separated field path (i.e. 63.2)
* @throws ISOException on error
*/
public void unset (String fpath) throws ISOException {
*/
public void unset(String fpath) {
StringTokenizer st = new StringTokenizer (fpath, ".");
ISOMsg m = this;
ISOMsg lastm = m;
Expand All @@ -382,7 +395,7 @@ public void unset (String fpath) throws ISOException {
m = (ISOMsg) obj;
}
else {
// No real way of unset further subfield, exit. Perhaps should be ISOException?
// No real way of unset further subfield, exit.
break;
}
} else {
Expand Down Expand Up @@ -521,11 +534,14 @@ public ISOComponent getComponent(int fldno) {
* Return the object value associated with the given field number
* @param fldno the Field Number
* @return the field Object
* @throws ISOException on error
*/
public Object getValue(int fldno) throws ISOException {
public Object getValue(int fldno) {
ISOComponent c = getComponent(fldno);
return c != null ? c.getValue() : null;
try {
return c != null ? c.getValue() : null;
} catch (ISOException ex) {
return null; //never happens for the given arguments of getValue method
}
}
/**
* Return the object value associated with the given field path
Expand Down Expand Up @@ -588,15 +604,11 @@ public ISOComponent getComponent (String fpath) throws ISOException {
public String getString (int fldno) {
String s = null;
if (hasField (fldno)) {
try {
Object obj = getValue(fldno);
if (obj instanceof String)
s = (String) obj;
else if (obj instanceof byte[])
s = ISOUtil.hexString ((byte[]) obj);
} catch (ISOException e) {
return null; // make PMD happy by returning here and avoiding an empty catch
}
Object obj = getValue(fldno);
if (obj instanceof String)
s = (String) obj;
else if (obj instanceof byte[])
s = ISOUtil.hexString((byte[]) obj);
}
return s;
}
Expand Down Expand Up @@ -626,15 +638,11 @@ else if (obj instanceof byte[])
public byte[] getBytes (int fldno) {
byte[] b = null;
if (hasField (fldno)) {
try {
Object obj = getValue(fldno);
if (obj instanceof String)
b = ((String) obj).getBytes(ISOUtil.CHARSET);
else if (obj instanceof byte[])
b = (byte[]) obj;
} catch (ISOException ignored) {
return null;
}
Object obj = getValue(fldno);
if (obj instanceof String)
b = ((String) obj).getBytes(ISOUtil.CHARSET);
else if (obj instanceof byte[])
b = (byte[]) obj;
}
return b;
}
Expand Down
45 changes: 21 additions & 24 deletions jpos/src/main/java/org/jpos/iso/filter/ChannelInfoFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,30 @@ public void setConfiguration (Configuration cfg)
socketInfoField = cfg.get("socket-info", null);
}

@Override
public ISOMsg filter (ISOChannel channel, ISOMsg m, LogEvent evt) {
try {
if (channelNameField != null)
m.set (channelNameField, channel.getName());
if (socketInfoField != null && channel instanceof BaseChannel) {
Socket socket = ((BaseChannel)channel).getSocket();
InetSocketAddress remoteAddr =
(InetSocketAddress) socket.getRemoteSocketAddress();
InetSocketAddress localAddr =
(InetSocketAddress) socket.getLocalSocketAddress();
if (channelNameField != null)
m.set(channelNameField, channel.getName());
if (socketInfoField != null && channel instanceof BaseChannel) {
Socket socket = ((BaseChannel) channel).getSocket();
InetSocketAddress remoteAddr =
(InetSocketAddress) socket.getRemoteSocketAddress();
InetSocketAddress localAddr =
(InetSocketAddress) socket.getLocalSocketAddress();

StringBuilder sb = new StringBuilder();
if (socketInfoField.equals(channelNameField)) {
sb.append (channel.getName());
sb.append (' ');
}
sb.append (localAddr.getAddress().getHostAddress());
sb.append (':');
sb.append (Integer.toString (localAddr.getPort()));
sb.append (' ');
sb.append (remoteAddr.getAddress().getHostAddress());
sb.append (':');
sb.append (Integer.toString (remoteAddr.getPort()));
m.set (socketInfoField, sb.toString());
StringBuilder sb = new StringBuilder();
if (socketInfoField.equals(channelNameField)) {
sb.append(channel.getName());
sb.append(' ');
}
} catch (ISOException e) {
evt.addMessage (e);
sb.append(localAddr.getAddress().getHostAddress());
sb.append(':');
sb.append(Integer.toString (localAddr.getPort()));
sb.append(' ');
sb.append(remoteAddr.getAddress().getHostAddress());
sb.append(':');
sb.append(Integer.toString (remoteAddr.getPort()));
m.set (socketInfoField, sb.toString());
}
return m;
}
Expand Down
Loading

0 comments on commit 5ce639e

Please sign in to comment.