Source Code Cross Referenced for SocketUtils.java in  » Net » edtftpj » com » enterprisedt » net » ftp » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » edtftpj » com.enterprisedt.net.ftp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *
003:         *  Copyright (C) 2000-2006  Enterprise Distributed Technologies Ltd
004:         *
005:         *  www.enterprisedt.com
006:         *
007:         *  Change Log:
008:         *
009:         *        $Log: SocketUtils.java,v $
010:         *        Revision 1.5  2007-12-18 07:53:07  bruceb
011:         *        extra debug
012:         *
013:         *        Revision 1.4  2007-05-29 06:07:21  bruceb
014:         *        use casts to remove compile warnings
015:         *
016:         *        Revision 1.3  2007-05-29 04:16:44  bruceb
017:         *        added isConnected()
018:         *
019:         *        Revision 1.2  2006/11/02 17:37:31  bruceb
020:         *        remove unneeded sendUrgentData()
021:         *
022:         *        Revision 1.1  2006/10/27 15:38:01  bruceb
023:         *        used for connect with timeout
024:         *
025:         *
026:         */package com.enterprisedt.net.ftp;
027:
028:        import java.io.IOException;
029:        import java.lang.reflect.Constructor;
030:        import java.lang.reflect.InvocationTargetException;
031:        import java.lang.reflect.Method;
032:        import java.net.InetAddress;
033:        import java.net.Socket;
034:
035:        import com.enterprisedt.util.debug.Logger;
036:
037:        /**
038:         * Utility class that allows 1.4 socket methods to be called while
039:         * still being able to be compiled in 1.1.x
040:         * 
041:         * @author Bruce Blackshaw
042:         * @version $Revision: 1.5 $
043:         */
044:        public class SocketUtils {
045:
046:            /**
047:             * Logging object
048:             */
049:            private static Logger log = Logger.getLogger("SocketUtils");
050:
051:            /**
052:             * Create a connected socket, using a timeout if it is available. 
053:             * Availability is tested by trying to create instances of the 
054:             * required classes and methods (JRE 1.4+)
055:             * 
056:             * @param host     remote host to connect to
057:             * @param port     port on remote host
058:             * @param timeout  timeout in milliseconds on 
059:             * @exception IOException
060:             */
061:            public static Socket createSocket(InetAddress host, int port,
062:                    int timeout) throws IOException {
063:
064:                // don't bother going thru the below if a timeout isn't asked for ...
065:                if (timeout == 0) {
066:                    return new Socket(host, port);
067:                } else {
068:                    // attempt to set up 1.4 and later's Socket.connect method which
069:                    // provides a timeout
070:                    try {
071:                        // get the correct connect method
072:                        Class socketAddress = Class
073:                                .forName("java.net.SocketAddress");
074:                        Method connectMethod = Socket.class.getMethod(
075:                                "connect", new Class[] { socketAddress,
076:                                        int.class });
077:
078:                        // create an unconnected socket instance
079:                        Socket sock = (Socket) Socket.class.newInstance();
080:
081:                        // need an InetSocketAddress instance for connect()
082:                        Class inetSocketAddress = Class
083:                                .forName("java.net.InetSocketAddress");
084:                        Constructor inetSocketAddressCtr = inetSocketAddress
085:                                .getConstructor(new Class[] {
086:                                        InetAddress.class, int.class });
087:                        Object address = inetSocketAddressCtr
088:                                .newInstance(new Object[] { host,
089:                                        new Integer(port) });
090:
091:                        // now invoke the connect method with the timeout
092:                        log.debug("Invoking connect with timeout=" + timeout);
093:                        connectMethod.invoke(sock, new Object[] { address,
094:                                new Integer(timeout) });
095:                        log.debug("Connected successfully");
096:                        return sock;
097:                    } catch (InvocationTargetException ex) {
098:                        Throwable target = ex.getTargetException();
099:                        if (target instanceof  IOException)
100:                            throw (IOException) target;
101:                        log.debug("Could not use timeout connecting to host ("
102:                                + ex.toString() + ")");
103:                        return new Socket(host, port);
104:                    } catch (Exception ex) {
105:                        log.debug("Could not use timeout connecting to host ("
106:                                + ex.toString() + ")");
107:                        return new Socket(host, port);
108:                    }
109:                }
110:            }
111:
112:            /**
113:             * Test if a socket is connected by using the isConnected method, only
114:             * available from JRE 1.4+. So invoke using reflection. If can't check
115:             * it assumes the socket is connected.
116:             * 
117:             * @param sock     socket to test
118:             * @exception IOException
119:             */
120:            public static boolean isConnected(Socket sock) throws IOException {
121:                try {
122:                    // get the isConnected method
123:                    Method connectedMethod = Socket.class.getMethod(
124:                            "isConnected", (Class[]) null);
125:
126:                    Boolean result = (Boolean) connectedMethod.invoke(sock,
127:                            (Object[]) null);
128:                    return result.booleanValue();
129:                } catch (InvocationTargetException ex) {
130:                    Throwable target = ex.getTargetException();
131:                    if (target instanceof  IOException)
132:                        throw (IOException) target;
133:                    log.debug("Could not use Socket.isConnected ("
134:                            + ex.toString() + ")");
135:                    return true;
136:                } catch (Exception ex) {
137:                    log.debug("Could not use Socket.isConnected ("
138:                            + ex.toString() + ")");
139:                    return true;
140:                }
141:            }
142:
143:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.