- make AssetData persist itself automatically
- generalize AssetData prop file suffix creation

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10226 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
nor..67 12 years ago
parent 361e45d04e
commit b0fd69c6da
  1. 116
      sdk/jme3-core/src/com/jme3/gde/core/assets/AssetData.java

@ -32,31 +32,48 @@
package com.jme3.gde.core.assets; package com.jme3.gde.core.assets;
import com.jme3.asset.AssetKey; import com.jme3.asset.AssetKey;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.cookies.SaveCookie; import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileAlreadyLockedException; import org.openide.filesystems.FileAlreadyLockedException;
import org.openide.filesystems.FileLock; import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject; import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil; import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions; import org.openide.util.Exceptions;
import org.openide.util.Mutex;
import org.openide.util.Mutex.Action;
/** /**
* Global object to access actual jME3 data within an AssetDataObject, available
* through the Lookup of any AssetDataObject. AssetDataObjects that wish to use
* *
* @author normenhansen * @author normenhansen
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class AssetData extends Properties { public class AssetData {
private static final Logger logger = Logger.getLogger(AssetData.class.getName());
private final Mutex propsMutex = new Mutex();
private final Properties props = new Properties();
private AssetDataObject file; private AssetDataObject file;
private String extension = "jmpdata"; private String extension = "jmpdata";
private Date lastLoaded;
public AssetData(AssetDataObject file) { public AssetData(AssetDataObject file) {
this.file = file; this.file = file;
FileObject primaryFile = file.getPrimaryFile();
if (primaryFile != null) {
extension = primaryFile.getExt() + "data";
}
} }
public AssetData(AssetDataObject file, String extension) { public AssetData(AssetDataObject file, String extension) {
@ -64,6 +81,10 @@ public class AssetData extends Properties {
this.extension = extension; this.extension = extension;
} }
public void setExtension(String extension) {
this.extension = extension;
}
public AssetKey<?> getAssetKey() { public AssetKey<?> getAssetKey() {
return file.getAssetKey(); return file.getAssetKey();
} }
@ -104,41 +125,61 @@ public class AssetData extends Properties {
return file.getFailedList(); return file.getFailedList();
} }
@Override public synchronized String getProperty(final String key) {
public synchronized String getProperty(String key) { return propsMutex.readAccess(new Action<String>() {
return super.getProperty(key); public String run() {
readProperties();
return props.getProperty(key);
}
});
} }
@Override public synchronized String getProperty(final String key, final String defaultValue) {
public synchronized String getProperty(String key, String defaultValue) { return propsMutex.readAccess(new Action<String>() {
// loadProperties(); public String run() {
return super.getProperty(key, defaultValue); readProperties();
return props.getProperty(key, defaultValue);
}
});
} }
@Override public synchronized String setProperty(final String key, final String value) {
public synchronized Object setProperty(String key, String value) { return propsMutex.writeAccess(new Action<String>() {
Object obj= super.setProperty(key, value); public String run() {
// try { String ret = (String) props.setProperty(key, value);
// saveProperties(); readProperties();
// } catch (FileAlreadyLockedException ex) { writeProperties();
// Exceptions.printStackTrace(ex); return ret;
// } catch (IOException ex) { }
// Exceptions.printStackTrace(ex); });
// }
return obj;
} }
@Deprecated
public void loadProperties() { public void loadProperties() {
clear(); }
FileObject myFile = FileUtil.findBrother(file.getPrimaryFile(), extension);
@Deprecated
public void saveProperties() throws FileAlreadyLockedException, IOException {
}
private void readProperties() {
propsMutex.readAccess(new Runnable() {
public void run() {
final FileObject myFile = FileUtil.findBrother(file.getPrimaryFile(), extension);
if (myFile == null) { if (myFile == null) {
return; return;
} }
final Date lastMod = myFile.lastModified();
if (!lastMod.equals(lastLoaded)) {
propsMutex.writeAccess(new Runnable() {
public void run() {
props.clear();
lastLoaded = lastMod;
InputStream in = null; InputStream in = null;
try { try {
in = myFile.getInputStream(); in = new BufferedInputStream(myFile.getInputStream());
try { try {
load(in); props.load(in);
} catch (IOException ex) { } catch (IOException ex) {
Exceptions.printStackTrace(ex); Exceptions.printStackTrace(ex);
} }
@ -151,9 +192,19 @@ public class AssetData extends Properties {
Exceptions.printStackTrace(ex); Exceptions.printStackTrace(ex);
} }
} }
logger.log(Level.INFO, "Read AssetData properties for {0}", file);
}
});
}
}
});
} }
public void saveProperties() throws FileAlreadyLockedException, IOException { private void writeProperties() {
//writeAccess because we write lastMod date, not because we write to the file
//the mutex protects the properties object, not the file
propsMutex.writeAccess(new Runnable() {
public void run() {
OutputStream out = null; OutputStream out = null;
FileLock lock = null; FileLock lock = null;
try { try {
@ -163,19 +214,26 @@ public class AssetData extends Properties {
myFile = FileUtil.createData(pFile.getParent(), pFile.getName() + "." + extension); myFile = FileUtil.createData(pFile.getParent(), pFile.getName() + "." + extension);
} }
lock = myFile.lock(); lock = myFile.lock();
out = myFile.getOutputStream(lock); out = new BufferedOutputStream(myFile.getOutputStream(lock));
store(out, ""); props.store(out, "");
out.flush();
lastLoaded = myFile.lastModified();
logger.log(Level.INFO, "Written AssetData properties for {0}", file);
} catch (IOException e) {
Exceptions.printStackTrace(e);
} finally { } finally {
if (out != null) { if (out != null) {
try {
out.close(); out.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
} }
if (lock != null) { if (lock != null) {
lock.releaseLock(); lock.releaseLock();
} }
} }
} }
});
public void setExtension(String extension) {
this.extension = extension;
} }
} }

Loading…
Cancel
Save