- 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. 206
      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,19 +81,23 @@ 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();
} }
public void setAssetKey(AssetKey key){ public void setAssetKey(AssetKey key) {
file.setAssetKeyData(key); file.setAssetKeyData(key);
} }
public void setModified(boolean modified){ public void setModified(boolean modified) {
file.setModified(modified); file.setModified(modified);
} }
public void setSaveCookie(SaveCookie cookie){ public void setSaveCookie(SaveCookie cookie) {
file.setSaveCookie(cookie); file.setSaveCookie(cookie);
} }
@ -87,95 +108,132 @@ public class AssetData extends Properties {
public void saveAsset() throws IOException { public void saveAsset() throws IOException {
file.saveAsset(); file.saveAsset();
} }
public void closeAsset(){ public void closeAsset() {
file.closeAsset(); file.closeAsset();
} }
public List<FileObject> getAssetList(){ public List<FileObject> getAssetList() {
return file.getAssetList(); return file.getAssetList();
} }
public List<AssetKey> getAssetKeyList(){ public List<AssetKey> getAssetKeyList() {
return file.getAssetKeyList(); return file.getAssetKeyList();
} }
public List<AssetKey> getFailedList() { public List<AssetKey> getFailedList() {
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);
if (myFile == null) {
return;
}
InputStream in = null;
try {
in = myFile.getInputStream();
try {
load(in);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
} catch (FileNotFoundException ex) {
Exceptions.printStackTrace(ex);
} finally {
try {
in.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
} }
@Deprecated
public void saveProperties() throws FileAlreadyLockedException, IOException { public void saveProperties() throws FileAlreadyLockedException, IOException {
OutputStream out = null; }
FileLock lock = null;
try { private void readProperties() {
FileObject pFile = file.getPrimaryFile(); propsMutex.readAccess(new Runnable() {
FileObject myFile = FileUtil.findBrother(pFile, extension); public void run() {
if (myFile == null) { final FileObject myFile = FileUtil.findBrother(file.getPrimaryFile(), extension);
myFile = FileUtil.createData(pFile.getParent(), pFile.getName() + "." + extension); if (myFile == null) {
} return;
lock = myFile.lock(); }
out = myFile.getOutputStream(lock); final Date lastMod = myFile.lastModified();
store(out, ""); if (!lastMod.equals(lastLoaded)) {
} finally { propsMutex.writeAccess(new Runnable() {
if (out != null) { public void run() {
out.close(); props.clear();
} lastLoaded = lastMod;
if (lock != null) { InputStream in = null;
lock.releaseLock(); try {
in = new BufferedInputStream(myFile.getInputStream());
try {
props.load(in);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
} catch (FileNotFoundException ex) {
Exceptions.printStackTrace(ex);
} finally {
try {
in.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
logger.log(Level.INFO, "Read AssetData properties for {0}", file);
}
});
}
} }
} });
} }
public void setExtension(String extension) { private void writeProperties() {
this.extension = extension; //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;
FileLock lock = null;
try {
FileObject pFile = file.getPrimaryFile();
FileObject myFile = FileUtil.findBrother(pFile, extension);
if (myFile == null) {
myFile = FileUtil.createData(pFile.getParent(), pFile.getName() + "." + extension);
}
lock = myFile.lock();
out = new BufferedOutputStream(myFile.getOutputStream(lock));
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 {
if (out != null) {
try {
out.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
if (lock != null) {
lock.releaseLock();
}
}
}
});
} }
} }

Loading…
Cancel
Save