1 /**************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 ****************************************************************/ 19 20 package org.apache.jsieve; 21 22 import java.util.Map; 23 24 import org.apache.jsieve.exception.LookupException; 25 import org.apache.jsieve.tests.ExecutableTest; 26 27 /** 28 * Maps Test names to configured Test 29 * implementation classes. 30 */ 31 public class TestManagerImpl implements TestManager { 32 33 private final Map classNameMap; 34 35 /** 36 * TestManager is instanciated with getInstance 37 */ 38 public TestManagerImpl(final Map classNameMap) { 39 super(); 40 this.classNameMap = classNameMap; 41 } 42 43 /** 44 * <p> 45 * Method lookup answers the class to which a Test name is mapped. 46 * </p> 47 * 48 * @param name - 49 * The name of the Test 50 * @return Class - The class of the Test 51 * @throws LookupException 52 */ 53 public Class lookup(String name) throws LookupException { 54 Class testClass = null; 55 56 try { 57 testClass = getClass().getClassLoader().loadClass( 58 getClassName(name)); 59 } catch (ClassNotFoundException e) { 60 throw new LookupException("Test named '" + name + "' not found."); 61 } 62 if (!ExecutableTest.class.isAssignableFrom(testClass)) 63 throw new LookupException("Class " + testClass.getName() 64 + " must implement " + ExecutableTest.class.getName()); 65 return testClass; 66 } 67 68 /** 69 * <p> 70 * Method newInstance answers an instance of the class to which a Test name 71 * is mapped. 72 * </p> 73 * 74 * @param name - 75 * The name of the Test 76 * @return Class - The class of the Test 77 * @throws LookupException 78 */ 79 public ExecutableTest getTest(String name) throws LookupException { 80 try { 81 return (ExecutableTest) lookup(name).newInstance(); 82 } catch (InstantiationException e) { 83 throw new LookupException(e.getMessage()); 84 } catch (IllegalAccessException e) { 85 throw new LookupException(e.getMessage()); 86 } 87 } 88 89 /** 90 * <p> 91 * Method getClassName answers the name of the class to which a Test name is 92 * mapped. 93 * </p> 94 * 95 * @param name - 96 * The name of the Test 97 * @return String - The name of the class 98 * @throws LookupException 99 */ 100 private String getClassName(String name) throws LookupException { 101 final String className = (String) classNameMap.get(name.toLowerCase()); 102 if (null == className) 103 throw new LookupException("Test named '" + name + "' not mapped."); 104 return className; 105 } 106 }