1 23 24 29 30 package com.sun.enterprise.tools.upgrade.miscconfig; 31 32 40 import com.sun.enterprise.tools.upgrade.common.*; 41 import java.io.*; 42 import com.sun.enterprise.util.i18n.StringManager; 43 import com.sun.enterprise.tools.upgrade.logging.*; 44 import java.util.logging.*; 45 import java.util.Enumeration ; 46 import java.util.Collections ; 47 import java.util.LinkedList ; 48 import java.util.Iterator ; 49 import java.util.Vector ; 50 51 public class ConfigTransfers implements BaseModule{ 52 53 private static final String SECMODDB = "secmod.db"; 54 55 private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.miscconfig"); 56 private Logger logger = CommonInfoModel.getDefaultLogger(); 57 private Vector recoveryList = new Vector (); 58 59 62 public ConfigTransfers() { 63 64 } 65 66 public boolean upgrade(CommonInfoModel commonInfo) { 67 transferServerPolicy(commonInfo.getSourceServerPolicyFileName(), commonInfo.getTargetServerPolicyFileName()); 68 new DefaultWebXMLTransfer().transform(commonInfo.getSourceDefaultWebXMLFileName(), commonInfo.getTargetDefaultWebXMLFileName()); 70 new SunACCTransfer().transform(commonInfo.getSourceSunACCFileName(), commonInfo.getTargetSunACCFileName()); 71 72 if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){ 73 new InitConfTransfer(commonInfo).transform(); 74 75 }else{ 76 } 79 return true; 80 } 81 82 public void recovery(CommonInfoModel commonInfo) { 83 Enumeration e = recoveryList.elements(); 84 while(e.hasMoreElements()){ 85 String recoverPath = (String )e.nextElement(); 86 String backupPath = recoverPath + ".bak"; 87 try { 88 UpgradeUtils.getUpgradeUtils(commonInfo).copyFile(backupPath, recoverPath); 89 new File(backupPath).delete(); 90 } catch (IOException ioe) { 91 logger.log(Level.SEVERE, stringManager.getString("upgrade.realm.recoveryFailureMessage",ioe.getMessage()),new Object []{recoverPath,ioe}); 92 } 93 } 94 } 95 96 101 private void transferServerPolicy(String sourcePolicyFileName, String destPolicyFileName){ 102 if (!backup(destPolicyFileName)) { 103 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.backupFailureMessage")); 104 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage")); 105 return; 106 } 107 logger.log(Level.INFO, stringManager.getString("upgrade.configTransfers.serverPolicy.startMessage")); 108 PolicyParser sourcePolicy = new PolicyParser(); 109 PolicyParser targetPolicy = new PolicyParser(); 110 try { 111 sourcePolicy.read(new FileReader(sourcePolicyFileName)); 112 targetPolicy.read(new FileReader(destPolicyFileName)); 113 } catch (PolicyParser.ParsingException pe) { 114 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage") + pe.getLocalizedMessage()); 115 return; 116 } catch (IOException ioe) { 117 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage"),ioe.getMessage()); 118 return; 119 } 120 Enumeration sourceElements = sourcePolicy.grantElements(); 121 Enumeration targetElements = targetPolicy.grantElements(); 122 123 while(sourceElements.hasMoreElements()) { 124 PolicyParser.GrantEntry sourceGrantEntry = (PolicyParser.GrantEntry)sourceElements.nextElement(); 125 boolean matchedGrantEntry = false; 126 while (targetElements.hasMoreElements()) { 127 PolicyParser.GrantEntry targetGrantEntry = (PolicyParser.GrantEntry)targetElements.nextElement(); 128 if(targetGrantEntry.codeBase == null && sourceGrantEntry.codeBase == null) { 129 matchedGrantEntry = true; 130 } else if (targetGrantEntry.codeBase != null && sourceGrantEntry.codeBase != null) { 131 if (targetGrantEntry.codeBase.equals(sourceGrantEntry.codeBase)) { 132 matchedGrantEntry = true; 134 } 135 } 136 if(matchedGrantEntry) { 137 Enumeration sourcePermissions = sourceGrantEntry.permissionElements(); 139 while(sourcePermissions.hasMoreElements()) { 140 boolean matchedPermission = false; 141 PolicyParser.PermissionEntry sourcePermission = (PolicyParser.PermissionEntry)sourcePermissions.nextElement(); 142 145 Enumeration targetPermissions = targetGrantEntry.permissionElements(); 146 while(targetPermissions.hasMoreElements()) { 147 PolicyParser.PermissionEntry targetPermission = (PolicyParser.PermissionEntry)targetPermissions.nextElement(); 148 if(targetPermission.equals(sourcePermission)) { 149 matchedPermission = true; 150 break; 151 } 152 } 153 if(!matchedPermission){ 154 targetGrantEntry.add(sourcePermission); 155 } 156 } 157 Iterator sourcePrincipalIterator = sourceGrantEntry.principals.iterator(); 159 while(sourcePrincipalIterator.hasNext()) { 160 boolean matchedPrincipal = false; 161 PolicyParser.PrincipalEntry sourcePrincipalEntry = (PolicyParser.PrincipalEntry)sourcePrincipalIterator.next(); 162 165 Iterator targetPrincipalIterator = targetGrantEntry.principals.iterator(); 166 while(targetPrincipalIterator.hasNext()) { 167 PolicyParser.PrincipalEntry targetPrincipalEntry = (PolicyParser.PrincipalEntry)targetPrincipalIterator.next(); 168 if(targetPrincipalEntry.equals(sourcePrincipalEntry)) { 169 matchedPrincipal = true; 170 break; 171 } 172 } 173 if(!matchedPrincipal) { 174 targetGrantEntry.principals.add(sourcePrincipalEntry); 175 } 176 } 177 break; 178 } 179 } 180 if (!matchedGrantEntry) { 181 targetPolicy.add(sourceGrantEntry); 182 } 183 } 184 try { 185 targetPolicy.write(new FileWriter(destPolicyFileName)); 186 } catch (IOException ioe) { 187 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage"),ioe.getMessage()); 188 return; 189 } 190 } 191 192 private void transferSecModDb(CommonInfoModel commonInfo) { 193 String sourcePath = commonInfo.getSourceDomainPath(); 194 String targetPath = commonInfo.getDestinationDomainPath(); 195 File sourceFile = new File(sourcePath + File.separator + "config" + File.separator + SECMODDB); 196 File targetFile = new File(targetPath + File.separator + "config" + File.separator + SECMODDB); 197 if(!sourceFile.exists()) return; 198 if(targetFile.exists()) { 199 backup(targetFile.getAbsolutePath()); 200 } 201 try { 202 UpgradeUtils.getUpgradeUtils(commonInfo).copyFile(sourceFile.getAbsolutePath(), targetFile.getAbsolutePath()); 203 } catch(IOException e) { 204 logger.log(Level.WARNING, stringManager.getString("upgrade.configTransfers.secModDb.failureMessage") + e.getLocalizedMessage()); 205 } 206 } 207 208 private boolean backup(String filename) { 209 try{ 210 File targetFile = new File(filename); 211 boolean renamed = targetFile.renameTo(new File(filename +".bak")); 212 if(!renamed){ 213 renamed = targetFile.delete(); 215 } 216 if(renamed){ 217 targetFile = new File(filename); 218 targetFile.createNewFile(); 219 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename + ".bak"))); 220 PrintWriter writer = new PrintWriter(new FileOutputStream(targetFile)); 221 String readLine = null; 222 while((readLine = reader.readLine()) != null){ 223 writer.println(readLine); 224 } 225 writer.flush(); 226 writer.close(); 227 reader.close(); 228 return true; 229 }else{ 232 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.renameFailureMessage")); 234 } 235 }catch(Exception ex){ 236 logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage"),ex); 238 } 239 return false; 240 } 241 242 263 264 public String getName() { 265 return stringManager.getString("upgrade.configTransfers.moduleName"); 266 } 267 268 } 269 | Popular Tags |