Java tutorial
/******************************************************************************* * Copyright 2014 Juan Diego Navarre Gonzalez * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package net.navasoft.madcoin.backend.services.controller.exception.impl; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Locale; import java.util.Map; import javax.annotation.Resource; import net.navasoft.madcoin.backend.services.controller.exception.AllowedExceptionMessage; import net.navasoft.madcoin.backend.services.controller.exception.ControllerException; import net.navasoft.madcoin.backend.services.controller.exception.ControllerExceptionArgument; import org.apache.commons.lang.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.MessageSource; import org.springframework.stereotype.Component; /** * A factory for creating ControllerException objects. */ @Component public class ControllerExceptionFactory { /** * locator. * * @since 27/07/2014, 06:48:34 PM */ private static Map<String, Class<? extends ControllerException>> locator; /** * language. * * @since 27/07/2014, 06:48:30 PM */ private static Locale language; /** * exception messages. * * @since 27/07/2014, 06:48:33 PM */ private static MessageSource exceptionMessages; protected static MessageSource tips; /** * Sets the locator. * * @param injectedLocator * the injected locator * @since 27/07/2014, 06:48:34 PM */ @Resource(name = "controllerExceptionMapper") public void setLocator(Map<String, Class<? extends ControllerException>> injectedLocator) { locator = injectedLocator; } /** * Sets the message bundle. * * @param messageProvider * the new message bundle * @since 27/07/2014, 06:48:33 PM */ @Autowired(required = true) @Qualifier("messageSource") public void setMessageBundle(final MessageSource messageProvider) { exceptionMessages = messageProvider; } /** * Sets the tips bundle. * * @param messageProvider * the new message bundle * @since 27/07/2014, 06:48:33 PM */ @Autowired(required = true) @Qualifier("messageSource") public void setTips(final MessageSource messageProvider) { tips = messageProvider; } /** * Sets the language * * @param internationalization * the injected language * @since 27/07/2014, 06:48:34 PM */ @Autowired @Qualifier("locale") public void setLanguage(Locale internationalization) { language = internationalization; } /** * Instantiates a new net.navasoft.madcoin.backend.model.controller.impl * exception factory. * * @since 27/07/2014, 06:48:34 PM */ public ControllerExceptionFactory() { try { AllowedExceptionMessage.initialize(); } catch (IOException e) { e.printStackTrace(); } } /** * Creates a new ControllerException object. * * @param expectedException * the expected exception * @param allowedTips * forwarded service tips * @param variables * the variables * @return the session net.navasoft.madcoin.backend.model.controller.impl * exception */ public static ControllerException createException(String expectedException, int allowedTips, ControllerExceptionArgument... variables) { ControllerException childMade = null; try { variables = (ControllerExceptionArgument[]) ArrayUtils.add(variables, new ControllerExceptionArgument(allowedTips)); Object builtException = locator.get(expectedException) .getConstructor(MessageSource.class, Locale.class, ControllerExceptionArgument[].class) .newInstance(exceptionMessages, language, (Object) variables); childMade = ControllerException.class.cast(builtException); childMade.prepareTips(allowedTips, tips); childMade.formulateTips(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } return childMade; } }