src/share/classes/com/sun/jndi/ldap/Connection.java

Print this page




 221             // ignore generic exceptions.
 222             // Also catches all IO errors generated by socket creation.
 223             CommunicationException ce =
 224                 new CommunicationException(host + ":" + port);
 225             ce.setRootCause(e);
 226             throw ce;
 227         }
 228 
 229         worker = Obj.helper.createThread(this);
 230         worker.setDaemon(true);
 231         worker.start();
 232     }
 233 
 234     /*
 235      * Create an InetSocketAddress using the specified hostname and port number.
 236      */
 237     private Object createInetSocketAddress(String host, int port)
 238             throws NoSuchMethodException {
 239 
 240         try {
 241             Class inetSocketAddressClass =
 242                 Class.forName("java.net.InetSocketAddress");
 243 
 244             Constructor inetSocketAddressCons =
 245                 inetSocketAddressClass.getConstructor(new Class[]{
 246                 String.class, int.class});
 247 
 248             return inetSocketAddressCons.newInstance(new Object[]{
 249                 host, new Integer(port)});
 250 
 251         } catch (ClassNotFoundException e) {



 252             throw new NoSuchMethodException();
 253 
 254         } catch (InstantiationException e) {
 255             throw new NoSuchMethodException();
 256 
 257         } catch (InvocationTargetException e) {
 258             throw new NoSuchMethodException();
 259 
 260         } catch (IllegalAccessException e) {
 261             throw new NoSuchMethodException();
 262         }
 263     }
 264 
 265     /*
 266      * Create a Socket object using the specified socket factory and time limit.
 267      *
 268      * If a timeout is supplied and unconnected sockets are supported then
 269      * an unconnected socket is created and the timeout is applied when
 270      * connecting the socket. If a timeout is supplied but unconnected sockets
 271      * are not supported then the timeout is ignored and a connected socket
 272      * is created.
 273      */
 274     private Socket createSocket(String host, int port, String socketFactory,
 275             int connectTimeout) throws Exception {
 276 
 277         Socket socket = null;
 278 
 279         if (socketFactory != null) {
 280 
 281             // create the factory
 282 
 283             Class socketFactoryClass = Obj.helper.loadClass(socketFactory);
 284             Method getDefault =
 285                 socketFactoryClass.getMethod("getDefault", new Class[]{});
 286             Object factory = getDefault.invoke(null, new Object[]{});
 287 
 288             // create the socket
 289 
 290             Method createSocket = null;
 291 
 292             if (connectTimeout > 0) {
 293 
 294                 try {
 295                     createSocket = socketFactoryClass.getMethod("createSocket",
 296                         new Class[]{});
 297 
 298                     Method connect = Socket.class.getMethod("connect",
 299                         new Class[]{Class.forName("java.net.SocketAddress"),
 300                         int.class});
 301                     Object endpoint = createInetSocketAddress(host, port);
 302 
 303                     // unconnected socket
 304                     socket =
 305                         (Socket)createSocket.invoke(factory, new Object[]{});
 306 
 307                     if (debug) {
 308                         System.err.println("Connection: creating socket with " +
 309                             "a timeout using supplied socket factory");
 310                     }
 311 
 312                     // connected socket
 313                     connect.invoke(socket, new Object[]{
 314                         endpoint, new Integer(connectTimeout)});
 315 
 316                 } catch (NoSuchMethodException e) {
 317                     // continue (but ignore connectTimeout)
 318                 }
 319             }
 320 
 321             if (socket == null) {
 322                 createSocket = socketFactoryClass.getMethod("createSocket",
 323                     new Class[]{String.class, int.class});
 324 
 325                 if (debug) {
 326                     System.err.println("Connection: creating socket using " +
 327                         "supplied socket factory");
 328                 }
 329                 // connected socket
 330                 socket = (Socket) createSocket.invoke(factory,
 331                     new Object[]{host, new Integer(port)});
 332             }
 333         } else {
 334 
 335             if (connectTimeout > 0) {
 336 
 337                 try {
 338                     Constructor socketCons =
 339                         Socket.class.getConstructor(new Class[]{});
 340 
 341                     Method connect = Socket.class.getMethod("connect",
 342                         new Class[]{Class.forName("java.net.SocketAddress"),
 343                         int.class});
 344                     Object endpoint = createInetSocketAddress(host, port);
 345 
 346                     socket = (Socket) socketCons.newInstance(new Object[]{});
 347 
 348                     if (debug) {
 349                         System.err.println("Connection: creating socket with " +
 350                             "a timeout");
 351                     }
 352                     connect.invoke(socket, new Object[]{
 353                         endpoint, new Integer(connectTimeout)});
 354 
 355                 } catch (NoSuchMethodException e) {
 356                     // continue (but ignore connectTimeout)
 357                 }
 358             }
 359 
 360             if (socket == null) {
 361                 if (debug) {
 362                     System.err.println("Connection: creating socket");
 363                 }
 364                 // connected socket
 365                 socket = new Socket(host, port);
 366             }




 221             // ignore generic exceptions.
 222             // Also catches all IO errors generated by socket creation.
 223             CommunicationException ce =
 224                 new CommunicationException(host + ":" + port);
 225             ce.setRootCause(e);
 226             throw ce;
 227         }
 228 
 229         worker = Obj.helper.createThread(this);
 230         worker.setDaemon(true);
 231         worker.start();
 232     }
 233 
 234     /*
 235      * Create an InetSocketAddress using the specified hostname and port number.
 236      */
 237     private Object createInetSocketAddress(String host, int port)
 238             throws NoSuchMethodException {
 239 
 240         try {
 241             Class<?> inetSocketAddressClass =
 242                 Class.forName("java.net.InetSocketAddress");
 243 
 244             Constructor<?> inetSocketAddressCons =
 245                 inetSocketAddressClass.getConstructor(new Class<?>[]{
 246                 String.class, int.class});
 247 
 248             return inetSocketAddressCons.newInstance(new Object[]{
 249                 host, new Integer(port)});
 250 
 251         } catch (ClassNotFoundException |
 252                  InstantiationException |
 253                  InvocationTargetException |
 254                  IllegalAccessException e) {
 255             throw new NoSuchMethodException();
 256 








 257         }
 258     }
 259 
 260     /*
 261      * Create a Socket object using the specified socket factory and time limit.
 262      *
 263      * If a timeout is supplied and unconnected sockets are supported then
 264      * an unconnected socket is created and the timeout is applied when
 265      * connecting the socket. If a timeout is supplied but unconnected sockets
 266      * are not supported then the timeout is ignored and a connected socket
 267      * is created.
 268      */
 269     private Socket createSocket(String host, int port, String socketFactory,
 270             int connectTimeout) throws Exception {
 271 
 272         Socket socket = null;
 273 
 274         if (socketFactory != null) {
 275 
 276             // create the factory
 277 
 278             Class<?> socketFactoryClass = Obj.helper.loadClass(socketFactory);
 279             Method getDefault =
 280                 socketFactoryClass.getMethod("getDefault", new Class<?>[]{});
 281             Object factory = getDefault.invoke(null, new Object[]{});
 282 
 283             // create the socket
 284 
 285             Method createSocket = null;
 286 
 287             if (connectTimeout > 0) {
 288 
 289                 try {
 290                     createSocket = socketFactoryClass.getMethod("createSocket",
 291                         new Class<?>[]{});
 292 
 293                     Method connect = Socket.class.getMethod("connect",
 294                         new Class<?>[]{Class.forName("java.net.SocketAddress"),
 295                         int.class});
 296                     Object endpoint = createInetSocketAddress(host, port);
 297 
 298                     // unconnected socket
 299                     socket =
 300                         (Socket)createSocket.invoke(factory, new Object[]{});
 301 
 302                     if (debug) {
 303                         System.err.println("Connection: creating socket with " +
 304                             "a timeout using supplied socket factory");
 305                     }
 306 
 307                     // connected socket
 308                     connect.invoke(socket, new Object[]{
 309                         endpoint, new Integer(connectTimeout)});
 310 
 311                 } catch (NoSuchMethodException e) {
 312                     // continue (but ignore connectTimeout)
 313                 }
 314             }
 315 
 316             if (socket == null) {
 317                 createSocket = socketFactoryClass.getMethod("createSocket",
 318                     new Class<?>[]{String.class, int.class});
 319 
 320                 if (debug) {
 321                     System.err.println("Connection: creating socket using " +
 322                         "supplied socket factory");
 323                 }
 324                 // connected socket
 325                 socket = (Socket) createSocket.invoke(factory,
 326                     new Object[]{host, new Integer(port)});
 327             }
 328         } else {
 329 
 330             if (connectTimeout > 0) {
 331 
 332                 try {
 333                     Constructor<Socket> socketCons =
 334                         Socket.class.getConstructor(new Class<?>[]{});
 335 
 336                     Method connect = Socket.class.getMethod("connect",
 337                         new Class<?>[]{Class.forName("java.net.SocketAddress"),
 338                         int.class});
 339                     Object endpoint = createInetSocketAddress(host, port);
 340 
 341                     socket = socketCons.newInstance(new Object[]{});
 342 
 343                     if (debug) {
 344                         System.err.println("Connection: creating socket with " +
 345                             "a timeout");
 346                     }
 347                     connect.invoke(socket, new Object[]{
 348                         endpoint, new Integer(connectTimeout)});
 349 
 350                 } catch (NoSuchMethodException e) {
 351                     // continue (but ignore connectTimeout)
 352                 }
 353             }
 354 
 355             if (socket == null) {
 356                 if (debug) {
 357                     System.err.println("Connection: creating socket");
 358                 }
 359                 // connected socket
 360                 socket = new Socket(host, port);
 361             }