KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > nonssl > RMIMultiHomedServerSocketFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * RMIMultiHomedServerSocketFactory.java
26  * Indentation Information:
27  * 0. Please (try to) preserve these settings.
28  * 1. No tabs are used, all spaces.
29  * 2. In vi/vim -
30  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
31  * 3. In S1 Studio -
32  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
33  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = True.
34  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
35  * Unit Testing Information:
36  * 0. Is Standard Unit Test Written (y/n):
37  * 1. Unit Test Location: (The instructions should be in the Unit Test Class itself).
38  */

39
40 package com.sun.enterprise.admin.server.core.jmx.nonssl;
41
42 import com.sun.enterprise.config.serverbeans.AdminService;
43 import com.sun.enterprise.config.ConfigContext;
44 import com.sun.enterprise.config.ConfigException;
45 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
46 import com.sun.enterprise.config.serverbeans.Config;
47 import com.sun.enterprise.config.serverbeans.JmxConnector;
48 import com.sun.enterprise.server.ApplicationServer;
49 import com.sun.enterprise.server.ServerContext;
50 import java.rmi.server.RMIServerSocketFactory JavaDoc;
51 import java.net.ServerSocket JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.net.InetAddress JavaDoc;
54 import java.net.UnknownHostException JavaDoc;
55 import java.rmi.server.RMIServerSocketFactory JavaDoc;
56 import java.security.SecureRandom JavaDoc;
57 import javax.management.remote.JMXConnector JavaDoc;
58
59 /** This is the custom RMI server socket factory that helps bind RMI servers to a specific
60  * IP interface as has been specified in the domain->config->admin-service->jmx-connector->address
61  * in domain.xml. In the absence of a specific entry, the default RMI behavior of binding to all
62  * IP interfaces on the host is observed.
63  * @author Nandini.Ektare@sun.com
64  * @since Sun Java System Application Server 8.1 UR 2
65  */

66 public class RMIMultiHomedServerSocketFactory implements RMIServerSocketFactory JavaDoc {
67
68     private static final String JavaDoc DEFAULT_ADDRESS = "0.0.0.0";
69     
70     private String JavaDoc address = DEFAULT_ADDRESS;
71
72     public RMIMultiHomedServerSocketFactory(String JavaDoc host) {
73     address = host;
74         /*ServerContext serverCtx = ApplicationServer.getServerContext();
75     if (serverCtx != null) {
76         try {
77                 ConfigContext configCtx = serverCtx.getConfigContext();
78                 Config config = ServerBeansFactory.getConfigBean(configCtx);
79                 AdminService as = config.getAdminService();
80                 JmxConnector[] jc = as.getJmxConnector();
81                 address = jc[0].getAddress();
82             } catch (ConfigException ex) {
83                 return null;
84             }
85     }*/

86     }
87     
88     /** Implementation of the only method in {@link RMIServerSocketFactory}. This
89      * method is called for creating the server socket.
90      * @return instance of ServerSocket
91      */

92     public ServerSocket JavaDoc createServerSocket(int port) throws IOException JavaDoc {
93         try {
94             InetAddress JavaDoc bindAddress = null;
95             ServerSocket JavaDoc ss = null;
96             if (address.equals(DEFAULT_ADDRESS))
97                 ss = new ServerSocket JavaDoc(port);
98             else {
99                 bindAddress = InetAddress.getByName(address);
100                 ss = new ServerSocket JavaDoc(port, 0, bindAddress);
101             }
102             debug(ss);
103             return (ss);
104         } catch (Exception JavaDoc e) {
105             throw new IOException JavaDoc(e.getMessage());
106         }
107     }
108     
109     /**
110      * Overriding the base class method here to ensure that when a functionally
111      * equivalent instance of server socket factory is passed, the same factory
112      * is reused instead of creating new objects
113      */

114     public boolean equals(Object JavaDoc anotherFactory) {
115         
116         if (anotherFactory != null &&
117             anotherFactory.getClass().equals(this.getClass())) {
118     
119             RMIMultiHomedServerSocketFactory rmhssf =
120                 (RMIMultiHomedServerSocketFactory) anotherFactory;
121     
122             if (this.address == null && rmhssf.address == null) return true;
123             if (this.address == null ^ rmhssf.address == null) return false;
124         return this.address.equals(rmhssf.address);
125         }
126         return false;
127     }
128     
129     private void debug (ServerSocket JavaDoc sss) {
130         // prints the debug information - suppress after beta
131
String JavaDoc prefix = "RMI/TLS Server Debug Message: " ;
132         boolean DEBUG = Boolean.getBoolean("Debug");
133         if (sss != null) {
134             if (DEBUG) {
135                 System.out.println(prefix + "ServerSocket local port = " + sss.getLocalPort());
136                 System.out.println(prefix + "ServerSocket host address = " + sss.getInetAddress().getHostAddress());
137                 System.out.println(prefix + "ServerSocket bound status = " + sss.isBound());
138             }
139         }
140         else {
141             System.out.println(prefix + " Catastrophe: no server socket");
142         }
143     }
144 }
145
Popular Tags