|
From: <ian...@us...> - 2007-09-05 03:21:29
|
Revision: 362
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=362&view=rev
Author: iansmith
Date: 2007-09-04 20:21:32 -0700 (Tue, 04 Sep 2007)
Log Message:
-----------
First cut at a new mojo for pushing templates/spaces to the server from the populate directory.
ogoglio-server is configured to use this plugin. It can be run from the command line with :
mvn og:populate
in a directory with a POM that has the configuration settings.
This lacks the ability to reverse the mapping, which one sorely needs for development. I'll do that soon.
Moved around the package name and a few other things to make more sense in an ogoglio world.
Modified Paths:
--------------
maven/trunk/dev-plugins/pom.xml
Added Paths:
-----------
maven/trunk/dev-plugins/src/main/java/com/ogoglio/
maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/
maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/PopulateMojo.java
maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/StaticVelocityMojo.java
Removed Paths:
-------------
maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/StaticVelocitySitePlugin.java
maven/trunk/dev-plugins/src/main/java/com/transmutable/
Modified: maven/trunk/dev-plugins/pom.xml
===================================================================
--- maven/trunk/dev-plugins/pom.xml 2007-09-05 03:18:04 UTC (rev 361)
+++ maven/trunk/dev-plugins/pom.xml 2007-09-05 03:21:32 UTC (rev 362)
@@ -11,9 +11,8 @@
<version>0.0.1-SNAPSHOT</version>
<relativePath>../ogoglio</relativePath>
</parent>
- <artifactId>dev-plugins</artifactId>
+ <artifactId>og-plugin</artifactId>
<packaging>maven-plugin</packaging>
-
<build>
<plugins>
<plugin>
@@ -38,6 +37,12 @@
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
+ <!-- because we are client of the server -->
+ <dependency>
+ <groupId>com.ogoglio</groupId>
+ <artifactId>ogoglio-common</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </dependency>
</dependencies>
</project>
Copied: maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin (from rev 359, maven/trunk/dev-plugins/src/main/java/com/transmutable/plugin)
Added: maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/PopulateMojo.java
===================================================================
--- maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/PopulateMojo.java (rev 0)
+++ maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/PopulateMojo.java 2007-09-05 03:21:32 UTC (rev 362)
@@ -0,0 +1,223 @@
+package com.ogoglio.plugin;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import nanoxml.XMLElement;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+
+import com.agical.rmock.core.util.StringUtils;
+import com.ogoglio.client.WebAPIAuthenticator;
+import com.ogoglio.client.WebAPIClient;
+import com.ogoglio.client.WebAPIClientWire;
+import com.ogoglio.client.WebAPIDescriptor;
+import com.ogoglio.util.StreamUtils;
+import com.ogoglio.xml.PossessionDocument;
+import com.ogoglio.xml.SpaceDocument;
+import com.ogoglio.xml.TemplateDocument;
+
+/**
+ * @goal populate
+ */
+public class PopulateMojo extends AbstractMojo {
+ /**
+ * @parameter
+ */
+ private File baseDir;
+ /**
+ * @parameter
+ */
+ private String serviceURI;
+ /**
+ * @parameter
+ */
+ private String username;
+ /**
+ * @parameter
+ */
+ private String password;
+
+ private HashMap<Long,Long> templateIdMap = new HashMap<Long,Long>();
+ private List<SpaceDocument> spaces=new ArrayList<SpaceDocument>();
+
+ private static final String TEMPLATE_PREFIX="template-";
+ private static final String SPACE_PREFIX="space-";
+
+ public void execute() throws MojoExecutionException {
+ if (baseDir==null) {
+ getLog().info("No populate baseDir given. No population done.");
+ return;
+ }
+
+ if ((!baseDir.exists()) || (!baseDir.isDirectory()) || (!baseDir.canRead())) {
+ getLog().error("Unable to access base dir:"+baseDir.getAbsolutePath());
+ return;
+ }
+
+ if (serviceURI==null) {
+ getLog().info("No populate serviceURI given. No population done.");
+ return;
+ }
+
+ try {
+ WebAPIClientWire wire=new WebAPIClientWire();
+ WebAPIDescriptor descriptor=new WebAPIDescriptor(new URI(serviceURI));
+ WebAPIAuthenticator auth=new WebAPIAuthenticator(wire,descriptor,username,password);
+ WebAPIClient client=new WebAPIClient(descriptor,auth,wire);
+ //we are now ready to upload
+
+ File[] templates = baseDir.listFiles();
+
+ for (int i=0; i<templates.length;++i) {
+ File candidate=templates[i];
+ if (candidate.getName().startsWith(TEMPLATE_PREFIX)) {
+ uploadTemplate(client,candidate);
+ } else if (candidate.getName().startsWith(SPACE_PREFIX)) {
+ readSpace(client,candidate);
+ } else {
+ if (!candidate.getName().equals(".svn")) {
+ getLog().warn("Unable to process in populate:"+candidate.getAbsolutePath());
+ }
+ }
+ }
+
+ patchSpaces(client);
+
+ } catch (URISyntaxException e) {
+ throw new MojoExecutionException("Bad URI syntax("+serviceURI+")",e);
+ } catch (IOException e) {
+ throw new MojoExecutionException("IO Exception (possibly auth?"+username+","+password+")",e);
+ }
+ }
+
+ private void patchSpaces(WebAPIClient client) throws MojoExecutionException {
+ SpaceDocument fakeSpaceDoc,realSpaceDoc;
+
+ for (int i=0; i<spaces.size();++i) {
+ fakeSpaceDoc=spaces.get(i);
+ try {
+ realSpaceDoc = client.createSpace(fakeSpaceDoc.getDisplayName());
+ TemplateDocument templates[] = fakeSpaceDoc.getTemplateDocuments();
+ for (int j=0; j<templates.length;++j) {
+ long trueId = templateIdMap.get(new Long(templates[j].getTemplateID())).longValue();
+ PossessionDocument possDoc = client.createPossession(trueId);
+ client.addPossessionToSpace(possDoc.getPossessionID(), fakeSpaceDoc.getSpaceID());
+ TemplateDocument realTemplateDoc=client.getTemplateDocument(username, trueId);
+ getLog().info("Added Template "+realTemplateDoc.getDisplayName()+" to space "+realSpaceDoc.getDisplayName());
+ }
+ //client.setSpacePublished(realSpaceDoc.getSpaceID(), fakeSpaceDoc.getSpacePublished());
+ long realSpaceID = realSpaceDoc.getSpaceID();
+ client.setSpaceSeaLevel(realSpaceID, fakeSpaceDoc.getSeaLevel());
+ client.setSpaceMaxGuests(realSpaceID, fakeSpaceDoc.getMaxGuests());
+ client.setSpaceDisplayName(realSpaceID, fakeSpaceDoc.getDisplayName());
+ client.setSpaceSeaLevel(realSpaceID, fakeSpaceDoc.getSeaLevel());
+ } catch (IOException e) {
+ throw new MojoExecutionException("IOException patching space ("+fakeSpaceDoc.getDisplayName()+":"+fakeSpaceDoc.getSpaceID()+")",e);
+ }
+ }
+
+ }
+
+ private void readSpace(WebAPIClient client, File candidate) throws MojoExecutionException{
+ String name = candidate.getName();
+ String num= name.substring(SPACE_PREFIX.length());
+ int spaceFakeId=-189;
+ try {
+ spaceFakeId = Integer.parseInt(num);
+ } catch (NumberFormatException e) {
+ getLog().error("Badly formed space name:"+name+". Ignoring.");
+ }
+ if (spaceFakeId==-189) {
+ return;
+ }
+ try {
+ FileInputStream inputStream = new FileInputStream(candidate);
+ String docContent = StreamUtils.readInput(inputStream);
+ SpaceDocument doc=new SpaceDocument(XMLElement.parseElementFromString(docContent));
+ spaces.add(doc);
+ inputStream.close();
+ } catch (IOException e) {
+ throw new MojoExecutionException("IO Error reading space",e);
+ }
+
+
+ }
+
+ private void uploadTemplate(WebAPIClient client, File candidate) throws MojoExecutionException{
+ String name = candidate.getName();
+ String num= name.substring(TEMPLATE_PREFIX.length());
+
+ int templateFakeId=-188;
+ try {
+ templateFakeId = Integer.parseInt(num);
+ } catch (NumberFormatException e) {
+ getLog().error("Badly formed template name:"+name+". Ignoring.");
+ }
+ if (templateFakeId==-188) {
+ return;
+ }
+ getLog().info("Uploading template #"+templateFakeId+" to "+serviceURI);
+ File[] objs=candidate.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ return name.endsWith(".obj");
+ }
+ });
+ if (objs.length!=1) {
+ throw new MojoExecutionException("Badly formed directory "+name+". Should have 1 obj file but has "+objs.length);
+ }
+ try {
+ String templateName=objs[0].getName();
+ InputStream objStream=new FileInputStream(objs[0]);
+ TemplateDocument doc = client.createTemplate(templateName.substring(0,templateName.length()-4));//.obj
+ client.uploadTemplateGeometryStream(username,doc.getTemplateID(),0,objStream);
+
+ templateIdMap.put(new Long(templateFakeId), new Long(doc.getTemplateID()));
+ getLog().info("Created template from "+objs[0].getName()+
+ " ["+templateFakeId+" -> "+doc.getTemplateID()+"]");
+
+ File[] notObjs=candidate.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ return !name.endsWith(".obj");
+ }
+ });
+ for (int i=0; i<notObjs.length; ++i) {
+ File supportFile = notObjs[i];
+ String supportName = supportFile.getName();
+ if (supportFile.getName().endsWith(".js")) {
+ String script = StreamUtils.readInput(new FileInputStream(supportFile));
+ client.updateTemplateScript(username, doc.getTemplateID(), script);
+ getLog().info("Uploaded script file:"+supportName);
+ } else {
+ if ((!(supportName.endsWith(".gif"))) && (!(supportName.endsWith(".jpg"))) &&
+ (!(supportName.endsWith(".png"))) && (!(supportName.endsWith(".mtl")))) {
+
+ if ((!(supportName.endsWith(".blend"))) && (!(supportName.endsWith(".3DS"))) &&
+ (!(supportName.endsWith(".txt"))) && (!(supportName.equals(".svn")))) {
+ getLog().warn("Don't know what to do with file "+supportName+". Ignoring.");
+ }
+ continue;
+ }
+ FileInputStream supportStream = new FileInputStream(supportFile);
+ client.uploadTemplateResourceStream(username, doc.getTemplateID(), supportName, supportStream);
+ getLog().info("Uploaded support file:"+supportName);
+ }
+ }
+
+ } catch (IOException e) {
+ throw new MojoExecutionException("IO Error in upload",e);
+ }
+ }
+
+}
\ No newline at end of file
Copied: maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/StaticVelocityMojo.java (from rev 359, maven/trunk/dev-plugins/src/main/java/com/transmutable/plugin/StaticVelocitySitePlugin.java)
===================================================================
--- maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/StaticVelocityMojo.java (rev 0)
+++ maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/StaticVelocityMojo.java 2007-09-05 03:21:32 UTC (rev 362)
@@ -0,0 +1,110 @@
+package com.ogoglio.plugin;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+
+/**
+ * @goal buildHtml
+ */
+public class StaticVelocityMojo extends AbstractMojo {
+
+ /**
+ * @parameter
+ */
+ private File templateDirectory;
+ /**
+ * @parameter
+ */
+ private File targetDirectory;
+
+ public void execute() throws MojoExecutionException {
+ if (templateDirectory==null) {
+ getLog().warn("No velocity templates configured!");
+ return;
+ } else if ((!templateDirectory.exists()) || (!templateDirectory.canRead())) {
+ getLog().warn("Can't find any velocity templates to compile!");
+ return;
+ }
+
+ if (targetDirectory.exists()==false) {
+ if (targetDirectory.mkdir()==false) {
+ getLog().error("Unable to create target directory:"+targetDirectory.getName());
+ throw new MojoExecutionException("Bad target directory");
+ }
+ }
+
+ try {
+ VelocityEngine engine = new VelocityEngine();
+ engine.setProperty("file.resource.loader.path", templateDirectory.getCanonicalPath());
+ engine.init();
+
+ VelocityContext velocityContext = new VelocityContext();
+
+ File[] children = templateDirectory.listFiles();
+ for (int i = 0; children != null && i < children.length; i++) {
+ if (children[i].getName().endsWith(".html")) {
+ Template template = engine.getTemplate(children[i].getName());
+ File output = new File(targetDirectory, children[i].getName());
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
+ template.merge(velocityContext, writer);
+ writer.flush();
+ writer.close();
+ } else if (children[i].getName().endsWith(".vm")) { //ignore velocity fragments
+ continue;
+ } else if(children[i].getName().equals(".svn")) { //ignore svn dir
+ continue;
+ } else {
+ copy(children[i], targetDirectory);
+ }
+ }
+ } catch (ResourceNotFoundException rnfe) {
+ getLog().error(rnfe);
+ throw new MojoExecutionException("Can't find resource",rnfe);
+ } catch (ParseErrorException pee) {
+ getLog().warn("Syntax error in template:" + pee);
+ throw new MojoExecutionException("Bad syntax",pee);
+ } catch (Exception e) {
+ throw new MojoExecutionException("Unexpected exception",e);
+ }
+
+ }
+
+ private void copy(File source, File destinationDirectory) throws IOException {
+ if(source.isDirectory()){
+ File newDir = new File(destinationDirectory, source.getName());
+ newDir.mkdir();
+ File[] children = source.listFiles();
+ for (int i = 0; i < children.length; i++) {
+ copy(children[i], newDir);
+ }
+ } else {
+ File newFile = new File(destinationDirectory, source.getName());
+ if(newFile.exists() && source.lastModified() == newFile.lastModified()){
+ return;
+ }
+ FileOutputStream output = new FileOutputStream(newFile);
+ FileInputStream input = new FileInputStream(source);
+ byte[] buff = new byte[2048];
+ int read = 0;
+ while( (read = input.read(buff)) > 0){
+ output.write(buff, 0, read);
+ }
+ output.flush();
+ output.close();
+ input.close();
+ }
+ }
+}
+
Deleted: maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/StaticVelocitySitePlugin.java
===================================================================
--- maven/trunk/dev-plugins/src/main/java/com/transmutable/plugin/StaticVelocitySitePlugin.java 2007-09-04 23:28:02 UTC (rev 359)
+++ maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/StaticVelocitySitePlugin.java 2007-09-05 03:21:32 UTC (rev 362)
@@ -1,110 +0,0 @@
-package com.transmutable.plugin;
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-
-import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.velocity.Template;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.exception.ParseErrorException;
-import org.apache.velocity.exception.ResourceNotFoundException;
-
-/**
- * @goal buildHtml
- */
-public class StaticVelocitySitePlugin extends AbstractMojo {
-
- /**
- * @parameter
- */
- private File templateDirectory;
- /**
- * @parameter
- */
- private File targetDirectory;
-
- public void execute() throws MojoExecutionException {
- if (templateDirectory==null) {
- getLog().warn("No velocity templates configured!");
- return;
- } else if ((!templateDirectory.exists()) || (!templateDirectory.canRead())) {
- getLog().warn("Can't find any velocity templates to compile!");
- return;
- }
-
- if (targetDirectory.exists()==false) {
- if (targetDirectory.mkdir()==false) {
- getLog().error("Unable to create target directory:"+targetDirectory.getName());
- throw new MojoExecutionException("Bad target directory");
- }
- }
-
- try {
- VelocityEngine engine = new VelocityEngine();
- engine.setProperty("file.resource.loader.path", templateDirectory.getCanonicalPath());
- engine.init();
-
- VelocityContext velocityContext = new VelocityContext();
-
- File[] children = templateDirectory.listFiles();
- for (int i = 0; children != null && i < children.length; i++) {
- if (children[i].getName().endsWith(".html")) {
- Template template = engine.getTemplate(children[i].getName());
- File output = new File(targetDirectory, children[i].getName());
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
- template.merge(velocityContext, writer);
- writer.flush();
- writer.close();
- } else if (children[i].getName().endsWith(".vm")) { //ignore velocity fragments
- continue;
- } else if(children[i].getName().equals(".svn")) { //ignore svn dir
- continue;
- } else {
- copy(children[i], targetDirectory);
- }
- }
- } catch (ResourceNotFoundException rnfe) {
- getLog().error(rnfe);
- throw new MojoExecutionException("Can't find resource",rnfe);
- } catch (ParseErrorException pee) {
- getLog().warn("Syntax error in template:" + pee);
- throw new MojoExecutionException("Bad syntax",pee);
- } catch (Exception e) {
- throw new MojoExecutionException("Unexpected exception",e);
- }
-
- }
-
- private void copy(File source, File destinationDirectory) throws IOException {
- if(source.isDirectory()){
- File newDir = new File(destinationDirectory, source.getName());
- newDir.mkdir();
- File[] children = source.listFiles();
- for (int i = 0; i < children.length; i++) {
- copy(children[i], newDir);
- }
- } else {
- File newFile = new File(destinationDirectory, source.getName());
- if(newFile.exists() && source.lastModified() == newFile.lastModified()){
- return;
- }
- FileOutputStream output = new FileOutputStream(newFile);
- FileInputStream input = new FileInputStream(source);
- byte[] buff = new byte[2048];
- int read = 0;
- while( (read = input.read(buff)) > 0){
- output.write(buff, 0, read);
- }
- output.flush();
- output.close();
- input.close();
- }
- }
-}
-
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|