* The Context for the Application
*
* @return The newly created Application or null if unable to create
*/
public Application createApplication(Context context) {
Application application = null;
// Try to instantiate a new target application
// First, find the application class name
String applicationClassName = getInitParameter(Application.KEY, null);
// Load the application class using the given class name
if (applicationClassName != null) {
try {
// According to
// http://www.caucho.com/resin-3.0/webapp/faq.xtp#Class.forName()-doesn't-seem-to-work-right
// this approach may need to used when loading classes.
Class<?> targetClass;
ClassLoader loader = Thread.currentThread()
.getContextClassLoader();
if (loader != null)
targetClass = Class.forName(applicationClassName, false,
loader);
else
targetClass = Class.forName(applicationClassName);
try {
// Create a new instance of the application class by
// invoking the constructor with the Context parameter.
application = (Application) targetClass.getConstructor(
Context.class).newInstance(context);
} catch (NoSuchMethodException e) {
log(
"[Noelios Restlet Engine] - The ServerServlet couldn't invoke the constructor of the target class. Please check this class has a constructor with a single parameter of Context. The empty constructor and the context setter wille used instead. "
+ applicationClassName, e);
// The constructor with the Context parameter does not
// exist. Instantiate an application with the default
// constructor then invoke the setContext method.
application = (Application) targetClass.getConstructor()
.newInstance();
}
} catch (ClassNotFoundException e) {
log(
"[Noelios Restlet Engine] - The ServerServlet couldn't find the target class. Please check that your classpath includes "
+ applicationClassName, e);
} catch (InstantiationException e) {
log(
"[Noelios Restlet Engine] - The ServerServlet couldn't instantiate the target class. Please check this class has an empty constructor "
+ applicationClassName, e);
} catch (IllegalAccessException e) {
log(
"[Noelios Restlet Engine] - The ServerServlet couldn't instantiate the target class. Please check that you have to proper access rights to "
+ applicationClassName, e);
} catch (NoSuchMethodException e) {
log(
"[Noelios Restlet Engine] - The ServerServlet couldn't invoke the constructor of the target class. Please check this class has a constructor with a single parameter of Context "
+ applicationClassName, e);
} catch (InvocationTargetException e) {
log(
"[Noelios Restlet Engine] - The ServerServlet couldn't instantiate the target class. An exception was thrown while creating "
+ applicationClassName, e);
}
if (application != null) {
// Set the context based on the Servlet's context
application.setContext(new ServletContextAdapter(this,
application, context));
}
}
return application;