Source Code Cross Referenced for TransportMgr.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » jndi » provider » dns » 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 » Apache Harmony Java SE » org package » org.apache.harmony.jndi.provider.dns 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         */
018:
019:        /**
020:         * @author Alexei Y. Zakharov
021:         * @version $Revision: 1.1.2.4 $
022:         */package org.apache.harmony.jndi.provider.dns;
023:
024:        import java.io.BufferedOutputStream;
025:        import java.io.IOException;
026:        import java.io.InputStream;
027:        import java.net.DatagramPacket;
028:        import java.net.DatagramSocket;
029:        import java.net.InetAddress;
030:        import java.net.Socket;
031:        import java.net.SocketTimeoutException;
032:        import java.net.UnknownHostException;
033:
034:        import org.apache.harmony.jndi.internal.nls.Messages;
035:
036:        /**
037:         * Contains service methods that are used for transporting DNS messages from DNS
038:         * client to DNS server and vice versa.
039:         */
040:        public class TransportMgr {
041:
042:            /**
043:             * Sends the packet contained in <code>outBuf</code>, receives the answer
044:             * and stores it in <code>inBuf</code> array.
045:             * 
046:             * @param server
047:             *            server's IP-address in string form
048:             * @param serverPort
049:             *            server port
050:             * @param outBuf
051:             *            bytes of the message to send
052:             * @param outBufLen
053:             *            length of the <code>outBuf</code>
054:             * @param inBuf
055:             *            buffer to store received bytes at
056:             * @param inBufLen
057:             *            length of the <code>inBuf</code>
058:             * @param timeout
059:             *            time to wait for an answer, in milliseconds; 0 stands for
060:             *            infinite timeout
061:             * @return number of received bytes
062:             * @throws SocketTimeoutException
063:             *             in case of timeout
064:             * @throws SecurityException
065:             *             if security violation error occured; normally this happens if
066:             *             the access to the network subsystem has not been granted
067:             * @throws DomainProtocolException
068:             *             if some configuration or network problem encountered
069:             */
070:            public static int sendReceiveUDP(String server, int serverPort,
071:                    byte[] outBuf, int outBufLen, byte[] inBuf, int inBufLen,
072:                    int timeout) throws DomainProtocolException,
073:                    SocketTimeoutException, SecurityException {
074:
075:                byte srvAddrArr[] = null;
076:                InetAddress srvAddr = null;
077:                DatagramSocket dSocket = null;
078:                DatagramPacket inPacket = null;
079:                DatagramPacket outPacket = null;
080:
081:                try {
082:                    srvAddrArr = ProviderMgr.parseIpStr(server);
083:                } catch (IllegalArgumentException e) {
084:                    // jndi.40=Unable to connect: bad IP address
085:                    throw new DomainProtocolException(Messages
086:                            .getString("jndi.40")); //$NON-NLS-1$
087:                }
088:                try {
089:                    dSocket = new DatagramSocket();
090:                    srvAddr = InetAddress.getByAddress(srvAddrArr);
091:                    dSocket.connect(srvAddr, serverPort);
092:                    outPacket = new DatagramPacket(outBuf, outBufLen, srvAddr,
093:                            serverPort);
094:                    dSocket.setSoTimeout(timeout);
095:                    dSocket.send(outPacket);
096:                    inPacket = new DatagramPacket(inBuf, inBufLen, srvAddr,
097:                            serverPort);
098:                    dSocket.receive(inPacket);
099:                } catch (IllegalStateException e) {
100:                    // jndi.41=Error while querying DNS server
101:                    throw new DomainProtocolException(Messages
102:                            .getString("jndi.41"), e); //$NON-NLS-1$
103:                } catch (SocketTimeoutException e) {
104:                    throw (e);
105:                } catch (IOException e) {
106:                    // jndi.41=Error while querying DNS server
107:                    throw new DomainProtocolException(Messages
108:                            .getString("jndi.41"), e); //$NON-NLS-1$
109:                } finally {
110:                    if (dSocket != null) {
111:                        dSocket.close();
112:                    }
113:                }
114:                if (inPacket != null) {
115:                    return inPacket.getLength();
116:                }
117:                // jndi.42=unknown error
118:                throw new DomainProtocolException(Messages.getString("jndi.42")); //$NON-NLS-1$
119:            }
120:
121:            /**
122:             * Establishes TCP connection, transmit bytes from <code>inBuf</code>,
123:             * stores received answer in <code>outBuf</code>.
124:             * 
125:             * @param server
126:             *            the server's IP-address in string form
127:             * @param serverPort
128:             *            server port
129:             * @param outBuf
130:             *            bytes of the message to send
131:             * @param outBufLen
132:             *            length of the <code>outBuf</code>
133:             * @param inBuf
134:             *            buffer to store received bytes at
135:             * @param inBufLen
136:             *            length of the <code>inBuf</code>
137:             * @param timeout
138:             *            time to wait for an answer, in milliseconds; 0 stands for
139:             *            infinite timeout
140:             * @return number of received bytes
141:             * @throws SocketTimeoutException
142:             *             in case of timeout
143:             * @throws SecurityException
144:             *             if security violation error occured; normally this happens if
145:             *             the access to the network subsystem has not been granted
146:             * @throws DomainProtocolException
147:             *             if some configuration or network problem encountered TODO
148:             *             pool of connections may speed up things
149:             */
150:            public static int sendReceiveTCP(String server, int serverPort,
151:                    byte[] outBuf, int outBufLen, byte[] inBuf, int inBufLen,
152:                    int timeout) throws DomainProtocolException,
153:                    SocketTimeoutException, SecurityException {
154:
155:                byte srvAddrArr[] = null;
156:                InetAddress srvAddr = null;
157:                Socket socket = null;
158:                InputStream iStream = null;
159:                BufferedOutputStream oStream = null;
160:                byte tmpArr[] = new byte[2];
161:                int inLen;
162:                int actualLen = -1;
163:
164:                try {
165:                    srvAddrArr = ProviderMgr.parseIpStr(server);
166:                } catch (IllegalArgumentException e) {
167:                    // jndi.40=Unable to connect: bad IP address
168:                    throw new DomainProtocolException(Messages
169:                            .getString("jndi.40")); //$NON-NLS-1$
170:                }
171:                try {
172:                    srvAddr = InetAddress.getByAddress(srvAddrArr);
173:                    socket = new Socket(srvAddr, serverPort);
174:                    socket.setSoTimeout(timeout);
175:                    oStream = new BufferedOutputStream(socket.getOutputStream());
176:                    ProviderMgr.write16Int(outBufLen, tmpArr, 0);
177:                    oStream.write(tmpArr, 0, 2);
178:                    oStream.write(outBuf, 0, outBufLen);
179:                    oStream.flush();
180:                    iStream = socket.getInputStream();
181:                    iStream.read(tmpArr, 0, 2);
182:                    inLen = ProviderMgr.parse16Int(tmpArr, 0);
183:                    if (inLen > inBufLen) {
184:                        // jndi.43=Output buffer is too small
185:                        throw new DomainProtocolException(Messages
186:                                .getString("jndi.43")); //$NON-NLS-1$
187:                    }
188:                    actualLen = iStream.read(inBuf, 0, inLen);
189:                    if (actualLen != inLen) {
190:                        // jndi.44=Error while receiving message over TCP
191:                        throw new DomainProtocolException(Messages
192:                                .getString("jndi.44")); //$NON-NLS-1$
193:                    }
194:                } catch (IllegalStateException e) {
195:                    // jndi.41=Error while querying DNS server
196:                    throw new DomainProtocolException(Messages
197:                            .getString("jndi.41"), e); //$NON-NLS-1$
198:                } catch (SocketTimeoutException e) {
199:                    throw (e);
200:                } catch (IOException e) {
201:                    // jndi.41=Error while querying DNS server
202:                    throw new DomainProtocolException(Messages
203:                            .getString("jndi.41"), e); //$NON-NLS-1$
204:                } finally {
205:                    if (socket != null && !socket.isClosed()) {
206:                        try {
207:                            socket.close();
208:                        } catch (IOException e) {
209:                        }
210:                    }
211:                }
212:                return actualLen;
213:            }
214:
215:            /**
216:             * Tries to determine IP address by hostname using
217:             * <code>Inet4Address.getByName(String)</code> method.
218:             * 
219:             * @return determined address or <code>null</code> if not found
220:             * @see java.net.InetAddress#getByName(String)
221:             */
222:            public static InetAddress getIPByName_OS(String hostname) {
223:                InetAddress addr = null;
224:
225:                try {
226:                    addr = InetAddress.getByName(hostname);
227:                } catch (UnknownHostException e) {
228:                    // ignore it
229:                }
230:                return addr;
231:            }
232:
233:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.