Source Code Cross Referenced for GenericDataConnectionInitiator.java in  » Net » ColoradoFTP » com » coldcore » coloradoftp » connection » impl » 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 » ColoradoFTP » com.coldcore.coloradoftp.connection.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.coldcore.coloradoftp.connection.impl;
002:
003:        import com.coldcore.coloradoftp.command.Reply;
004:        import com.coldcore.coloradoftp.connection.ConnectionPool;
005:        import com.coldcore.coloradoftp.connection.ControlConnection;
006:        import com.coldcore.coloradoftp.connection.DataConnection;
007:        import com.coldcore.coloradoftp.connection.DataConnectionInitiator;
008:        import com.coldcore.coloradoftp.factory.ObjectFactory;
009:        import com.coldcore.coloradoftp.factory.ObjectName;
010:        import com.coldcore.coloradoftp.session.Session;
011:        import com.coldcore.coloradoftp.session.SessionAttributeName;
012:        import org.apache.log4j.Logger;
013:
014:        import java.net.InetSocketAddress;
015:        import java.nio.channels.SocketChannel;
016:
017:        /**
018:         * @see com.coldcore.coloradoftp.connection.DataConnectionInitiator
019:         */
020:        public class GenericDataConnectionInitiator implements 
021:                DataConnectionInitiator, Runnable {
022:
023:            private static Logger log = Logger
024:                    .getLogger(GenericDataConnectionInitiator.class);
025:            protected String ip;
026:            protected int port;
027:            protected boolean active;
028:            protected ControlConnection controlConnection;
029:            protected ConnectionPool dataConnectionPool;
030:            protected SocketChannel sc;
031:            protected Reply errorReply;
032:            protected Thread thr;
033:            protected long sleep;
034:            protected boolean aborted;
035:
036:            public GenericDataConnectionInitiator() {
037:                sleep = 100L;
038:            }
039:
040:            protected Reply getErrorReply() {
041:                if (errorReply == null) {
042:                    errorReply = (Reply) ObjectFactory
043:                            .getObject(ObjectName.REPLY);
044:                    errorReply.setCode("425");
045:                    errorReply.setText("Can't open data connection.");
046:                }
047:                return errorReply;
048:            }
049:
050:            /** Get thread sleep time
051:             * @return Time in mills
052:             */
053:            public long getSleep() {
054:                return sleep;
055:            }
056:
057:            /** Set thread sleep time
058:             * @param sleep Time in mills
059:             */
060:            public void setSleep(long sleep) {
061:                this .sleep = sleep;
062:            }
063:
064:            /** Test if user got a "150" reply
065:             * @return TRUE if user got the reply, FALSE if not yet
066:             */
067:            protected boolean isReply150() {
068:                Session session = controlConnection.getSession();
069:                Long bytesWrote = (Long) session
070:                        .getAttribute(SessionAttributeName.BYTE_MARKER_150_REPLY);
071:                if (bytesWrote == null
072:                        || controlConnection.getBytesWrote() == bytesWrote
073:                        || controlConnection.getOutgoingBufferSize() != 0)
074:                    return false;
075:                log.debug("User got a 150 reply");
076:                return true;
077:            }
078:
079:            public String getIp() {
080:                return ip;
081:            }
082:
083:            public void setIp(String ip) {
084:                this .ip = ip;
085:            }
086:
087:            public int getPort() {
088:                return port;
089:            }
090:
091:            public void setPort(int port) {
092:                this .port = port;
093:            }
094:
095:            public void run() {
096:                while (active) {
097:
098:                    DataConnection dataConnection = null;
099:                    try {
100:
101:                        /* We cannot open the socket yet. We must wait until user receives the positive "150" reply.
102:                         * The reply might not be in the buffer of the control connection just yet.
103:                         */
104:                        if (!isReply150()) {
105:                            Thread.sleep(sleep);
106:                            continue;
107:                        }
108:
109:                        //Get required objects
110:                        dataConnectionPool = (ConnectionPool) ObjectFactory
111:                                .getObject(ObjectName.DATA_CONNECTION_POOL);
112:
113:                        //Configure socket and connect
114:                        sc = SocketChannel.open();
115:                        sc.connect(new InetSocketAddress(ip, port)); //Thread blocks here...
116:                        if (!sc.finishConnect())
117:                            throw new RuntimeException("Failed finishConnect");
118:                        String ip = sc.socket().getInetAddress()
119:                                .getHostAddress();
120:                        log.debug("New data connection established (IP " + ip
121:                                + ")");
122:
123:                        //Create new connection instance
124:                        dataConnection = (DataConnection) ObjectFactory
125:                                .getObject(ObjectName.DATA_CONNECTION);
126:                        dataConnection.initialize(sc);
127:
128:                        //If there is a data connection already then kill it
129:                        DataConnection existing = controlConnection
130:                                .getDataConnection();
131:                        if (existing != null && !existing.isDestroyed()) {
132:                            log
133:                                    .warn("BUG: Replacing existing data connection with a new one!");
134:                            existing.destroyNoReply();
135:                        }
136:
137:                        //Configure the data connection and wire it with the control connection and add to pool
138:                        controlConnection.setDataConnection(dataConnection);
139:                        dataConnection.setControlConnection(controlConnection);
140:                        configure(dataConnection);
141:                        dataConnectionPool.add(dataConnection);
142:                        log.debug("New data connection is ready");
143:
144:                        active = false;
145:
146:                    } catch (Throwable e) {
147:
148:                        //If aborted then do not post an error message
149:                        if (!aborted) {
150:                            log.warn("Failed to establish a connection with "
151:                                    + ip + ":" + port + " (ignoring)", e);
152:                            try {
153:                                dataConnection.destroyNoReply();
154:                            } catch (Throwable ex) {
155:                            }
156:                            try {
157:                                sc.close();
158:                            } catch (Throwable ex) {
159:                                log.error(
160:                                        "Cannot close the channel (ignoring)",
161:                                        e);
162:                            }
163:
164:                            controlConnection.reply(getErrorReply());
165:                        }
166:
167:                        active = false;
168:                    }
169:
170:                }
171:                log.debug("Data connection initiator thread finished");
172:            }
173:
174:            public boolean isActive() {
175:                return active;
176:            }
177:
178:            public synchronized void activate() {
179:                if (active) {
180:                    log
181:                            .warn("Data connection initiator was active when activate routine was called");
182:                    return;
183:                }
184:
185:                active = true;
186:                aborted = false;
187:
188:                //Start this class
189:                thr = new Thread(this );
190:                thr.start();
191:            }
192:
193:            public synchronized void abort() {
194:                aborted = true;
195:                if (!active)
196:                    return;
197:
198:                //Close the channel
199:                try {
200:                    if (sc != null && sc.isOpen())
201:                        sc.close();
202:                } catch (Throwable e) {
203:                    log.error("Cannot close channel (ignoring)", e);
204:                }
205:
206:                controlConnection.reply(getErrorReply());
207:
208:                //Clear the attribute to prevent misuse by future instances
209:                Session session = controlConnection.getSession();
210:                session
211:                        .removeAttribute(SessionAttributeName.BYTE_MARKER_150_REPLY);
212:
213:                active = false;
214:            }
215:
216:            public ControlConnection getControlConnection() {
217:                return controlConnection;
218:            }
219:
220:            public void setControlConnection(ControlConnection controlConnection) {
221:                this .controlConnection = controlConnection;
222:            }
223:
224:            /** Configure connection before adding it to a pool
225:             * @param connection Connection
226:             */
227:            public void configure(DataConnection connection) {
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.