📄 deleteserviceaction.java
字号:
/* * DeleteServiceAction.java * * Created on August 12, 2003, 2:14 PM */package gov.nist.struts.webapp.monitor;import gov.nist.security.authentication.UserTag;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.io.IOException;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import java.util.jar.JarEntry;import java.util.jar.JarFile;import javax.servlet.ServletContext;import org.apache.log4j.Logger;/** This action represents the action of deleting the service. * It will delete the service physically and remove all information about it * @author DERUELLE Jean */public class DeleteServiceAction extends org.apache.struts.action.Action { /** log4j logging*/ static Logger logger = Logger.getLogger(DeleteServiceAction.class); /** Creates a new instance of DeleteServiceAction */ public DeleteServiceAction() { } /** * @see org.apache.struts.action.Action#perform(org.apache.struts.action.ActionMapping actionMapping, org.apache.struts.action.ActionForm actionForm, javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) */ public org.apache.struts.action.ActionForward perform(org.apache.struts.action.ActionMapping actionMapping, org.apache.struts.action.ActionForm actionForm, javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws java.io.IOException, javax.servlet.ServletException { ServiceForm startServiceForm=(ServiceForm)actionForm; //Get the main class name String mainClass=startServiceForm.getMainClass(); String userURI=startServiceForm.getUserURI(); //Get the application context ServletContext applicationContext=this.getServlet().getServletContext(); //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"); //Get the file name String fileName=getFileName(mappingFile,userURI,mainClass); //Stop the stack before deleting the service Hashtable monitors=(Hashtable)applicationContext.getAttribute("monitors"); //Delete the service /*Service service=(Service)monitors.get(mainClass+":"+userURI); if(service.isRunning()){ service.stopService(); } */ monitors.remove(mainClass+":"+userURI); //Destroy the directory tree and the file uploaded //and the service from the xml services file removeInXMLMappingFile(mappingFile, userURI, fileName, mainClass); //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"); UserTag userTag=findUser(userURI); String uploadPath= uploadLocation.concat(userTag.getUserGroup())+ File.separatorChar+ userTag.getUserName()+File.separatorChar; //Remove physically the files removeAllFilesFromAService(fileName,mainClass,uploadPath); //Delete the service from the session attribute holding all the services for the user //services.remove(mainClass); //log the informations logger.info("The service "+mainClass+" of the user "+userURI+" has been deleted by the user "); //Get the user from the session context UserTag user=(UserTag)httpServletRequest.getSession().getAttribute("user"); if(user.getUserGroup().equals("admin")) return actionMapping.findForward("deleted_admin"); //return a forward to monitor.jsp return actionMapping.findForward("deleted"); } /** * method which look for a service in the mapping file * and return the name of the file containing it * @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 mainClass - the fully qualified name of main class of the service * @return true the name of the file containing the service(can be a jar file) */ private synchronized String getFileName(String mappingFile, String userURI, 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().equals(userURI) ){ Vector services=userServicesTag.getServices(); // for(int j=0;j<services.size();j++){ ServiceTag serviceTag=(ServiceTag)services.elementAt(j); if(serviceTag.getMainClass().equals(mainClass)){ return serviceTag.getFileName(); } } } } return null; } /** * we remove the service from the xml file where is stored all the information relative to the services * @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 removeInXMLMappingFile(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().equals(userURI)){ Vector services=userServicesTag.getServices(); Enumeration e=services.elements(); while(e.hasMoreElements()){ ServiceTag serviceTag=(ServiceTag)e.nextElement(); if(serviceTag.getMainClass().equals(mainClass) && serviceTag.getFileName().equals(fileName) ){ services.removeElement(serviceTag); e=services.elements(); } } } } servicesParser.writeToXMLFile(usersServices); } /** * Removing all files uploaded and rewrited files too * @param fileName - the file name containing all the classes of the service * @param mainClass - the fully qualified name of main class of the service * @param uploadPath - the path where the file has been uploaded */ private void removeAllFilesFromAService(String fileName, String mainClass, String uploadPath){ String suffix=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length()); if(suffix.equals("class")){ //Delete the file File file=new File(uploadPath.concat(mainClass.replace('.',File.separatorChar))+".class"); if(file.exists()) if(file.isFile()) file.delete(); //Delete the file rewritten File dumpFile=new File(uploadPath+"dump"+File.separatorChar+mainClass.replace('.',File.separatorChar)+".class"); if(dumpFile.exists()) if(dumpFile.isFile()) dumpFile.delete(); } if(suffix.equals("jar")){ JarFile jarFile=null; try{ jarFile = new JarFile(uploadPath.concat(fileName)); } catch(IOException ioe){ ioe.printStackTrace(); } Enumeration enum = jarFile.entries(); //for each entry of the jar while (enum.hasMoreElements()){ JarEntry entry=(JarEntry)enum.nextElement(); //Delete the file extracted String entryClassName=entry.getName(); File file=new File(uploadPath.concat(entryClassName)); if(file.exists()) if(file.isFile()) file.delete(); //Delete the file rewritten File dumpFile=new File(uploadPath+"dump"+File.separatorChar+entryClassName); if(dumpFile.exists()) if(dumpFile.isFile()) dumpFile.delete(); } //close the jar File try{ jarFile.close(); } catch(IOException ioe){ ioe.printStackTrace(); } //Delete the jar file File file=new File(uploadPath.concat(fileName)); if(file.exists()) if(file.isFile()) file.delete(); } } 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 + -