FileDocCategorySizeDatePackage
MemoryHashLoginInfoStore.javaAPI DocGlassfish v2 API12378Fri May 04 22:30:32 BST 2007com.sun.appserv.management.client.prefs

MemoryHashLoginInfoStore

public class MemoryHashLoginInfoStore extends Object implements LoginInfoStore
A {@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.
since
Appserver 9.0

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 voidcommit(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 booleanexists(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.StringgetName()

        return ( store.getAbsoluteFile().getAbsolutePath() );
    
public java.util.Collectionlist()

        final Collection<LoginInfo> logins = state.values(); // no need to access disk
        return (Collections.unmodifiableCollection(logins) );
    
private voidprotect()

        /*
             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 LoginInforead(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 voidremove(java.lang.String host, int port)

        final HostPortKey key = new HostPortKey(host, port);
        final LoginInfo gone  = state.remove(key);
        commit(key, gone);
    
public intsize()

        return ( state.size() ); // no need to access disk
    
public voidstore(LoginInfo login)

        this.store(login, false);
    
public voidstore(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();