📄 uploadaction.java
字号:
//this shouldn't happen return null; } /** Create the directory tree for the packages of the class in parameter * we create in the same time the same directory tree for the dump directory * which is the directory where to put the byte code files rewrote with the safe class loader * @param fullClassName - the fully qualified name of the class(name of the class + packages name) * @param uploadPath - the path where to create the packages directory tree * @return the complete path to the class */ private String createPackagesDirectory( String fullClassName, String uploadPath) { String packagePath = uploadPath; String dumpPath = packagePath + File.separatorChar + "dump"; if (fullClassName == null) return null; else { int index = fullClassName.lastIndexOf("."); if (index != -1) { //We remove the name of the class at the end //of the string for keeping only the package name String packageName = fullClassName.substring(0, index); logger.info("the package name of the class is " + packageName); StringTokenizer st = new StringTokenizer(packageName, "."); //we create the directory tree for the upload path and for the dump directory //each time we found a '.' we create a new directory if it doesn't already exist while (st.hasMoreTokens()) { String token = st.nextToken(); packagePath = packagePath + File.separatorChar + token; dumpPath = dumpPath + File.separatorChar + token; java.io.File packageDirectory = new java.io.File(packagePath); if (!packageDirectory.exists()) packageDirectory.mkdir(); java.io.File dumpDirectory = new java.io.File(dumpPath); if (!dumpDirectory.exists()) dumpDirectory.mkdir(); } } return packagePath; } } /** * This method get the Main-Class attribute of the manifest of a jar file * @param jarFile - the jar file from where to look for * into the manifest for the Main-Class attribute * @return the Main-Class to run extracted from the manifest */ private String getMainClassFromManifest(JarFile jarFile) { //we read from the Main-Class attribute of the manifest try { Manifest manifest = jarFile.getManifest(); Attributes attributes = manifest.getMainAttributes(); String className = attributes.getValue("Main-Class"); className = className.replace('/', '.'); logger.info( "fully qualified name of the main class extracted from the manifest : " + className); return className; } catch (Exception e) { logger.warn(e.toString()); } return null; } /** * we specify that this service must have its bytecode rewritten * @param mappingFile - the mapping File where is stored all the information relative to the services * @param user - the user * @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, UserTag user, String fileName, String mainClass, String serviceType) { //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 password from the session context String uri = user.getUri(); XMLServicesParser servicesParser = new XMLServicesParser(mappingFile); Vector usersServices = servicesParser.getUsersServicesTagList(); boolean found = false; if (usersServices != null) { for (int i = 0; i < usersServices.size(); i++) { UserServicesTag userServicesTag = (UserServicesTag) usersServices.elementAt(i); if (userServicesTag.getUserName().equals(userName) && userServicesTag.getPassword().equals(password)) { Vector services = userServicesTag.getServices(); //if the service has already been uploaded in the past //we changed just the flag to tell it has to be rewrite 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(true); found = true; } } //if we did'nt found the service we add it to the mapping file //for the user if (!found) { ServiceTag serviceTag = new ServiceTag( mainClass, fileName, serviceType, true); services.addElement(serviceTag); found = true; } } } } else { usersServices = new Vector(); } //If the user has not been found in the file //We had him and his service if (!found) { UserServicesTag userServicesTag = new UserServicesTag(userName, password, uri, null); Vector services = userServicesTag.getServices(); ServiceTag serviceTag = new ServiceTag(mainClass, fileName, serviceType, true); services.addElement(serviceTag); usersServices.addElement(userServicesTag); } 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) { File service = new File(uploadPath+ File.separatorChar +"tmp"+ File.separatorChar+fileName); if (!service.exists()) { System.out.println("no previous service to remove " +service.getName()); return; } 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+ File.separatorChar +"tmp"+ File.separatorChar+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(); } //Delete the jar file File file = new File(uploadPath+ File.separatorChar +"tmp"+ File.separatorChar+fileName); if (file.exists()) if (file.isFile()) file.delete(); //close the jar File try { jarFile.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -