View Javadoc

1   /*$Id: LifecycleListener.java,v 1.5 2004/11/27 13:20:03 pah Exp $
2    * Created on 16-Sep-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.component;
12  
13  import org.astrogrid.config.SimpleConfig;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  import java.net.MalformedURLException;
19  import java.net.URL;
20  
21  
22  import javax.naming.NamingException;
23  import javax.servlet.ServletContextEvent;
24  import javax.servlet.ServletContextListener;
25  
26  /*** receives notifications of the servlet context (i.e. the jes webapp) starting up and shutting down. passes this on to the component manager.
27   * @author Noel Winstanley nw@jb.man.ac.uk 16-Sep-2004
28   *
29   */
30  public class LifecycleListener implements ServletContextListener {
31      /***
32       * Commons Logger for this class
33       */
34      private static final Log logger = LogFactory
35              .getLog(LifecycleListener.class);
36  
37      /*** Construct a new LifecycleListener
38       * 
39       */
40      public LifecycleListener() {
41          super();
42      }
43  
44      /***
45       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
46       */
47      public void contextInitialized(ServletContextEvent arg0) {
48          try {
49              URL endpointURL = arg0.getServletContext().getResource("/");//try this to see if it needs to actually exist....
50              logger.info(" service endpoint in LifecycleListener init " + endpointURL);
51              // don't do this - it forces the whole config system to startup - which is a pain if the config file isn't available yet 
52              //SimpleConfig.getSingleton().setProperty(EmptyCEAComponentManager.SERVICE_ENDPOINT_URL,endpointURL);
53              // whack it in JNDI instead,
54              if (endpointURL != null) {
55                 // writeEndpointConfig(endpointURL); dont do this here it is a strange jndi ref
56              } else {
57                  logger.warn("Could not determine service endpoint");
58              } 
59          } catch (MalformedURLException e) {
60              logger.error("Could not set service endpoint url",e);            
61          }   
62          // don't startup just yet.
63          //logger.info("Starting component manager");
64          //logger.info(CEAComponentManagerFactory.getInstance().information()); // this does the 'start'
65      }
66      /***
67       * Writes the endpoint url to Config. The endpoint can then be picked up from
68       * the config system. 
69       * 
70       * @param endpointURL
71       * @throws NamingException
72       */
73      static void writeEndpointConfig(URL endpointURL) {
74  //        try {
75  //            Context root = new InitialContext();
76  //            String urlStr = endpointURL.toString();
77  //            root.rebind("java:comp/env/"
78  //                    + EmptyCEAComponentManager.SERVICE_ENDPOINT_URL, urlStr);
79  //            root.close();
80  //        }
81  //        catch (NamingException e) {
82  //            logger
83  //                    .error("Could not set service endpoint url - JNDI problem",
84  //                            e);
85  //        }
86          //just use the standard config ability to write a property.
87   
88  //because of possible firewalling NAT issues, it is impossible to set this reliably - better to make this an install config issue      
89  //        SimpleConfig.getSingleton().setProperty(EmptyCEAComponentManager.SERVICE_ENDPOINT_URL, endpointURL);
90  //        logger.info("service endpoint stored as "+endpointURL);
91  //        storedEndpoint = true;
92      }
93      
94      static boolean storedEndpoint = false;
95      
96      /***
97       * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
98       */
99      public void contextDestroyed(ServletContextEvent arg0) {
100         logger.info("Stopping component manager");
101         CEAComponentManagerFactory.getInstance().stop();
102         logger.info("Stopped component manager");
103     }
104 
105 }
106 
107 
108 /* 
109 $Log: LifecycleListener.java,v $
110 Revision 1.5  2004/11/27 13:20:03  pah
111 result of merge of pah_cea_bz561 branch
112 
113 Revision 1.4.2.1  2004/11/15 16:56:20  pah
114 do not try to store the service url - firewall NAT might make this pointless - better to set in config
115 
116 Revision 1.4  2004/10/08 20:00:20  pah
117 do not store the endpoint here - not good
118 
119 Revision 1.3  2004/09/22 10:52:50  pah
120 getting rid of some unused imports
121 
122 Revision 1.2  2004/09/17 01:20:22  nw
123 added lifecycle listener and threadpool
124 
125 Revision 1.1.2.1  2004/09/16 18:35:33  nw
126 added liftcycle listener
127  
128 */