|
From: <tre...@us...> - 2008-03-14 21:43:02
|
Revision: 791
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=791&view=rev
Author: trevorolio
Date: 2008-03-14 14:43:02 -0700 (Fri, 14 Mar 2008)
Log Message:
-----------
Fixed up the populate mojo so that it's smarter about updating templates and spaces which already exist instead of deleting them and building a new one from scratch.
Also, fixed a shutdown error caused due to a bad cast in CometChannelManager.
Modified Paths:
--------------
maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/PopulateMojo.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/SpaceDuplicator.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/message/server/CometChannelManager.java
Modified: maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/PopulateMojo.java
===================================================================
--- maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/PopulateMojo.java 2008-03-13 22:05:00 UTC (rev 790)
+++ maven/trunk/dev-plugins/src/main/java/com/ogoglio/plugin/PopulateMojo.java 2008-03-14 21:43:02 UTC (rev 791)
@@ -7,7 +7,6 @@
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
-import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
@@ -22,6 +21,7 @@
import org.apache.maven.plugin.MojoExecutionException;
+import com.ogoglio.client.SpaceDuplicator;
import com.ogoglio.client.WebAPIClient;
import com.ogoglio.util.StreamUtils;
import com.ogoglio.xml.DoorDocument;
@@ -29,7 +29,6 @@
import com.ogoglio.xml.SettingDocument;
import com.ogoglio.xml.SpaceDocument;
import com.ogoglio.xml.TemplateDocument;
-import com.ogoglio.xml.TemplateSupportFileDocument;
import com.ogoglio.xml.ThingDocument;
/**
@@ -37,7 +36,9 @@
*/
public class PopulateMojo extends OgServiceMojoBase {
public static final String PROPERTIES_FILE_NAME = "template.properties";
-
+
+ public static final String[] RESOURCE_FILES_EXTENSIONS = { "js", "gif", "jpg", "png", "mtl" };
+
private Map<Long, Long> templateIdMap = new HashMap<Long, Long>();
private Map<Long, SpaceDocument> localSpaces = new HashMap<Long, SpaceDocument>();
@@ -65,9 +66,9 @@
for (int i = 0; i < templates.length; ++i) {
File candidate = templates[i];
if (candidate.getName().startsWith(TEMPLATE_PREFIX)) {
- uploadTemplate(client, candidate, existingServerTemplates);
+ handleTemplate(client, candidate, existingServerTemplates);
} else if (candidate.getName().startsWith(SPACE_PREFIX)) {
- readSpace(client, candidate, i);
+ handleSpace(client, candidate, i);
} else {
if (!candidate.getName().equals(".svn")) {
getLog().warn("Unable to process in populate:" + candidate.getAbsolutePath());
@@ -103,38 +104,28 @@
}
private void patchSpaces(WebAPIClient client) throws MojoExecutionException {
- SpaceDocument fakeSpaceDoc, realSpaceDoc;
Iterator<Long> iter = localSpaces.keySet().iterator();
while (iter.hasNext()) {
try {
long localModTime = iter.next();
- fakeSpaceDoc = localSpaces.get(localModTime);
- if (serverSpaceNameToSpaceDoc.containsKey(fakeSpaceDoc.getDisplayName())) {
- realSpaceDoc = serverSpaceNameToSpaceDoc.get(fakeSpaceDoc.getDisplayName());
-
- Date fileModified = new Date(localModTime);
- Date serverModified = TemplateSupportFileDocument.fmt.parse(realSpaceDoc.getLastModifiedAsUTC());
- if (fileModified.before(serverModified)) {
- getLog().info("Not patching space document " + realSpaceDoc.getDisplayName() + ". Server copy is newer.");
- continue;
- } else {
- //need to torch the server version b/c it's too old
- client.deleteSpace(realSpaceDoc.getSpaceID());
- getLog().info("Deleted old server version of " + realSpaceDoc.getDisplayName() + " [" + realSpaceDoc.getSpaceID() + "]");
+ SpaceDocument fakeSpaceDoc = localSpaces.get(localModTime);
+ SpaceDocument realSpaceDoc = serverSpaceNameToSpaceDoc.get(fakeSpaceDoc.getDisplayName());
+ if (realSpaceDoc != null) {
+ SpaceDuplicator.emptySpace(realSpaceDoc.getSpaceID(), client, true);
+ } else {
+ realSpaceDoc = client.createSpace(fakeSpaceDoc.getDisplayName());
+ if (realSpaceDoc == null) {
+ throw new MojoExecutionException("Could not create a space for population: " + fakeSpaceDoc.getDisplayName());
}
}
- realSpaceDoc = client.createSpace(fakeSpaceDoc.getDisplayName());
- if (realSpaceDoc == null) {
- throw new MojoExecutionException("Could not create a space for population: " + fakeSpaceDoc.getDisplayName());
- }
long realSpaceID = realSpaceDoc.getSpaceID();
client.setSpaceMaxGuests(realSpaceID, fakeSpaceDoc.getMaxGuests());
client.setSpacePublished(realSpaceID, fakeSpaceDoc.isPublished());
client.setSpaceSeaLevel(realSpaceID, fakeSpaceDoc.getSeaLevel());
client.setSpaceDisplaySea(realSpaceID, fakeSpaceDoc.getDisplaySea());
client.setSpaceBackgroundColor(realSpaceID, fakeSpaceDoc.getBackgroundColor());
-
+
ThingDocument things[] = fakeSpaceDoc.getThingDocuments();
for (int j = 0; j < things.length; ++j) {
ThingDocument thing = things[j];
@@ -159,15 +150,13 @@
}
getLog().info("Patched up space " + realSpaceDoc.getDisplayName() + " [" + realSpaceDoc.getSpaceID() + "]");
- } catch (ParseException e) {
- throw new MojoExecutionException("Parse exception patching space", e);
} catch (IOException e) {
throw new MojoExecutionException("IOException patching space", e);
}
}
}
- private void readSpace(WebAPIClient client, File candidate, int fileNumber) throws MojoExecutionException {
+ private void handleSpace(WebAPIClient client, File candidate, int fileNumber) throws MojoExecutionException {
String name = candidate.getName();
String num = name.substring(SPACE_PREFIX.length());
int spaceFakeId = -189;
@@ -184,10 +173,10 @@
String docContent = StreamUtils.readInput(inputStream);
SpaceDocument doc = new SpaceDocument(XMLElement.parseElementFromString(docContent));
long timeStamp = candidate.lastModified();
- if(localSpaces.get(timeStamp) != null){
- // Rare occurrence: sometimes files checked out by SVN on MacOS X end up having exactly the same timestamp
- // Simply increment it by a few milliseconds and use the new value for the index
- timeStamp = candidate.lastModified() + fileNumber;
+ if (localSpaces.get(timeStamp) != null) {
+ // Rare occurrence: sometimes files checked out by SVN on MacOS X end up having exactly the same timestamp
+ // Simply increment it by a few milliseconds and use the new value for the index
+ timeStamp = candidate.lastModified() + fileNumber;
}
localSpaces.put(timeStamp, doc);
inputStream.close();
@@ -197,69 +186,59 @@
}
- private void uploadTemplate(WebAPIClient client, File candidate, Map<String, TemplateDocument> existing) throws MojoExecutionException {
- String name = candidate.getName();
- long templateFakeId = dirNameToTemplateNumber(candidate);
+ private void handleTemplate(WebAPIClient client, File candidate, Map<String, TemplateDocument> existing) throws MojoExecutionException {
+ try {
+ long templateFakeId = dirNameToTemplateNumber(candidate);
- File[] objs = candidate.listFiles(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return name.endsWith(".obj");
+ 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 " + candidate.getName() + ". Should have 1 obj file but has " + objs.length);
}
- });
- if (objs.length != 1) {
- throw new MojoExecutionException("Badly formed directory " + name + ". Should have 1 obj file but has " + objs.length);
- }
- String templateVisibleName = fileToTemplateName(objs[0]);
- if (!checkFileTemplateIsNewer(client, candidate, existing, templateVisibleName)) {
- getLog().info("Not uploading template " + templateVisibleName + ". Server copy is newer.");
- templateIdMap.put(templateFakeId, existing.get(templateVisibleName).getTemplateID());
- return;
- } else {
- getLog().info("Uploading template #" + templateFakeId + " to " + serviceURI);
- }
+ String templateVisibleName = fileToTemplateName(objs[0]);
+ long templateRealID = checkForExistingTemplate(client, existing, templateVisibleName);
+ TemplateDocument doc = null;
+ if (templateRealID == -1) {
+ doc = fileNameToTemplateDocument(client, objs[0]);
+ getLog().info("Uploading new template with fakeID [" + templateFakeId + "] (" + templateVisibleName + ") to " + serviceURI);
+ } else {
+ doc = client.getTemplateDocument(client.getAuthenticator().getUsername(), templateRealID);
+ getLog().info("Updating existing template with fakeID [" + templateFakeId + "] (" + templateVisibleName + ") to " + serviceURI);
+ //TODO delete the files we no longer need from the existing template
+ }
- try {
- TemplateDocument doc = fileNameToTemplateDocument(client, objs[0]);
-
+ templateIdMap.put(templateFakeId, doc.getTemplateID());
+ Properties templateProperties = new Properties();
File propertiesFile = new File(candidate, PROPERTIES_FILE_NAME);
if (propertiesFile.exists() && propertiesFile.canRead()) {
- Properties templateProperties = new Properties();
templateProperties.load(new FileInputStream(propertiesFile));
- if (templateProperties.size() > 0) {
- updateTemplateDoc(client, doc, templateProperties);
- }
}
+ updateTemplateDoc(client, doc, templateProperties);
InputStream objStream = new FileInputStream(objs[0]);
client.uploadTemplateGeometryStream(username, doc.getTemplateID(), 0, objStream);
- templateIdMap.put(templateFakeId, doc.getTemplateID());
- getLog().info("Created template from " + objs[0].getName() + " [" + templateFakeId + " -> " + doc.getTemplateID() + "]");
-
- File[] notObjs = candidate.listFiles(new FilenameFilter() {
+ File[] resourceFiles = candidate.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
- return !name.endsWith(".obj");
+ for (int i = 0; i < RESOURCE_FILES_EXTENSIONS.length; i++) {
+ if (name != null && name.endsWith("." + RESOURCE_FILES_EXTENSIONS[i])) {
+ return true;
+ }
+ }
+ return false;
}
});
- 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));
+ for (int i = 0; i < resourceFiles.length; ++i) {
+ if (resourceFiles[i].getName().endsWith(".js")) {
+ String script = StreamUtils.readInput(new FileInputStream(resourceFiles[i]));
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"))) && !supportName.equals(PROPERTIES_FILE_NAME)) {
- 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);
+ FileInputStream supportStream = new FileInputStream(resourceFiles[i]);
+ client.uploadTemplateResourceStream(username, doc.getTemplateID(), resourceFiles[i].getName(), supportStream);
}
}
@@ -269,151 +248,54 @@
}
private void updateTemplateDoc(WebAPIClient client, TemplateDocument doc, Properties templateProperties) throws IOException {
- String[] keys = (String[])templateProperties.keySet().toArray(new String[0]);
- double rotX = 0, rotY = 0, rotZ = 0;
-
- for (int i = 0; i < keys.length; i++) {
- if("seat".equals(keys[i])){
- doc.setSeat("true".equals(templateProperties.get(keys[i])));
- } else if("seatX".equals(keys[i])){
- doc.setSeatPosition(Double.parseDouble((String)templateProperties.get(keys[i])), doc.getSeatPosition().y, doc.getSeatPosition().z);
- } else if("seatY".equals(keys[i])){
- doc.setSeatPosition(doc.getSeatPosition().x, Double.parseDouble((String)templateProperties.get(keys[i])), doc.getSeatPosition().z);
- } else if("seatZ".equals(keys[i])){
- doc.setSeatPosition(doc.getSeatPosition().x, doc.getSeatPosition().y, Double.parseDouble((String)templateProperties.get(keys[i])));
- } else if("seatRotX".equals(keys[i])){
- rotX = Double.parseDouble(templateProperties.getProperty(keys[i]));
- } else if("seatRotY".equals(keys[i])){
- rotY = Double.parseDouble(templateProperties.getProperty(keys[i]));
- } else if("seatRotZ".equals(keys[i])){
- rotZ = Double.parseDouble(templateProperties.getProperty(keys[i]));
- } else if("attachment".equals(keys[i])){
- doc.setAttachment("true".equals(templateProperties.get(keys[i])));
- } else {
- System.err.println("Unknown properties in template.properties: " + keys[i]);
- }
- }
-
- if(rotX != 0 || rotY != 0 || rotZ != 0){
- Transform3D transform = new Transform3D();
- transform.setEuler(new Vector3d(rotX, rotY, rotZ));
- Quat4d quat = new Quat4d();
- transform.get(quat);
- doc.setSeatRotation(quat);
- }
+ if (templateProperties == null) {
+ doc.setAttachment(false);
+ doc.setSeat(false);
+ doc.setSeatPosition(0, 0, 0);
+ doc.setSeatRotation(0, 0, 0, 0);
+ } else {
+ String[] keys = (String[]) templateProperties.keySet().toArray(new String[0]);
+ double rotX = 0, rotY = 0, rotZ = 0;
- getLog().info("Updating template " + doc.getDisplayName() + " with properties from template.properties");
- client.updateTemplate(doc);
- }
-
- private boolean checkFileTemplateIsNewer(WebAPIClient client, File candidate, Map<String, TemplateDocument> existing, String displayName) throws MojoExecutionException {
- //is it even on server?
- if (!existing.containsKey(displayName)) {
- return true;
- }
-
- TemplateDocument serverCopy = existing.get(displayName);
-
- //first check support files
- File[] notObjs = candidate.listFiles(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return !name.endsWith(".obj");
+ for (int i = 0; i < keys.length; i++) {
+ if ("seat".equals(keys[i])) {
+ doc.setSeat("true".equals(templateProperties.get(keys[i])));
+ } else if ("seatX".equals(keys[i])) {
+ doc.setSeatPosition(Double.parseDouble((String) templateProperties.get(keys[i])), doc.getSeatPosition().y, doc.getSeatPosition().z);
+ } else if ("seatY".equals(keys[i])) {
+ doc.setSeatPosition(doc.getSeatPosition().x, Double.parseDouble((String) templateProperties.get(keys[i])), doc.getSeatPosition().z);
+ } else if ("seatZ".equals(keys[i])) {
+ doc.setSeatPosition(doc.getSeatPosition().x, doc.getSeatPosition().y, Double.parseDouble((String) templateProperties.get(keys[i])));
+ } else if ("seatRotX".equals(keys[i])) {
+ rotX = Double.parseDouble(templateProperties.getProperty(keys[i]));
+ } else if ("seatRotY".equals(keys[i])) {
+ rotY = Double.parseDouble(templateProperties.getProperty(keys[i]));
+ } else if ("seatRotZ".equals(keys[i])) {
+ rotZ = Double.parseDouble(templateProperties.getProperty(keys[i]));
+ } else if ("attachment".equals(keys[i])) {
+ doc.setAttachment("true".equals(templateProperties.get(keys[i])));
+ } else {
+ System.err.println("Unknown properties in template.properties: " + keys[i]);
+ }
}
- });
- //walk support files
- for (int i = 0; i < notObjs.length; ++i) {
- File supportFile = notObjs[i];
- String supportName = supportFile.getName();
- String modTime = null;
- if (supportName.endsWith(".js")) {
- if (!serverCopy.hasScriptFile()) {
- getLog().info("You have added a javascript file since the last server update.");
- return true;
- }
- modTime = serverCopy.getScriptModifiedTime();
- } else if ((!(supportName.endsWith(".gif"))) && (!(supportName.endsWith(".jpg"))) && (!(supportName.endsWith(".png"))) && (!(supportName.endsWith(".mtl")))) {
- continue;
- } else {
- modTime = serverCopy.getSupportFileModifiedTime(supportName);
+ if (rotX != 0 || rotY != 0 || rotZ != 0) {
+ Transform3D transform = new Transform3D();
+ transform.setEuler(new Vector3d(rotX, rotY, rotZ));
+ Quat4d quat = new Quat4d();
+ transform.get(quat);
+ doc.setSeatRotation(quat);
}
- try {
- //if there is no modtime, then the file doesn't exist on the server
- if (modTime == null) {
- getLog().info("Added support file " + supportName + " to template since server copy created");
- client.deleteTemplate(serverCopy.getTemplateID());
- return true;
- }
- //modtime is now set to what the server thinks the modtime is
- Date serverTime = fmt.parse(modTime);
- if (serverTime.before(new Date(supportFile.lastModified()))) {
- getLog().info("File " + supportFile + " has been modified since last upload to server...");
- client.deleteTemplate(serverCopy.getTemplateID());
- return true; //we can bail out now
- }
- } catch (IOException e) {
- throw new MojoExecutionException("unable to delete server copy of template " + serverCopy.getDisplayName(), e);
- } catch (ParseException e) {
- throw new MojoExecutionException("unable to comprehend modified time", e);
- }
}
+ client.updateTemplate(doc);
+ }
- //at this point we've checked the files that exist on the client side, but we might
- //have deleted one
- boolean destroyServerTemplate = false;
- //special case for js
- if (serverCopy.hasScriptFile()) {
- File[] jsFiles = candidate.listFiles(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return name.endsWith(".js");
- }
- });
- if (jsFiles.length > 1) {
- throw new MojoExecutionException("Only one script file is allowed per template (" + candidate.getPath() + ")");
- }
- if (jsFiles.length == 0) {
- getLog().info("Javascript file removed since last server update.");
- destroyServerTemplate = true;
- }
+ private long checkForExistingTemplate(WebAPIClient client, Map<String, TemplateDocument> existing, String displayName) throws MojoExecutionException {
+ TemplateDocument doc = existing.get(displayName);
+ if (doc != null) {
+ return doc.getTemplateID();
}
- //now support files
- String name[] = serverCopy.getAllSupportFileNames();
- for (int i = 0; i < name.length; ++i) {
- File f = new File(candidate, name[i]);
- if (!f.exists()) {
- getLog().info("You have removed the file " + name[i] + " from the template.");
- destroyServerTemplate = true;
- break;
- }
- }
- //check the obj file
- File[] objs = candidate.listFiles(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return name.endsWith(".obj");
- }
- });
- if (objs.length != 1) {
- throw new MojoExecutionException("You should have exactly one object (geometry) file per template directory (was " + objs.length + ")");
- }
- try {
- Date fileMod = new Date(objs[0].lastModified());
- Date onServerMod = fmt.parse(serverCopy.getGeometryModifiedTime(0));
- if (onServerMod.before(fileMod)) {
- getLog().info("Local geometry file is newer than server copy.");
- destroyServerTemplate = true;
- }
- //implement destruction
- if (destroyServerTemplate) {
- client.deleteTemplate(serverCopy.getTemplateID());
- return true;
- } else {
- return false;
- }
- } catch (IOException e) {
- throw new MojoExecutionException("unable to delete server copy of template " + serverCopy.getDisplayName(), e);
- } catch (ParseException e) {
- throw new MojoExecutionException("unable to comprehend modified time", e);
- }
+ return -1;
}
}
\ No newline at end of file
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/SpaceDuplicator.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/SpaceDuplicator.java 2008-03-13 22:05:00 UTC (rev 790)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/SpaceDuplicator.java 2008-03-14 21:43:02 UTC (rev 791)
@@ -104,9 +104,17 @@
}
public static void emptySpace(long spaceID, WebAPIClient client) throws IOException {
+ emptySpace(spaceID, client, false);
+ }
+
+ public static void emptySpace(long spaceID, WebAPIClient client, boolean deleteRemovedPossessions) throws IOException {
ThingDocument[] thingDocs = client.getThingDocuments(spaceID);
for (int i = 0; i < thingDocs.length; i++) {
+ long possessionID = thingDocs[i].getPossessionID();
client.removePossessionFromSpace(thingDocs[i].getPossessionID());
+ if(deleteRemovedPossessions){
+ client.deletePossession(possessionID);
+ }
}
DoorDocument[] doorDocs = client.getDoorDocuments(spaceID);
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/message/server/CometChannelManager.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/message/server/CometChannelManager.java 2008-03-13 22:05:00 UTC (rev 790)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/message/server/CometChannelManager.java 2008-03-14 21:43:02 UTC (rev 791)
@@ -93,7 +93,7 @@
public void shutdown() {
Iterator iter = sessionToChannel.keySet().iterator();
while (iter.hasNext()) {
- String key = (String) iter.next();
+ CometEvent key = (CometEvent)iter.next();
TCPChannel channel = (TCPChannel) sessionToChannel.get(key);
channel.cleanup();
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|