1 23 24 29 30 package com.sun.enterprise.tools.upgrade.miscconfig; 31 32 36 import java.io.*; 37 import java.util.logging.*; 38 import com.sun.enterprise.util.i18n.StringManager; 39 import com.sun.enterprise.tools.upgrade.logging.*; 40 41 public class SunACCTransfer { 42 43 private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.miscconfig"); 44 private Logger logger = com.sun.enterprise.tools.upgrade.common.CommonInfoModel.getDefaultLogger(); 45 private String docType = null; 46 private String secPolicyProperty = null; 47 48 public SunACCTransfer() { 49 } 50 public void transform(String sourceFileName, String targetFileName){ 51 logger.log(Level.INFO, stringManager.getString("upgrade.configTransfers.sunacc.startMessage")); 52 File sourceFile = new File(sourceFileName); 53 File targetFile = new File(targetFileName); 54 try { 55 docType = this.getDOCTYPEString(targetFile); 56 secPolicyProperty = this.getSecurityConfigValue(targetFile); 57 } catch(Exception e) {} 58 59 boolean renamed = targetFile.renameTo(new File(targetFileName+".bak")); 60 if(!renamed){ 61 renamed = targetFile.delete(); 63 } 64 if(renamed){ 65 try{ 66 targetFile = new File(targetFileName); 67 targetFile.createNewFile(); 68 this.transferFileContents(sourceFile, targetFile); 69 }catch(Exception ex){ 70 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.sunacc.startFailureMessage"),ex); 72 } 73 }else{ 74 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.sunacc.renameFailureMessage")); 76 } 77 } 78 private String getDOCTYPEString(File target) throws Exception { 79 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(target))); 80 String readLine = null; 81 while((readLine = reader.readLine()) != null){ 82 if(readLine.indexOf("<!DOCTYPE") != -1){ 83 return readLine; 84 } 85 } 86 return null; 87 } 88 89 private String getSecurityConfigValue(File target) throws Exception { 90 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(target))); 91 String readLine = null; 92 while((readLine = reader.readLine()) != null){ 93 if(readLine.indexOf("<property") != -1){ 94 if(readLine.indexOf("security.config") != -1) { 95 return readLine; 96 } 97 } 98 } 99 return null; 100 } 101 102 private void transferFileContents(File source, File target) throws Exception { 103 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(source))); 104 PrintWriter writer = new PrintWriter(new FileOutputStream(target)); 105 String readLine = null; 106 while((readLine = reader.readLine()) != null){ 107 if((readLine.indexOf("<!DOCTYPE") == -1) && (readLine.indexOf("<property") == -1)) { 108 writer.println(readLine); 109 } else if(readLine.indexOf("<!DOCTYPE") != -1){ 110 if(docType != null) 111 writer.println(docType); 112 else 113 writer.println(); 114 } else if(readLine.indexOf("<property") != -1) { 115 if(readLine.indexOf("security.config") != -1) { 116 if(secPolicyProperty != null) 117 writer.println(secPolicyProperty); 118 else 119 writer.println(); 120 } else { 121 writer.println(readLine); 122 } 123 } 124 } 125 writer.flush(); 126 writer.close(); 127 reader.close(); 128 } 129 } 130 | Popular Tags |