View Javadoc
1   /*
2    * This program is free software; you can redistribute it and/or
3    * modify it under the terms of the GNU General Public License
4    * as published by the Free Software Foundation; either version 2
5    * of the License, or (at your option) any later version.
6    *
7    * This program is distributed in the hope that it will be useful,
8    * but WITHOUT ANY WARRANTY; without even the implied warranty of
9    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10   * GNU General Public License for more details.
11   * 
12   * Full GNU GPL license terms : http://www.fsf.org/licenses/gpl.txt
13   * 
14   * Author : mchaplin@users.sourceforge.net
15   * 
16   * Created on Apr 26, 2004
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          // Iterate components looking for rmiClassName attribute
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         // Discover network configuration at runtime
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         // For each network interface
125         while (enm.hasMoreElements()) {
126             netInt = (NetworkInterface) enm.nextElement();
127             enm2 = netInt.getInetAddresses();
128             // Retrieve IP addresses
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         // FIXME optimize
142         
143         // TODO check if port not bound
144         ret = new String[] {(String) allListenAddress.iterator().next(), "2099"};
145         return ret;
146     }
147 
148     /* (non-Javadoc)
149      * @see net.wmind.container.component.ComponentI#getContainer()
150      */
151     public ContainerI getContainer() {
152         // TODO Auto-generated method stub
153         return null;
154     }
155 
156     /* (non-Javadoc)
157      * @see net.wmind.container.component.ComponentI#reset()
158      */
159     public void reset() {
160         // TODO Auto-generated method stub
161         
162     }
163 
164     /* (non-Javadoc)
165      * @see net.wmind.container.component.ComponentI#setContainer(net.wmind.container.ContainerI)
166      */
167     public void setContainer(ContainerI container) {
168         // TODO Auto-generated method stub
169         
170     }
171     
172     
173 }