KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > configuration > StartServicesServlet


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.ui.web.admin.configuration;
15
16 import java.io.IOException JavaDoc;
17 import java.security.SecureRandom JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import javax.servlet.ServletConfig JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServlet JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.log4j.BasicConfigurator;
29 import org.apache.log4j.Logger;
30 import org.ejbca.core.ejb.ServiceLocator;
31 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocal;
32 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocalHome;
33 import org.ejbca.core.ejb.services.IServiceTimerSessionLocalHome;
34 import org.ejbca.core.model.InternalResources;
35 import org.ejbca.core.model.ca.caadmin.CAInfo;
36 import org.ejbca.core.model.log.Admin;
37 import org.ejbca.util.CertTools;
38
39 /**
40  * Servlet used to start services by calling the ServiceSession.load() at startup<br>
41  *
42  *
43  *
44  * @version $Id: StartServicesServlet.java,v 1.11.2.1 2007/03/09 17:58:15 anatom Exp $
45  *
46  * @web.servlet name = "StartServices"
47  * display-name = "StartServicesServlet"
48  * description="Servlet used to start services by calling the ServiceSession.load()"
49  * load-on-startup = "1"
50  *
51  * @web.servlet-mapping url-pattern = "/configuration/startservices"
52  *
53  * @web.env-entry description="Determines if log4j should be initilized explicitly, needed for glassfish"
54  * name="LOG4JCONFIG"
55  * type="java.lang.String"
56  * value="${logging.log4j.config}"
57  *
58  * @version $Id: StartServicesServlet.java,v 1.11.2.1 2007/03/09 17:58:15 anatom Exp $
59  */

60 public class StartServicesServlet extends HttpServlet JavaDoc {
61
62     private static final Logger log = Logger.getLogger(StartServicesServlet.class);
63     /** Internal localization of logs and errors */
64     private static final InternalResources intres = InternalResources.getInstance();
65     
66     /**
67      * Method used to remove all active timers
68      * @see javax.servlet.GenericServlet#destroy()
69      */

70     public void destroy() {
71         String JavaDoc iMsg = intres.getLocalizedMessage("startservice.shutdown");
72         log.info(iMsg);
73         
74         log.debug(">destroy calling ServiceSession.unload");
75         try {
76             getServiceHome().create().unload();
77         } catch (Exception JavaDoc e) {
78             log.error(e);
79         }
80         super.destroy();
81     }
82
83
84     private IServiceTimerSessionLocalHome servicehome = null;
85
86     private synchronized IServiceTimerSessionLocalHome getServiceHome() throws IOException JavaDoc {
87         try{
88             if(servicehome == null){
89                 servicehome = (IServiceTimerSessionLocalHome)ServiceLocator.getInstance().getLocalHome(IServiceTimerSessionLocalHome.COMP_NAME);
90             }
91           } catch(Exception JavaDoc e){
92              throw new java.io.IOException JavaDoc("Authorization Denied");
93           }
94           return servicehome;
95     }
96       
97
98     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
99         super.init(config);
100         String JavaDoc iMsg = intres.getLocalizedMessage("startservice.startup");
101         log.info(iMsg);
102
103         log.debug(">init calling ServiceSession.load");
104         try {
105             getServiceHome().create().load();
106         } catch (Exception JavaDoc e) {
107             log.error("Error init ServiceSession: ", e);
108         }
109         
110         log.debug(">init initializing log4j");
111         String JavaDoc configfile = ServiceLocator.getInstance().getString("java:comp/env/LOG4JCONFIG");
112         if (!StringUtils.equals(configfile, "false")) {
113             // Configure log4j
114
if (StringUtils.equals(configfile, "basic")) {
115                 // Set up a simple configuration that logs on the console.
116
BasicConfigurator.configure();
117             } else {
118                 // TODO: log4j.xml configuration
119
}
120         }
121
122         // Reinstall BC-brovider to help re-deploys to work
123
log.debug("Re-installing BC-provider");
124         CertTools.removeBCProvider();
125         CertTools.installBCProvider();
126
127         // Run java seed collector, that can take a little time the first time it is run
128
log.debug(">init initializing random seed");
129         SecureRandom JavaDoc rand = new SecureRandom JavaDoc();
130         rand.nextInt();
131         
132         // Load CAs at startup to improve impression of speed the first time a CA is accessed, it takes a little time to load it.
133
log.debug("init loading CAs into cache");
134         try {
135             ICAAdminSessionLocalHome casessionhome = (ICAAdminSessionLocalHome)ServiceLocator.getInstance().getLocalHome(ICAAdminSessionLocalHome.COMP_NAME);
136             ICAAdminSessionLocal casession;
137             casession = casessionhome.create();
138             Admin admin = new Admin(Admin.TYPE_CACOMMANDLINE_USER, "StartServicesServlet");
139             Collection JavaDoc caids = casession.getAvailableCAs(admin);
140             Iterator JavaDoc iter = caids.iterator();
141             while (iter.hasNext()) {
142                 int caid = ((Integer JavaDoc)iter.next()).intValue();
143                 CAInfo ca = casession.getCAInfo(admin, caid);
144                 log.debug("Found CA: "+ca.getName()+", with expire time: "+ca.getExpireTime());
145             }
146         } catch (Exception JavaDoc e) {
147             log.error("Error creating CAAdminSession: ", e);
148         }
149
150     } // init
151

152     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
153         throws IOException JavaDoc, ServletException JavaDoc {
154         log.debug(">doPost()");
155         doGet(req, res);
156         log.debug("<doPost()");
157     } //doPost
158

159     public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws java.io.IOException JavaDoc, ServletException JavaDoc {
160         log.debug(">doGet()");
161         res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Servlet doesn't support requests is only loaded on startup.");
162         log.debug("<doGet()");
163     } // doGet
164

165 }
166
Popular Tags