1 package org.sapia.ubik.rmi.naming; 2 3 import org.sapia.ubik.net.Uri; 4 import org.sapia.ubik.net.UriSyntaxException; 5 import org.sapia.ubik.rmi.naming.remote.proxy.JNDIHandler; 6 7 import java.util.HashMap ; 8 import java.util.Map ; 9 10 import javax.naming.NameNotFoundException ; 11 import javax.naming.NamingException ; 12 13 14 60 public class ServiceLocator { 61 64 public static final int UNDEFINED_PORT = -1; 65 66 70 public static final String HANDLER_PATTERN = "ubik.rmi.naming.service.handler"; 71 72 77 public static final String UBIK_SCHEME = "ubik"; 78 79 84 private static Map _handlers = new HashMap (); 85 86 static { 87 registerHandler(UBIK_SCHEME, new JNDIHandler()); 88 89 ServiceHandler handler; 90 String [] propNames = (String []) System.getProperties().keySet() 91 .toArray(new String [System.getProperties() 92 .size()]); 93 String propName; 94 String className; 95 String scheme; 96 97 for (int i = 0; i < propNames.length; i++) { 98 propName = (String ) propNames[i]; 99 100 if (propName.startsWith(HANDLER_PATTERN)) { 101 scheme = propName.substring(HANDLER_PATTERN.length() + 1); 102 103 className = System.getProperty(propName); 104 105 if (className == null) { 106 throw new IllegalStateException ( 107 "no class name defined for transport provider: " + propName); 108 } 109 110 try { 111 handler = (ServiceHandler) Class.forName(className).newInstance(); 112 registerHandler(scheme, handler); 113 } catch (Throwable e) { 114 e.printStackTrace(); 115 throw new IllegalStateException ( 116 "could not instantiate transport provider: " + className); 117 } 118 } 119 } 120 } 121 122 133 public static Object lookup(String url) 134 throws NameNotFoundException , NamingException { 135 Uri uri; 136 137 try { 138 uri = Uri.parse(url); 139 } catch (UriSyntaxException e) { 140 NamingException exc = new NamingException ("could not look up " + url); 141 exc.setRootCause(e); 142 throw exc; 143 } 144 145 ServiceHandler handler = (ServiceHandler) _handlers.get(uri.getScheme()); 146 147 if (handler == null) { 148 throw new NamingException ("no handler found for: " + uri.getScheme()); 149 } 150 151 return handler.handleLookup(uri.getHost(), uri.getPort(), 152 uri.getQueryString().getPath(), uri.getQueryString().getParameters()); 153 } 154 155 162 public static void registerHandler(String scheme, ServiceHandler handler) 163 throws IllegalStateException { 164 if (_handlers.get(scheme) != null) { 165 throw new IllegalStateException ( 166 "service handler already registered for :" + scheme); 167 } 168 169 _handlers.put(scheme, handler); 170 } 171 } 172 | Popular Tags |