KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > verifier > tests > HttpListenerTest


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  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31
32 package com.sun.enterprise.admin.verifier.tests;
33
34 /* Test case to check the validity of Http listener fields
35  * Author : srini@sun.com
36  */

37
38 import java.net.*;
39 import java.util.StringTokenizer JavaDoc;
40
41 // 8.0 XML Verifier
42
//import com.sun.enterprise.tools.verifier.Result;
43
import com.sun.enterprise.config.serverbeans.Server;
44 import com.sun.enterprise.config.serverbeans.*;
45 import com.sun.enterprise.config.serverbeans.Resources;
46 import com.sun.enterprise.config.serverbeans.Applications;
47 import com.sun.enterprise.config.ConfigContext;
48 import com.sun.enterprise.config.ConfigException;
49 import com.sun.enterprise.config.serverbeans.*;
50 import com.sun.enterprise.config.ConfigContextEvent;
51 import com.sun.enterprise.admin.common.ObjectNames;
52
53 import com.sun.enterprise.admin.verifier.*;
54
55 // Logging
56
import java.util.logging.Logger JavaDoc;
57 import java.util.logging.Level JavaDoc;
58 import com.sun.logging.LogDomains;
59
60
61 public class HttpListenerTest extends ServerXmlTest implements ServerCheck {
62     
63
64      // Logging
65
static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER);
66     private static final String JavaDoc DELIMITER=",";
67     //<addition author="irfan@sun.com" [bug/rfe]-id="4704985" >
68
protected HttpListener thisListener;
69     //</addition>
70

