package a;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.api.Executor;
import org.api.ExecutorFactory;
/**
*
*/
public class NewServlet extends HttpServlet {
/**
* Processes requests for both HTTP GET
and POST
methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("");
out.println("
");
out.println("Servlet NewServlet");
out.println("");
out.println("");
out.println("Servlet NewServlet at " + request.getContextPath () + "
");
out.println("");
out.println("");
} finally {
out.close();
}
}
//
/**
* Handles the HTTP GET
method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST
method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
@Override
public void init(ServletConfig arg) throws ServletException {
super.init(arg);
List providers = null;
try {
providers = getProviders(ExecutorFactory.class);
} catch (Throwable t) {
throw new ServletException(t);
}
if (providers != null) {
Iterator it = providers.iterator();
while (it.hasNext()) {
getServletContext().log("PROVIDER INSTANCE=" + it.next());
}
}
}
private List getProviders(Class service) throws Exception {
String prefix = "META-INF/services/";
String serviceName = service.getName();
getServletContext().log("ServiceName=" + serviceName);
Enumeration urls = getClass().getClassLoader().getResources(prefix + serviceName);
if (urls == null) {
return null;
}
List providers = new ArrayList();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
getServletContext().log("URL=" + url);
InputStream in = null;
BufferedReader r = null;
ArrayList names = new ArrayList();
try {
URLConnection uc = url.openConnection();
uc.setUseCaches(false);
in = uc.getInputStream();
r = new BufferedReader(new InputStreamReader(
in, "utf-8"));
int lc = 1;
while ((lc = parseLine(service, url, r, lc, names)) >= 0);
Iterator clNames = names.iterator();
while (clNames != null && clNames.hasNext()) {
providers.add(getClass().getClassLoader().loadClass(
clNames.next()).newInstance());
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
// do nothing
}
}
if (r != null) {
try {
r.close();
} catch (IOException ioe) {
// do nothing
}
}
}
}
return providers;
}
// Parse a single line from the given configuration file, adding the name
// on the line to the names list.
private int parseLine(Class service, URL u, BufferedReader r, int lc,
List names)
throws IOException, ServiceConfigurationError
{
String ln = r.readLine();
getServletContext().log("PROVIDER=" + ln);
if (ln == null) {
return -1;
}
int ci = ln.indexOf('#');
if (ci >= 0) ln = ln.substring(0, ci);
ln = ln.trim();
int n = ln.length();
if (n != 0) {
if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
fail(service, u, lc, "Illegal configuration-file syntax");
int cp = ln.codePointAt(0);
if (!Character.isJavaIdentifierStart(cp))
fail(service, u, lc, "Illegal provider-class name: " + ln);
for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
cp = ln.codePointAt(i);
if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
fail(service, u, lc, "Illegal provider-class name: " + ln);
}
if (!names.contains(ln)) {
names.add(ln);
}
}
return lc + 1;
}
private static void fail(Class service, String msg, Throwable cause)
throws ServiceConfigurationError
{
throw new ServiceConfigurationError(service.getName() + ": " + msg,
cause);
}
private static void fail(Class service, String msg)
throws ServiceConfigurationError
{
throw new ServiceConfigurationError(service.getName() + ": " + msg);
}
private static void fail(Class service, URL u, int line, String msg)
throws ServiceConfigurationError
{
fail(service, u + ":" + line + ": " + msg);
}
}