javaURLContext.java

00001 /*
00002  *
00003  * JOnAS: Java(TM) Open Application Server
00004  * Copyright (C) 1999 Bull S.A.
00005  * Contact: jonas-team@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  * --------------------------------------------------------------------------
00023  * $Id: javaURLContext.java,v 1.12 2003/10/09 13:53:09 durieuxp Exp $
00024  * --------------------------------------------------------------------------
00025  */
00026 
00027 package org.objectweb.jonas.naming.java;
00028 
00029 import java.util.Hashtable;
00030 
00031 import javax.naming.Context;
00032 import javax.naming.Name;
00033 import javax.naming.NameNotFoundException;
00034 import javax.naming.NameParser;
00035 import javax.naming.NamingEnumeration;
00036 import javax.naming.NamingException;
00037 
00038 import org.objectweb.jonas.common.Log;
00039 import org.objectweb.jonas.naming.EJBNameParser;
00040 import org.objectweb.jonas.naming.NamingManager;
00041 
00042 import org.objectweb.util.monolog.api.Logger;
00043 import org.objectweb.util.monolog.api.BasicLevel;
00044 
00055 public class javaURLContext implements Context {
00056     
00060     private static Logger logger = Log.getLogger(Log.JONAS_NAMING_PREFIX);
00061 
00065     private static final String URL_PREFIX = "java:";
00066 
00070     private Hashtable myEnv = null;
00071 
00075     private static NameParser myParser = new EJBNameParser();
00076     
00081     private static NamingManager naming;
00082 
00083     /*
00084      * Get unique instance
00085      */
00086     static {
00087         try {
00088             naming = NamingManager.getInstance();
00089         } catch (Exception e) {
00090             logger.log(BasicLevel.ERROR, "cannot get instance of NamingManager");
00091         }
00092     }
00093     
00099     public javaURLContext(Hashtable env) throws NamingException {
00100         myEnv = (Hashtable) env.clone ();
00101     }
00102     
00109     private String getRelativeName(String name) throws NamingException {
00110  
00111         // We suppose that all names must be prefixed as this
00112         if (!name.startsWith(URL_PREFIX)) {
00113             logger.log(BasicLevel.ERROR, "relative name! :" + name);
00114             throw new NameNotFoundException("Invalid name:" + name);
00115         }
00116         if (name.endsWith("/")) {
00117             name = name.substring(URL_PREFIX.length() + 1);
00118         } else {
00119             name = name.substring(URL_PREFIX.length());
00120         }
00121 
00122         return name;
00123     }
00124 
00131     private Name getRelativeName(Name name) throws NamingException {
00132         if (name.get(0).equals(URL_PREFIX)) {
00133             return (name.getSuffix(1));
00134         } else {
00135             logger.log(BasicLevel.ERROR, "relative name! :" + name);
00136             throw new NameNotFoundException("Invalid name:" + name);
00137         }
00138     }
00139 
00148     private Context findContext() throws NamingException {
00149         return naming.getComponentContext();
00150     }
00151 
00152 
00153     // ------------------------------------------------------------------
00154     // Context implementation
00155     // ------------------------------------------------------------------
00156 
00164     public Object lookup(Name name) throws NamingException {
00165         return findContext().lookup(getRelativeName(name));
00166     }
00167 
00175     public Object lookup(String name) throws NamingException {
00176         return findContext().lookup(getRelativeName(name));
00177     }
00178     
00194     public void bind(Name name, Object obj) throws NamingException {
00195         findContext().bind(getRelativeName(name), obj);
00196     }
00197 
00212     public void bind(String name, Object obj) throws NamingException {
00213         findContext().bind(getRelativeName(name), obj);
00214     }
00215 
00235     public void rebind(Name name, Object obj) throws NamingException {
00236         findContext().rebind(getRelativeName(name), obj);
00237     }
00238 
00251     public void rebind(String name, Object obj) throws NamingException {
00252         findContext().rebind(getRelativeName(name), obj);
00253     }
00254 
00276     public void unbind(Name name) throws NamingException {
00277         findContext().unbind(getRelativeName(name));
00278     }
00279 
00289     public void unbind(String name) throws NamingException {
00290         findContext().unbind(getRelativeName(name));
00291     }
00292 
00303     public void rename(Name oldName, Name newName) throws NamingException {
00304         findContext().rename(getRelativeName(oldName), getRelativeName(newName));
00305     }
00306 
00317     public void rename(String oldName, String newName) throws NamingException {
00318         findContext().rename(getRelativeName(oldName), getRelativeName(newName));
00319     }
00320 
00340     public NamingEnumeration list(Name name) throws NamingException {
00341         return findContext().list(getRelativeName(name));
00342     }
00343 
00356     public NamingEnumeration list(String name) throws NamingException {
00357         return findContext().list(getRelativeName(name));
00358     }
00359 
00379     public NamingEnumeration listBindings(Name name) throws NamingException {
00380         return findContext().listBindings(getRelativeName(name));
00381     }
00382 
00395     public NamingEnumeration listBindings(String name) throws NamingException {
00396         return findContext().listBindings(getRelativeName(name));
00397     }
00398 
00432     public void destroySubcontext(Name name) throws NamingException {
00433         findContext().destroySubcontext(getRelativeName(name));
00434     }
00435 
00448     public void destroySubcontext(String name) throws NamingException {
00449         findContext().destroySubcontext(getRelativeName(name));
00450     }
00451 
00472     public Context createSubcontext(Name name) throws NamingException {
00473         return findContext().createSubcontext(getRelativeName(name));
00474     }
00475 
00490     public Context createSubcontext(String name) throws NamingException {
00491         return findContext().createSubcontext(getRelativeName(name));
00492     }
00493 
00508     public Object lookupLink(Name name) throws NamingException {
00509         return findContext().lookupLink(getRelativeName(name));
00510     }
00511 
00523     public Object lookupLink(String name) throws NamingException {
00524         return findContext().lookupLink(getRelativeName(name));
00525     }
00526 
00546     public NameParser getNameParser(Name name) throws NamingException {
00547         return findContext().getNameParser(getRelativeName(name));
00548     }
00549 
00560     public NameParser getNameParser(String name) throws NamingException {
00561         return findContext().getNameParser(getRelativeName(name));
00562     }
00563 
00577     public Name composeName(Name name, Name prefix) throws NamingException {
00578         prefix = (Name) name.clone();
00579         return prefix.addAll(name);
00580     }
00581 
00593     public String composeName(String name, String prefix) throws NamingException {
00594         return prefix + "/" + name;
00595     }
00596 
00613     public Object addToEnvironment(String propName, Object propVal) throws NamingException {
00614         return findContext().addToEnvironment(propName, propVal);
00615     }
00616 
00631     public Object removeFromEnvironment(String propName) throws NamingException {
00632         return findContext().removeFromEnvironment(propName);
00633     }
00634 
00650     public Hashtable getEnvironment() throws NamingException {
00651         return findContext().getEnvironment();
00652     }
00653 
00665     public void close() throws NamingException {
00666         findContext().close();
00667     }
00668 
00678     public String getNameInNamespace() throws NamingException {
00679         return URL_PREFIX;
00680     }
00681 }

Generated on Tue Feb 15 15:05:19 2005 for JOnAS by  doxygen 1.3.9.1