*
* @return The newly created Application or null if unable to create
*/
@SuppressWarnings("unchecked")
protected Application createApplication(Context parentContext) {
Application application = null;
// Try to instantiate a new target application
// First, find the application class name
final String applicationClassName = getInitParameter(APPLICATION_KEY,
null);
// Load the application class using the given class name
if (applicationClassName != null) {
try {
final Class<?> targetClass = loadClass(applicationClassName);
try {
// Instantiate an application with the default constructor
// then invoke the setContext method.
application = (Application) targetClass.getConstructor()
.newInstance();
// Set the context based on the Servlet's context
application.setContext(new ServletContextAdapter(this,
parentContext));
} 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 without parameter. The constructor with a parameter of type Context will be used instead.");
// The constructor with the Context parameter does not
// exist. Create a new instance of the application class by
// invoking the constructor with the Context parameter.
application = (Application) targetClass.getConstructor(
Context.class).newInstance(
new ServletContextAdapter(this, parentContext));
}
} 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) {
final Context applicationContext = application.getContext();
// Copy all the servlet parameters into the context
String initParam;
// Copy all the Servlet component initialization parameters