KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > adventnet > adaptors > html > JettyHtmlServer


1 /**
2  * The XMOJO Project 5
3  * Copyright © 2003 XMOJO.org. All rights reserved.
4
5  * NO WARRANTY
6
7  * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
8  * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
9  * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
10  * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
11  * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
13  * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
14  * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
15  * REPAIR OR CORRECTION.
16
17  * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
18  * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
19  * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
20  * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
21  * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
22  * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
23  * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
24  * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGES.
26 **/

27
28 package com.adventnet.adaptors.html;
29
30 import java.io.File JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.net.URL JavaDoc;
36
37 import com.adventnet.agent.logging.Log;
38 import com.adventnet.agent.logging.LogFactory;
39 import com.adventnet.agent.utilities.xml.XMLNode;
40 import com.adventnet.agent.utilities.xml.XMLDataReader;
41 import com.adventnet.agent.utilities.xml.XMLDataWriter;
42
43 import org.mortbay.util.*;
44 import org.mortbay.jetty.*;
45 import org.mortbay.http.*;
46
47 /**
48  * This class plugs in the Jetty WebServer to the HtmlAdaptor.
49  *
50  * @see HttpServerInterface
51  */

52 public class JettyHtmlServer implements HttpServerInterface
53 {
54     private static final int defaultPortNumber = 8030;
55
56     private String JavaDoc configFileName ="conf/http/etc/JettyConfig.xml";
57     private String JavaDoc jarConfig = "conf/http/etc/JarConfig.xml";
58
59     private Properties JavaDoc props = null;
60     private boolean authentication = false;
61     private int port = defaultPortNumber;
62     private boolean isStarted = false;
63     private boolean sslSupport = false;
64     private boolean isJarFile = false;
65     private boolean fromSetPort = false;
66
67     private Server server = null;
68
69     private Log log = null;
70
71     /**
72      * Primary constructor for the HttpServerImpl without any configuration options.
73      */

74     public JettyHtmlServer()
75     {
76         this(defaultPortNumber, false, false, false);
77     }
78
79     /**
80      * Secondary constructor with configuration options.
81      *
82      * Note: For this version, authentication is disabled and hence will not
83      * have any effect by setting authentication to true.
84      *
85      * @param port The port to start the WebServer
86      *
87      * @param authentication The boolean flag to turn ON/OFF
88      * Authentication of the Requests. Default is false.
89      */

90     public JettyHtmlServer(int port, boolean authentication)
91     {
92         this(port, authentication, false, false);
93     }
94
95     /**
96      * Constructor with configuration options.
97      *
98      * Note: For this version, authentication and SSL support are disabled and
99      * hence will not have any effect by setting true to those parameters.
100      *
101      * @param port The port to start the WebServer
102      *
103      * @param authentication The boolean flag to turn ON/OFF
104      * Authentication of the Requests. Default is false.
105      *
106      * @param sslSupport The boolean flag to turn ON/OFF SSL Support.
107      * Default is false.
108      */

109     public JettyHtmlServer(int port, boolean authentication, boolean sslSupport)
110     {
111         this(port, authentication, sslSupport, false);
112     }
113
114     /**
115      * Constructor with configuration options.
116      *
117      * Note: For this version, authentication and SSL support are disabled and
118      * hence will not have any effect by setting true to those parameters.
119      *
120      * @param port The port to start the WebServer
121      *
122      * @param authentication The boolean flag to turn ON/OFF
123      * Authentication of the Requests. Default is false.
124      *
125      * @param sslSupport The boolean flag to turn ON/OFF SSL Support.
126      * Default is false.
127      *
128      * @param isJarFile Boolean flag to indicate configuration files taken from Jar.
129      */

130     public JettyHtmlServer(int port, boolean authentication,
131                            boolean sslSupport, boolean isJarFile)
132     {
133         log = getLogObject();
134         this.authentication = authentication;
135         this.sslSupport = sslSupport;
136         this.port = port;
137         this.isJarFile = isJarFile;
138
139         if(!isJarFile)
140         {
141             XMLDataReader xmld =
142                 new XMLDataReader(HtmlAdaptor.getParentDirectory() + File.separator
143                                   + configFileName );
144             XMLNode rootNode = xmld.getRootNode();
145
146             Vector JavaDoc vec = rootNode.getChildNodes();
147             changePort(rootNode, (new Integer JavaDoc(port)).toString());
148
149             changeSSL(rootNode, sslSupport);
150             changeAgentDir(rootNode, HtmlAdaptor.getParentDirectory());
151
152             XMLDataWriter xw = new XMLDataWriter(HtmlAdaptor.getParentDirectory() + File.separator + configFileName ,rootNode);
153         }
154     }
155
156     /**
157      * Setter for the configuration file Name with location.
158      *
159      * @param configFileName The configuration file name with path.
160      */

161     public void setConfigFileName(String JavaDoc configFileName)
162     {
163         this.configFileName = configFileName;
164
165         try
166         {
167             restartHttpServer();
168         }
169         catch (Exception JavaDoc ex)
170         {
171             log.error("Exception", ex);
172         }
173     }
174
175     /**
176      * Getter for the configFileName.
177      *
178      * @return The configuration file Name by which the Server is running.
179      */

180     public String JavaDoc getConfigFileName()
181     {
182         return this.configFileName;
183     }
184
185     /**
186      * Setter for the configuration file Name which will be inside the
187      * jar containing this class.
188      *
189      * @param jarConfig The configuration file name with path as in the jar File.
190      */

191     public void setConfigJarFile(String JavaDoc jarConfig)
192     {
193         this.jarConfig = jarConfig;
194
195         try
196         {
197             restartHttpServer();
198         }
199         catch(Exception JavaDoc ex)
200         {
201             log.error("Exception", ex);
202         }
203     }
204
205     /**
206      * Setter for the configuration file Name which will be inside the
207      * jar containing this class.
208      *
209      * @return The configuration file name by which the Server is running
210      * with path as in the jar File.
211      */

212     public String JavaDoc getConfigJarFile()
213     {
214         return this.jarConfig;
215     }
216
217     /**
218      * Returns the port number in which the http server gets started.
219      *
220      * @return the port number.
221      *
222      * @see setPort(Integer port)
223      */

224     public Integer JavaDoc getPort()
225     {
226         return new Integer JavaDoc(port);
227     }
228
229     /**
230      * Sets the port number in which the http server to get started.
231      *
232      * @param the port number of the http web-server.
233      *
234      * @see getPort()
235      */

236     public void setPort(Integer JavaDoc port) throws Exception JavaDoc
237     {
238         setPort(port.intValue());
239     }
240
241     /**
242      * Sets the port number in which the http server to get started.
243      *
244      * @param the port number of the http web-server.
245      *
246      * @see getPort()
247      */

248     public void setPort(int port)
249     {
250         if(port < 0 || port > 65535)
251         {
252             return;
253         }
254
255         if(port != this.port)
256         {
257             this.port = port;
258
259             if(!isJarFile)
260             {
261                 XMLDataReader xmld =
262                     new XMLDataReader(HtmlAdaptor.getParentDirectory()
263                                         + File.separator + configFileName );
264
265                 XMLNode rootNode = xmld.getRootNode();
266
267                 changePort(rootNode, String.valueOf(port));
268
269                 XMLDataWriter xw =
270                     new XMLDataWriter(HtmlAdaptor.getParentDirectory()
271                                 + File.separator + configFileName, rootNode);
272             }
273             else
274             {
275                 fromSetPort = true;
276             }
277
278             //restart the Http server at the specified port
279
log.info("applying the new port number by restarting the Http server");
280
281             try
282             {
283                 restartHttpServer();
284                 log.info("successfully changed to new port number");
285             }
286             catch(Exception JavaDoc ex)
287             {
288                 log.error("Exception while trying to change server port number", ex);
289                 log.fatal("Failed to change to new port number");
290             }
291         }
292     }
293
294     /**
295      * Setter for enabling and disabling Authentication of the WebServer.
296      *
297      * Note: For this version, this is disabled and hence will not have
298      * any effect by calling this method.
299      *
300      * @param auth The flag for turning ON/OFF the authentication of WebServer.
301      */

302     public void setAuthentication(boolean auth)
303     {
304         //this.authentication = auth;
305
this.authentication = false;
306     }
307
308     /**
309      * To know whether the authentication is enabled in the WebServer.
310      *
311      * Note: For this version, this is disabled and hence will always return false.
312      *
313      * @return true if the authentication of the WebServer is turned ON else false.
314      */

315     public boolean isAuthentication()
316     {
317         return this.authentication;
318     }
319
320     /**
321      * This method adds a new User with the userName and password
322      * to the Authentication of the WebServer.
323      *
324      * Note: For this version, this is disabled and hence will not have
325      * any effect by calling this method.
326      *
327      * @param userName The user name to be added.
328      *
329      * @param password The password for the user.
330      */

331     public void addUser(String JavaDoc userName, String JavaDoc passwd)
332     {
333         //this is not supported in this release
334
}
335
336     /**
337      * Setter for enabling and disabling SSL Support of the WebServer.
338      *
339      * Note: For this version, this is disabled and hence will not have
340      * any effect by calling this method.
341      *
342      * @param sslSupport The flag for turning ON/OFF the SSL Support of WebServer.
343      */

344     public void setSslSupport(boolean sslSupport)
345     {
346         //this.sslSupport = sslSupport;
347
this.sslSupport = false;
348     }
349
350     /**
351      * To know whether the SSL Support is enabled in the WebServer.
352      *
353      * Note: For this version, this is disabled and hence will always return false.
354      *
355      * @return true if the SSL Support of the WebServer is turned ON else false.
356      */

357     public boolean isSslSupport()
358     {
359         return this.sslSupport;
360     }
361
362     /**
363      * Starts the HttpServer - Jetty WebServer.
364      *
365      * @see stopHttpServer()
366      *
367      * @exception Exception on error while starting WebServer.
368      */

369     public void startHttpServer() throws Exception JavaDoc
370     {
371         System.setProperty("LOG_FILE", "logs/agent.log");
372         System.setProperty("LOG_CLASSES", "org.mortbay.util.WriterLogSink");
373
374         if(!isJarFile)
375         {
376             server = new Server(HtmlAdaptor.getParentDirectory()+
377                                 File.separator+configFileName);
378         }
379         else
380         {
381             URL JavaDoc url = this.getClass().getResource("/"+jarConfig);
382             server = new Server(url);
383             if((sslSupport) || (fromSetPort))
384             {
385                 //removes the listener configured through xml file.
386
Collection JavaDoc lisCol = server.getListeners();
387                 if(!(lisCol.isEmpty())){
388                     Iterator JavaDoc iter = lisCol.iterator();
389                     if(iter.hasNext())
390                         server.removeListener((HttpListener)iter.next());
391                 }
392
393                 if(!sslSupport)
394                 {
395                     SocketListener socLis = new SocketListener(new InetAddrPort(this.port));
396                     server.addListener(socLis);
397                 }
398             }
399         }
400
401         server.start();
402         isStarted = true;
403     }
404
405     /**
406      * Stops the WebServer.
407      *
408      * @exception Exception on error while stopping WebServer.
409      */

410     public void stopHttpServer() throws Exception JavaDoc
411     {
412         server.stop();
413         isStarted = false;
414     }
415
416     /**
417      * Restarts the WebServer.
418      *
419      * @exception Exception while error trying to restart the WebServer
420      */

421     public void restartHttpServer() throws Exception JavaDoc
422     {
423         try
424         {
425             stopHttpServer();
426         }
427         catch(Exception JavaDoc ee)
428         {
429             throw new Exception JavaDoc("Exception while stopping WebServer in restart");
430         }
431
432         try
433         {
434             startHttpServer();
435         }
436         catch(Exception JavaDoc eee)
437         {
438             throw new Exception JavaDoc("Exception while starting WebServer in restart");
439         }
440     }
441
442     void changeAgentDir(XMLNode node, String JavaDoc agentDir)
443     {
444         if (node.getNodeName().equals("Call")
445             && node.getAttributeList().get("name").equals("addContext"))
446         {
447             XMLNode setNode = (XMLNode)node.getChildNodes().elementAt(1);
448
449             if (setNode.getAttributeList().get("name").equals("ResourceBase"))
450             {
451                 XMLNode sysNode = (XMLNode)setNode.getChildNodes().elementAt(0);
452                 sysNode.getAttributeList().put("default", agentDir+"/conf/http/ssi");
453                 return;
454             }
455
456         }
457
458         Vector JavaDoc childVec = node.getChildNodes();
459         if(childVec != null)
460         {
461             int len = childVec.size();
462
463             for(int i = 0; i <len; i++)
464             {
465                 changeAgentDir((XMLNode)childVec.elementAt(i), agentDir);
466             }
467         }
468     }
469
470     private boolean changeSSL(XMLNode node, boolean flag)
471     {
472         if (node.getNodeName().equals("Call")
473             && node.getAttributeList().get("name").equals("addListener"))
474         {
475             XMLNode argNode = (XMLNode)node.getChildNodes().elementAt(0);
476             XMLNode newNode = (XMLNode)argNode.getChildNodes().elementAt(0);
477
478             if(flag)
479             {
480                 newNode.getAttributeList().put("class", "org.mortbay.http.SunJsseListener");
481
482                 XMLNode setNode = null;
483
484                 Vector JavaDoc vec= newNode.getChildNodes();
485                 int size = vec.size();
486                 boolean keyStore = true;
487                 boolean keyPassword = true;
488                 boolean password = true;
489
490                 String JavaDoc keyStoreStr = null;
491                 String JavaDoc keyPasswordStr = null;
492                 String JavaDoc passwordStr = null;
493
494                 XMLDataReader myReader =
495                     new XMLDataReader(HtmlAdaptor.getParentDirectory() + File.separator
496                                       + "etc" + File.separator + "HtmlAdaptorSSLConf.xml");
497                 XMLNode myConfigNode = myReader.getRootNode();
498                 Vector JavaDoc configChilds = myConfigNode.getChildNodes();
499                 int configSize = configChilds.size();
500                 for (int j = 0; j < configSize; j++)
501                 {
502                     XMLNode configChild = (XMLNode)configChilds.get(j);
503                     if (configChild.getNodeName().equals("Store") &&
504                         configChild.getAttribute("name").equals("keyStore"))
505                     {
506                         Vector JavaDoc properties = configChild.getChildNodes();
507                         int propsSize = properties.size();
508                         for (int k = 0; k < propsSize; k++)
509                         {
510                             XMLNode prop = (XMLNode)properties.get(k);
511                             String JavaDoc value = (String JavaDoc)prop.getAttribute("name");
512                             if (value.equals("Name"))
513                             {
514                                 keyStoreStr = (String JavaDoc)prop.getAttribute("value");
515                             }
516                             else if (value.equals("Password"))
517                             {
518                                 passwordStr = (String JavaDoc)prop.getAttribute("value");
519                             }
520                             else if (value.equals("Keypassword"))
521                             {
522                                 keyPasswordStr = (String JavaDoc)prop.getAttribute("value");
523                             }
524
525                             if (keyPasswordStr != null &&
526                                 passwordStr != null &&
527                                 keyStoreStr != null)
528                             {
529                                 break;
530                             }
531                         }
532                     }
533                 }
534
535                 Vector JavaDoc newNodeChilds = newNode.getChildNodes();
536                 int newNodeChildSize = newNodeChilds.size();
537                 for (int i = 0; i < newNodeChildSize; i++)
538                 {
539                     XMLNode newNodeChild = (XMLNode)newNodeChilds.get(i);
540                     if (newNodeChild.getNodeName().equals("Set"))
541                     {
542                         if (newNodeChild.getAttribute("name").equals("Keystore"))
543                         {
544                             newNodeChild.removeFromParent();
545                         }
546                         else if (newNodeChild.getAttribute("name").equals("Password"))
547                         {
548                             newNodeChild.removeFromParent();
549                         }
550                         else if (newNodeChild.getAttribute("name").equals("KeyPassword"))
551                         {
552                             newNodeChild.removeFromParent();
553                         }
554                         else if (newNodeChild.getAttribute("name").equals("ConfigFileName"))
555                         {
556                             newNodeChild.removeFromParent();
557                         }
558                     }
559                 }
560
561                 XMLNode addNode = new XMLNode();
562                 addNode.setNodeType(XMLNode.ELEMENT);
563                 addNode.setNodeName("Set");
564
565                 addNode.setAttribute("name", "Keystore");
566                 addNode.setNodeValue("etc/adventnetkey");
567                 newNode.addChildNode(addNode);
568
569                 addNode = new XMLNode();
570                 addNode.setNodeType(XMLNode.ELEMENT);
571                 addNode.setNodeName("Set");
572
573                 addNode.setAttribute("name", "KeyPassword");
574                 addNode.setNodeValue("adventnet");
575                 newNode.addChildNode(addNode);
576
577                 addNode = new XMLNode();
578                 addNode.setNodeType(XMLNode.ELEMENT);
579                 addNode.setNodeName("Set");
580
581                 addNode.setAttribute("name", "Password");
582                 addNode.setNodeValue("adventnet");
583                 if (password)
584                 {
585                     newNode.addChildNode(addNode);
586                 }
587                 return true;
588             }
589             else
590             {
591                 if (newNode.getAttributeList().get("class").equals("org.mortbay.http.SunJsseListener"))
592                 {
593                     newNode.getAttributeList().put("class", "org.mortbay.http.SocketListener");
594
595                     XMLNode setNode = null;
596                     for (int i=0; i<newNode.getChildNodes().size(); i++)
597                     {
598                         setNode = (XMLNode)newNode.getChildNodes().elementAt(i);
599
600                         if (setNode.getAttributeList().get("name").equals("KeyPassword") ||
601                             setNode.getAttributeList().get("name").equals("Keystore") ||
602                             setNode.getAttributeList().get("name").equals("Password"))
603                         {
604                             setNode.removeFromParent();
605                             // return true;
606
}
607                     }
608                 }
609             }
610         }
611
612         Vector JavaDoc childVec = node.getChildNodes();
613
614         if(childVec != null)
615         {
616             int len = childVec.size();
617             for (int i = 0; i <len; i++)
618             {
619                 if (changeSSL((XMLNode)childVec.elementAt(i), flag))
620                 {
621                     return true;
622                 }
623             }
624         }
625
626         return false;
627     }
628
629     //this method changes the value of port in the config file
630
private void changePort(XMLNode node, String JavaDoc port)
631     {
632         if (node.getNodeName().equals("Call")
633             && node.getAttributeList().get("name").equals("addListener"))
634         {
635             XMLNode argNode = (XMLNode)node.getChildNodes().elementAt(0);
636             XMLNode newNode = (XMLNode)argNode.getChildNodes().elementAt(0);
637
638             XMLNode setNode = null;
639
640             for (int i=0; i<newNode.getChildNodes().size(); i++)
641             {
642                 setNode = (XMLNode)newNode.getChildNodes().elementAt(i);
643
644                 if (setNode.getAttributeList().get("name").equals("Port"))
645                 {
646                     setNode.setNodeValue(port);
647                     return;
648                 }
649             }
650         }
651
652         Vector JavaDoc childVec = node.getChildNodes();
653         if(childVec != null)
654         {
655             int len = childVec.size();
656
657             for (int i = 0; i <len; i++)
658             {
659                 changePort((XMLNode)childVec.elementAt(i),port);
660             }
661         }
662     }
663
664     private Log getLogObject()
665     {
666         try
667         {
668             log = LogFactory.getInstance("HTML");
669         }
670         catch(InstantiationException JavaDoc ie)
671         {
672             // ie.printStackTrace();
673
}
674
675         return log;
676     }
677 }
Popular Tags