Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1009 → Rev 1010

/hostadmiral/trunk/src/ak/hostadmiral/core/listener/file/InterfaceFile.java
0,0 → 1,68
package ak.hostadmiral.core.listener.file;
 
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.FileWriter;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.UserModifiedListener;
 
import org.apache.log4j.Logger;
 
public class InterfaceFile
{
private static final Logger logger = Logger.getLogger(InterfaceFile.class);
 
private static String fileName;
protected static Object lock = new Object();
 
public static final String PROTOCOL_NAME = "HostAdmiral_FileInterface";
public static final String PROTOCOL_VERSION = "0.1";
 
public static String getFileName()
{
return fileName;
}
 
public static void setFileName(String fileName_)
{
fileName = fileName_;
}
 
protected static String escape(String s)
{
// FIXME: any other problem characters? optimize it?
s = s.replaceAll("\0", "\\\\0");
s = s.replaceAll("\\\\", "\\\\\\\\");
s = s.replaceAll("\t", "\\\\t");
s = s.replaceAll("\n", "\\\\n");
return s;
}
 
protected static void send(String message)
throws ModelException
{
synchronized(lock) {
try {
Writer out = new BufferedWriter(new FileWriter(fileName));
if(PROTOCOL_NAME != null) {
out.write(PROTOCOL_NAME);
out.write(" ");
}
if(PROTOCOL_VERSION != null) {
out.write(PROTOCOL_VERSION);
out.write("\n");
}
out.write(message);
out.write("\n\n");
out.close();
}
catch(Exception ex) {
logger.error("Cannot save message to file", ex);
throw new ModelException("Cannot save message to file:" + ex.getMessage());
// FIMXE: or just throw "internal server error" message?
}
}
}
}