FileDocCategorySizeDatePackage
RegStoreFileParser.javaAPI DocGlassfish v2 API15439Mon May 21 13:03:52 BST 2007com.sun.enterprise.security.jmac.config

RegStoreFileParser

public final class RegStoreFileParser extends Object
Used by GFServerConfigProvider to parse the configuration file. If a file does not exist originally, the default providers are not used. A file is only created if needed, which happens if providers are registered or unregistered through the store() or delete() methods.
author
Bobby Bissett

Fields Summary
private static Logger
logger
private static final String
SEP
private static final String
CON_ENTRY
private static final String
REG_ENTRY
private static final String
REG_CTX
private static final String
LAYER
private static final String
APP_CTX
private static final String
DESCRIPTION
private static final String[]
INDENT
private File
confFile
private List
entries
Constructors Summary
RegStoreFileParser(String pathParent, String pathChild, boolean create)

    
    /*
     * Loads the configuration file from the given filename.
     * If a file is not found, then the default entries
     * stored in GFAuthConfigFactory are used. Otherwise,
     * the file is parsed to load the entries.
     *
     * The boolean argument tells whether to create the config
     * file always (true) or only if it's needed (false).
     */
          
        try {
            confFile = new File(pathParent, pathChild);
            if (confFile.exists()) {
                loadEntries();
            } else {
                if (create) {
                    synchronized (confFile) {
                        entries = GFAuthConfigFactory.getDefaultProviders();
                        writeEntries();
                    }
                } else {
                    if (logger.isLoggable(Level.FINER)) {
                        logger.log(Level.FINER, "jmac.factory_file_not_found",
                            pathParent + File.pathSeparator + pathChild);
                    }
                }
            }
        } catch (IOException ioe) {
            logWarningDefault(ioe);
        } catch (IllegalArgumentException iae) {
            logWarningDefault(iae);
        }
        
        // file not parsed
        if (entries == null) {
            entries = GFAuthConfigFactory.getDefaultProviders();
        }
    
Methods Summary
private booleancheckAndAddToList(java.lang.String className, javax.security.auth.message.config.AuthConfigFactory.RegistrationContext ctx, java.util.Map props)


        // convention is to use null for empty properties
        if (props != null && props.isEmpty()) {
            props = null;
        }
        EntryInfo newEntry = new EntryInfo(className, props, ctx);
        EntryInfo entry = getMatchingRegEntry(newEntry);
        
        // there is no matching entry, so add to list
        if (entry == null) {
            entries.add(newEntry);
            return true;
        }
        
        // if constructor entry, don't need to check reg context
        if (entry.isConstructorEntry()) {
            return false;
        }
        
        // otherwise, check reg contexts to see if there is a match
        if (entry.getRegContexts().contains(ctx)) {
            return false;
        }
        
        // no matching context in existing entry, so add to existing entry
        entry.getRegContexts().add(new RegistrationContextImpl(ctx));
        return true;
    
private booleancheckAndRemoveFromList(javax.security.auth.message.config.AuthConfigFactory.RegistrationContext target)

        boolean retValue = false;
        for (EntryInfo info : entries) {
            if (info.isConstructorEntry()) {
                continue;
            }

            Iterator<RegistrationContext> iter = 
                    info.getRegContexts().iterator();
            while (iter.hasNext()) {
                RegistrationContext ctx = iter.next();
                if (ctx.equals(target)) {
                    iter.remove();
                    retValue = true;
                }
            }
        }
        return retValue;
    
private voidclearExistingFile()

        if (confFile.exists()) {
            confFile.delete();
        }
        confFile.createNewFile();
    
voiddelete(javax.security.auth.message.config.AuthConfigFactory.RegistrationContext ctx)

        synchronized (confFile) {
            if (checkAndRemoveFromList(ctx)) {
                try {
                    writeEntries();
                } catch (IOException ioe) {
                    logWarningUpdated(ioe);
                }
            }
        }
    
private EntryInfogetMatchingRegEntry(EntryInfo target)

        for (EntryInfo info : entries) {
            if (info.equals(target)) {
                return info;
            }
        }
        return null;
    
java.util.ListgetPersistedEntries()

        return entries;
    
private voidloadEntries()

        entries = new ArrayList<EntryInfo>();
        BufferedReader reader = new BufferedReader(new FileReader(confFile));
        String line = reader.readLine();
        while (line != null) {
            String trimLine = line.trim(); // can't trim readLine() result
            if (trimLine.startsWith(CON_ENTRY)) {
                entries.add(readConEntry(reader));
            } else if (trimLine.startsWith(REG_ENTRY)) {
                entries.add(readRegEntry(reader));
            }
            line = reader.readLine();
        }
    
private voidlogWarningDefault(java.lang.Exception exception)

        if (logger.isLoggable(Level.WARNING)) {
            logger.log(Level.WARNING,
                "jmac.factory_could_not_read", exception.toString());
        }
    
private voidlogWarningUpdated(java.lang.Exception exception)

        if (logger.isLoggable(Level.WARNING)) {
            logger.log(Level.WARNING,
                "jmac.factory_could_not_persist", exception.toString());
        }
    
private EntryInforeadConEntry(java.io.BufferedReader reader)

        // entry must contain class name as next line
        String className = reader.readLine().trim();
        Map<String, String> properties = readProperties(reader);
        return new EntryInfo(className, properties);
    
private java.util.MapreadProperties(java.io.BufferedReader reader)

        
        String line = reader.readLine().trim();
        if (line.equals("}")) {
            return null;
        }
        Map<String, String> properties = new HashMap<String, String>();
        while (!line.equals("}")) {
            properties.put(line.substring(0, line.indexOf(SEP)),
                line.substring(line.indexOf(SEP) + 1, line.length()));
            line = reader.readLine().trim();
        }
        return properties;
    
private javax.security.auth.message.config.AuthConfigFactory.RegistrationContextreadRegContext(java.io.BufferedReader reader)

        
        String layer = null;
        String appCtx = null;
        String description = null;
        String line = reader.readLine().trim();
        while (!line.equals("}")) {
            String value = line.substring(line.indexOf(SEP) + 1,
                line.length());
            if (line.startsWith(LAYER)) {
                layer = value;
            } else if (line.startsWith(APP_CTX)) {
                appCtx = value;
            } else if (line.startsWith(DESCRIPTION)) {
                description = value;
            }
            line = reader.readLine().trim();
        }
        return new RegistrationContextImpl(layer, appCtx, description, true);
    
private EntryInforeadRegEntry(java.io.BufferedReader reader)

        String className = null;
        Map<String, String> properties = null;
        List<RegistrationContext> ctxs =
            new ArrayList<RegistrationContext>();
        String line = reader.readLine().trim();
        while (!line.equals("}")) {
            if (line.startsWith(CON_ENTRY)) {
                EntryInfo conEntry = readConEntry(reader);
                className = conEntry.getClassName();
                properties = conEntry.getProperties();
            } else if (line.startsWith(REG_CTX)) {
                ctxs.add(readRegContext(reader));
            }
            line = reader.readLine().trim();
        }
        return new EntryInfo(className, properties, ctxs);
    
voidstore(java.lang.String className, javax.security.auth.message.config.AuthConfigFactory.RegistrationContext ctx, java.util.Map properties)

        synchronized (confFile) {
            if (checkAndAddToList(className, ctx, properties)) {
                try {
                    writeEntries();
                } catch (IOException ioe) {
                    logWarningUpdated(ioe);
                }
            }
        }
    
private voidwriteConEntry(EntryInfo info, java.io.PrintWriter out, int i)

        out.println(INDENT[i++] + CON_ENTRY + " {");
        out.println(INDENT[i] + info.getClassName());
        Map<String, String> props = info.getProperties();
        if (props != null) {
            for (String key : props.keySet()) {
                out.println(INDENT[i] + key + SEP + props.get(key));
            }
        }
        out.println(INDENT[--i] + "}");
    
private voidwriteEntries()

        if (!confFile.canWrite() && logger.isLoggable(Level.WARNING)) {
            logger.log(Level.WARNING, "jmac.factory_cannot_write_file",
                confFile.getPath());
        }
        clearExistingFile();
        PrintWriter out = new PrintWriter(confFile);
        int indent = 0;
        for (EntryInfo info : entries) {
            if (info.isConstructorEntry()) {
                writeConEntry(info, out, indent);
            } else {
                writeRegEntry(info, out, indent);
            }
        }
        out.close();
    
private voidwriteRegEntry(EntryInfo info, java.io.PrintWriter out, int i)

        out.println(INDENT[i++] + REG_ENTRY + " {");
        if (info.getClassName() != null) {
            writeConEntry(info, out, i);
        }
        for (RegistrationContext ctx : info.getRegContexts()) {
            out.println(INDENT[i++] + REG_CTX + " {");
            if (ctx.getMessageLayer() != null) {
                out.println(INDENT[i] + LAYER + SEP + ctx.getMessageLayer());
            }
            if (ctx.getAppContext() != null) {
                out.println(INDENT[i] + APP_CTX + SEP + ctx.getAppContext());
            }
            if (ctx.getDescription() != null) {
                out.println(INDENT[i] + DESCRIPTION +
                    SEP + ctx.getDescription());
            }
            out.println(INDENT[--i] + "}");
        }
        out.println(INDENT[--i] + "}");