KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3  */

4
5 package org.jfox.ioc.connector;
6
7 import java.io.IOException JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.jfox.ioc.util.Marshaller;
14
15 /**
16  * 表示当前节点
17  * 在一个JVM中,应该为一单例对象
18  * 但是因为需要通过JGroup序列化,所以不能简单通过static实现单例
19  * 需要使用该对象的必须直接从ServerNode.THE_NODE 引用
20  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
21  */

22
23 public class ServerNode implements Serializable JavaDoc {
24
25     public final static transient ServerNode THE_NODE = new ServerNode();
26
27     /**
28      * 该节点的 ip 地址
29      */

30     private String JavaDoc ip;
31
32     /**
33      * 该节点的机器名
34      */

35     private String JavaDoc hostName;
36
37     /**
38      * 该节点的接入服务的端口
39      */

40     private int port;
41
42     /**
43      * jgroup port
44      */

45     private int jgport;
46
47     /**
48      * 节点中 protocol=>ConnectorRemote的对应表
49      */

50     private transient Map JavaDoc<String JavaDoc, ConnectorRemote> connectors = new HashMap JavaDoc<String JavaDoc, ConnectorRemote>();
51
52     public ServerNode() {
53     }
54
55     public void setIp(String JavaDoc ip) {
56         this.ip = ip;
57     }
58
59     public String JavaDoc getIp() {
60         return ip;
61     }
62
63     public void setHostName(String JavaDoc hostName) {
64         this.hostName = hostName;
65     }
66
67     public String JavaDoc getHostName() {
68         return hostName;
69     }
70
71     public void setPort(int port) {
72         this.port = port;
73     }
74
75     public int getPort() {
76         return port;
77     }
78
79     public int getJgport() {
80         return jgport;
81     }
82
83     public void setJgport(int jgport) {
84         this.jgport = jgport;
85     }
86
87     /**
88      *
89      * @param connectors
90      */

91     public void setConnectors(Map JavaDoc<String JavaDoc, ConnectorRemote> connectors){
92         this.connectors = connectors;
93     }
94
95     public ConnectorRemote getConnector(String JavaDoc protocol) {
96         return connectors.get(protocol.trim().toUpperCase());
97     }
98
99     public String JavaDoc toString() {
100         return ip + ":" + port;
101     }
102
103     public boolean equals(Object JavaDoc o) {
104         if(this == o) return true;
105         if(o == null || getClass() != o.getClass()) return false;
106
107         final ServerNode that = (ServerNode) o;
108
109         if(jgport != that.jgport) return false;
110         if(port != that.port) return false;
111         if(!ip.equals(that.ip)) return false;
112
113         return true;
114     }
115
116     public int hashCode() {
117         int result;
118         result = ip.hashCode();
119         result = 29 * result + port;
120         result = 29 * result + jgport;
121         return result;
122     }
123
124     public void registerConnector(String JavaDoc protocol, ConnectorRemote remote) {
125 // MarshalledObject mobj = Marshaller.marshall(remote);
126
connectors.put(protocol.trim().toUpperCase(), remote);
127     }
128
129     public void unregisterConnector(String JavaDoc protocol) {
130         connectors.remove(protocol.trim().toUpperCase());
131     }
132
133     public Map JavaDoc<String JavaDoc, ConnectorRemote> listConnectors() {
134         return Collections.unmodifiableMap(connectors);
135     }
136
137     public void removeConnector(String JavaDoc protocol) {
138         connectors.remove(protocol);
139     }
140
141     public Map JavaDoc<String JavaDoc, ConnectorRemote> getConnectors() {
142         return connectors;
143     }
144
145     private void writeObject(java.io.ObjectOutputStream JavaDoc out)
146             throws IOException JavaDoc {
147         out.defaultWriteObject();
148         out.writeInt(connectors.size());
149         for(Map.Entry JavaDoc<String JavaDoc,ConnectorRemote> entry : connectors.entrySet()){
150             out.writeObject(entry.getKey());
151             out.writeObject(Marshaller.marshall(entry.getValue()));
152         }
153     }
154
155     private void readObject(java.io.ObjectInputStream JavaDoc in)
156             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
157         in.defaultReadObject();
158         int size = in.readInt();
159         connectors = new HashMap JavaDoc<String JavaDoc,ConnectorRemote>(size);
160         while(size-- > 0){
161             String JavaDoc key = (String JavaDoc)in.readObject();
162             ConnectorRemote remote = (ConnectorRemote)Marshaller.unmarshall(in.readObject());
163             connectors.put(key,remote);
164         }
165
166     }
167
168     public static void main(String JavaDoc[] args) {
169
170     }
171 }
172
Popular Tags