/* * Copyright (2007-2012) Schibsted ASA * This file is part of Possom. * * Possom is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Possom is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Possom. If not, see <http://www.gnu.org/licenses/>. * */ package no.sesat.search.run.handler; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import no.sesat.commons.ioc.BaseContext; import no.sesat.commons.ioc.ContextWrapper; import no.sesat.search.run.handler.AbstractRunHandlerConfig.Controller; import no.sesat.search.site.config.SiteClassLoaderFactory; import no.sesat.search.site.config.Spi; /** Obtain a working RunHandler. * * * * @version <tt>$Id: RunHandlerFactory.java 5819 2007-10-11 07:47:05Z ssmiweve $</tt> */ public final class RunHandlerFactory { // Constants ----------------------------------------------------- // Attributes ---------------------------------------------------- // Static -------------------------------------------------------- // Constructors -------------------------------------------------- /** Creates a new instance of RunHandlerFactory */ private RunHandlerFactory() { } // Public -------------------------------------------------------- /** * * @param context * @param config * @return */ public static RunHandler getController( final RunHandler.Context context, final RunHandlerConfig config){ final String name = "no.sesat.search.run.handler." + config.getClass().getAnnotation(Controller.class).value(); try{ final SiteClassLoaderFactory.Context ctlContext = ContextWrapper.wrap( SiteClassLoaderFactory.Context.class, new BaseContext() { public Spi getSpi() { return Spi.RUN_HANDLER_CONTROL; } }, context ); final ClassLoader ctlLoader = SiteClassLoaderFactory.instanceOf(ctlContext).getClassLoader(); @SuppressWarnings("unchecked") final Class<? extends RunHandler> cls = (Class<? extends RunHandler>)ctlLoader.loadClass(name); final Constructor<? extends RunHandler> constructor = cls.getConstructor(RunHandlerConfig.class); return constructor.newInstance(config); } catch (ClassNotFoundException ex) { throw new IllegalArgumentException(ex); } catch (NoSuchMethodException ex) { throw new IllegalArgumentException(ex); } catch (InvocationTargetException ex) { throw new IllegalArgumentException(ex); } catch (InstantiationException ex) { throw new IllegalArgumentException(ex); } catch (IllegalAccessException ex) { throw new IllegalArgumentException(ex); } } }