71     public HttpListenerTest() {
72     }
73  
74     // check method invoked by the command line verifier
75
public Result check(ConfigContext context) {
76             Result result;
77             result = super.getInitializedResult();
78             // 8.0 XML Verifier
79
/*try {
80                 Server server = (Server)context.getRootConfigBean();
81                 HttpService http = server.getHttpService();
82                 HttpListener[] httpListener = http.getHttpListener();
83                 String httpPort = null;
84                 String httpAddress = null;
85                 for(int j=0;j<httpListener.length;j++) {
86                         httpPort = httpListener[j].getPort();
87                         httpAddress = httpListener[j].getAddress();
88                         try {
89                             if(StaticTest.isPortValid(Integer.parseInt(httpPort)))
90                                 result.passed("HttpListener Port is valid - " + httpPort );
91                             else
92                                 result.failed("HttpListener Port is Invalid - " + httpPort );
93                         }catch(NumberFormatException e){
94                                result.failed("Invalid Number for Http Listener Port - " + httpPort);
95                         }
96                         try{
97                                // <addition> srini@sun.com Bug : 4697248
98                                if(httpAddress == null || httpAddress.equals("")) {
99                                    result.failed("Http Address cannot be Null");
100                                    return result;
101                                }
102                                if(StaticTest.checkAddress(httpAddress))
103                                // </addition> Bug : 4697248
104                                     InetAddress.getByName(httpAddress).getHostName();
105                                result.passed("Valid Http Listener IP");
106                         }catch(UnknownHostException e){
107                                result.failed("Host name of Http Listener not resolvable - " + httpAddress);
108                         }
109                  }
110
111             }
112             catch(Exception ex) {
113                 // Logging
114                 _logger.log(Level.FINE, "serverxmlverifier.exception", ex);
115                 result.failed("Exception : " + ex.getMessage());
116             }*/

117             return result;
118     }
119     
120     // check method called from the admin GUI and iasadmin
121
public Result check(ConfigContextEvent ccce) {
122         //<addition author="irfan@sun.com" [bug/rfe]-id="4704985" >
123
Result result = new Result();
124         //</addition>
125
Object JavaDoc value = ccce.getObject();
126                 ConfigContext context = ccce.getConfigContext();
127                 //Result result = new Result(); - 4704985
128
String JavaDoc beanName = ccce.getBeanName();
129                 if(beanName!=null) {
130                     String JavaDoc name = ccce.getName();
131                     // Bug: 4700257
132
result = testSave(name, (String JavaDoc)value, context);
133                     return result;
134                 }
135                 HttpListener http = (HttpListener)value;
136                 //<addition author="irfan@sun.com" [bug/rfe]-id="4704985" >
137
thisListener = http;
138                 //</addition>
139
if(ccce.getChoice().equals(StaticTest.DELETE))
140         {
141         try {
142                 Config config = StaticTest.getConfig(context);
143                 if(config != null) {
144                         HttpService service = config.getHttpService();
145                         // 8.0 XML Verifier
146
//Server server = (Server)context.getRootConfigBean();
147
//HttpService service = server.getHttpService();
148
VirtualServer[] vs = service.getVirtualServer();
149
150                         for (int i=0; vs != null && i < vs.length; i++) {
151                             String JavaDoc httpListener = vs[i].getHttpListeners();
152                             if (httpListener != null) {
153                                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(httpListener, DELIMITER);
154                                 StringBuffer JavaDoc newHttpListener = new StringBuffer JavaDoc("");
155                                 String JavaDoc token = "";
156                                 while (st.hasMoreTokens()) {
157                                     token = st.nextToken();
158                                     if (token.equals(thisListener.getId())) {
159                                         result.failed("This listener is being referenced by some virtual server. Please remove the reference and try again.");
160                                         return result;
161                                     }
162                                 }
163                             }
164                         } // end of for
165
result.passed("Test passed for referential integrity");
166                 }
167               } catch (Exception JavaDoc e) {
168                    result.failed("Exception Caught : " + e.getMessage());
169               }
170               return result;
171         }
172                 // check if Http ID is valid object name Bug : 4698687 : start
173
String JavaDoc id = http.getId();
174                 if(StaticTest.checkObjectName(id, result))
175                     result.passed("Valid Object Name");
176                 else {
177                     result.failed("Http Listener ID Invalid ");
178                     return result;
179                 }
180                 // End Bug : 4698687
181

182                 try{
183                      if(StaticTest.isPortValid(Integer.parseInt(http.getPort())))
184                         result.passed("passed ***");
185                      else {
186                         result.failed("Invalid Port Number");
187                         return result;
188                      }
189                      String JavaDoc port = http.getPort();
190                      // <addition> srini@sun.com Bug : 4700257
191
// Checks if any other listeners uses the same port, true if used, false not used
192
//<addition author="irfan@sun.com" [bug/rfe]-id="4704985" >
193
/*if(isPortUsed(port, context,result)) {
194                          result.failed("Port Already In Use");
195                          return result;
196                      }*/

197                      if(ccce.getChoice().equals(StaticTest.ADD) && isPortUsed(port, context,result)) {
198                          return result;
199                      }
200                      // </addition>
201
}
202                 catch(NumberFormatException JavaDoc e){
203                     result.failed("Number Format Exception");
204                     return result;
205                 }
206                 String JavaDoc httpAddress = http.getAddress();
207                 try{
208                     // <addition> srini@sun.com Bug:4697248
209
if(httpAddress == null || httpAddress.equals("")) {
210                           result.failed("Http Address cannot be Null value");
211                           return result;
212                     }
213                     if(StaticTest.checkAddress(httpAddress))
214                     // </addition> Bug:4697248
215
InetAddress.getByName(httpAddress).getHostName();
216                     result.passed("Valid Http Listener IP Address");
217                 }catch(UnknownHostException e){
218                     result.failed("Host name not resolvable - " + httpAddress);
219                     return result;
220                 }
221                 String JavaDoc virtualServer = http.getDefaultVirtualServer();
222                 try {
223                      // 8.0 XML Verifier
224
//Server server = (Server)context.getRootConfigBean();
225
//boolean exists = checkVSExists(virtualServer, server);
226
Config config = StaticTest.getConfig(context);
227                      if( config!=null ) {
228                          boolean exists = checkVSExists(virtualServer, config);
229                          if(exists)
230                              result.passed("Virtual Server found in vs class");
231                          else
232                              result.failed("Virtual Server not found in vs class");
233                      }
234                 }catch(Exception JavaDoc e){
235                      result.failed("Virtual Server specified not available ");
236                 }
237                 return result;
238     }
239     
240     public Result testSave(String JavaDoc name, String JavaDoc value, ConfigContext context) {
241             Result result = new Result();
242             result.passed("Passed ");
243             if(name.equals(ServerTags.ADDRESS)){
244                  try{
245                     // <addition> srini@sun.com Bug:4697248
246
if(value == null || value.equals("")) {
247                         result.failed("Http Address cannot be Null");
248                         return result;
249                     }
250                     if(StaticTest.checkAddress(value))
251                     // <addition> srini@sun.com Bug:4697248
252
InetAddress.getByName(value).getHostName();
253                     result.passed("Valid Http Listener IP Address");
254                  }catch(UnknownHostException e){
255                     result.failed("Host name not resolvable - " + value);
256                  }
257             }
258             if(name.equals(ServerTags.PORT)){
259                 try{
260                      if(StaticTest.isPortValid(Integer.parseInt(value)))
261                         result.passed("passed ***");
262                      else
263                         result.failed("Invalid Port Number");
264                      // <addition> srini@sun.com Bug : 4700257
265
// Checks if any other listeners uses the same port, true if used, false not used
266
//<addition author="irfan@sun.com" [bug/rfe]-id="4704985" >
267
/* Removing the port duplicacy check as it is impossible to check for
268                       * duplicate port using a single algo for the paths through asadmin,
269                       * admin GUI and Forte. This requires a change in the flow of code
270                       * Refer BugTraq for more info on this
271                       *
272                      if( value!=null ) {
273                              if(isPortUsed(value, context)) {
274                                  result.failed("Port Already In Use");
275                                  return result;
276                              }
277                      }*/

278                      // </addition>
279
}
280                 catch(NumberFormatException JavaDoc e){
281                     result.failed("Number Format Exception");
282                 }
283             }
284             return result;
285     }
286     
287     /**
288         Checks whether a virtual server with given id is available in the given
289         server. Current hierarchy is http-service has many virtual-servers. An Http lsnr
290         can have any one of these virtual servers as its default-virtual-server.
291         @param vsID String representing the id of vs specified
292         @param server ConfigBean representing the server.xml
293         @return true if and only if the given vsID exists in given Server,
294         false otherwise
295     */

