1 48 49 package com.caucho.burlap.client; 50 51 import com.caucho.burlap.io.AbstractBurlapInput; 52 import com.caucho.burlap.io.BurlapInput; 53 import com.caucho.burlap.io.BurlapOutput; 54 import com.caucho.burlap.io.BurlapRemoteObject; 55 import com.caucho.burlap.io.BurlapRemoteResolver; 56 import com.caucho.services.client.ServiceProxyFactory; 57 58 import javax.naming.Context ; 59 import javax.naming.Name ; 60 import javax.naming.NamingException ; 61 import javax.naming.RefAddr ; 62 import javax.naming.Reference ; 63 import javax.naming.spi.ObjectFactory ; 64 import java.io.IOException ; 65 import java.io.InputStream ; 66 import java.io.OutputStream ; 67 import java.lang.reflect.Proxy ; 68 import java.net.HttpURLConnection ; 69 import java.net.MalformedURLException ; 70 import java.net.URL ; 71 import java.net.URLConnection ; 72 import java.util.Hashtable ; 73 74 114 public class BurlapProxyFactory implements ServiceProxyFactory, ObjectFactory { 115 private BurlapRemoteResolver _resolver; 116 117 private String _user; 118 private String _password; 119 private String _basicAuth; 120 121 private boolean _isOverloadEnabled = false; 122 123 126 public BurlapProxyFactory() 127 { 128 _resolver = new BurlapProxyResolver(this); 129 } 130 131 134 public void setUser(String user) 135 { 136 _user = user; 137 _basicAuth = null; 138 } 139 140 143 public void setPassword(String password) 144 { 145 _password = password; 146 _basicAuth = null; 147 } 148 149 152 public boolean isOverloadEnabled() 153 { 154 return _isOverloadEnabled; 155 } 156 157 160 public void setOverloadEnabled(boolean isOverloadEnabled) 161 { 162 _isOverloadEnabled = isOverloadEnabled; 163 } 164 165 168 public BurlapRemoteResolver getRemoteResolver() 169 { 170 return _resolver; 171 } 172 173 176 protected URLConnection openConnection(URL url) 177 throws IOException 178 { 179 URLConnection conn = url.openConnection(); 180 181 conn.setDoOutput(true); 182 183 if (_basicAuth != null) 184 conn.setRequestProperty("Authorization", _basicAuth); 185 else if (_user != null && _password != null) { 186 _basicAuth = "Basic " + base64(_user + ":" + _password); 187 conn.setRequestProperty("Authorization", _basicAuth); 188 } 189 190 return conn; 191 } 192 193 201 public Object create(String url) 202 throws MalformedURLException , ClassNotFoundException 203 { 204 BurlapMetaInfoAPI metaInfo; 205 206 metaInfo = (BurlapMetaInfoAPI) create(BurlapMetaInfoAPI.class, url); 207 208 String apiClassName = 209 (String ) metaInfo._burlap_getAttribute("java.api.class"); 210 211 if (apiClassName == null) 212 throw new BurlapRuntimeException(url + " has an unknown api."); 213 214 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 215 216 Class apiClass = Class.forName(apiClassName, false, loader); 217 218 return create(apiClass, url); 219 } 220 221 235 public Object create(Class api, String urlName) 236 throws MalformedURLException 237 { 238 URL url = new URL (urlName); 239 240 try { 241 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 243 244 conn.setRequestProperty("Connection", "close"); 245 246 InputStream is = conn.getInputStream(); 247 248 is.close(); 249 250 conn.disconnect(); 251 } catch (IOException e) { 252 } 253 254 BurlapProxy handler = new BurlapProxy(this, url); 255 256 return Proxy.newProxyInstance(api.getClassLoader(), 257 new Class [] { api, 258 BurlapRemoteObject.class }, 259 handler); 260 } 261 262 public AbstractBurlapInput getBurlapInput(InputStream is) 263 { 264 AbstractBurlapInput in = new BurlapInput(is); 265 in.setRemoteResolver(getRemoteResolver()); 266 267 return in; 268 } 269 270 public BurlapOutput getBurlapOutput(OutputStream os) 271 { 272 BurlapOutput out = new BurlapOutput(os); 273 274 return out; 275 } 276 277 280 public Object getObjectInstance(Object obj, Name name, 281 Context nameCtx, 282 Hashtable <?,?> environment) 283 throws Exception 284 { 285 Reference ref = (Reference ) obj; 286 287 String api = null; 288 String url = null; 289 String user = null; 290 String password = null; 291 292 for (int i = 0; i < ref.size(); i++) { 293 RefAddr addr = ref.get(i); 294 295 String type = addr.getType(); 296 String value = (String ) addr.getContent(); 297 298 if (type.equals("type")) 299 api = value; 300 else if (type.equals("url")) 301 url = value; 302 else if (type.equals("user")) 303 setUser(value); 304 else if (type.equals("password")) 305 setPassword(value); 306 } 307 308 if (url == null) 309 throw new NamingException ("`url' must be configured for BurlapProxyFactory."); 310 if (api == null) 312 throw new NamingException ("`type' must be configured for BurlapProxyFactory."); 313 314 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 315 Class apiClass = Class.forName(api, false, loader); 316 317 return create(apiClass, url); 318 } 319 320 323 private String base64(String value) 324 { 325 StringBuffer cb = new StringBuffer (); 326 327 int i = 0; 328 for (i = 0; i + 2 < value.length(); i += 3) { 329 long chunk = (int) value.charAt(i); 330 chunk = (chunk << 8) + (int) value.charAt(i + 1); 331 chunk = (chunk << 8) + (int) value.charAt(i + 2); 332 333 cb.append(encode(chunk >> 18)); 334 cb.append(encode(chunk >> 12)); 335 cb.append(encode(chunk >> 6)); 336 cb.append(encode(chunk)); 337 } 338 339 if (i + 1 < value.length()) { 340 long chunk = (int) value.charAt(i); 341 chunk = (chunk << 8) + (int) value.charAt(i + 1); 342 chunk <<= 8; 343 344 cb.append(encode(chunk >> 18)); 345 cb.append(encode(chunk >> 12)); 346 cb.append(encode(chunk >> 6)); 347 cb.append('='); 348 } 349 else if (i < value.length()) { 350 long chunk = (int) value.charAt(i); 351 chunk <<= 16; 352 353 cb.append(encode(chunk >> 18)); 354 cb.append(encode(chunk >> 12)); 355 cb.append('='); 356 cb.append('='); 357 } 358 359 return cb.toString(); 360 } 361 362 public static char encode(long d) 363 { 364 d &= 0x3f; 365 if (d < 26) 366 return (char) (d + 'A'); 367 else if (d < 52) 368 return (char) (d + 'a' - 26); 369 else if (d < 62) 370 return (char) (d + '0' - 52); 371 else if (d == 62) 372 return '+'; 373 else 374 return '/'; 375 } 376 } 377 378 | Popular Tags |