|
From: <ian...@us...> - 2007-10-07 00:23:16
|
Revision: 484
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=484&view=rev
Author: iansmith
Date: 2007-10-06 17:23:19 -0700 (Sat, 06 Oct 2007)
Log Message:
-----------
After feeling guilty about the build situations, I've added at least a bandage.
There is a new mojo connected to ogoglio-server's build process that verifies that your server.xml is complete with respect to the context.xml in the server's war. It will print out various helpful messages if it sees your configuration is wrong. This means that the context.xml in a war is *the law* and that we will refuse to build (much less run) on a development machine that is incorrectly configured in terms of server.xml in the tomcat dir.
You need to add <tomcat.home> to your settings.xml--but I have improved the error messages so at least it will tell you if you don't have it.
Updated the wiki.
Modified Paths:
--------------
maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/appdev/servlet/AbstractResourceServlet.java
maven/trunk/ogoglio-server/pom.xml
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java
Added Paths:
-----------
maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/VerifyEnvironmentMojo.java
Added: maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/VerifyEnvironmentMojo.java
===================================================================
--- maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/VerifyEnvironmentMojo.java (rev 0)
+++ maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/VerifyEnvironmentMojo.java 2007-10-07 00:23:19 UTC (rev 484)
@@ -0,0 +1,167 @@
+package com.ogoglio.plugin;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import nanoxml.XMLElement;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+
+/**
+ * @goal verifyEnvironment
+ */
+public class VerifyEnvironmentMojo extends AbstractMojo {
+ private static final String RESOURCE_MSG = "Typically, <Resource> configurations look like this:\n" + "<Resource name=\"UserDatabase\" auth=\"Container\"" + "\ttype=\"org.apache.catalina.UserDatabase\"\n" + "\tdescription=\"User database that can be updated and saved (for the manager appl.)\"\n" + "\tfactory=\"org.apache.catalina.users.MemoryUserDatabaseFactory\"\n"
+ + "\tpathname=\"conf/tomcat-users.xml\" />\n" + "<Resource name=\"ogoglioDB\" scope=\"Shareable\"\n" + "\ttype=\"javax.sql.DataSource\" auth='Container'\n" + "\tfactory=\"org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory\"\n" + "\turl=\"jdbc:mysql://127.0.0.1/og\"\n" + "\tdriverClassName=\"com.mysql.jdbc.Driver\"\n" + "\tusername=\"oguser\n" + "\tpassword=\"sssh\"\n"
+ + "\tdialect=\"org.hibernate.dialect.MySQLDialect\"\n" + "\tshow_sql=\"false\"\n" + "\tmaxIdle=\"5\"\n" + "\tmaxActive=\"50\"/>";
+
+
+ private static final String[] MAJOR_RESOURCES = {
+ "ogoglioDB",
+ "UserDatabase"
+ };
+ /**
+ * @parameter
+ */
+ protected File contextFile;
+
+ /**
+ * @parameter
+ */
+ protected File tomcatDir;
+
+ public void execute() throws MojoExecutionException, MojoFailureException {
+
+ verifyNotNull(tomcatDir, "tomcat.home");
+
+ fileOkForReading(contextFile);
+ if ((!tomcatDir.isDirectory()) || (!tomcatDir.canRead())) {
+ throw new MojoExecutionException("Can't read tomcat directory:" + tomcatDir);
+ }
+ File serverFile = new File(new File(tomcatDir, "conf"), "server.xml");
+ fileOkForReading(serverFile);
+
+ getLog().info("Verifying project context variables against server.xml config in tomcat.");
+
+ try {
+ XMLElement serverRoot = new XMLElement();
+ serverRoot.parseFromReader(new FileReader(serverFile));
+
+ XMLElement contextRoot = new XMLElement();
+ contextRoot.parseFromReader(new FileReader(contextFile));
+
+ checkContextAgainstServer(contextRoot, serverRoot);
+ } catch (IOException e) {
+ throw new MojoExecutionException("Can't read in the server or context files!", e);
+ }
+ }
+
+ private void checkContextAgainstServer(XMLElement contextRoot, XMLElement serverRoot) throws MojoExecutionException, MojoFailureException {
+ Map<String, String> resourceLinkMap;
+
+ resourceLinkMap = computeExpectedLinks(contextRoot);
+ List<String> badLinks = findMissingLinks(serverRoot, resourceLinkMap);
+ if (badLinks.size() != 0) {
+ getLog().error("Bad tomcat config file server.xml. You seem to be missing:");
+ Iterator<String> it = badLinks.iterator();
+ while (it.hasNext()) {
+ String missingGlobal = it.next();
+ getLog().error("<Environment name=\"" + missingGlobal + "\" value=\"someValue\" type=\"java.lang.String\"/>");
+ }
+ throw new MojoFailureException("Failed to verify that server.xml and context.xml are in sync!");
+ }
+ }
+
+ private List<String> findMissingLinks(XMLElement serverRoot, Map<String, String> resourceLinkMap) throws MojoExecutionException, MojoFailureException {
+ ArrayList<String> result = new ArrayList<String>();
+ List<String> areDefined=new ArrayList<String>();
+
+ XMLElement global = getChildElementSafe(serverRoot, "GlobalNamingResources");
+ XMLElement[] resource = global.getChildren("Resource");
+ if (resource.length != MAJOR_RESOURCES.length ) {
+ throw new MojoFailureException("You don't have the resource tags for the user database and the mysql database!" + RESOURCE_MSG);
+ } else {
+ for (int i=0; i<resource.length;++i) {
+ areDefined.add(resource[i].getStringAttribute("name"));
+ }
+ for (int i=0; i<MAJOR_RESOURCES.length;++i){
+ if (!areDefined.contains(MAJOR_RESOURCES[i])) {
+ throw new MojoFailureException("You are missing resource "+MAJOR_RESOURCES[i]+"."+RESOURCE_MSG);
+ }
+ }
+ }
+
+ XMLElement[] env = global.getChildren("Environment");
+ for (int i=0; i<env.length;++i) {
+ areDefined.add(env[i].getStringAttribute("name",""));
+ }
+
+ Iterator<String> it = resourceLinkMap.keySet().iterator();
+ while (it.hasNext()) {
+ String target = it.next();
+ if (!areDefined.contains(target)) {
+ result.add(target);
+ }
+ }
+ return result;
+
+ }
+
+ private Map<String,String> computeExpectedLinks(XMLElement contextRoot) throws MojoExecutionException {
+ Map<String,String> result=new HashMap<String,String>();
+
+ XMLElement[] link=contextRoot.getChildren("ResourceLink");
+ if (link.length==0) {
+ throw new MojoExecutionException("Can't find any resource links inside context element! Normally, they look like this: <ResourceLink name=\"ogoglio/mediaURL\" global=\"mediaURL\" type=\"java.lang.String\"/>");
+ }
+
+ for (int i=0; i<link.length;++i) {
+ if (link[i].getAttribute("name")==null) {
+ throw new MojoExecutionException("Badly formed resource link inside context element:"+link[i].toString()+". Normally, they look like this: <ResourceLink name=\"ogoglio/mediaURL\" global=\"mediaURL\" type=\"java.lang.String\"/>");
+ }
+ if (link[i].getAttribute("global")==null) {
+ throw new MojoExecutionException("Badly formed resource link inside context element:"+link[i].toString()+". Normally, they look like this: <ResourceLink name=\"ogoglio/mediaURL\" global=\"mediaURL\" type=\"java.lang.String\"/>");
+ }
+ if (link[i].getAttribute("type")==null) {
+ throw new MojoExecutionException("Badly formed resource link inside context element:"+link[i].toString()+". Normally, they look like this: <ResourceLink name=\"ogoglio/mediaURL\" global=\"mediaURL\" type=\"java.lang.String\"/>");
+ }
+ String name=(String)link[i].getAttribute("name");
+ String global=(String)link[i].getAttribute("global");
+ String type=(String)link[i].getAttribute("type");
+ if ((!("java.lang.String".equals(type))) && (!("javax.sql.DataSource".equals(type)))) {
+ throw new MojoExecutionException("We don't understand that resource link inside context element:"+link[i].toString()+". Normally, they look like this: <ResourceLink name=\"ogoglio/mediaURL\" global=\"mediaURL\" type=\"java.lang.String\"/>");
+ }
+ result.put(global, name);
+ }
+ return result;
+ }
+
+ private XMLElement getChildElementSafe(XMLElement contextRoot, String childName) throws MojoExecutionException {
+ XMLElement context = contextRoot.getChild(childName);
+ if (context == null) {
+ throw new MojoExecutionException("No \""+childName+"\" element found!");
+ }
+ return context;
+ }
+
+ private void fileOkForReading(File f) throws MojoExecutionException {
+ if ((!f.exists()) || (!f.canRead())) {
+ throw new MojoExecutionException("Can't read context file:" + contextFile);
+ }
+ }
+
+ public void verifyNotNull(File f, String var) throws MojoExecutionException {
+ if (f == null) {
+ throw new MojoExecutionException("You probably need to set your " + var + " in your settings.xml");
+ }
+ }
+}
Modified: maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/appdev/servlet/AbstractResourceServlet.java
===================================================================
--- maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/appdev/servlet/AbstractResourceServlet.java 2007-10-06 03:11:44 UTC (rev 483)
+++ maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/appdev/servlet/AbstractResourceServlet.java 2007-10-07 00:23:19 UTC (rev 484)
@@ -137,6 +137,13 @@
String[] pathElements = getPathElements((String) baseResource.getPathElement(), requestURI);
+ if (pathElements==null) {
+ Log.error("Can't understand path elements in "+getClass().getCanonicalName()+"! "+requestURI);
+ Log.error(" baseResource for errant service() is "+baseResource.getClass().getCanonicalName());
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
SiteResource requestedResource = getSiteResource(pathElements, baseResource);
if (requestedResource == null) {
Modified: maven/trunk/ogoglio-server/pom.xml
===================================================================
--- maven/trunk/ogoglio-server/pom.xml 2007-10-06 03:11:44 UTC (rev 483)
+++ maven/trunk/ogoglio-server/pom.xml 2007-10-07 00:23:19 UTC (rev 484)
@@ -204,6 +204,8 @@
</populateDir>
<reverseFile>/tmp/reverse.xml</reverseFile>
<goalPrefix>og</goalPrefix>
+ <contextFile>${basedir}/src/main/webapp/META-INF/context.xml</contextFile>
+ <tomcatDir>${tomcat.home}</tomcatDir>
</configuration>
@@ -223,6 +225,13 @@
</targetDirectory>
</configuration>
</execution>
+ <execution>
+ <phase>process-resources</phase>
+ <id>verifyXMLProperties</id>
+ <goals>
+ <goal>verifyEnvironment</goal>
+ </goals>
+ </execution>
</executions>
</plugin>
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java 2007-10-06 03:11:44 UTC (rev 483)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/SpaceServlet.java 2007-10-07 00:23:19 UTC (rev 484)
@@ -90,7 +90,7 @@
return new BaseSpaceResource();
} else {
Log.info("Turning off space servlet, this host doesn't want it.");
- return new Four04SiteResource("account");
+ return new Four04SiteResource("space");
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|