31 32 import java.util.Hashtable; 33 import java.net.MalformedURLException; 34 35 /** 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 myEnv = null; 52 53 public GenericURLContext(Hashtable env) { 54 // context that is not tied to any specific URL 55 myEnv = env; // copied on write 56 } 57 58 public void close() throws NamingException { 59 myEnv = null; 60 } 61 62 public String getNameInNamespace() throws NamingException { 63 return ""; // %%% check this out: A URL context's name is "" 64 } 65 66 /** 67 * Resolves 'name' into a target context with remaining name. 68 * For example, with a JNDI URL "jndi://dnsname/rest_name", 69 * this method resolves "jndi://dnsname/" to a target context, 70 * and returns the target context with "rest_name". 71 * The definition of "root URL" and how much of the URL to 72 * consume is implementation specific. 73 * If rename() is supported for a particular URL scheme, 74 * getRootURLContext(), getURLPrefix(), and getURLSuffix() 75 * must be in sync wrt how URLs are parsed and returned. 76 */ 77 abstract protected ResolveResult getRootURLContext(String url, 78 Hashtable env) throws NamingException; 79 80 /** 81 * Returns the suffix of the url. The result should be identical to 82 * that of calling getRootURLContext().getRemainingName(), but 83 * without the overhead of doing anything with the prefix like 84 * creating a context. 85 *<p> 86 * This method returns a Name instead of a String because to give 87 * the provider an opportunity to return a Name (for example, 88 * for weakly separated naming systems like COS naming). 89 *<p> 90 * The default implementation uses skips 'prefix', calls 91 * UrlUtil.decode() on it, and returns the result as a single component 92 * CompositeName. 93 * Subclass should override if this is not appropriate. 94 * This method is used only by rename(). 95 * If rename() is supported for a particular URL scheme, 96 * getRootURLContext(), getURLPrefix(), and getURLSuffix() 97 * must be in sync wrt how URLs are parsed and returned. 98 *<p> 470 } 471 } 472 473 public String composeName(String name, String prefix) 474 throws NamingException { 475 if (prefix.equals("")) { 476 return name; 477 } else if (name.equals("")) { 478 return prefix; 479 } else { 480 return (prefix + "/" + name); 481 } 482 } 483 484 public Name composeName(Name name, Name prefix) throws NamingException { 485 Name result = (Name)prefix.clone(); 486 result.addAll(name); 487 return result; 488 } 489 490 public Object removeFromEnvironment(String propName) 491 throws NamingException { 492 if (myEnv == null) { 493 return null; 494 } 495 myEnv = (Hashtable)myEnv.clone(); 496 return myEnv.remove(propName); 497 } 498 499 public Object addToEnvironment(String propName, Object propVal) 500 throws NamingException { 501 myEnv = (myEnv == null) ? 502 new Hashtable(11, 0.75f) : (Hashtable)myEnv.clone(); 503 return myEnv.put(propName, propVal); 504 } 505 506 public Hashtable getEnvironment() throws NamingException { 507 if (myEnv == null) { 508 return new Hashtable(5, 0.75f); 509 } else { 510 return (Hashtable)myEnv.clone(); 511 } 512 } 513 514 /* 515 // To test, declare getURLPrefix and getURLSuffix static. 516 517 public static void main(String[] args) throws Exception { 518 String[] tests = {"file://host:port", 519 "file:///rest/of/name", 520 "file://host:port/rest/of/name", 521 "file:/rest/of/name", 522 "file:rest/of/name"}; 523 for (int i = 0; i < tests.length; i++) { 524 String pre = getURLPrefix(tests[i]); 525 System.out.println(pre); 526 System.out.println(getURLSuffix(pre, tests[i])); 527 } 528 } 529 */ 530 } | 31 32 import java.util.Hashtable; 33 import java.net.MalformedURLException; 34 35 /** 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. 77 */ 78 abstract protected ResolveResult getRootURLContext(String url, 79 Hashtable<?,?> env) throws NamingException; 80 81 /** 82 * Returns the suffix of the url. The result should be identical to 83 * that of calling getRootURLContext().getRemainingName(), but 84 * without the overhead of doing anything with the prefix like 85 * creating a context. 86 *<p> 87 * This method returns a Name instead of a String because to give 88 * the provider an opportunity to return a Name (for example, 89 * for weakly separated naming systems like COS naming). 90 *<p> 91 * The default implementation uses skips 'prefix', calls 92 * UrlUtil.decode() on it, and returns the result as a single component 93 * CompositeName. 94 * Subclass should override if this is not appropriate. 95 * This method is used only by rename(). 96 * If rename() is supported for a particular URL scheme, 97 * getRootURLContext(), getURLPrefix(), and getURLSuffix() 98 * must be in sync wrt how URLs are parsed and returned. 99 *<p> 471 } 472 } 473 474 public String composeName(String name, String prefix) 475 throws NamingException { 476 if (prefix.equals("")) { 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", 527 "file:rest/of/name"}; 528 for (int i = 0; i < tests.length; i++) { 529 String pre = getURLPrefix(tests[i]); 530 System.out.println(pre); 531 System.out.println(getURLSuffix(pre, tests[i])); 532 } 533 } 534 */ 535 } |