1 48 49 package com.caucho.hessian.client; 50 51 import com.caucho.hessian.io.*; 52 import com.caucho.services.client.ServiceProxyFactory; 53 54 import javax.naming.Context ; 55 import javax.naming.Name ; 56 import javax.naming.NamingException ; 57 import javax.naming.RefAddr ; 58 import javax.naming.Reference ; 59 import javax.naming.spi.ObjectFactory ; 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.io.OutputStream ; 63 import java.io.PrintWriter ; 64 import java.lang.reflect.InvocationHandler ; 65 import java.lang.reflect.Proxy ; 66 import java.net.MalformedURLException ; 67 import java.net.URL ; 68 import java.net.URLConnection ; 69 import java.util.Hashtable ; 70 import java.util.logging.Logger ; 71 72 112 public class HessianProxyFactory implements ServiceProxyFactory, ObjectFactory { 113 protected static Logger log 114 = Logger.getLogger(HessianProxyFactory.class.getName()); 115 116 private SerializerFactory _serializerFactory; 117 private HessianRemoteResolver _resolver; 118 119 private String _user; 120 private String _password; 121 private String _basicAuth; 122 123 private boolean _isOverloadEnabled = false; 124 125 private boolean _isHessian2Reply = false; 126 private boolean _isHessian2Request = false; 127 128 private boolean _isChunkedPost = false; 129 private boolean _isDebug = false; 130 131 private long _readTimeout = -1; 132 133 private String _connectionFactoryName = "jms/ConnectionFactory"; 134 135 138 public HessianProxyFactory() 139 { 140 _resolver = new HessianProxyResolver(this); 141 } 142 143 146 public void setUser(String user) 147 { 148 _user = user; 149 _basicAuth = null; 150 } 151 152 155 public void setPassword(String password) 156 { 157 _password = password; 158 _basicAuth = null; 159 } 160 161 165 public void setConnectionFactoryName(String connectionFactoryName) 166 { 167 _connectionFactoryName = connectionFactoryName; 168 } 169 170 173 public void setDebug(boolean isDebug) 174 { 175 _isDebug = isDebug; 176 } 177 178 181 public boolean isDebug() 182 { 183 return _isDebug; 184 } 185 186 189 public boolean isOverloadEnabled() 190 { 191 return _isOverloadEnabled; 192 } 193 194 197 public void setOverloadEnabled(boolean isOverloadEnabled) 198 { 199 _isOverloadEnabled = isOverloadEnabled; 200 } 201 202 205 public void setChunkedPost(boolean isChunked) 206 { 207 _isChunkedPost = isChunked; 208 } 209 210 213 public boolean isChunkedPost() 214 { 215 return _isChunkedPost; 216 } 217 218 221 public long getReadTimeout() 222 { 223 return _readTimeout; 224 } 225 226 229 public void setReadTimeout(long timeout) 230 { 231 _readTimeout = timeout; 232 } 233 234 237 public void setHessian2Reply(boolean isHessian2) 238 { 239 _isHessian2Reply = isHessian2; 240 } 241 242 245 public void setHessian2Request(boolean isHessian2) 246 { 247 _isHessian2Request = isHessian2; 248 249 if (isHessian2) 250 _isHessian2Reply = true; 251 } 252 253 256 public HessianRemoteResolver getRemoteResolver() 257 { 258 return _resolver; 259 } 260 261 264 public void setSerializerFactory(SerializerFactory factory) 265 { 266 _serializerFactory = factory; 267 } 268 269 272 public SerializerFactory getSerializerFactory() 273 { 274 if (_serializerFactory == null) 275 _serializerFactory = new SerializerFactory(); 276 277 return _serializerFactory; 278 } 279 280 283 protected URLConnection openConnection(URL url) 284 throws IOException 285 { 286 URLConnection conn = url.openConnection(); 287 288 conn.setDoOutput(true); 289 290 if (_readTimeout > 0) { 291 try { 292 conn.setReadTimeout((int) _readTimeout); 293 } catch (Throwable e) { 294 } 295 } 296 297 conn.setRequestProperty("Content-Type", "x-application/hessian"); 298 299 if (_basicAuth != null) 300 conn.setRequestProperty("Authorization", _basicAuth); 301 else if (_user != null && _password != null) { 302 _basicAuth = "Basic " + base64(_user + ":" + _password); 303 conn.setRequestProperty("Authorization", _basicAuth); 304 } 305 306 return conn; 307 } 308 309 317 public Object create(String url) 318 throws MalformedURLException , ClassNotFoundException 319 { 320 HessianMetaInfoAPI metaInfo; 321 322 metaInfo = (HessianMetaInfoAPI) create(HessianMetaInfoAPI.class, url); 323 324 String apiClassName = 325 (String ) metaInfo._hessian_getAttribute("java.api.class"); 326 327 if (apiClassName == null) 328 throw new HessianRuntimeException(url + " has an unknown api."); 329 330 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 331 332 Class apiClass = Class.forName(apiClassName, false, loader); 333 334 return create(apiClass, url); 335 } 336 337 351 public Object create(Class api, String urlName) 352 throws MalformedURLException 353 { 354 return create(api, urlName, 355 Thread.currentThread().getContextClassLoader()); 356 } 357 358 372 public Object create(Class api, String urlName, ClassLoader loader) 373 throws MalformedURLException 374 { 375 InvocationHandler handler = null; 376 377 if (urlName.startsWith("jms:")) { 378 String jndiName = urlName.substring("jms:".length()); 379 380 try { 381 handler = new HessianJMSProxy(this, jndiName, _connectionFactoryName); 382 } catch (Exception e) { 383 log.info("Unable to create JMS proxy: " + e); 384 return null; 385 } 386 } 387 else { 388 URL url = new URL (urlName); 389 handler = new HessianProxy(this, url); 390 } 391 392 return Proxy.newProxyInstance(api.getClassLoader(), 393 new Class [] { api, 394 HessianRemoteObject.class }, 395 handler); 396 } 397 398 public AbstractHessianInput getHessianInput(InputStream is) 399 { 400 AbstractHessianInput in; 401 402 if (_isDebug) 403 is = new HessianDebugInputStream(is, new PrintWriter (System.out)); 404 405 if (_isHessian2Reply) 406 in = new Hessian2Input(is); 407 else 408 in = new HessianInput(is); 409 410 in.setRemoteResolver(getRemoteResolver()); 411 412 in.setSerializerFactory(getSerializerFactory()); 413 414 return in; 415 } 416 417 public AbstractHessianOutput getHessianOutput(OutputStream os) 418 { 419 AbstractHessianOutput out; 420 421 if (_isHessian2Request) 422 out = new Hessian2Output(os); 423 else { 424 HessianOutput out1 = new HessianOutput(os); 425 out = out1; 426 427 if (_isHessian2Reply) 428 out1.setVersion(2); 429 } 430 431 out.setSerializerFactory(getSerializerFactory()); 432 433 return out; 434 } 435 436 439 public Object getObjectInstance(Object obj, Name name, 440 Context nameCtx, Hashtable <?,?> environment) 441 throws Exception 442 { 443 Reference ref = (Reference ) obj; 444 445 String api = null; 446 String url = null; 447 String user = null; 448 String password = null; 449 450 for (int i = 0; i < ref.size(); i++) { 451 RefAddr addr = ref.get(i); 452 453 String type = addr.getType(); 454 String value = (String ) addr.getContent(); 455 456 if (type.equals("type")) 457 api = value; 458 else if (type.equals("url")) 459 url = value; 460 else if (type.equals("user")) 461 setUser(value); 462 else if (type.equals("password")) 463 setPassword(value); 464 } 465 466 if (url == null) 467 throw new NamingException ("`url' must be configured for HessianProxyFactory."); 468 if (api == null) 470 throw new NamingException ("`type' must be configured for HessianProxyFactory."); 471 472 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 473 Class apiClass = Class.forName(api, false, loader); 474 475 return create(apiClass, url); 476 } 477 478 481 private String base64(String value) 482 { 483 StringBuffer cb = new StringBuffer (); 484 485 int i = 0; 486 for (i = 0; i + 2 < value.length(); i += 3) { 487 long chunk = (int) value.charAt(i); 488 chunk = (chunk << 8) + (int) value.charAt(i + 1); 489 chunk = (chunk << 8) + (int) value.charAt(i + 2); 490 491 cb.append(encode(chunk >> 18)); 492 cb.append(encode(chunk >> 12)); 493 cb.append(encode(chunk >> 6)); 494 cb.append(encode(chunk)); 495 } 496 497 if (i + 1 < value.length()) { 498 long chunk = (int) value.charAt(i); 499 chunk = (chunk << 8) + (int) value.charAt(i + 1); 500 chunk <<= 8; 501 502 cb.append(encode(chunk >> 18)); 503 cb.append(encode(chunk >> 12)); 504 cb.append(encode(chunk >> 6)); 505 cb.append('='); 506 } 507 else if (i < value.length()) { 508 long chunk = (int) value.charAt(i); 509 chunk <<= 16; 510 511 cb.append(encode(chunk >> 18)); 512 cb.append(encode(chunk >> 12)); 513 cb.append('='); 514 cb.append('='); 515 } 516 517 return cb.toString(); 518 } 519 520 public static char encode(long d) 521 { 522 d &= 0x3f; 523 if (d < 26) 524 return (char) (d + 'A'); 525 else if (d < 52) 526 return (char) (d + 'a' - 26); 527 else if (d < 62) 528 return (char) (d + '0' - 52); 529 else if (d == 62) 530 return '+'; 531 else 532 return '/'; 533 } 534 } 535 536 | Popular Tags |