MemoryHashLoginInfoStorepublic class MemoryHashLoginInfoStore extends Object implements LoginInfoStoreA {@link LoginInfoStore} that reads the information from the default file ".asadminpass"
and stores it as a map in the memory. It is not guaranteed that the concurrent
modifications will yield consistent results. This class is not thread safe. The
serial access has to be ensured by the callers. |
Fields Summary |
---|
public static final String | DEFAULT_STORE_NAME | private static final sun.misc.BASE64Encoder | encoder | private static final sun.misc.BASE64Decoder | decoder | private Map | state | private final File | store |
Constructors Summary |
---|
public MemoryHashLoginInfoStore()Creates a new instance of MemoryHashLoginInfoStore. A side effect of calling
this constructor is that if the default store does not exist, it will be created.
This does not pose any harm or surprises.
BufferedReader br = null;
BufferedWriter bw = null;
try {
final File dir = new File (System.getProperty("user.home"));
store = new File(dir, DEFAULT_STORE_NAME);
if (!store.exists()) {
store.createNewFile();
bw = new BufferedWriter(new FileWriter(store));
FileMapTransform.writePreamble(bw);
state = new HashMap<HostPortKey, LoginInfo> ();
}
else {
br = new BufferedReader(new FileReader(store));
state = FileMapTransform.readAll(br);
}
}
catch(final Exception e) {
throw new StoreException(e);
}
finally {
try {
if (br != null)
br.close();
}
catch(final Exception ee) {}; //ignore
try {
if (bw != null)
bw.close();
}
catch(final Exception ee) {}; //ignore
}
|
Methods Summary |
---|
private void | commit(com.sun.appserv.management.client.prefs.MemoryHashLoginInfoStore$HostPortKey key, LoginInfo old)
BufferedWriter writer = null;
try {
//System.out.println("before commit");
writer = new BufferedWriter(new FileWriter(store));
FileMapTransform.writeAll(state.values(), writer);
}
catch(final Exception e) {
state.put(key, old); //try to roll back, first memory
try { // then disk, if the old value is not null
if (old != null) {
writer = new BufferedWriter(new FileWriter(store));
FileMapTransform.writeAll(state.values(), writer);
}
}
catch(final Exception ae) {
throw new RuntimeException("catastrophe, can't write it to file");
}//ignore, can't do much
}
finally {
try {
writer.close();
} catch(final Exception ee) {} //ignore
}
| public boolean | exists(java.lang.String host, int port)
final HostPortKey key = new HostPortKey(host, port);
final boolean exists = state.containsKey(key); //no need to access disk
return ( exists );
| public java.lang.String | getName()
return ( store.getAbsoluteFile().getAbsolutePath() );
| public java.util.Collection | list()
final Collection<LoginInfo> logins = state.values(); // no need to access disk
return (Collections.unmodifiableCollection(logins) );
| private void | protect()
/*
note: if this is Windows we still try 'chmod' -- they may have MKS or
some other UNIXy package for Windows.
cacls is too dangerous to use because it requires a "Y" to be written to
stdin of the cacls process. If cacls doesn't exist or if they are using
a non-NTFS file system we would hang here forever.
*/
try
{
if(store == null || !store.exists())
return;
ProcessBuilder pb = new ProcessBuilder("chmod", "0600", store.getAbsolutePath());
pb.start();
}
catch(Exception e)
{
// we tried...
}
| public LoginInfo | read(java.lang.String host, int port)
final HostPortKey key = new HostPortKey(host, port);
final LoginInfo login = state.get(key); //no need to access disk
return ( login );
| public void | remove(java.lang.String host, int port)
final HostPortKey key = new HostPortKey(host, port);
final LoginInfo gone = state.remove(key);
commit(key, gone);
| public int | size()
return ( state.size() ); // no need to access disk
| public void | store(LoginInfo login)
this.store(login, false);
| public void | store(LoginInfo login, boolean overwrite)
if (login == null)
throw new IllegalArgumentException("null_arg");
final String host = login.getHost();
final int port = login.getPort();
if (!overwrite && this.exists(host, port)) {
throw new StoreException("Login exists for host: " + host + " port: " + port);
}
final HostPortKey key = new HostPortKey(host, port);
final LoginInfo old = state.get(key);
state.put(key, login);
//System.out.println("committing: " + login);
commit(key, old);
protect();
|
|