36 * This abstract class is a generic URL context that accepts as the
37 * name argument either a string URL or a Name whose first component
38 * is a URL. It resolves the URL to a target context and then continues
39 * the operation using the remaining name in the target context as if
40 * the first component names a junction.
41 *
42 * A subclass must define getRootURLContext()
43 * to process the URL into head/tail pieces. If it wants to control how
44 * URL strings are parsed and compared for the rename() operation, then
45 * it should override getNonRootURLSuffixes() and urlEquals().
46 *
47 * @author Scott Seligman
48 * @author Rosanna Lee
49 */
50 abstract public class GenericURLContext implements Context {
51 protected Hashtable<String, Object> myEnv = null;
52
53 @SuppressWarnings("unchecked") // Expect Hashtable<String, Object>
54 public GenericURLContext(Hashtable<?,?> env) {
55 // context that is not tied to any specific URL
56 myEnv = (Hashtable<String, Object>)env; // copied on write
57 }
58
59 public void close() throws NamingException {
60 myEnv = null;
61 }
62
63 public String getNameInNamespace() throws NamingException {
64 return ""; // %%% check this out: A URL context's name is ""
65 }
66
67 /**
68 * Resolves 'name' into a target context with remaining name.
69 * For example, with a JNDI URL "jndi://dnsname/rest_name",
70 * this method resolves "jndi://dnsname/" to a target context,
71 * and returns the target context with "rest_name".
72 * The definition of "root URL" and how much of the URL to
73 * consume is implementation specific.
74 * If rename() is supported for a particular URL scheme,
75 * getRootURLContext(), getURLPrefix(), and getURLSuffix()
76 * must be in sync wrt how URLs are parsed and returned.
477 return name;
478 } else if (name.equals("")) {
479 return prefix;
480 } else {
481 return (prefix + "/" + name);
482 }
483 }
484
485 public Name composeName(Name name, Name prefix) throws NamingException {
486 Name result = (Name)prefix.clone();
487 result.addAll(name);
488 return result;
489 }
490
491 @SuppressWarnings("unchecked") // clone()
492 public Object removeFromEnvironment(String propName)
493 throws NamingException {
494 if (myEnv == null) {
495 return null;
496 }
497 myEnv = (Hashtable<String, Object>)myEnv.clone();
498 return myEnv.remove(propName);
499 }
500
501 @SuppressWarnings("unchecked") // clone()
502 public Object addToEnvironment(String propName, Object propVal)
503 throws NamingException {
504 myEnv = (myEnv == null)
505 ? new Hashtable<String, Object>(11, 0.75f)
506 : (Hashtable<String, Object>)myEnv.clone();
507 return myEnv.put(propName, propVal);
508 }
509
510 @SuppressWarnings("unchecked") // clone()
511 public Hashtable<String, Object> getEnvironment() throws NamingException {
512 if (myEnv == null) {
513 return new Hashtable<>(5, 0.75f);
514 } else {
515 return (Hashtable<String, Object>)myEnv.clone();
516 }
517 }
518
519 /*
520 // To test, declare getURLPrefix and getURLSuffix static.
521
522 public static void main(String[] args) throws Exception {
523 String[] tests = {"file://host:port",
524 "file:///rest/of/name",
525 "file://host:port/rest/of/name",
526 "file:/rest/of/name",
|
36 * This abstract class is a generic URL context that accepts as the
37 * name argument either a string URL or a Name whose first component
38 * is a URL. It resolves the URL to a target context and then continues
39 * the operation using the remaining name in the target context as if
40 * the first component names a junction.
41 *
42 * A subclass must define getRootURLContext()
43 * to process the URL into head/tail pieces. If it wants to control how
44 * URL strings are parsed and compared for the rename() operation, then
45 * it should override getNonRootURLSuffixes() and urlEquals().
46 *
47 * @author Scott Seligman
48 * @author Rosanna Lee
49 */
50 abstract public class GenericURLContext implements Context {
51 protected Hashtable<String, Object> myEnv = null;
52
53 @SuppressWarnings("unchecked") // Expect Hashtable<String, Object>
54 public GenericURLContext(Hashtable<?,?> env) {
55 // context that is not tied to any specific URL
56 myEnv =
57 (Hashtable<String, Object>)(env == null ? null : env.clone());
58 }
59
60 public void close() throws NamingException {
61 myEnv = null;
62 }
63
64 public String getNameInNamespace() throws NamingException {
65 return ""; // %%% check this out: A URL context's name is ""
66 }
67
68 /**
69 * Resolves 'name' into a target context with remaining name.
70 * For example, with a JNDI URL "jndi://dnsname/rest_name",
71 * this method resolves "jndi://dnsname/" to a target context,
72 * and returns the target context with "rest_name".
73 * The definition of "root URL" and how much of the URL to
74 * consume is implementation specific.
75 * If rename() is supported for a particular URL scheme,
76 * getRootURLContext(), getURLPrefix(), and getURLSuffix()
77 * must be in sync wrt how URLs are parsed and returned.
478 return name;
479 } else if (name.equals("")) {
480 return prefix;
481 } else {
482 return (prefix + "/" + name);
483 }
484 }
485
486 public Name composeName(Name name, Name prefix) throws NamingException {
487 Name result = (Name)prefix.clone();
488 result.addAll(name);
489 return result;
490 }
491
492 @SuppressWarnings("unchecked") // clone()
493 public Object removeFromEnvironment(String propName)
494 throws NamingException {
495 if (myEnv == null) {
496 return null;
497 }
498 return myEnv.remove(propName);
499 }
500
501 @SuppressWarnings("unchecked") // clone()
502 public Object addToEnvironment(String propName, Object propVal)
503 throws NamingException {
504 if (myEnv == null) {
505 myEnv = new Hashtable<String, Object>(11, 0.75f);
506 }
507 return myEnv.put(propName, propVal);
508 }
509
510 @SuppressWarnings("unchecked") // clone()
511 public Hashtable<String, Object> getEnvironment() throws NamingException {
512 if (myEnv == null) {
513 return new Hashtable<>(5, 0.75f);
514 } else {
515 return (Hashtable<String, Object>)myEnv.clone();
516 }
517 }
518
519 /*
520 // To test, declare getURLPrefix and getURLSuffix static.
521
522 public static void main(String[] args) throws Exception {
523 String[] tests = {"file://host:port",
524 "file:///rest/of/name",
525 "file://host:port/rest/of/name",
526 "file:/rest/of/name",
|