KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > connector > ConnectorServer


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.connector;
8
9 import java.io.BufferedOutputStream JavaDoc;
10 import java.io.BufferedReader JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStreamReader JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.net.ServerSocket JavaDoc;
15 import java.net.Socket JavaDoc;
16 import java.net.SocketException JavaDoc;
17 import java.net.InetAddress JavaDoc;
18 import java.rmi.MarshalledObject JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.concurrent.ExecutorService JavaDoc;
22 import java.util.concurrent.Executors JavaDoc;
23
24 import org.jfox.ioc.common.AbstractService;
25 import org.jfox.ioc.ext.ActiveComponent;
26 import org.jfox.ioc.ext.SingletonComponent;
27 import org.jfox.ioc.util.Marshaller;
28
29 /**
30  * 用来启动 1099 端口,用来发送 ConnectorService stub
31  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
32  */

33
34 public class ConnectorServer extends AbstractService implements ActiveComponent, SingletonComponent {
35     /**
36      * ConnectorServer的端口号,用于发送 connector stub
37      */

38     protected static int DEFAULT_PORT = 1099;
39
40     protected ServerSocket JavaDoc ssocket = null;
41
42     /**
43      * 当前节点
44      */

45     public static final ServerNode theNode = ServerNode.THE_NODE;
46
47     /**
48      * 对ClusterServer的引用,如果启动了Cluste Server,要对 ConnectorRemote 进行包装
49      */

50     protected static ClusterServer clusterServer = null;
51
52     private ExecutorService JavaDoc pool = Executors.newCachedThreadPool();
53
54     public ConnectorServer() throws Exception JavaDoc {
55         this(DEFAULT_PORT);
56     }
57
58     public ConnectorServer(int port) throws Exception JavaDoc {
59         InetAddress JavaDoc address = InetAddress.getLocalHost();
60         String JavaDoc ip = address.getHostAddress();
61         String JavaDoc hostName = address.getCanonicalHostName();
62         theNode.setIp(ip);
63         theNode.setPort(port);
64         theNode.setHostName(hostName);
65     }
66
67     public int getPort() {
68         return theNode.getPort();
69     }
70
71     public void setPort(int port) {
72         theNode.setPort(port);
73     }
74
75     protected void doStart() throws Exception JavaDoc {
76         if(context.getRegistry().isRegisteredImplemetation(ClusterServer.class)) {
77             // 注册了集群服务组件,启动了集群功能
78
try {
79                 clusterServer = (ClusterServer) context.getRegistry().getComponentInstance(ClusterServer.class);
80             }
81             catch(Exception JavaDoc e) {
82                 e.printStackTrace();
83             }
84         }
85 // theNode.setConnectors(connectorManager.getConnectors());
86
Thread JavaDoc t = new Thread JavaDoc(this, this.getClass().getName());
87         t.start();
88         Thread.sleep(50);
89 // t.join();
90
}
91
92     public static ClusterServer getClusterServer() {
93         return clusterServer;
94     }
95
96     protected void doStop() throws Exception JavaDoc {
97         ssocket.close();
98     }
99
100     protected void doInit() throws Exception JavaDoc {
101         ssocket = new ServerSocket JavaDoc(theNode.getPort());
102         ssocket.setReuseAddress(true);
103     }
104
105     protected void doDestroy() throws Exception JavaDoc {
106         ssocket = null;
107     }
108
109     public void run() {
110         while(isStarted()) {
111             Socket JavaDoc socket = null;
112             try {
113                 socket = ssocket.accept();
114                 socket.setReuseAddress(true);
115             }
116             catch(SocketException JavaDoc e) {
117 // e.printStackTrace();
118
}
119             catch(IOException JavaDoc e){
120                 e.printStackTrace();
121             }
122             if(socket != null) {
123                 pool.execute(new Sender(socket));
124             }
125         }
126     }
127
128     protected ConnectorRemote getConnectorRemote(String JavaDoc protocol){
129         ConnectorRemote remote = theNode.getConnector(protocol);
130         if(clusterServer != null){
131             // 注册了集群服务组件,启动了集群功能
132
try {
133                 List JavaDoc<ServerNode> nodes = clusterServer.getClusterNodes();
134                 List JavaDoc<ConnectorRemote> remotes = new ArrayList JavaDoc<ConnectorRemote>(nodes.size());
135                 for(ServerNode node : nodes ){
136                     remotes.add(node.getConnector(protocol));
137                 }
138                 remote = new ClusterableConnectorRemote(remote,remotes.toArray(new ConnectorRemote[remotes.size()]));
139             }
140             catch(Exception JavaDoc e){
141                 e.printStackTrace();
142             }
143         }
144         return remote;
145     }
146
147     private class Sender implements Runnable JavaDoc {
148         private Socket JavaDoc socket;
149
150         public Sender(Socket JavaDoc socket) {
151             this.socket = socket;
152         }
153
154         public void run() {
155             try {
156                 BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc((socket.getInputStream())));
157                 String JavaDoc protocol = in.readLine();
158                 ConnectorRemote remote = getConnectorRemote(protocol);
159                 MarshalledObject JavaDoc mobj = Marshaller.marshall(remote);
160                 ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(socket.getOutputStream()));
161                 out.writeObject(mobj);
162                 out.flush();
163                 socket.close();
164             }
165             catch(Exception JavaDoc e) {
166                 logger.warn(e.getMessage(), e);
167             }
168         }
169     }
170
171     public static void main(String JavaDoc[] args) {
172
173     }
174 }
175
Popular Tags