001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.protocol.http; 018 019import java.lang.reflect.InvocationTargetException; 020 021import org.apache.wicket.WicketRuntimeException; 022 023/** 024 * Factory that creates application objects based on the class name specified in the 025 * {@link ContextParamWebApplicationFactory#APP_CLASS_PARAM} context variable. 026 * 027 * @author Igor Vaynberg (ivaynberg) 028 */ 029public class ContextParamWebApplicationFactory implements IWebApplicationFactory 030{ 031 /** 032 * context parameter name that must contain the class name of the application 033 */ 034 public static final String APP_CLASS_PARAM = "applicationClassName"; 035 036 /* @see IWebApplicationFactory#createApplication(WicketFilter) */ 037 @Override 038 public WebApplication createApplication(WicketFilter filter) 039 { 040 final String applicationClassName = filter.getFilterConfig().getInitParameter( 041 APP_CLASS_PARAM); 042 043 if (applicationClassName == null) 044 { 045 throw new WicketRuntimeException( 046 "servlet init param [" + 047 APP_CLASS_PARAM + 048 "] is missing. If you are trying to use your own implementation of IWebApplicationFactory and get this message then the servlet init param [" + 049 WicketFilter.APP_FACT_PARAM + "] is missing"); 050 } 051 052 return createApplication(applicationClassName); 053 } 054 055 /** 056 * Instantiates the application instance. 057 * 058 * @param applicationClassName 059 * the classname of the application to create 060 * @return the web application 061 */ 062 protected WebApplication createApplication(final String applicationClassName) 063 { 064 try 065 { 066 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 067 if (loader == null) 068 { 069 loader = getClass().getClassLoader(); 070 } 071 072 // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6500212 073 // final Class<?> applicationClass = loader.loadClass(applicationClassName); 074 final Class<?> applicationClass = Class.forName(applicationClassName, false, loader); 075 if (WebApplication.class.isAssignableFrom(applicationClass)) 076 { 077 // Construct WebApplication subclass 078 return (WebApplication)applicationClass.getDeclaredConstructor().newInstance(); 079 } 080 else 081 { 082 throw new WicketRuntimeException("Application class " + applicationClassName + 083 " must be a subclass of WebApplication"); 084 } 085 } 086 catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SecurityException 087 | NoSuchMethodException | InvocationTargetException e) 088 { 089 throw new WicketRuntimeException("Unable to create application of class " + 090 applicationClassName, e); 091 } 092 } 093 094 /** {@inheritDoc} */ 095 @Override 096 public void destroy(WicketFilter filter) 097 { 098 } 099 100}