1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.mchaplin.ioc.rmi;
20
21
22 import java.net.InetAddress;
23 import java.net.NetworkInterface;
24 import java.net.SocketException;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.HashSet;
28 import java.util.Iterator;
29
30 import net.mchaplin.ioc.ContainerI;
31 import net.mchaplin.ioc.component.ComponentFactoryI;
32 import net.mchaplin.ioc.component.ComponentI;
33 import net.mchaplin.ioc.component.DefaultComponentFactory;
34 import net.mchaplin.ioc.model.Component;
35 import net.mchaplin.ioc.model.Container;
36
37
38 /***
39 *
40 * Use the 'Hollywood Principle : don't call me, i'll call you
41 *
42 * @author mchaplin@users.sourceforge.net
43 *
44 */
45 public class RmiServerFactory extends DefaultComponentFactory implements ComponentFactoryI {
46
47 private Container context = null;
48 private RmiServer instance = null;
49 private RmiServerConfiguration config = null;
50
51 public RmiServerFactory(Container ctx, ContainerI aContainer) {
52 super(aContainer);
53 context = ctx;
54 }
55
56 public ComponentI retrieveInstance() {
57 if (this.instance == null) {
58 config = this.configure();
59 instance = this.createInstance(config);
60 }
61 return instance;
62 }
63
64 /***
65 *
66 * @param config
67 * @return
68 */
69 private RmiServer createInstance(RmiServerConfiguration config) {
70 instance = new RmiServer((RmiServerConfiguration) config, container);
71 return instance;
72 }
73
74 /***
75 * Read configuration from PersistentConfiguration
76 * and values an RmiServerConfiguration instance
77 *
78 * @param config
79 * @return
80 */
81 private RmiServerConfiguration configure() {
82
83 Component component = null;
84 ArrayList rmiServices = new ArrayList();
85 int i=0;
86 config = new RmiServerConfiguration();
87
88 Iterator it = context.getComponents().iterator();
89 while (it.hasNext()) {
90 component = (Component) it.next();
91 if (component.getRmiClassname() != null
92 && !component.getRmiClassname().equals("")
93 && component.getLoadOnStartup().equals("true")) {
94
95 rmiServices.add(component.getRmiClassname());
96 }
97 i++;
98
99 }
100 config.setRmiServices(rmiServices);
101
102 String[] network = getNetworkConfiguration();
103 config.setHostAddress(network[0]);
104 config.setHostPort(Integer.parseInt(network[1]));
105 return config;
106 }
107
108 private String[] getNetworkConfiguration() {
109
110 Enumeration enm = null;
111 Enumeration enm2 = null;
112 NetworkInterface netInt = null;
113 InetAddress netAddress = null;
114 HashSet hostName = new HashSet();
115 HashSet allListenAddress = new HashSet();
116 String[] ret = null;
117
118 try {
119 enm = NetworkInterface.getNetworkInterfaces();
120 } catch (SocketException e) {
121 log.error("Socket Exception occurdd while retrieving network configuration");
122 e.printStackTrace();
123 }
124
125 while (enm.hasMoreElements()) {
126 netInt = (NetworkInterface) enm.nextElement();
127 enm2 = netInt.getInetAddresses();
128
129 while (enm2.hasMoreElements()) {
130 netAddress = (InetAddress) enm2.nextElement();
131 if (!netAddress.getHostName().equals("localhost")
132 && !hostName.contains(netAddress.getHostName())) {
133 hostName.add(netAddress.getHostName());
134 }
135 if (!netAddress.getHostAddress().equals("127.0.0.1")
136 && !hostName.contains(netAddress.getHostAddress())) {
137 allListenAddress.add(netAddress.getHostAddress());
138 }
139 }
140 }
141
142
143
144 ret = new String[] {(String) allListenAddress.iterator().next(), "2099"};
145 return ret;
146 }
147
148
149
150
151 public ContainerI getContainer() {
152
153 return null;
154 }
155
156
157
158
159 public void reset() {
160
161
162 }
163
164
165
166
167 public void setContainer(ContainerI container) {
168
169
170 }
171
172
173 }