📄 rewriterprocessor.java
字号:
/* * RewriterProcesor.java * * Created on June 3, 2003, 3:36 PM */package gov.nist.security.bcs;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Vector;import java.util.jar.JarEntry;import java.util.jar.JarFile;import gov.nist.security.authentication.UserTag;import gov.nist.security.log.ServiceLog;import org.apache.log4j.Logger;/** * This class is the one of the main part of the application * It extracts the files of the jar and load these ones through the SafeClassLoader * for rewriting and start the service in a new Process under a security manager * @author DERUELLE Jean */public class RewriterProcessor implements Runnable { /**STATUS that the rewriting process can take*/ public static int NOT_REWRITTEN=0; public static int IN_REWRITING=1; public static int REWRITTEN=2; public static int REWRITING_ERROR=3; /**the rewriting thread*/ private Thread rewriteThread = null; /**the uploaded file name*/ private String fileName=null; /**the mainClass of the service to load*/ private String mainClass=null; /**type of service to rewrite*/ private String serviceType=null; /**the directory of the user where his service has been uploaded*/ private String userUploadDirectory=null; /**list of the permissions to check upon the service*/ private Hashtable sipPermissions=null; /** log4j logging*/ static private Logger logger = Logger.getLogger(RewriterProcessor.class); /**tells if it's a we must rewrite the bytecode or not * (if false, implies that the classes of the service must have already been rewritten in the dump directory)*/ private boolean to_rewrite=true; /**the path of the web server */ private String webServerPath=null; /**the group of the user*/ private String userGroup=null; /**the sip URI with which the services of the user will register with our stateless proxy*/ private String sipUri=null; /**description of the error that happened during the rewriting process*/ private String rewriting_error=null; /**status of the rewriting process*/ private int status; /**service of the user*/ protected Process proc=null; /**service of the user*/ protected Service service=null; /** * Constructor of EngineRequestAction with some parameters * to load the user's service with the good permissions * @param service - the service of the user to load * @param mainClass - the mainClass of the service to load * @param URLUploadDirectory - the directory from which we will load the service * @param sipPermissions - the permissions to check upon the service * @param to_rewrite - tells if it's a we must rewrite the bytecode or not * (if false, implies that the classes of the service must have already been rewritten in the dump directory) * @param webServerPath - the path to the web server * @param user - the actual user for who we are rewriting the service */ public RewriterProcessor( Service service, String URLUploadDirectory, Hashtable sipPermissions, String webServerPath, UserTag user) { this.service=service; this.mainClass = service.getMainClass(); this.fileName=service.getFileName(); this.userUploadDirectory=URLUploadDirectory; this.sipPermissions=sipPermissions; this.to_rewrite=service.getToRewrite(); this.serviceType=service.getServiceType(); this.webServerPath=webServerPath; this.userGroup=user.getUserGroup(); this.sipUri=user.getUri(); status=NOT_REWRITTEN; } /** * start the rewrite thread */ public void start() { if (rewriteThread == null) { rewriteThread = new Thread(this); rewriteThread.start(); } status=IN_REWRITING; } /** * Extract the files of the jar and load these ones through the SafeClassLoader * for rewriting and start the service in a new Process under a security manager */ public void run(){ //if me must rewrite the byte code if(to_rewrite){ //Create the class loader which will rewrite the code of the class it will load ByteCodeRewriter byteCodeRewriter=new ByteCodeRewriter( sipPermissions, userUploadDirectory, mainClass, sipUri); try{ //if the file uploaded was not a jar we load the class if(fileName.indexOf(".jar")==-1) byteCodeRewriter.rewriteClass(mainClass); //else we extract the jar and load/rewrote all the classes within the jar one by one else{ List classesList=extractJar(userUploadDirectory.concat(fileName), mainClass); //Rewrite all the classes in the jar Iterator it=classesList.iterator(); while(it.hasNext()){ String className=(String)it.next(); byteCodeRewriter.rewriteClass(className); logger.debug(className+" has been rewrote"); //classLoader.find=false; } } } //This error can be catch if a .jar is missing at the runtime catch(NoClassDefFoundError ncdfe){ //parent=null; ncdfe.printStackTrace(); logger.warn(ncdfe.toString()); logger.warn("Service " + mainClass + " not loaded ! File missing."); } catch(SecurityException se){ logger.debug(se.getMessage()); rewriting_error=se.getMessage(); status=REWRITING_ERROR; return; } } String groupUploadDirectory=userUploadDirectory; groupUploadDirectory=groupUploadDirectory.substring(0, groupUploadDirectory.indexOf(userGroup)); groupUploadDirectory=groupUploadDirectory.concat(userGroup); String cmd=null; //if we are on windows if(File.pathSeparatorChar==';'){ cmd="java -cp "+"\""+webServerPath+File.separatorChar +"lib"+File.separatorChar+"sip-sdp.jar"+"\""+File.pathSeparatorChar +"\""+webServerPath+File.separatorChar +"lib"+File.separatorChar+"mail.jar"+"\""+File.pathSeparatorChar +"\""+webServerPath+File.separatorChar +"lib"+File.separatorChar+"activation.jar"+"\""+File.pathSeparatorChar +userUploadDirectory+"dump"+File.separatorChar+File.pathSeparatorChar +"\""+webServerPath+File.separatorChar +"lib"+File.separatorChar+"jain-sip-services.jar"+"\""+File.pathSeparatorChar +"\""+webServerPath+File.separatorChar +"lib"+File.separatorChar+"log4j-1.2.8.jar"+"\""+File.pathSeparatorChar +"\""+webServerPath+File.separatorChar +"lib"+File.separatorChar+"nist-rtp-gateway.jar"+"\""+" " +"-Djava.security.manager " +"-Djava.security.policy=="+groupUploadDirectory +File.separatorChar+userGroup+".policy"+" "+mainClass; } //on linux else{ cmd="java -cp "+webServerPath+File.separatorChar +"lib"+File.separatorChar+"sip-sdp.jar"+File.pathSeparatorChar +webServerPath+File.separatorChar +"lib"+File.separatorChar+"mail.jar"+File.pathSeparatorChar +webServerPath+File.separatorChar +"lib"+File.separatorChar+"activation.jar"+File.pathSeparatorChar +userUploadDirectory+"dump"+File.separatorChar+File.pathSeparatorChar +webServerPath+File.separatorChar +"lib"+File.separatorChar+"jain-sip-services.jar"+File.pathSeparatorChar +webServerPath+File.separatorChar +"lib"+File.separatorChar+"log4j-1.2.8.jar"+File.pathSeparatorChar +webServerPath+File.separatorChar +"lib"+File.separatorChar+"nist-rtp-gateway.jar"+" " +"-Djava.security.manager " +"-Djava.security.policy=="+groupUploadDirectory +File.separatorChar+userGroup+".policy"+" "+mainClass; } String line=null; String logServiceFile=mainClass+"_log.txt"; //If the service is Java-based we start it in a new JVM if(serviceType.equalsIgnoreCase("Java")){ //start the service of the user in a new Process under a security manager Runtime runtime=Runtime.getRuntime(); logger.info( cmd +" in the following directory : "+ userUploadDirectory+ "dump"+File.separatorChar); try{ proc=runtime.exec(cmd, null, new File(userUploadDirectory+"dump"+File.separatorChar)); //Start the logging of the user Process //if you do not want to send the log to a file but to the standard output
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -