📄 startserviceaction.java
字号:
package gov.nist.struts.webapp.monitor;import gov.nist.security.authentication.UserTag;import gov.nist.security.bcs.RewriterProcessor;import gov.nist.security.bcs.Service;import gov.nist.struts.webapp.upload.ServiceTag;import gov.nist.struts.webapp.upload.UserServicesTag;import gov.nist.struts.webapp.upload.XMLServicesParser;import java.io.File;import java.util.*;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionForward;import javax.servlet.*;import javax.servlet.http.*;import org.apache.log4j.Logger;/** * This class represents the action of starting a service * It will check if the service has been rewritten or not and start the processing of * bytecode rewriting if the process has not been bytecode rewritten and finally start the service * @author Jean Deruelle */public class StartServiceAction extends Action { /** log4j logging*/ private static Logger logger = Logger.getLogger(StartServiceAction.class); /** * @see org.apache.struts.action.Action#perform(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) */ public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ServiceForm startServiceForm=(ServiceForm)form; //Get the main class name String mainClass=startServiceForm.getMainClass(); String userURI=startServiceForm.getUserURI(); UserTag userTag=findUser(userURI); //Get the application context ServletContext applicationContext=this.getServlet().getServletContext(); Hashtable monitors = (Hashtable) applicationContext.getAttribute("monitors"); //Get the customized permissions from the application context Hashtable customPermissions=(Hashtable)applicationContext.getAttribute("permissions"); //Get the mappingFile Location from the application context String mappingFile=(String)applicationContext.getAttribute("mappingFile"); //constructs the upload path from the session context //Done this way because admin can start services of other users String uploadLocation=(String)applicationContext.getAttribute("uploadDirectory"); String uploadPath= uploadLocation.concat(userTag.getUserGroup())+ File.separatorChar+ userTag.getUserName()+File.separatorChar; //Get the user name from the session context //String userName=user.getUserName(); //Get the password from the session context //String password=user.getUserPassword(); //get the service from the mapping file ServiceTag serviceTag=getService(mappingFile,userURI,mainClass); //if we have to rewrite the service //before invoking it boolean toRewrite=serviceTag.getToRewrite(); //Get the file name String fileName=null; if(toRewrite) fileName=serviceTag.getFileName(); Service service=new Service( serviceTag.getMainClass(), serviceTag.getFileName(), serviceTag.getToRewrite(), serviceTag.getServiceType()); //put the service newly uploaded monitors.put(service.getMainClass() + ":" + userURI,service); //We run the service of the user in a thread RewriterProcessor rewriterProcessor= new RewriterProcessor( service, uploadPath, customPermissions, request.getSession().getServletContext().getRealPath("WEB-INF/"), findUser(userURI)); rewriterProcessor.start(); //rewriterProcessor.waitUntilStarted(); /*Hashtable services=(Hashtable)request.getSession().getAttribute("services"); services.put(mainClass,"written");*/ request.getSession().setAttribute("starting up",rewriterProcessor); //specify in the mapping file that the file has been rewritten if(toRewrite) writeNotifyInXMLMappingFile(mappingFile,userURI,fileName,mainClass); //return a forward to display.jsp return mapping.findForward("starting_up"); } /** * we specify in the xml file where is stored all the information relative to the services * that the service has been bytecode rewritten * @param mappingFile - the mapping File where is stored all the information relative to the services * @param userName - the user name * @param password - the password * @param fileName - the file name containing all the classes of the service * @param mainClass - the fully qualified name of main class of the service */ private synchronized void writeNotifyInXMLMappingFile(String mappingFile, String userURI, String fileName, String mainClass){ XMLServicesParser servicesParser=new XMLServicesParser(mappingFile); Vector usersServices=servicesParser.getUsersServicesTagList(); for(int i=0;i<usersServices.size();i++){ UserServicesTag userServicesTag=(UserServicesTag)usersServices.elementAt(i); if(userServicesTag.getUri().equalsIgnoreCase(userURI) ){ Vector services=userServicesTag.getServices(); // for(int j=0;j<services.size();j++){ ServiceTag serviceTag=(ServiceTag)services.elementAt(j); if(serviceTag.getMainClass().equals(mainClass) && serviceTag.getFileName().equals(fileName) ){ serviceTag.setToRewrite(false); } } } } servicesParser.writeToXMLFile(usersServices); } /** * method which return the Service corresponding to all the parameters * @param mappingFile - the name of the xml file where is stored all the information relative to the services * @param userName - the user name to whom the service belongs * @param password - the password associated to the user name * @param mainClass - the fully qualified name of the service's main class * @return the service */ private synchronized ServiceTag getService(String mappingFile, String userURI, String mainClass){ XMLServicesParser servicesParser=new XMLServicesParser(mappingFile); Vector usersServices=servicesParser.getUsersServicesTagList(); //Look for the services of the user for(int i=0;i<usersServices.size();i++){ UserServicesTag userServicesTag=(UserServicesTag)usersServices.elementAt(i); if(userServicesTag.getUri().equalsIgnoreCase(userURI)){ Vector services=userServicesTag.getServices(); //Look for the good service among the user's services for(int j=0;j<services.size();j++){ ServiceTag serviceTag=(ServiceTag)services.elementAt(j); //Check if the service's rewritten state if(serviceTag.getMainClass().equals(mainClass) ){ return serviceTag; } } } } return null; } private UserTag findUser(String userURI){ //Get the application context ServletContext applicationContext=this.getServlet().getServletContext(); //Get the database of the users Vector users=(Vector)applicationContext.getAttribute("users"); if(users!=null){ for(int i=0;i<users.size();i++){ UserTag user=(UserTag)users.elementAt(i); if(user.getUri().equalsIgnoreCase(userURI) ) return user; } } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -