FileDocCategorySizeDatePackage
RepositoryContext.javaAPI DocGlassfish v2 API16023Fri May 04 22:35:10 BST 2007com.sun.enterprise.repository

RepositoryContext

public class RepositoryContext extends Object implements Context
Repository Context
author
Harish Prabandham

Fields Summary
private static Logger
_logger
This environment property specifies the path of the file that stores all the contents of the repository.
public static final String
REPOSITORY_NAME
public static final String
REPOSITORY_DIR
Hashtable
myEnv
private Properties
bindings
private static final boolean
debug
static NameParser
myParser
private static final String
DEFAULT_NAMETABLE_DIR
private static final String
DEFAULT_NAME
private static final String
FILE_EXT
Constructors Summary
RepositoryContext(Hashtable environment)

    
      
        myEnv = (environment != null)
	    ? (Hashtable)(environment.clone()) 
	    : null;
        resurrectTable();
    
Methods Summary
public java.lang.ObjectaddToEnvironment(java.lang.String propName, java.lang.Object propVal)

        if (myEnv == null) {
            myEnv = new Hashtable(5, 0.75f);
	} 
	return myEnv.put(propName, propVal);
    
public voidbind(java.lang.String name, java.lang.Object obj)

        if (name.equals("")) {
            throw new InvalidNameException("Cannot bind empty name");
        }
        if (bindings.get(name) != null) {
            throw new NameAlreadyBoundException(
                    "Use rebind to override");
        }
        bindings.put(name, obj);
		store();
    
public voidbind(javax.naming.Name name, java.lang.Object obj)

        // Flat namespace; no federation; just call string version
        bind(name.toString(), obj);
    
public voidclose()

    store();
 	myEnv = null;
	bindings = null;
    
public java.lang.StringcomposeName(java.lang.String name, java.lang.String prefix)

        Name result = composeName(new CompositeName(name),
                                  new CompositeName(prefix));
        return result.toString();
    
public javax.naming.NamecomposeName(javax.naming.Name name, javax.naming.Name prefix)

        Name result = (Name)(prefix.clone());
        result.addAll(name);
        return result;
    
public javax.naming.ContextcreateSubcontext(java.lang.String name)

        throw new OperationNotSupportedException(
                "RepositoryContext does not support subcontexts");
    
public javax.naming.ContextcreateSubcontext(javax.naming.Name name)

        // Flat namespace; no federation; just call string version
        return createSubcontext(name.toString());
    
public voiddestroySubcontext(java.lang.String name)

        throw new OperationNotSupportedException(
                "RepositoryContext does not support subcontexts");
    
public voiddestroySubcontext(javax.naming.Name name)

        // Flat namespace; no federation; just call string version
        destroySubcontext(name.toString());
    
public java.util.HashtablegetEnvironment()

	if (myEnv == null) {
	    // Must return non-null
	    myEnv = new Hashtable(3, 0.75f);
	}
        return myEnv;
    
static java.lang.StringgetFilePath(java.lang.String dirName, java.lang.String filename)


        return (String)
        AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            String path = filename + FILE_EXT;
	
            if(path != null) {
                // Check if this file exists...
                String fname = FileUtil.getAbsolutePath(dirName + path);
                File f = new File(fname);
                
                if(!f.exists())
                    path = DEFAULT_NAME + FILE_EXT;
            }
            else
                path = DEFAULT_NAME + FILE_EXT;
            
            return FileUtil.getAbsolutePath(dirName + path);
        }});
    
public java.lang.StringgetNameInNamespace()

	throw new OperationNotSupportedException("Context.getNameInNamespace() not implemented");
    
public javax.naming.NameParsergetNameParser(java.lang.String name)

        return myParser;
    
public javax.naming.NameParsergetNameParser(javax.naming.Name name)

        // Flat namespace; no federation; just call string version
        return getNameParser(name.toString());
    
public static java.lang.StringgetRepositoryName(java.lang.String fname)

	String filepath = getFilePath(DEFAULT_NAMETABLE_DIR, fname);
	int fileIndex = filepath.lastIndexOf(File.separator); 
	String filename = null;
	
	if(fileIndex > 0)
	    filename = filepath.substring(fileIndex + 1);	
	else
	    filename = filepath;
	
	int extIndex = filename.lastIndexOf(FILE_EXT);
	String fwithoutext;

	if(extIndex > 0)
	    fwithoutext = filename.substring(0, extIndex);
	else
	    fwithoutext = filename;
	
	return fwithoutext;
    
private java.lang.StringgetStoragePath()

	String dirName = (String) myEnv.get(REPOSITORY_DIR);
	if (dirName == null) {
	    dirName = DEFAULT_NAMETABLE_DIR;
	}

	// System.out.println("REPOSITORY: " + dirName);
// START OF IASRI 4660742
		//_logger.log(Level.FINE,"REPOSITORY: " + dirName);
// END OF IASRI 4660742
        return getFilePath(dirName, (String) myEnv.get(REPOSITORY_NAME));
    
public javax.naming.NamingEnumerationlist(java.lang.String name)

        if (name.equals("")) {
            // listing this context
            return new RepNames(bindings.keys());
        } 

        // Perhaps 'name' names a context
        Object target = lookup(name);
        if (target instanceof Context) {
            return ((Context)target).list("");
        }
        throw new NotContextException(name + " cannot be listed");
    