296     private boolean checkVSExists(String JavaDoc vsID, Config config) {
297         VirtualServer vs = config.getHttpService().getVirtualServerById(vsID);
298         if(vs != null)
299         return true;
300         return false;
301     }
302     
303     // Added to check if port is already used by a listener Bug : 4700257
304
//public boolean isPortUsed(String port, ConfigContext context){
305
public boolean isPortUsed(String JavaDoc port, ConfigContext context, Result result)
306     {
307         result.passed("...");
308         boolean flag = false;
309         try {
310             // 8.0 XML Verifier
311
//Server server = (Server)context.getRootConfigBean();
312
//HttpListener[] httpListener = server.getHttpService().getHttpListener();
313
Config config = StaticTest.getConfig(context);
314             if(config!=null) {
315                 HttpListener[] httpListener = config.getHttpService().getHttpListener();
316                 for(int i=0;i<httpListener.length;i++) {
317                     if(!thisListener.getId().equals(httpListener[i].getId()))
318                     {
319                         if(port.equals(httpListener[i].getPort())
320                             && thisListener.getAddress().equals(httpListener[i].getAddress()))
321                         {
322                             flag=true;
323                             result.failed("Port Already In Use by : " + httpListener[i].getId());
324                             break;
325                         }
326                     }
327                 }
328             }
329         } catch(Exception JavaDoc e) {
330             //e.printStackTrace();
331
result.failed("Exception Caught : " + e.getMessage());
332         }
333         return flag;
334     }
335     
336 }
337
Popular Tags