1 23 24 29 30 package com.sun.enterprise.tools.upgrade.realm; 31 32 import com.sun.enterprise.tools.upgrade.transform.elements.*; 33 import java.io.File ; 34 import java.io.IOException ; 35 36 import javax.xml.parsers.DocumentBuilder ; 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 import javax.xml.parsers.FactoryConfigurationError ; 39 import javax.xml.parsers.ParserConfigurationException ; 40 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.SAXParseException ; 43 44 import java.io.File ; 45 import java.io.FileReader ; 46 import java.io.FileWriter ; 47 import java.io.IOException ; 48 import java.io.FileInputStream ; 49 import java.io.FileOutputStream ; 50 import java.io.BufferedReader ; 51 import java.io.BufferedWriter ; 52 import java.io.FileNotFoundException ; 53 import java.nio.channels.FileChannel ; 54 import java.nio.MappedByteBuffer ; 55 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.DOMException ; 58 import org.w3c.dom.Element ; 59 import org.w3c.dom.Node ; 60 import org.w3c.dom.NodeList ; 61 import org.w3c.dom.NamedNodeMap ; 62 63 import javax.xml.transform.Transformer ; 64 import javax.xml.transform.TransformerFactory ; 65 import javax.xml.transform.TransformerException ; 66 import javax.xml.transform.TransformerConfigurationException ; 67 68 import javax.xml.transform.dom.DOMSource ; 69 import javax.xml.transform.stream.StreamResult ; 70 import javax.xml.transform.OutputKeys ; 71 72 import com.sun.enterprise.tools.upgrade.common.CommonInfoModel; 73 import com.sun.enterprise.tools.upgrade.common.UpgradeConstants; 74 import com.sun.enterprise.tools.upgrade.common.UpdateProgressManager; 75 import com.sun.enterprise.util.i18n.StringManager; 76 import com.sun.enterprise.util.RelativePathResolver; 77 import com.sun.enterprise.tools.upgrade.logging.*; 78 import java.util.logging.*; 79 import java.util.*; 80 import java.util.jar.*; 81 import java.util.zip.*; 82 83 87 public class RealmUpgrade implements com.sun.enterprise.tools.upgrade.common.BaseModule { 88 89 private String AS7_FILE_REALM = "com.iplanet.ias.security.auth.realm.file.FileRealm"; 90 private String AS7_LDAP_REALM = "com.iplanet.ias.security.auth.realm.ldap.LDAPRealm"; 91 private String AS7_CERTIFICATE_REALM = "com.iplanet.ias.security.auth.realm.certificate.CertificateRealm"; 92 private String AS7_SOLARIS_REALM = "com.iplanet.ias.security.auth.realm.solaris.SolarisRealm"; 93 private String AS8_FILE_REALM = "com.sun.enterprise.security.auth.realm.file.FileRealm"; 94 private String AS8_LDAP_REALM = "com.sun.enterprise.security.auth.realm.ldap.LDAPRealm"; 95 private String AS8_CERTIFICATE_REALM = "com.sun.enterprise.security.auth.realm.certificate.CertificateRealm"; 96 private String AS8_SOLARIS_REALM = "com.sun.enterprise.security.auth.realm.solaris.SolarisRealm"; 97 98 private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.realm"); 99 private Logger logger = CommonInfoModel.getDefaultLogger(); 100 private CommonInfoModel commonInfo = null; 101 private Vector recoveryList = new Vector(); 102 103 104 public RealmUpgrade() { 105 } 106 107 public boolean upgrade(CommonInfoModel commonInfoModel) { 108 logger.log(Level.INFO, stringManager.getString("upgrade.realm.startMessage")); 109 this.commonInfo = commonInfoModel; 110 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 111 factory.setNamespaceAware(true); 113 if(commonInfo.getSourceDomainRootFlag()) { 114 factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",Boolean.FALSE); 115 } 116 String realmname =""; 117 try { 118 DocumentBuilder builder = factory.newDocumentBuilder(); 119 if(!commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){ 120 builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler )Class.forName 121 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance()); 122 } 123 Document sourceDoc = builder.parse( new File (commonInfo.getSourceConfigXMLFile())); 124 NodeList nl = sourceDoc.getElementsByTagName("auth-realm"); 126 for(int i =0; i < nl.getLength(); i++){ 127 Node node = nl.item(i); 128 NamedNodeMap attributes = node.getAttributes(); 129 130 if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){ 131 String classname = (attributes.getNamedItem("classname")).getNodeValue(); 133 if (!(classname.equals(AS7_FILE_REALM) || classname.equals(AS7_LDAP_REALM) || 134 classname.equals(AS7_CERTIFICATE_REALM) || classname.equals(AS7_SOLARIS_REALM))) { 135 NodeList nlist = sourceDoc.getElementsByTagName("java-config"); 136 Node njava = nlist.item(0); NamedNodeMap java_attrs = njava.getAttributes(); 138 String classpath = (java_attrs.getNamedItem("server-classpath")).getNodeValue(); 139 migrateClass(classname, classpath); 140 } 141 } else if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_80) || 142 commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_81)){ 143 String classname = (attributes.getNamedItem("classname")).getNodeValue(); 145 if (!(classname.equals(AS8_FILE_REALM) || classname.equals(AS8_LDAP_REALM) || 146 classname.equals(AS8_CERTIFICATE_REALM) || classname.equals(AS8_SOLARIS_REALM))) { 147 NodeList nlist = sourceDoc.getElementsByTagName("java-config"); 148 Node njava = nlist.item(0); NamedNodeMap java_attrs = njava.getAttributes(); 150 String classpath = (java_attrs.getNamedItem("server-classpath")).getNodeValue(); 151 migrateClass(classname, classpath); 152 } 153 } 154 realmname = (attributes.getNamedItem("name")).getNodeValue(); 156 String classname = (attributes.getNamedItem("classname")).getNodeValue(); 157 if ( classname.equals(AS8_FILE_REALM) ) { 158 NodeList props = node.getChildNodes(); 159 for( int j=0; j < props.getLength(); j++ ) { 160 Node propnode = props.item(j); 161 if(propnode.getNodeName().equals("property")) { NamedNodeMap attrs = propnode.getAttributes(); 163 if (attrs != null && (attrs.getNamedItem("name").getNodeValue()).equals("file")) { 164 Node valueNode = attrs.getNamedItem("value"); 165 System.setProperty("com.sun.aas.instanceRoot", commonInfo.getSourceInstancePath()); 166 String rawSourceRealmPath = valueNode.getNodeValue(); 167 String sourceRealmPath = RelativePathResolver.resolvePath(rawSourceRealmPath); 168 File sourceRealmFile = new File (sourceRealmPath); 169 String targetRealmPath = commonInfo.getDestinationDomainPath() + File.separator + commonInfo.CONFIG + File.separator + sourceRealmFile.getName(); 170 File targetRealmFile = new File (targetRealmPath); 171 backup(targetRealmPath); transferKeys(sourceRealmFile, targetRealmFile, builder); 173 } 174 } 175 } 176 } 177 } 178 179 } catch (Exception ex){ 180 logger.log(Level.SEVERE, stringManager.getString("upgrade.realm.migrationFailureMessage",ex.getMessage()),new Object [] {realmname,ex}); 181 ex.printStackTrace(); 182 UpdateProgressManager.getProgressManager().setContinueUpgrade(false); 183 return false; 184 } 185 return true; 186 } 187 188 private void backup(String filePath) throws IOException { 189 String backupFilePath = filePath + ".bak"; 190 copyFile(filePath, backupFilePath); 191 recoveryList.add(filePath); 192 } 193 194 public void recovery(CommonInfoModel commonInfo) { 195 Enumeration e = recoveryList.elements(); 196 while(e.hasMoreElements()){ 197 String recoverPath = (String )e.nextElement(); 198 String backupPath = recoverPath + ".bak"; 199 try { 200 copyFile(backupPath, recoverPath); 201 new File (backupPath).delete(); 202 } catch (IOException ioe) { 203 logger.log(Level.SEVERE, stringManager.getString("upgrade.realm.recoveryFailureMessage",ioe.getMessage()),new Object []{recoverPath,ioe}); 204 } 205 } 206 207 } 208 209 210 private void migrateClass(String classname, String classpath) { 214 boolean found = false; 215 StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator, false); 216 String [] fileList = new String [st.countTokens()]; 217 for(int i=0;i<fileList.length;i++) { 218 fileList[i] = st.nextToken(); 219 } 220 String targetDir = commonInfo.getDestinationDomainPath() + File.separator + "lib" + File.separator + "ext"; 221 String file = classname; 222 for(int i =0;i<fileList.length;i++) { 223 String fileName = fileList[i]; 224 if(fileName.endsWith(".jar")) { 225 try { 226 JarFile jarFile = new JarFile(fileName); 227 Enumeration ee = jarFile.entries(); 228 while(ee.hasMoreElements()) { 229 ZipEntry entry = (ZipEntry)ee.nextElement(); 230 String entryString = entry.toString(); 231 String className = entryString.replaceAll("/","."); 232 if(className.equals(file)) { 233 String target = targetDir + File.pathSeparatorChar + jarFile.getName(); 234 copyFile(fileName,target); 235 updateDomainClassPath(jarFile.getName()); 236 found = true; 237 break; 238 } 239 } 240 } catch (IOException ioe) { 241 (commonInfo.getDefaultLogger()).info(stringManager.getString("upgrade.realm.customRealmClassMessage") 242 + fileName + stringManager.getString("upgrade.realm.IOExceptionMessage")); 243 } 244 } 245 246 } 247 if(!found) { 248 (commonInfo.getDefaultLogger()).info(stringManager.getString("upgrade.realm.customRealmClassMessage") 249 + classname + stringManager.getString("upgrade.realm.manuallyRelocatedMessage")); 250 } 251 } 252 253 private void copyFile(String source, String target) throws IOException { 254 copyFile(source, target, false); 255 } 256 257 public void copyFile(String source, String target, boolean append) throws IOException { 258 FileReader in = new FileReader (new File (source)); 260 FileWriter out = new FileWriter (new File (target), append); 261 int c; 262 while ((c = in.read()) != -1) { 263 out.write(c); 264 } 265 in.close(); 266 out.close(); 267 } 268 269 private void updateDomainClassPath(String fileName){ 270 logger.log(Level.INFO, stringManager.getString("upgrade.realm.updateDomainClassPathMessage")); 272 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 273 factory.setNamespaceAware(true); 275 try { 276 DocumentBuilder builderDomainXml = factory.newDocumentBuilder(); 277 builderDomainXml.setEntityResolver((org.xml.sax.helpers.DefaultHandler )Class.forName 278 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance()); 279 Document resultDoc = builderDomainXml.parse( new File (commonInfo.getTargetConfigXMLFile()) ); 280 this.updateClassPathString(resultDoc,fileName); 281 282 284 TransformerFactory tFactory = TransformerFactory.newInstance(); 286 Transformer transformer = tFactory.newTransformer(); 287 if (resultDoc.getDoctype() != null){ 288 String systemValue = resultDoc.getDoctype().getSystemId(); 289 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue); 290 String pubValue = resultDoc.getDoctype().getPublicId(); 291 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubValue); 292 } 293 DOMSource source = new DOMSource (resultDoc); 294 StreamResult result = new StreamResult (new FileOutputStream (commonInfo.getTargetConfigXMLFile())); 295 transformer.transform(source, result); 296 }catch (Exception ex){ 297 logger.log(Level.SEVERE, stringManager.getString("upgrade.realm.updateDomainFailureMessage"),ex); 298 } 299 } 300 private void updateClassPathString(Document domainXML, String fileName){ 301 Element docEle = domainXML.getDocumentElement(); 303 NodeList configList = docEle.getElementsByTagName("java-config"); 304 Element javaConfElement = (Element )configList.item(0); 306 javaConfElement.setAttribute("server-classpath", javaConfElement.getAttribute("server-classpath")+"${path.separator}${com.sun.aas.installRoot}"+ 307 File.separator+"lib"+File.separator+"ext"+File.separator+fileName); 308 } 309 310 public String getName() { 311 return stringManager.getString("upgrade.realm.moduleName"); 312 } 313 314 private void transferKeys(File sourceRealmFile, File targetRealmFile, DocumentBuilder builder) throws FileNotFoundException , IOException , SAXException { 315 Document sourceDoc = builder.parse( new File (commonInfo.getTargetConfigXMLFile())); 316 BufferedReader reader = new BufferedReader (new FileReader (sourceRealmFile)); 317 BufferedWriter writer = new BufferedWriter (new FileWriter (targetRealmFile)); 318 String entry; 319 while(reader.ready()) { 320 entry = reader.readLine(); 321 if( entry.startsWith("admin") && !commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_81)) { 322 NodeList nl = sourceDoc.getElementsByTagName("auth-realm"); 326 for(int i =0; i < nl.getLength(); i++){ 327 Node node = nl.item(i); 328 NamedNodeMap attributes = node.getAttributes(); 329 String name = (attributes.getNamedItem("name")).getNodeValue(); 330 if (name.equals("admin-realm")) { 331 NodeList props = node.getChildNodes(); 333 for( int j=0; j < props.getLength(); j++ ) { 334 Node propnode = props.item(j); 335 if(propnode.getNodeName().equals("property")) { NamedNodeMap attrs = propnode.getAttributes(); 337 if (attrs != null && (attrs.getNamedItem("name").getNodeValue()).equals("file")) { 338 Node valueNode = attrs.getNamedItem("value"); 339 System.setProperty("com.sun.aas.instanceRoot", commonInfo.getDestinationDomainPath()); 340 String rawSourceRealmPath = valueNode.getNodeValue(); 341 String adminRealmPath = RelativePathResolver.resolvePath(rawSourceRealmPath); 342 File adminRealmFile = new File (adminRealmPath); 343 backup(adminRealmPath); BufferedWriter adminRealmWriter = new BufferedWriter (new FileWriter (adminRealmFile)); 345 adminRealmWriter.write(entry); 346 adminRealmWriter.newLine(); 347 adminRealmWriter.write("# Domain User and Password - Do Not Delete Entry Above"); 348 adminRealmWriter.close(); 349 } 350 } 351 } 352 } 353 } 354 } else { 355 if(!entry.startsWith("#")) { writer.write(entry); 357 writer.newLine(); 358 } 359 } 360 } 361 writer.close(); 362 reader.close(); 363 } 364 } 365 | Popular Tags |