}
private static boolean addToSrvFile(SrvAddr srv)
{
boolean hasEntry = false;
boolean needHeader = true;
csv csvr = new csv(srvCacheFile);
try
{
File csvw = new File(cacheFilePath);
csvw.mkdirs();
csvw = new File(srvCacheFile + ".tmp");
if (csvw.exists() && csvw.lastModified() > System.currentTimeMillis() - 300000)
{ // file more recent than the last 5 minutes -> some other client app is updating ?
return false;
}
srvCacheW = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(srvCacheFile + ".tmp")));
String s, hdr;
int srv_col = -1,ctx_col = -1;
boolean done = false;
while((s=csvr.readLine()) != null)
{
if (s.length() == 0) continue;
if (s.startsWith("#") || s.startsWith(";") || s.startsWith("%")) continue;
if (!done)
{
hdr = s;
if ((srv_col=csvr.findcol(hdr,"NAME")) < 0) throw new NoSuchFieldException();
if ((ctx_col=csvr.findcol(hdr,"CONTEXT")) < 0) throw new NoSuchFieldException();
srvCacheW.write(srvHdr.trim() + crlf);
needHeader = false;
done = true;
continue;
}
if (csvr.namcmp(srv.expName,s,srv_col) == 0 &&
csvr.namcmp(srv.eqmContext,s,ctx_col) == 0)
{ // found the entry -> update the particulars
s = srv.expName.trim() + ", " + srv.fecName.trim() + ", " + srv.eqmName.trim()
+ ", " + srv.eqmContext.trim() + ", " + srv.subSystem.trim();
hasEntry = true;
}
srvCacheW.write(s + crlf);
}
if (needHeader) srvCacheW.write(srvHdr.trim() + crlf);
if (!hasEntry)
{ // it's a new one !
s = srv.expName.trim() + "," + srv.fecName.trim() + "," + srv.eqmName.trim()
+ "," + srv.eqmContext.trim() + "," + srv.subSystem.trim();
srvCacheW.write(s + crlf);
}
srvCacheW.close();
csvr.close();
csvw = new File(srvCacheFile);
csvw.delete();
File srvf = new File(srvCacheFile + ".tmp");
srvf.renameTo(new File(srvCacheFile));
}
catch (Exception e)
{ // database corrupt or not found
MsgLog.log("TSrvEntry.addToSrvFile", e.getMessage(),TErrorList.database_not_loaded,e,1);
hasEntry = false;
}
finally
{
csvr.close();
}
return hasEntry;
}