/* * Copyright 2003 Federal Chancellery Austria * MOA-SPSS has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egovernment.moa.spss.server.invoke; import iaik.server.modules.IAIKException; import iaik.server.modules.IAIKRuntimeException; import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Map; import at.gv.egovernment.moa.spss.MOAApplicationException; import at.gv.egovernment.moa.spss.MOAException; import at.gv.egovernment.moa.spss.MOASystemException; /** * Map an exception from the iaik namespace to a * MOAException. * * @author Patrick Peck * @version $Id$ */ public class IaikExceptionMapper { /** The argument classes for MOAExceptions. */ private static final Class[] CONSTRUCTOR_ARGS = new Class[] { String.class, Object[].class, Throwable.class }; /** The exception mapping, as an array. */ private static final Object[][] MESSAGES = { { iaik.server.modules.IAIKException.class, "9900", MOASystemException.class }, { iaik.server.modules.IAIKRuntimeException.class, "9901", MOASystemException.class }, { iaik.server.modules.xmlsign.XMLSignatureCreationException.class, "2220", MOAApplicationException.class }, { iaik.server.modules.xmlsign.XMLSignatureCreationRuntimeException.class, "2220", MOAApplicationException.class }, { iaik.server.modules.xmlsign.InvalidKeyException.class, "2221", MOAApplicationException.class }, { iaik.server.modules.xmlsign.ManifestException.class, "2222", MOAApplicationException.class }, { iaik.server.modules.xmlsign.ReferenceException.class, "2223", MOAApplicationException.class }, { iaik.server.modules.xmlsign.HashUnavailableException.class, "2224", MOAApplicationException.class }, { iaik.server.modules.xmlsign.SignatureAlgorithmException.class, "2225", MOAApplicationException.class }, { iaik.server.modules.xmlsign.SignatureEmbeddingException.class, "2226", MOAApplicationException.class }, { iaik.server.modules.xmlsign.SignatureValueException.class, "2227", MOAApplicationException.class }, { iaik.server.modules.xmlsign.SignedPropertyException.class, "2228", MOAApplicationException.class }, { iaik.server.modules.xmlsign.SignerCertificateUnavailableException.class, "2229", MOAApplicationException.class }, { iaik.server.modules.xmlsign.SupplementException.class, "2230", MOAApplicationException.class }, { iaik.server.modules.xmlsign.TransformationException.class, "2233", MOAApplicationException.class }, { iaik.server.modules.cmsverify.CMSSignatureVerificationException.class, "2240", MOAApplicationException.class }, { iaik.server.modules.cmsverify.CMSSignatureVerificationRuntimeException.class, "2240", MOAApplicationException.class }, { iaik.server.modules.cmsverify.AlgorithmNotSupportedException.class, "2241", MOAApplicationException.class }, { iaik.server.modules.cmsverify.CMSSignatureParsingException.class, "2242", MOAApplicationException.class }, { iaik.server.modules.cmsverify.SignerCertificateUnavailableException.class, "2243", MOAApplicationException.class }, { iaik.server.modules.cmsverify.CMSSignatureVerificationRuntimeException.class, "2247", MOAApplicationException.class }, { iaik.server.modules.cmsverify.InitException.class, "2248", MOAApplicationException.class }, { iaik.server.modules.xmlverify.XMLSignatureVerificationException.class, "2240", MOAApplicationException.class }, { iaik.server.modules.xmlverify.XMLSignatureVerificationRuntimeException.class, "2240", MOAApplicationException.class }, { iaik.server.modules.xmlverify.AlgorithmNotSupportedException.class, "2241", MOAApplicationException.class }, { iaik.server.modules.xmlverify.ManifestException.class, "2262", MOAApplicationException.class }, { iaik.server.modules.xmlverify.PropertiesException.class, "2263", MOAApplicationException.class }, { iaik.server.modules.xmlverify.ReferenceException.class, "2264", MOAApplicationException.class }, { iaik.server.modules.xmlverify.HashUnavailableException.class, "2224", MOAApplicationException.class }, { iaik.server.modules.xmlverify.SignerCertificateUnavailableException.class, "2243", MOAApplicationException.class }, { iaik.server.modules.xmlverify.SupplementException.class, "2230", MOAApplicationException.class }, { iaik.server.modules.xmlverify.TransformationException.class, "2265", MOAApplicationException.class }, { iaik.server.modules.xmlverify.TransformationParsingException.class, "2269", MOAApplicationException.class }, { at.gv.egovernment.moa.sig.tsl.exception.TslException.class, "2290", MOAApplicationException.class }, { iaik.server.modules.cmssign.CMSSignatureCreationException.class, "2300", MOAApplicationException.class } , }; /** The single instance of this class. */ private static IaikExceptionMapper instance; /** The exception mapping, as a Map for fast lookup. */ private Map messages = new HashMap(); /** * Get the single instance of this class. * * @return The single instance of this class. */ public static synchronized IaikExceptionMapper getInstance() { if (instance == null) { instance = new IaikExceptionMapper(); } return instance; } /** * Create a new IaikExceptionMapper. * * Protected to disallow multple instances. */ protected IaikExceptionMapper() { registerMessages(); } /** * Build the complete IAIKException to message code mapping. */ protected void registerMessages() { int i; for (i = 0; i < MESSAGES.length; i++) { registerMessage( (Class) MESSAGES[i][0], (String) MESSAGES[i][1], (Class) MESSAGES[i][2]); } } /** * Register a single IAIKException to message mapping. * * @param iaikExceptionClass An exception from the iaik package. * @param messageId The corresponding error message id. * @param moaExceptionClass The type of MOAException that the * IAIKException is mapped to (usually * MOAApplicationException or MOASystemException). */ protected void registerMessage( Class iaikExceptionClass, String messageId, Class moaExceptionClass) { messages.put( iaikExceptionClass, new ExceptionMappingInfo(messageId, moaExceptionClass)); } /** * Map an iaik.xml.crypto.tsl.ex.TSLSearchException to a MOAException. * * @param tslSearchException The iaik.xml.crypto.tsl.ex.TSLSearchException to map. * @return A MOAException containing the message for the * given IAIKException. */ public MOAException map(at.gv.egovernment.moa.sig.tsl.exception.TslException tslSearchException) { return mapImpl(tslSearchException); } /** * Map an IAIKException to a MOAException. * * @param iaikException The IAIKException to map. * @return A MOAException containing the message for the * given IAIKException. */ public MOAException map(IAIKException iaikException) { return mapImpl(iaikException); } /** * Map an IAIKRuntimeException to a MOAException. * * @param iaikException The IAIKException to map. * @return A MOAException containing the message for the * given IAIKRuntimeException. */ public MOAException map(IAIKRuntimeException iaikException) { return mapImpl(iaikException); } /** * Map an IAIKException or IAIKRuntimeException to a * MOAException. * * @param iaikException The IAIKException or * IAIKRuntimeException to map. * @return A MOAException containing the message for the * given IAIKRuntimeException. */ private MOAException mapImpl(Exception iaikException) { MOAException moaException = createMoaException(iaikException); if (moaException == null) { return new MOASystemException("9999", null, iaikException); } return moaException; } /** * Create a MOAException from a given IAIKException * by looking it up in the mapping. * * @param iaikException The IAIKException to map. * @return A MOAException with an error code corresponding to * the given IAIKException. Returns null, if no * mapping could be found. */ protected MOAException createMoaException(Exception iaikException) { ExceptionMappingInfo info = lookupMessage(iaikException.getClass()); Constructor constructor; if (info == null) { return null; } // instantiate the proper MOAException and return it try { constructor = info.getMoaExceptionClass().getConstructor(CONSTRUCTOR_ARGS); return (MOAException) constructor.newInstance( new Object[] { info.getMessageId(), new Object[] { iaikException.getMessage()}, iaikException }); } catch (Exception e) { return null; } } /** * Recursively look up the message associated with an * IAIKException. * * This method walks up the exception inheritance hierarchy until it finds a * mapping. * * @param iaikExceptionClass The IAIKException to look up. * @return Information about the message id and * MOAException class that the iaikExceptionClass * maps to. If no mapping could be found, null is returned. */ protected ExceptionMappingInfo lookupMessage(Class iaikExceptionClass) { ExceptionMappingInfo info; // break if if (iaikExceptionClass.equals(Exception.class)) { return null; } // look up the exception class info = (ExceptionMappingInfo) messages.get(iaikExceptionClass); if (info == null) { return lookupMessage(iaikExceptionClass.getSuperclass()); } return info; } } /** * A class containing a mapping from an error message ID to a * MOAException class. * * @author Patrick Peck * @version $Id$ */ class ExceptionMappingInfo { /** The message ID. */ private String messageId; /** The MOAException class. */ private Class moaExceptionClass; /** * Create a new ExceptionMappingInfo. * * @param messageId The message ID. * @param moaExceptionClass The MOAException class. */ public ExceptionMappingInfo(String messageId, Class moaExceptionClass) { this.messageId = messageId; this.moaExceptionClass = moaExceptionClass; } /** * Return the message ID. * * @return The message ID. */ public String getMessageId() { return messageId; } /** * Returns the MOAException class that the message ID maps to. * * @return The MOAException class. */ public Class getMoaExceptionClass() { return moaExceptionClass; } }