public javax.naming.NamingEnumerationlist(javax.naming.Name name)

        // Flat namespace; no federation; just call string version
        return list(name.toString());
    
public javax.naming.NamingEnumerationlistBindings(java.lang.String name)

        if (name.equals("")) {
            // listing this context
            return new RepBindings(bindings.keys());
        } 

        // Perhaps 'name' names a context
        Object target = lookup(name);
        if (target instanceof Context) {
            return ((Context)target).listBindings("");
        }
        throw new NotContextException(name + " cannot be listed");
    
public javax.naming.NamingEnumerationlistBindings(javax.naming.Name name)

        // Flat namespace; no federation; just call string version
        return listBindings(name.toString());
    
public java.lang.Objectlookup(java.lang.String name)

    if (name.equals("")) {
            // Asking to look up this context itself.  Create and return
            // a new instance with its own independent environment.
            return (new RepositoryContext(myEnv));
        }
		// System.out.println("BINDINGS :" + bindings);
// START OF IASRI 4660742
		//_logger.log(Level.FINE,"BINDINGS :" + bindings);
// END OF IASRI 4660742
        Object answer = bindings.get(name);
        if (answer == null) {
            throw new NameNotFoundException(name + " not found");
        }
        return answer;
    
public java.lang.Objectlookup(javax.naming.Name name)

        // Flat namespace; no federation; just call string version
        return lookup(name.toString()); 
    
public java.lang.ObjectlookupLink(java.lang.String name)

        // This flat context does not treat links specially
        return lookup(name);
    
public java.lang.ObjectlookupLink(javax.naming.Name name)

        // Flat namespace; no federation; just call string version
        return lookupLink(name.toString());
    
private static voidprint(java.util.Hashtable ht)

        for (Enumeration en = ht.keys(); en.hasMoreElements(); ) {
            Object key = en.nextElement();
            Object value = ht.get(key);
	    if (debug)
// IASRI 4660742	      System.out.println("[" + key + ":" + key.getClass().getName() + 
// IASRI 4660742                               ", " + value + ":" + value.getClass().getName()
// IASRI 4660742                               + "]");
// START OF IASRI 4660742
		{
			if(_logger.isLoggable(Level.FINE))
				_logger.log(Level.FINE,"[" + key + ":" + key.getClass().getName() +", " + value + ":" + value.getClass().getName()+ "]");
		}
// END OF IASRI 4660742
        }
    
public voidrebind(java.lang.String name, java.lang.Object obj)

        if (name.equals("")) {
            throw new InvalidNameException("Cannot bind empty name");
        }
        bindings.put(name, obj);
		store();
    
public voidrebind(javax.naming.Name name, java.lang.Object obj)

        // Flat namespace; no federation; just call string version
        rebind(name.toString(), obj);
    
public java.lang.ObjectremoveFromEnvironment(java.lang.String propName)

        if (myEnv == null)
            return null;

	return myEnv.remove(propName);
    
public voidrename(java.lang.String oldname, java.lang.String newname)

        if (oldname.equals("") || newname.equals("")) {
            throw new InvalidNameException("Cannot rename empty name");
        }

        // Check if new name exists
        if (bindings.get(newname) != null) {
            throw new NameAlreadyBoundException(newname +
                                                " is already bound");
        }

        // Check if old name is bound
        Object oldBinding = bindings.remove(oldname);
        if (oldBinding == null) {
            throw new NameNotFoundException(oldname + " not bound");
        }

        bindings.put(newname, oldBinding);
		store();
    
public voidrename(javax.naming.Name oldname, javax.naming.Name newname)

        // Flat namespace; no federation; just call string version
        rename(oldname.toString(), newname.toString());
    
private voidresurrectTable()

        AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            bindings = new Properties();
            try {
                File f = new File(getStoragePath());
                if(debug)
// IASRI 4660742                    System.out.println("Loaded File: " + f.getAbsolutePath()); 
// START OF IASRI 4660742
		_logger.log(Level.FINE,"Loaded File: " + f.getAbsolutePath());
// END OF IASRI 4660742
                if(f.exists())
                    bindings.load(new FileInputStream(f));
            } catch (Exception e) {
// IASRI 4660742                e.printStackTrace();
// START OF IASRI 4660742
		_logger.log(Level.SEVERE,"enterprise.load_exception" ,e);
// END OF IASRI 4660742
            }
            return null;
        }});
    
private voidstore()

        try {
        if (bindings != null) {
            FileOutputStream fos = new FileOutputStream(getStoragePath());
            bindings.store(fos, "Repository resource mapping");
            fos.close();
        }
        } catch (Exception e) {
// IASRI 4660742            e.printStackTrace();
// START OF IASRI 4660742
		_logger.log(Level.SEVERE,"enterprise.store_exception" ,e);
// END OF IASRI 4660742
        }
	
public voidunbind(javax.naming.Name name)

        // Flat namespace; no federation; just call string version
        unbind(name.toString());
    
public voidunbind(java.lang.String name)

        if (name.equals("")) {
            throw new InvalidNameException("Cannot unbind empty name");
        }
        bindings.remove(name);
		store();