- From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
- Date: Thu, 06 Aug 2009 12:46:59 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn
In directory hutz:/tmp/cvs-serv2800/src/org/w3c/unicorn
Added Files:
Tag: dev2
Test.java Init.java Controller.java
Log Message:
w3 => w3c
--- NEW FILE: Controller.java ---
package org.w3c.unicorn;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.w3.unicorn.route.Route;
import org.w3c.unicorn.route.RouteParser;
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Logger logger;
private static boolean isInitialized = false;
@Override
public final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (isInitialized)
processRequest(request, response);
else
response.sendError(500, "Unicorn is not initialized properly. Check logs.");
}
@Override
public final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("Processing request: " + request.getRequestURL());
Route route = RouteParser.getRoute(request);
if (route.isSetUrl()) {
response.sendRedirect(route.getUrl());
logger.info("Request redirected to: " + route.getUrl());
}
else if (route.isSetAction()) {
RequestDispatcher dispatcher = getServletContext().getNamedDispatcher(route.getAction());
if (dispatcher == null) {
String error = "ERROR: " + route.getAction() + " does not corespond to any servlet-name in web.xml";
logger.error(error);
response.sendError(501, error);
} else {
try {
dispatcher.forward(request, response);
logger.info("Request dispatched to: " + route.getAction());
} catch (Exception e) {
logger.error("ERROR: " + e.getMessage(), e);
response.sendError(501, "ERROR: " + e.getMessage());
}
}
} else {
String error = "ERROR: No action associated with this route. Check route.xml.";
logger.error(error);
response.sendError(501, error);
}
}
static {
try {
Init.init();
logger = Logger.getLogger("Controller");
isInitialized = true;
logger.info("Unicorn initialized properly");
} catch (Exception e) {
try {
logger.error("Unicorn was not initialized properly. No request will be processed!");
} catch (Exception e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
--- NEW FILE: Test.java ---
package org.w3c.unicorn;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "${TEST} sdf zer ${TEST2}";
System.out.println(s.matches("\\$\\{[a-zA-Z_0-9]*\\}"));
Matcher matcher = Pattern.compile("\\$\\{[a-zA-Z_0-9]*\\}").matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
System.out.println(matcher.lookingAt());
System.out.println(matcher.group());
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
System.out.println(matcher.lookingAt());
System.out.println(matcher.group());
//replaceAll(repl)
}
}
--- NEW FILE: Init.java ---
package org.w3c.unicorn;
import java.io.File;
import java.net.URI;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.w3.unicorn.route.Route;
import org.w3c.unicorn.route.RouteParser;
import org.w3.unicorn.route.RoutesDocument;
import org.w3c.unicorn.util.Property;
import org.w3c.unicorn.util.UCNProperties;
public class Init {
private static Logger logger;
private static URI unicornHome;
public static void init() throws Exception {
// Checks that unicorn.home (JVM parameter) is set to an existing directory
String ucnHome = System.getProperty("unicorn.home");
if (ucnHome == null) {
String error = "ERROR: \"unicorn.home\" is not set in the JVM parameters. Please read the README file before trying to install Unicorn";
System.err.println(error);
throw new Exception(error);
} else if (!(new File(ucnHome)).exists() && (new File(ucnHome).isDirectory())){
String error = "ERROR: \"unicorn.home\" is not an existing directory: " + System.getProperty("unicorn.home");
System.err.println(error);
throw new Exception(error);
} else {
unicornHome = (new File(ucnHome)).toURI();
System.out.println("OK - \"unicorn.home\" was found: " + unicornHome.getPath());
}
// Log4j initialisation attempt
String log4jPath = unicornHome.getPath() + "/WEB-INF/conf/log4j.properties";
File log4jPropFile = new File(log4jPath);
if (!log4jPropFile.exists()) {
String error = "WARN: \"log4j.properties\" could not be found: " + log4jPath;
System.err.println(error);
System.err.println("WARN: Log4j will not be initialized.");
} else {
System.out.println("OK - Log4j initialized with file: " + log4jPath);
PropertyConfigurator.configure(log4jPropFile.toURI().toURL());
logger = Logger.getLogger("Init");
logger.info("Unicorn home directory was found");
logger.debug("Unicorn home path = " + unicornHome.getPath());
logger.info("Log4j has been successfully initialized");
logger.debug("Log4j properties file = " + log4jPropFile.toURI().toURL().getPath());
}
// Loading unicorn.properties
logger.debug("Loading unicorn.properties");
String unicornPath = unicornHome.getPath() + "/WEB-INF/conf/unicorn.properties";
logger.debug("Unicorn properties file = " + unicornPath);
File unicornPropFile = new File(unicornPath);
if (!unicornPropFile.exists()) {
String error = "ERROR: \"unicorn.properties\" could not be found: " + unicornPath;
System.err.println(error);
logger.error("Unicorn properties file does not exists! Path is: " + unicornPath);
throw new Exception(error);
} else {
UCNProperties ucnProperties = new UCNProperties();
ucnProperties.put("UNICORN_HOME", unicornHome.getPath());
ucnProperties.load(unicornPropFile.toURI().toURL().openStream());
Property.setUnicornProperties(ucnProperties);
logger.info("Unicorn properties file successfully loaded");
logger.debug("Loaded properties: " + Property.getUnicornProperties());
}
// Checking Unicorn properties
String[] mandatoryProperties = {"PATH_TO_CONF_FILES","ROUTE_XML","PATH_TO_CACHE"};
for (String property: mandatoryProperties) {
if (Property.get(property) == null) {
String error = "\""+ property +"\" is not defined in unicorn.properties. This property is mandatory.";
logger.error(error);
throw new Exception(error);
}
}
// Initialising RouteParser
logger.debug("Initialising RouteParser");
File routesFile = new File(Property.get("PATH_TO_CONF_FILES", "ROUTE_XML"));
if (!routesFile.exists()) {
String error = "Route xml file could not be found: " + routesFile;
logger.error(error);
throw new Exception(error);
}
RoutesDocument routesXML = RoutesDocument.Factory.parse(routesFile);
List<Route> tempRouteList = routesXML.getRoutes().getRouteList();
for(Route route : tempRouteList) {
logger.debug("Route found: " + route.xmlText());
if (route.isSetType() && route.getType().equals("index")) {
logger.debug("> This route is the Index route");
RouteParser.setIndex(route);
RouteParser.getRouteList().add(route);
} else if (route.isSetType() && route.getType().equals("404")) {
logger.debug("> This route is the 404 route");
RouteParser.setError404(route);
} else {
RouteParser.getRouteList().add(route);
}
}
if (RouteParser.getIndex() == null) {
String error = "There is non Index route. RouteParser is not initialized properly.";
logger.error(error);
throw new Exception(error);
} else if (RouteParser.getError404() == null) {
String error = "There is non 404 route. RouteParser is not initialized properly.";
logger.error(error);
throw new Exception(error);
} else {
logger.info("Unicorn routes successfully loaded");
}
}
public static void main(String argv[]) {
try {
init();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Property.init();
/*URL url;
HttpURLConnection connection;
try {
url = new URL("http://www.masdelafontanelle.fr");
connection = (HttpURLConnection) url.openConnection();
System.out.println(connection.getResponseCode());
System.out.println(connection.getContentType());
Map responseMap = connection.getHeaderFields();
System.out.println("");
for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext(); ) {
String key = (String) iterator.next();
System.out.print(key + " = ");
List values = (List) responseMap.get(key);
for (int i = 0; i < values.size(); i++) {
Object o = values.get(i);
System.out.print(o + ", ");
}
System.out.println("");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.error("toooooooooooooooooo");
logger.debug("Hello, my name is Homer Simpson.");
logger.debug("Hello, my name is Lisa Simpson.");
logger.debug("Hello, my name is Marge Simpson.");
logger.debug("Hello, my name is Bart Simpson.");
logger.debug("Hello, my name is Maggie Simpson.");
logger.info("We are the Simpsons!");
logger.info("Mmmmmm .... Chocolate.");
logger.info("Homer likes chocolate");
logger.info("Doh!");
logger.info("We are the Simpsons!");
logger.warn("Bart: I am through with working! Working is for chumps!" +
"Homer: Son, I'm proud of you. I was twice your age before " +
"I figured that out.");
logger.warn("Mmm...forbidden donut.");
logger.warn("D'oh! A deer! A female deer!");
logger.warn("Truly, yours is a butt that won't quit." +
"- Bart, writing as Woodrow to Ms. Krabappel.");
NDC.push("#23856");
logger.error("Dear Baby, Welcome to Dumpsville. Population: you.");
logger.error("Dear Baby, Welcome to Dumpsville. Population: you.",
new IOException("Dumpsville, USA"));
logger.error("Mr. Hutz, are you aware you're not wearing pants?");
logger.error("Mr. Hutz, are you aware you're not wearing pants?",
new IllegalStateException("Error !!"));
NDC.pop();
NDC.remove();
NDC.push("Another NDC");
// Log some information.
logger.fatal("Hello, my name is Bart Simpson.");
logger.error("Hi diddly ho good neighbour.");
// Clean up NDC
NDC.pop();
NDC.remove();
logger.fatal("Eep.");
logger.fatal("Mmm...forbidden donut.",
new SecurityException("Fatal Exception"));
logger.fatal("D'oh! A deer! A female deer!");
logger.fatal("Mmmmmm .... Chocolate.",
new SecurityException("Fatal Exception"));
*/
}
}
Received on Thursday, 6 August 2009 12:47:10 UTC