KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > common > VersionExtracter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * VersionExtracter.java
26  *
27  * Created on March 8, 2004, 2:23 PM
28  */

29
30 package com.sun.enterprise.tools.upgrade.common;
31
32
33 /**
34  *
35  * @author prakash
36  */

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 JavaDoc;
43 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
44 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
45 import javax.xml.parsers.ParserConfigurationException JavaDoc;
46
47 import org.xml.sax.SAXException JavaDoc;
48 import org.xml.sax.SAXParseException JavaDoc;
49 import org.w3c.dom.Document JavaDoc;
50 import org.w3c.dom.DOMException JavaDoc;
51 import org.w3c.dom.Element JavaDoc;
52 import org.w3c.dom.Node JavaDoc;
53 import org.w3c.dom.NodeList JavaDoc;
54
55 public class VersionExtracter {
56
57     private String JavaDoc 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     /** Creates a new instance of VersionExtracter */
64     public VersionExtracter(String JavaDoc iD,CommonInfoModel common) {
65         this.installDir = iD;
66         this.common = common;
67     }
68     /*
69      * Returns a version no. and edition info.
70      * Returns null, if version cannot be retrived.
71      */

72     public String JavaDoc getVersion() {
73         String JavaDoc asadminString = "asadmin";
74         if(System.getProperty("os.name").indexOf("indows") != -1){
75             asadminString = "asadmin.bat";
76         }
77         String JavaDoc appserverVersion = null;
78         String JavaDoc asAdminFileStr = this.installDir+File.separator+"bin"+File.separator+asadminString;
79         if(new File(asAdminFileStr).exists()){
80             String JavaDoc execCommand = asAdminFileStr+" version";
81             try{
82                 java.lang.Process JavaDoc asadminProcess = Runtime.getRuntime().exec(execCommand);
83                 BufferedReader pInReader = new BufferedReader(new InputStreamReader(asadminProcess.getInputStream()));
84                 String JavaDoc inLine = null;
85                 while((inLine = pInReader.readLine()) != null){
86                     if((appserverVersion = this.parseVersion(inLine)) != null)
87                         break;
88                 }
89                 asadminProcess.destroy();
90             }catch(Exception JavaDoc ex){
91                 // returns null if any exception
92
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 JavaDoc parseVersion(String JavaDoc versionString){
101         String JavaDoc appservString = stringManager.getString("common.versionextracter.appserver.string");
102         String JavaDoc app7String = stringManager.getString("common.versionextracter.appserver.7string");
103         if((versionString.indexOf(appservString) != -1) && (versionString.indexOf(app7String) != -1)){
104             // Try extracting the string from configFile.
105
String JavaDoc verEdString = extractVersionFromConfigFile();
106             if(verEdString != null)
107                 return verEdString;
108             else
109                 return UpgradeConstants.VERSION_AS7X_PE;
110         }
111         String JavaDoc app80String = stringManager.getString("common.versionextracter.appserver.80string");
112         String JavaDoc appPEString = stringManager.getString("common.versionextracter.appserver.platformEdition");
113         String JavaDoc appSEString = stringManager.getString("common.versionextracter.appserver.standardEdition");
114         String JavaDoc 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                 // Do we have 80 SE?
120
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 JavaDoc 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 JavaDoc 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 JavaDoc formatVersionEditionStrings(String JavaDoc[] 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 JavaDoc extractVersionFromConfigFile(){
181         String JavaDoc versionString = null;
182         String JavaDoc editionString = null;
183
184         File configFile = getConfigFile();
185         if(configFile == null || !(configFile.exists()) )
186             return null;
187         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
188         //factory.setValidating(true);
189
factory.setNamespaceAware(true);
190         try {
191             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
192             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
193                 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
194             Document JavaDoc adminServerDoc = builder.parse(configFile);
195             String JavaDoc publicID = adminServerDoc.getDoctype().getPublicId();
196             String JavaDoc systemID = adminServerDoc.getDoctype().getSystemId();
197             // Mail from Hanu - updated on 06/25 - for 7.1 SE it is sun-server_1_1
198
//sun-server_1_0 - AS7.0 PE/SE
199
//sun-server_1_1 - AS 7.0 EE and 7.1 SE (updated)
200
//sun-server_1_2 - AS 7.1EE (7.1 SE uses sun-server_1_1. There is no 7.1PE)
201
String JavaDoc appservString = stringManager.getString("common.versionextracter.appserver.string");
202             String JavaDoc app7xPESEString = stringManager.getString("common.versionextracter.appserver.7xPESEConfigString");
203             String JavaDoc app70EEString = stringManager.getString("common.versionextracter.appserver.70EEConfigString");
204             String JavaDoc app71EEString = stringManager.getString("common.versionextracter.appserver.71EEConfigString");
205             if((publicID.indexOf(appservString) != -1) && ((systemID.indexOf(app7xPESEString) != -1))){
206                 versionString = UpgradeConstants.VERSION_7X;
207                 // This could be either 7.0PE, 7.0SE. Same binary bits. Just the licence is upgraded
208
editionString = this.getEditionFor70PEAnd70SE(configFile);
209             }
210             if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app70EEString) != -1)){
211                 // Either 7.0EE or 7.1 SE
212
versionString = UpgradeConstants.VERSION_7X;
213                 //editionString = UpgradeConstants.EDITION_EE;
214
editionString = this.getEditionFor70EEAnd71SE(adminServerDoc);
215             }
216             if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app71EEString) != -1)){
217                 // 7.1 EE
218
versionString = UpgradeConstants.VERSION_7X;
219                 editionString = UpgradeConstants.EDITION_EE;
220             }
221             String JavaDoc app80String = stringManager.getString("common.versionextracter.appserver.80ConfigString");
222             if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app80String) != -1)){
223                 versionString = UpgradeConstants.VERSION_80;
224                 // There is no SE or EE. There is only 80 PE.
225
editionString = UpgradeConstants.EDITION_PE;
226             }
227             String JavaDoc 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 JavaDoc app90String = stringManager.getString("common.versionextracter.appserver.90ConfigString");
232             if((publicID.indexOf(appservString) != -1) && (systemID.indexOf(app90String) != -1)){
233                 versionString = UpgradeConstants.VERSION_90;
234             }
235             // Check for edition.
236
if(editionString == null){
237                 NodeList JavaDoc taggedElements = adminServerDoc.getDocumentElement().getElementsByTagName("jvm-options");
238                 for(int lh =0; lh < taggedElements.getLength(); lh++){
239                     Element JavaDoc element = (Element JavaDoc)taggedElements.item(lh);
240                     String JavaDoc jvmOptionsData = getTextNodeData(element);
241                     if(versionString.equals(UpgradeConstants.VERSION_7X)){
242                         if((jvmOptionsData.indexOf("EEORBInitializer") != -1) || (jvmOptionsData.indexOf("EEIIOPSocketFactory") != -1)){
243                             // for 7X EE is already found from its dtd version. only need to find if it is SE
244
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 EE is not found set to PE by default. We still need to know how to differentiate SE and EE
255
if(editionString == null){
256                     editionString = UpgradeConstants.EDITION_PE;
257                 }
258             }
259             return formatVersionEditionStrings(new String JavaDoc[]{versionString,editionString});
260         }catch (Exception JavaDoc ex){
261             logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureMessage"),ex);
262             //throw new HarnessException(stringManager.getString("upgrade.transform.startFailureMessage"));
263
}
264         return null;
265     }
266     private String JavaDoc getEditionFor70PEAnd70SE(File configFile){
267         // Since binary bits are same and the only difference is lincence upgrade, check for multiple instances. If there are multiple instances
268
// then it is SE, otherwise assume PE.
269
String JavaDoc editionString = UpgradeConstants.EDITION_PE;
270         java.util.Hashtable JavaDoc domainMapping = this.extractDomainsMapping();
271         if(domainMapping != null && !domainMapping.isEmpty()){
272             for(java.util.Iterator JavaDoc dIt = domainMapping.values().iterator(); dIt.hasNext(); ){
273                 DomainInfo dInfo = (DomainInfo)dIt.next();
274                 // If this domain has more than one instance, then it should be SE.
275
if(dInfo.getInstanceNames().size() > 2)
276                     return UpgradeConstants.EDITION_SE;
277             }
278             return UpgradeConstants.EDITION_PE;
279         }else{
280             // This will be the case of inplace upgrade or sols 10 integration.,
281
String JavaDoc domainsDir = getDomainAndConfigDirs()[0];
282             File domains[] = new File(domainsDir).listFiles();
283             if(domains != null){
284                 for(int i=0; i < domains.length; i++){
285                     // check if the item is a directory.
286
if(domains[i].isDirectory()){
287                         // Get no. of instances in the directory.
288
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 JavaDoc getEditionFor70EEAnd71SE(Document JavaDoc doc){
298         // Both 70EE and 71 EE uses the same dtd. The differences are 1. EE has availability-service element, 2. EE has jvm-option wiht hadb root, 3. EE has session-config element
299
NodeList JavaDoc taggedElements = doc.getDocumentElement().getElementsByTagName("jvm-options");
300         for(int lh =0; lh < taggedElements.getLength(); lh++){
301             Element JavaDoc element = (Element JavaDoc)taggedElements.item(lh);
302             String JavaDoc jvmOptionsData = getTextNodeData(element);
303             if((jvmOptionsData.indexOf("com.sun.aas.hadbRoot") != -1)){
304                 return UpgradeConstants.EDITION_EE;
305             }
306         }
307         NodeList JavaDoc availabilityEles = doc.getDocumentElement().getElementsByTagName("availability-service");
308         if(availabilityEles.getLength() != 0)
309             return UpgradeConstants.EDITION_EE;
310         // This can be uncommented as and when needed. Not necessary for now.
311
//NodeList availabilityEles = doc.getDocumentElement().getElementsByTagName("session-config");
312
//if(availabilityEles.getLength() != 0)
313
//return UpgradeConstants.EDITION_EE;
314
return UpgradeConstants.EDITION_SE;
315     }
316     private java.util.Hashtable JavaDoc extractDomainsMapping(){
317         // If the source is 7.x then it is not guaranteed that domains directory lives under <install_dir>
318
// Should create the domainMapping and obtain the config file.
319
File runtime70Jar = new File(this.installDir+File.separator+"lib"+File.separator+"admingui.ear");
320         boolean notTargetInstallation7x = true;
321         //This check is required because target is not given yet as CLI input and target can not be 7.x
322
if(common.getTargetInstallDir() !=null)
323             notTargetInstallation7x = !(common.getTargetInstallDir().equals(this.installDir));
324         /*runtime70Jar != null) ||*/
325         if(runtime70Jar.exists() &&!(common.checkSourceInputAsDomainRoot(this.installDir)) && notTargetInstallation7x ) {
326             // Its a valid 7.x directory.
327
Appserver70DomainNamesResolver as =new Appserver70DomainNamesResolver(this.installDir);
328             java.util.Hashtable JavaDoc 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 JavaDoc domainMapping = extractDomainsMapping();
336         if(domainMapping != null && !domainMapping.isEmpty()){
337             DomainInfo dInfo = (DomainInfo)domainMapping.values().iterator().next();
338             // admin-server has server.xml that uses sun_server_1_0.dtd where as server1 uses other dtds. So, lets get server.xml from server1 or other
339
String JavaDoc instanceName = null;
340             for(java.util.Iterator JavaDoc instIt = dInfo.getInstanceNames().iterator();instIt.hasNext();){
341                 instanceName = (String JavaDoc)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 JavaDoc[] 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 JavaDoc[] getDomainAndConfigDirs(){
357         String JavaDoc domainsDir = this.installDir+File.separator+"domains";
358         String JavaDoc 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                 //check if "backup dir exists, if yes, go and get the config of latest created domain
365
// latest created domain will be the backedup domain.
366
if(new File(this.installDir +File.separator +"backup").exists()) {
367                     domainsDir = this.installDir;
368                     String JavaDoc backupdomainPath = common.findLatestDomainDir(this.installDir);
369                     String JavaDoc 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 { // Now get from domain1, config location
376
String JavaDoc[] directories = new File(this.installDir).list();
377                     String JavaDoc 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+"domain1";
387
domainsDir = this.installDir+File.separator+domainName;
388                     if(! (new File(domainsDir).exists())){
389                         // not a valid directory.
390
domainsDir = null;
391                         configDir = null;
392
393                     }else{
394                         String JavaDoc 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                         // installDir directory itself is domains dir
401
domainsDir = this.installDir;
402                     }
403                 }
404             }
405         } else if(! (new File(domainsDir).exists())){
406             // inplace upgrade ?
407
domainsDir = this.installDir+File.separator+"domains_bak";
408             if(! (new File(domainsDir).exists())){
409                 // solaris 10 integration dir ?
410
String JavaDoc[] chList = new File(this.installDir).list();
411                 //domainsDir = this.installDir+File.separator+"domain1";
412
if((chList == null)||(chList.length <= 0)){
413                     // not a valid directory.
414
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                         //Checking if input is some domain
420
if(new File(installDir +File.separator +"config").isDirectory()){
421                             configDir = installDir+File.separator+"config";
422                             domainsDir = installDir;
423                         }else {
424                         // not a valid directory.
425
domainsDir = null;
426                         configDir = null;
427                         }
428                     }else{
429                         String JavaDoc 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                         // installDir directory itself is domains dir
436
domainsDir = this.installDir;
437                     }
438                 }
439             }else{
440                 String JavaDoc[] chList = new File(domainsDir).list();
441                 configDir = domainsDir+File.separator+chList[0]+File.separator+"config";
442             }
443         }else{
444             String JavaDoc[] chList = new File(domainsDir).list();
445             if(chList != null){
446                 String JavaDoc 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 JavaDoc[]{domainsDir,configDir};
458     }
459     private String JavaDoc getTextNodeData(Element JavaDoc element){
460         NodeList JavaDoc 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 JavaDoc[] args){
470        //VersionExtracter ve = new VersionExtracter("C:\\Softwares\\Sun\\AppServer7");
471
}
472
473 }
474
Popular Tags