package mixconfig.panels.smartcard; import java.util.List; import java.util.Map; import javax.swing.JOptionPane; import mixconfig.tools.dataretention.smartcard.ISmartCard; import mixconfig.tools.dataretention.smartcard.LogEntry; import mixconfig.tools.dataretention.smartcard.commands.ErrorCodeException; import mixconfig.tools.dataretention.smartcard.ConnectionManager; class SmartCardHelper { public static ISmartCard getISmartCardWithErrorHandling() { try { return addErrorHandlingToISmartCard(ConnectionManager.getInstance().connect()); } catch(java.net.ConnectException e) { JOptionPane.showMessageDialog(null, "There was an connection error.", "Error", JOptionPane.ERROR_MESSAGE) } } /** * http://groovy.codehaus.org/Groovy+way+to+implement+interfaces * http://stackoverflow.com/questions/1765469/dynamically-implement-interface-in-groovy-using-invokemethod * @param smartCard * @return */ public static ISmartCard addErrorHandlingToISmartCard(ISmartCard smartCard) { def map = [:] ISmartCard.class.methods.each() { method -> map[method.name] = { Object[] args -> try { return smartCard.invokeMethod(method.name, args); } catch(ErrorCodeException e) { JOptionPane.showMessageDialog(null, "There was an error.", "Error", JOptionPane.ERROR_MESSAGE) } catch(Exception e) { JOptionPane.showMessageDialog(null, "There was a totally unspecified error.", "Error", JOptionPane.ERROR_MESSAGE) } return null; } } return map.asType(ISmartCard.class) } }