private void buildAPIProcessor(String model, String mapping, ServletConfig cfg) throws IOException, ServletException, APIModelException { InputStream modelIn = getClass().getResourceAsStream(model); if (modelIn == null) { throw new ServletException("API model \"" + model + "\" not found"); } List<API> apis; try { if (model.endsWith(".xml") || model.endsWith(".wadl")) { apis = WADLReader.get().readModel("API", modelIn); } else if (model.endsWith(".txt") || model.endsWith(".md")) { apis = APITextReader.get().readModel("API", modelIn); } else { throw new ServletException("API model extension not supported for \"" + model + '\"'); } } finally { modelIn.close(); } log("Successfully read the API model from " + model); InputStream mappingIn = getClass().getResourceAsStream(mapping); if (mappingIn == null) { throw new ServletException("API mapping file \"" + mapping + "\" not found"); } Properties mappingProps = new Properties(); try { mappingProps.load(mappingIn); } finally { mappingIn.close(); } log("Successfully loaded the object mapping from " + mapping); processor = APIProcessorFactory.get().createProcessor(apis); for (String resName : mappingProps.stringPropertyNames()) { String className = mappingProps.getProperty(resName); Class<?> contextClass; try { contextClass = getClass().getClassLoader().loadClass(className); } catch (ClassNotFoundException cnfe) { throw new APIModelException("Cannot load class \"" + className + '\"', cnfe); } Object newHandler; try { newHandler = contextClass.newInstance(); } catch (InstantiationException ie) { throw new APIModelException("Can't create instance of \"" + className + '\"', ie); } catch (IllegalAccessException iae) { throw new APIModelException("Can't create instance of \"" + className + '\"', iae); } Object realHandler; if (newHandler instanceof HttpServlet) { HttpServlet handlerServlet = (HttpServlet) newHandler; handlerServlet.init(cfg); realHandler = handlerServlet; } else if (newHandler instanceof APIResourceHandler) { APIResourceHandler resHandler = (APIResourceHandler) newHandler; realHandler = new MasterResourceHandler(resHandler); } else { throw new ServletException( "Content class \"" + className + "\" is not of the right type. " + "It must extend HttpServlet or APIResourceHandler"); } processor.attachContext(resName, realHandler); } log("API processor initialized and ready to go"); }