KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > connection > impl > GenericControlConnector


1 package com.coldcore.coloradoftp.connection.impl;
2
3 import com.coldcore.coloradoftp.command.Command;
4 import com.coldcore.coloradoftp.command.CommandProcessor;
5 import com.coldcore.coloradoftp.connection.ConnectionPool;
6 import com.coldcore.coloradoftp.connection.ControlConnection;
7 import com.coldcore.coloradoftp.connection.ControlConnector;
8 import com.coldcore.coloradoftp.core.Core;
9 import com.coldcore.coloradoftp.core.CoreStatus;
10 import com.coldcore.coloradoftp.factory.ObjectFactory;
11 import com.coldcore.coloradoftp.factory.ObjectName;
12 import org.apache.log4j.Logger;
13
14 import java.io.IOException JavaDoc;
15 import java.net.InetSocketAddress JavaDoc;
16 import java.nio.channels.ServerSocketChannel JavaDoc;
17 import java.nio.channels.SocketChannel JavaDoc;
18
19 /**
20  * @see com.coldcore.coloradoftp.connection.ControlConnector
21  */

22 public class GenericControlConnector implements ControlConnector, Runnable JavaDoc {
23
24   private static Logger log = Logger.getLogger(GenericControlConnector.class);
25   protected ServerSocketChannel JavaDoc ssc;
26   protected int port;
27   protected boolean bound;
28   protected Core core;
29   protected CommandProcessor commandProcessor;
30   protected ConnectionPool controlConnectionPool;
31   protected Thread JavaDoc thr;
32   protected long sleep;
33
34
35   public GenericControlConnector() {
36     port = 21;
37     sleep = 100L;
38   }
39
40
41   /** Configure connection before adding it to a pool
42    * @param connection Connection
43    */

44   public void configure(ControlConnection connection) {
45     if (core.getStatus() == CoreStatus.POISONED) {
46       //Server is shutting down, reply and poison the connection
47
Command command = (Command) ObjectFactory.getObject(ObjectName.COMMAND_POISONED);
48       command.setConnection(connection);
49       commandProcessor.execute(command);
50       connection.poison();
51     } else {
52       //Server is ready, reply so
53
Command command = (Command) ObjectFactory.getObject(ObjectName.COMMAND_WELCOME);
54       command.setConnection(connection);
55       commandProcessor.execute(command);
56     }
57   }
58
59
60   public synchronized void bind() throws IOException JavaDoc {
61     if (bound) {
62       log.warn("Connector on port "+port+" was bound when bind routine was submitted");
63       throw new IllegalStateException JavaDoc("Unbind the connector on port "+port+" first");
64     }
65
66     //Get required objects
67
core = (Core) ObjectFactory.getObject(ObjectName.CORE);
68     commandProcessor = (CommandProcessor) ObjectFactory.getObject(ObjectName.COMMAND_PROCESSOR);
69     controlConnectionPool = (ConnectionPool) ObjectFactory.getObject(ObjectName.CONTROL_CONNECTION_POOL);
70
71     //Bind to the port
72
ssc = ServerSocketChannel.open();
73     ssc.socket().bind(new InetSocketAddress JavaDoc(port));
74
75     //Start this class
76
thr = new Thread JavaDoc(this);
77     thr.start();
78
79     bound = true;
80     log.info("Connector is bound to port "+port);
81   }
82
83
84   public synchronized void unbind() throws IOException JavaDoc {
85     if (!bound) {
86       log.warn("Connector on port "+port+" was not bound when unbind routine was submitted");
87       throw new IllegalStateException JavaDoc("Cannot unbind the connector on port "+port+", it is not bound");
88     }
89
90     //Unbind from the port
91
if (ssc.isOpen()) ssc.close();
92
93     //Wait for this class to stop (just in case)
94
try {
95       thr.join(30000);
96     } catch (Throwable JavaDoc e) {}
97
98     bound = false;
99     log.info("Connector on port "+port+" is unbound");
100   }
101
102
103   public boolean isBound() {
104     return bound;
105   }
106
107
108   public void run() {
109     while (bound) {
110
111       ControlConnection connection = null;
112       SocketChannel JavaDoc sc = null;
113       try {
114         sc = ssc.accept(); //Thread blocks here...
115
String JavaDoc ip = sc.socket().getInetAddress().getHostAddress();
116         log.debug("New control connection accepted (IP "+ip+")");
117
118         //Create new connection instance and initialize it
119
connection = (ControlConnection) ObjectFactory.getObject(ObjectName.CONTROL_CONNECTION);
120         connection.initialize(sc);
121
122         //Configure the control connection and add to pool
123
configure(connection);
124         controlConnectionPool.add(connection);
125         log.debug("New control connection is ready");
126
127         Thread.sleep(sleep);
128
129       } catch (Throwable JavaDoc e) {
130         log.warn("Failed to accept a connection (ignoring)", e);
131         try {
132           connection.destroy();
133         } catch (Throwable JavaDoc ex) {}
134         try {
135           sc.close();
136         } catch (Throwable JavaDoc ex) {}
137       }
138
139     }
140     log.debug("Control connector thread finished");
141   }
142
143
144   public void setPort(int port) {
145     if (port < 1) throw new IllegalArgumentException JavaDoc("Invalid port");
146     this.port = port;
147   }
148
149
150   public int getPort() {
151     return port;
152   }
153
154
155   /** Get thread sleep time
156    * @return Time in mills
157    */

158   public long getSleep() {
159     return sleep;
160   }
161
162
163   /** Set thread sleep time
164    * @param sleep Time in mills
165    */

166   public void setSleep(long sleep) {
167     this.sleep = sleep;
168   }
169 }
170
Popular Tags