1 23 24 29 30 package com.sun.enterprise.tools.upgrade.common; 31 32 33 37 import java.io.*; 38 import com.sun.enterprise.util.i18n.StringManager; 39 import com.sun.enterprise.tools.upgrade.logging.*; 40 import java.util.logging.*; 41 42 import javax.xml.parsers.DocumentBuilder ; 43 import javax.xml.parsers.DocumentBuilderFactory ; 44 import javax.xml.parsers.FactoryConfigurationError ; 45 import javax.xml.parsers.ParserConfigurationException ; 46 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.SAXParseException ; 49 import org.w3c.dom.Document ; 50 import org.w3c.dom.DOMException ; 51 import org.w3c.dom.Element ; 52 import org.w3c.dom.Node ; 53 import org.w3c.dom.NodeList ; 54 55 public class VersionExtracter { 56 57 private String installDir; 58 59 private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.common"); 60 private Logger logger = CommonInfoModel.getDefaultLogger(); 61 private CommonInfoModel common; 62 63 64 public VersionExtracter(String iD,CommonInfoModel common) { 65 this.installDir = iD; 66 this.common = common; 67 } 68 72 public String getVersion() { 73 String asadminString = "asadmin"; 74 if(System.getProperty("os.name").indexOf("indows") != -1){ 75 asadminString = "asadmin.bat"; 76 } 77 String appserverVersion = null; 78 String asAdminFileStr = this.installDir+File.separator+"bin"+File.separator+asadminString; 79 if(new File(asAdminFileStr).exists()){ 80 String execCommand = asAdminFileStr+" version"; 81 try{ 82 java.lang.Process asadminProcess = Runtime.getRuntime().exec(execCommand); 83 BufferedReader pInReader = new BufferedReader(new InputStreamReader(asadminProcess.getInputStream())); 84 String inLine = null; 85 while((inLine = pInReader.readLine()) != null){ 86 if((appserverVersion = this.parseVersion(inLine)) != null) 87 break; 88 } 89 asadminProcess.destroy(); 90 }catch(Exception ex){ 91 logger.log(Level.SEVERE, stringManager.getString("common.versionextracter.getVersionError"), ex); 93 } 94 } 95 if(appserverVersion == null){ 96 appserverVersion = extractVersionFromConfigFile(); 97 } 98 return appserverVersion; 99 } 100 private String parseVersion(String versionString){ 101 String appservString = stringManager.getString("common.versionextracter.appserver.string"); 102 String app7String = stringManager.getString("common.versionextracter.appserver.7string"); 103 if((versionString.indexOf(appservString) != -1) && (versionString.indexOf(app7String) != -1)){ 104 String verEdString = extractVersionFromConfigFile(); 106 if(verEdString != null) 107 return verEdString; 108 else 109 return UpgradeConstants.VERSION_AS7X_PE; 110 } 111 String app80String = stringManager.getString("common.versionextracter.appserver.80string"); 112 String appPEString = stringManager.getString("common.versionextracter.appserver.platformEdition"); 113 String appSEString = stringManager.getString("common.versionextracter.appserver.standardEdition"); 114 String appEEString = stringManager.getString("common.versionextracter.appserver.enterpriseEdition"); 115 if((versionString.indexOf(appservString) != -1) && (versionString.indexOf(app80String) != -1)){ 116 if(versionString.indexOf(appPEString) != -1){ 117 return UpgradeConstants.VERSION_AS80_PE; 118 }else if(versionString.indexOf(appSEString) != -1){ 119 return UpgradeConstants.VERSION_AS80_SE; 121 }else if(versionString.indexOf(appEEString) != -1){ 122 return UpgradeConstants.VERSION_AS81_EE; 123 } 124 return null; 125 } 126 String app81String = stringManager.getString("common.versionextracter.appserver.81string"); 127 if((versionString.indexOf(appservString) != -1) && (versionString.indexOf(app81String) != -1)){ 128 if(versionString.indexOf(appPEString) != -1){ 129 return UpgradeConstants.VERSION_AS81_PE; 130 }else if(versionString.indexOf(appEEString) != -1){ 131 return UpgradeConstants.VERSION_AS81_EE; 132 } 133 return null; 134 } 135 String app90String = stringManager.getString("common.versionextracter.appserver.90string"); 136 if((versionString.indexOf(appservString) != -1) && (versionString.indexOf(app90String) != -1)){ 137 if(versionString.indexOf(appPEString) != -1){ 138 return UpgradeConstants.VERSION_AS90_PE; 139 }else if(versionString.indexOf(appEEString) != -1){ 140 return UpgradeConstants.VERSION_AS90_EE; 141 } 142 return null; 143 } 144 return null; 145 } 146 public String formatVersionEditionStrings(String [] verEd){ 147 if(verEd != null){ 148 if(verEd[0].equals(UpgradeConstants.VERSION_7X)){ 149 if(verEd[1].equals(UpgradeConstants.EDITION_PE)){ 150 return UpgradeConstants.VERSION_AS7X_PE; 151 }if(verEd[1].equals(UpgradeConstants.EDITION_SE)){ 152 return UpgradeConstants.VERSION_AS7X_SE; 153 }if(verEd[1].equals(UpgradeConstants.EDITION_EE)){ 154 return UpgradeConstants.VERSION_AS7X_EE; 155 } 156 }else if(verEd[0].equals(UpgradeConstants.VERSION_80)){ 157 if(verEd[1].equals(UpgradeConstants.EDITION_PE)){ 158 return UpgradeConstants.VERSION_AS80_PE; 159 }if(verEd[1].equals(UpgradeConstants.EDITION_SE)){ 160 return UpgradeConstants.VERSION_AS80_SE; 161 }if(verEd[1].equals(UpgradeConstants.EDITION_EE)){ 162 return UpgradeConstants.VERSION_AS81_EE; 163 } 164 }else if(verEd[0].equals(UpgradeConstants.VERSION_81)){ 165 if(verEd[1].equals(UpgradeConstants.EDITION_PE)){ 166 return UpgradeConstants.VERSION_AS81_PE; 167 }if(verEd[1].equals(UpgradeConstants.EDITION_EE)){ 168 return UpgradeConstants.VERSION_AS81_EE; 169 } 170 }else if(verEd[0].equals(UpgradeConstants.VERSION_90)){ 171 if(verEd[1].equals(UpgradeConstants.EDITION_PE)){ 172 return UpgradeConstants.VERSION_AS90_PE; 173 }if(verEd[1].equals(UpgradeConstants.EDITION_EE)){ 174 return UpgradeConstants.VERSION_AS90_EE; 175 } 176 } 177 } 178 return null; 179 } 180 private String extractVersionFromConfigFile(){ 181 String versionString = null; 182 String editionString = null; 183 184 File configFile = getConfigFile(); 185 if(configFile == null || !(configFile.exists()) ) 186 return null; 187 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 188 factory.setNamespaceAware(true); 190 try { 191 DocumentBuilder builder = factory.newDocumentBuilder(); 192 builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler )Class.forName 193 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance()); 194 Document adminServerDoc = builder.parse(configFile); 195 String publicID = adminServerDoc.getDoctype().getPublicId(); 196 String systemID = adminServerDoc.getDoctype().getSystemId(); 197 String appservString = stringManager.getString("common.versionextracter.appserver.string"); 202 String app7xPESEString = stringManager.getString("common.versionextracter.appserver.7xPESEConfigString"); 203 String app70EEString = stringManager.getString("common.versionextracter.appserver.70EEConfigString"); 204 String app71EEString = stringManager.getString("common.versionextracter.appserver.71EEConfigString"); 205 if((publicID.indexOf(appservString) != -1) && ((systemID.indexOf(app7xPESEString) != -1))){ 206 versionString = UpgradeConstants.VERSION_7X; 207 editionString = this.getEditionFor70PEAnd70SE(configFile); 209 } 210 if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app70EEString) != -1)){ 211 versionString = UpgradeConstants.VERSION_7X; 213 editionString = this.getEditionFor70EEAnd71SE(adminServerDoc); 215 } 216 if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app71EEString) != -1)){ 217 versionString = UpgradeConstants.VERSION_7X; 219 editionString = UpgradeConstants.EDITION_EE; 220 } 221 String app80String = stringManager.getString("common.versionextracter.appserver.80ConfigString"); 222 if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app80String) != -1)){ 223 versionString = UpgradeConstants.VERSION_80; 224 editionString = UpgradeConstants.EDITION_PE; 226 } 227 String app81String = stringManager.getString("common.versionextracter.appserver.81ConfigString"); 228 if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app81String) != -1)){ 229 versionString = UpgradeConstants.VERSION_81; 230 } 231 String app90String = stringManager.getString("common.versionextracter.appserver.90ConfigString"); 232 if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app90String) != -1)){ 233 versionString = UpgradeConstants.VERSION_90; 234 } 235 if(editionString == null){ 237 NodeList taggedElements = adminServerDoc.getDocumentElement().getElementsByTagName("jvm-options"); 238 for(int lh =0; lh < taggedElements.getLength(); lh++){ 239 Element element = (Element )taggedElements.item(lh); 240 String jvmOptionsData = getTextNodeData(element); 241 if(versionString.equals(UpgradeConstants.VERSION_7X)){ 242 if((jvmOptionsData.indexOf("EEORBInitializer") != -1) || (jvmOptionsData.indexOf("EEIIOPSocketFactory") != -1)){ 243 editionString = UpgradeConstants.EDITION_SE; 245 break; 246 } 247 }else{ 248 if((jvmOptionsData.indexOf("EEPluggableFeatureImpl") != -1)){ 249 editionString = UpgradeConstants.EDITION_EE; 250 break; 251 } 252 } 253 } 254 if(editionString == null){ 256 editionString = UpgradeConstants.EDITION_PE; 257 } 258 } 259 return formatVersionEditionStrings(new String []{versionString,editionString}); 260 }catch (Exception ex){ 261 logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureMessage"),ex); 262 } 264 return null; 265 } 266 private String getEditionFor70PEAnd70SE(File configFile){ 267 String editionString = UpgradeConstants.EDITION_PE; 270 java.util.Hashtable domainMapping = this.extractDomainsMapping(); 271 if(domainMapping != null && !domainMapping.isEmpty()){ 272 for(java.util.Iterator dIt = domainMapping.values().iterator(); dIt.hasNext(); ){ 273 DomainInfo dInfo = (DomainInfo)dIt.next(); 274 if(dInfo.getInstanceNames().size() > 2) 276 return UpgradeConstants.EDITION_SE; 277 } 278 return UpgradeConstants.EDITION_PE; 279 }else{ 280 String domainsDir = getDomainAndConfigDirs()[0]; 282 File domains[] = new File(domainsDir).listFiles(); 283 if(domains != null){ 284 for(int i=0; i < domains.length; i++){ 285 if(domains[i].isDirectory()){ 287 if(domains[i].list().length > 2 && common.isValid70Domain(domains[i].getPath())) 289 return UpgradeConstants.EDITION_SE; 290 } 291 } 292 return UpgradeConstants.EDITION_PE; 293 } 294 } 295 return UpgradeConstants.EDITION_PE; 296 } 297 private String getEditionFor70EEAnd71SE(Document doc){ 298 NodeList taggedElements = doc.getDocumentElement().getElementsByTagName("jvm-options"); 300 for(int lh =0; lh < taggedElements.getLength(); lh++){ 301 Element element = (Element )taggedElements.item(lh); 302 String jvmOptionsData = getTextNodeData(element); 303 if((jvmOptionsData.indexOf("com.sun.aas.hadbRoot") != -1)){ 304 return UpgradeConstants.EDITION_EE; 305 } 306 } 307 NodeList availabilityEles = doc.getDocumentElement().getElementsByTagName("availability-service"); 308 if(availabilityEles.getLength() != 0) 309 return UpgradeConstants.EDITION_EE; 310 return UpgradeConstants.EDITION_SE; 315 } 316 private java.util.Hashtable extractDomainsMapping(){ 317 File runtime70Jar = new File(this.installDir+File.separator+"lib"+File.separator+"admingui.ear"); 320 boolean notTargetInstallation7x = true; 321 if(common.getTargetInstallDir() !=null) 323 notTargetInstallation7x = !(common.getTargetInstallDir().equals(this.installDir)); 324 325 if(runtime70Jar.exists() &&!(common.checkSourceInputAsDomainRoot(this.installDir)) && notTargetInstallation7x ) { 326 Appserver70DomainNamesResolver as =new Appserver70DomainNamesResolver(this.installDir); 328 java.util.Hashtable domainsMapping = as.getDomainNamesPathMapping(); 329 common.setInstallConfig70(as.getConfigDir70(this.installDir)); 330 return domainsMapping; 331 } 332 return null; 333 } 334 private File getConfigFile(){ 335 java.util.Hashtable domainMapping = extractDomainsMapping(); 336 if(domainMapping != null && !domainMapping.isEmpty()){ 337 DomainInfo dInfo = (DomainInfo)domainMapping.values().iterator().next(); 338 String instanceName = null; 340 for(java.util.Iterator instIt = dInfo.getInstanceNames().iterator();instIt.hasNext();){ 341 instanceName = (String )instIt.next(); 342 if(!instanceName.equals("admin-server")) 343 break; 344 } 345 return new File(dInfo.getInstancePath(instanceName)+File.separator+"config"+File.separator+"server.xml"); 346 } 347 String [] dCDirs = getDomainAndConfigDirs(); 348 if(dCDirs == null) 349 return null; 350 if(dCDirs[1].indexOf("server1") != -1){ 351 return new File(dCDirs[1]+File.separator+"server.xml"); 352 }else{ 353 return new File(dCDirs[1]+File.separator+"domain.xml"); 354 } 355 } 356 public String [] getDomainAndConfigDirs(){ 357 String domainsDir = this.installDir+File.separator+"domains"; 358 String configDir = null; 359 boolean domainRootSame = false; 360 if(common.checkSourceInputAsDomainRoot(this.installDir)) 361 domainRootSame = new File(common.getSourceDomainRoot()).equals(new File(common.getTargetDomainRoot())); 362 if(common.checkSourceInputAsDomainRoot(this.installDir) && domainRootSame ) { 363 if(domainRootSame) { 364 if(new File(this.installDir +File.separator +"backup").exists()) { 367 domainsDir = this.installDir; 368 String backupdomainPath = common.findLatestDomainDir(this.installDir); 369 String serverDir = backupdomainPath+File.separator+"server1"; 370 if(! (new File(serverDir).exists())){ 371 configDir = backupdomainPath+File.separator+"config"; 372 }else{ 373 configDir = backupdomainPath+File.separator+"server1"+File.separator+"config"; 374 } 375 } else { String [] directories = new File(this.installDir).list(); 377 String domainName = null; 378 for(int j =0;j<directories.length;j++) { 379 if(directories[j].equals("backup") || new File(this.installDir + File.separator + directories[j]).isFile()) 380 continue; 381 else { 382 domainName = directories[j]; 383 break; 384 } 385 } 386 domainsDir = this.installDir+File.separator+domainName; 388 if(! (new File(domainsDir).exists())){ 389 domainsDir = null; 391 configDir = null; 392 393 }else{ 394 String domainsDir2 = domainsDir+File.separator+"server1"; 395 if(! (new File(domainsDir2).exists())){ 396 configDir = domainsDir+File.separator+"config"; 397 }else{ 398 configDir = domainsDir+File.separator+"server1"+File.separator+"config"; 399 } 400 domainsDir = this.installDir; 402 } 403 } 404 } 405 } else if(! (new File(domainsDir).exists())){ 406 domainsDir = this.installDir+File.separator+"domains_bak"; 408 if(! (new File(domainsDir).exists())){ 409 String [] chList = new File(this.installDir).list(); 411 if((chList == null)||(chList.length <= 0)){ 413 domainsDir = null; 415 configDir = null; 416 }else{ 417 domainsDir = this.installDir+File.separator+chList[0]; 418 if(! (new File(domainsDir+File.separator+"config").exists())){ 419 if(new File(installDir +File.separator +"config").isDirectory()){ 421 configDir = installDir+File.separator+"config"; 422 domainsDir = installDir; 423 }else { 424 domainsDir = null; 426 configDir = null; 427 } 428 }else{ 429 String domainsDir2 = domainsDir+File.separator+"server1"; 430 if(! (new File(domainsDir2).exists())){ 431 configDir = domainsDir+File.separator+"config"; 432 }else{ 433 configDir = domainsDir+File.separator+"server1"+File.separator+"config"; 434 } 435 domainsDir = this.installDir; 437 } 438 } 439 }else{ 440 String [] chList = new File(domainsDir).list(); 441 configDir = domainsDir+File.separator+chList[0]+File.separator+"config"; 442 } 443 }else{ 444 String [] chList = new File(domainsDir).list(); 445 if(chList != null){ 446 String domainsDir2 = domainsDir+File.separator+chList[0]+File.separator+"server1"; 447 if(! (new File(domainsDir2).exists())){ 448 configDir = domainsDir+File.separator+chList[0]+File.separator+"config"; 449 }else{ 450 configDir = domainsDir+File.separator+chList[0]+File.separator+"server1"+File.separator+"config"; 451 } 452 } 453 } 454 if(configDir == null){ 455 return null; 456 } 457 return new String []{domainsDir,configDir}; 458 } 459 private String getTextNodeData(Element element){ 460 NodeList children = element.getChildNodes(); 461 for(int index=0; index < children.getLength(); index++){ 462 if(children.item(index).getNodeType() == Node.TEXT_NODE){ 463 return children.item(index).getNodeValue(); 464 } 465 } 466 return null; 467 } 468 469 public static void main(String [] args){ 470 } 472 473 } 474 | Popular Tags |