- 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. 128
      sdk/jme3-core/src/com/jme3/gde/core/assets/AssetData.java

@ -32,31 +32,48 @@
package com.jme3.gde.core.assets;
import com.jme3.asset.AssetKey;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileAlreadyLockedException;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
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
*/
@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 String extension = "jmpdata";
private Date lastLoaded;
public AssetData(AssetDataObject file) {
this.file = file;
FileObject primaryFile = file.getPrimaryFile();
if (primaryFile != null) {
extension = primaryFile.getExt() + "data";
}
}
public AssetData(AssetDataObject file, String extension) {
@ -64,19 +81,23 @@ public class AssetData extends Properties {
this.extension = extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public AssetKey<?> getAssetKey() {
return file.getAssetKey();
}
public void setAssetKey(AssetKey key){
public void setAssetKey(AssetKey key) {
file.setAssetKeyData(key);
}
public void setModified(boolean modified){
public void setModified(boolean modified) {
file.setModified(modified);
}
public void setSaveCookie(SaveCookie cookie){
public void setSaveCookie(SaveCookie cookie) {
file.setSaveCookie(cookie);
}
@ -88,15 +109,15 @@ public class AssetData extends Properties {
file.saveAsset();
}
public void closeAsset(){
public void closeAsset() {
file.closeAsset();
}
public List<FileObject> getAssetList(){
public List<FileObject> getAssetList() {
return file.getAssetList();
}
public List<AssetKey> getAssetKeyList(){
public List<AssetKey> getAssetKeyList() {
return file.getAssetKeyList();
}
@ -104,41 +125,61 @@ public class AssetData extends Properties {
return file.getFailedList();
}
@Override
public synchronized String getProperty(String key) {
return super.getProperty(key);
public synchronized String getProperty(final String key) {
return propsMutex.readAccess(new Action<String>() {
public String run() {
readProperties();
return props.getProperty(key);
}
});
}
@Override
public synchronized String getProperty(String key, String defaultValue) {
// loadProperties();
return super.getProperty(key, defaultValue);
public synchronized String getProperty(final String key, final String defaultValue) {
return propsMutex.readAccess(new Action<String>() {
public String run() {
readProperties();
return props.getProperty(key, defaultValue);
}
});
}
@Override
public synchronized Object setProperty(String key, String value) {
Object obj= super.setProperty(key, value);
// try {
// saveProperties();
// } catch (FileAlreadyLockedException ex) {
// Exceptions.printStackTrace(ex);
// } catch (IOException ex) {
// Exceptions.printStackTrace(ex);
// }
return obj;
public synchronized String setProperty(final String key, final String value) {
return propsMutex.writeAccess(new Action<String>() {
public String run() {
String ret = (String) props.setProperty(key, value);
readProperties();
writeProperties();
return ret;
}
});
}
@Deprecated
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) {
return;
}
final Date lastMod = myFile.lastModified();
if (!lastMod.equals(lastLoaded)) {
propsMutex.writeAccess(new Runnable() {
public void run() {
props.clear();
lastLoaded = lastMod;
InputStream in = null;
try {
in = myFile.getInputStream();
in = new BufferedInputStream(myFile.getInputStream());
try {
load(in);
props.load(in);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
@ -151,9 +192,19 @@ public class AssetData extends Properties {
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;
FileLock lock = null;
try {
@ -163,19 +214,26 @@ public class AssetData extends Properties {
myFile = FileUtil.createData(pFile.getParent(), pFile.getName() + "." + extension);
}
lock = myFile.lock();
out = myFile.getOutputStream(lock);
store(out, "");
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();
}
}
}
public void setExtension(String extension) {
this.extension = extension;
});
}
}

Loading…
Cancel
Save