KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Utils.java
26  *
27  * Created on April 19, 2004, 11:47 AM
28  */

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

36 import java.io.*;
37 import java.util.*;
38 import java.util.logging.*;
39 import com.sun.enterprise.tools.upgrade.logging.*;
40 import com.sun.enterprise.util.i18n.StringManager;
41 import com.sun.enterprise.tools.upgrade.cluster.*;
42
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.w3c.dom.Node JavaDoc;
47 import org.w3c.dom.NamedNodeMap JavaDoc;
48
49 import javax.xml.parsers.DocumentBuilder JavaDoc;
50 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
51 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
52 import javax.xml.parsers.ParserConfigurationException JavaDoc;
53
54 import javax.xml.transform.Transformer JavaDoc;
55 import javax.xml.transform.TransformerFactory JavaDoc;
56 import javax.xml.transform.TransformerException JavaDoc;
57 import javax.xml.transform.TransformerConfigurationException JavaDoc;
58
59 import javax.xml.transform.dom.DOMSource JavaDoc;
60 import javax.xml.transform.stream.StreamResult JavaDoc;
61 import javax.xml.transform.OutputKeys JavaDoc;
62
63 import com.sun.enterprise.tools.upgrade.transform.elements.BaseElement;
64 import sun.java2d.pipe.AATextRenderer;
65
66 public class UpgradeUtils {
67     private static UpgradeUtils upgradeUtils;
68     private StringManager stringManager = StringManager.getManager(LogService.UPGRADE_LOGGER);
69     private static Logger logger=LogService.getLogger(LogService.UPGRADE_LOGGER);
70     /** Creates a new instance of Utils */
71     private static CommonInfoModel common;
72     private UpgradeUtils(CommonInfoModel common) {
73         this.common = common;
74     }
75     public static UpgradeUtils getUpgradeUtils(CommonInfoModel cim){
76         //START CR 6348866
77
if(upgradeUtils == null) {
78             upgradeUtils = new UpgradeUtils(cim);
79         } else {
80             common = cim;
81         }
82         //END CR 6348866
83
return upgradeUtils;
84     }
85     public Hashtable enlist70DomainsFromSource(CommonInfoModel cmi){
86         String JavaDoc source = cmi.getSourceInstallDir();
87         Hashtable domainMapping = null;
88         if(!checkSourceInputAsDomainRoot(source,cmi)) {
89             cmi.getDomainList().clear();
90             Appserver70DomainNamesResolver as =new Appserver70DomainNamesResolver(source);
91             domainMapping = as.getDomainNamesPathMapping();
92             cmi.setInstallConfig70(as.getConfigDir70(source));
93             cmi.setDomainMapping(domainMapping);
94             Enumeration ee = domainMapping.keys();
95             while (ee.hasMoreElements()) {
96                 String JavaDoc domain = (String JavaDoc)ee.nextElement();
97                 cmi.addDomainName(domain);
98                 cmi.getTargetDomainNameMapping().put(domain,domain);
99             }
100         } else { // Input source is domainRoot now
101
File sourceDomain = new File(source);
102             String JavaDoc [] dirs = sourceDomain.list();
103             //sourceInputAsDomainRoot
104
/*if(dirs.length == 0) {
105                 sourceInputAsDomainRoot = false;
106                 return sourceInputAsDomainRoot;
107             } */

108             String JavaDoc domain = "";
109             domainMapping = new Hashtable();
110             for (int i=0;i<dirs.length;i++) {
111                 String JavaDoc domainPath=source + File.separator + dirs[i];
112                 File file = new File(domainPath);
113                 if(!(dirs[i].endsWith("8x")) && !(file.isFile()) && isValid70Domain(domainPath)) {
114                     domain=dirs[i];
115                     domainMapping.put(domain,new DomainInfo(domain,domainPath));
116                     cmi.addDomainName(domain);
117                     if(new File(cmi.getSourceDomainRoot()).equals(new File(cmi.getTargetDomainRoot())))
118                         cmi.getTargetDomainNameMapping().put(domain,domain+"_8x");
119                     else
120                         cmi.getTargetDomainNameMapping().put(domain,domain);
121                 }
122             }
123         }
124         System.setProperty("com.sun.aas.configRoot", cmi.getTargetInstallDir() + File.separator + "config");
125         return domainMapping;
126     }
127     public Hashtable enlistDomainsFromDomainsDirectory(CommonInfoModel cmi, String JavaDoc domainsDirStr){
128         File domainsDir = new File(domainsDirStr);
129         String JavaDoc [] dirs = domainsDir.list();
130         return enlistDomains(cmi,dirs, domainsDirStr);
131     }
132     
133     public Hashtable enlistDomains(CommonInfoModel cmi, String JavaDoc [] dirs, String JavaDoc domainRoot) {
134         Hashtable domainMapping = new Hashtable();
135         boolean domainRootSame = new File(cmi.getSourceDomainRoot()).equals(new File(cmi.getTargetDomainRoot()));
136         for (int i=0;i<dirs.length;i++) {
137             if(dirs[i].equals("backup"))
138                 continue;
139             String JavaDoc domainPath=domainRoot + File.separator + dirs[i];
140             if(new File(domainPath+"_8x").exists())
141                 continue;
142             File domainFile = new File(domainPath);
143             if(domainFile.isFile())
144                 continue;
145             if(domainRootSame) {
146                 //String latestdomain = findLatestDomainDir(cmi.getSourceDomainRoot(),null);
147
String JavaDoc latestdomain = findLatestDomainDir(cmi.getSourceDomainRoot(),dirs[i]);
148                 String JavaDoc backupDomainVersion = "";
149                 String JavaDoc actualDomainVersion = "garbage";
150                 if(latestdomain != null) {
151                     backupDomainVersion = (new VersionExtracter(latestdomain,cmi)).getVersion();
152                     String JavaDoc[] directories = new File(cmi.getSourceDomainRoot()).list();
153                     String JavaDoc domainName = null;
154                     for(int j =0;j<directories.length;j++) {
155                         if(directories[j].equals("backup") || new File(cmi.getSourceDomainRoot() + File.separator + directories[j]).isFile())
156                             continue;
157                         else {
158                             domainName = directories[j];
159                             break;
160                         }
161                     }
162                     //actualDomainVersion = (new VersionExtracter(cmi.getSourceDomainRoot() + File.separator + domainName,cmi)).getVersion();
163
actualDomainVersion = (new VersionExtracter(cmi.getSourceDomainRoot() + File.separator + dirs[i],cmi)).getVersion();
164                 }
165                 if(!actualDomainVersion.equals(cmi.getTargetVersionAndEdition()) && !actualDomainVersion.equals(backupDomainVersion)) {
166                     String JavaDoc timestamp=""+System.currentTimeMillis();
167                     String JavaDoc newDomainName = "";
168                     String JavaDoc domainName = dirs[i];
169                     if(dirs[i].endsWith("_8x")) {
170                         newDomainName = dirs[i].substring(0,dirs[i].indexOf("_8x"))+"_"+timestamp;
171                         domainName = dirs[i].substring(0,dirs[i].indexOf("_8x"));
172                     } else {
173                         newDomainName = domainName+"_"+timestamp;
174                     }
175                     String JavaDoc backup = cmi.getTargetDomainRoot()+File.separator+"backup";
176                     File backupFile = new File(backup);
177                     if(!(backupFile.isDirectory())) {
178                         backupFile.delete();
179                         backupFile.mkdir();
180                     }
181                     File domainDir = new File(domainPath);
182                     String JavaDoc sourceDomainPath = backup+File.separator+newDomainName;
183                     //This check will take care of all the domain ending with _8x
184
if(new File(domainPath+"_8x").exists()) {
185                         new File(domainPath+"_8x").renameTo(new File(sourceDomainPath));
186                         domainDir.delete();
187                     } else
188                         domainDir.renameTo(new File(sourceDomainPath));
189                     domainMapping.put(domainName,new DomainInfo(domainName,sourceDomainPath));
190                     cmi.addDomainName(domainName);
191                     cmi.getTargetDomainNameMapping().put(domainName,domainName);
192                 } else {
193                     String JavaDoc latestDomain = findLatestDomainDir(cmi.getTargetDomainRoot(),dirs[i]);
194                     domainMapping.put(dirs[i],new DomainInfo(dirs[i],latestDomain));
195                     cmi.addDomainName(dirs[i]);
196                     cmi.getTargetDomainNameMapping().put(dirs[i],dirs[i]);
197                 }
198             } else {
199                 domainMapping.put(dirs[i],new DomainInfo(dirs[i],domainPath));
200                 cmi.addDomainName(dirs[i]);
201                 cmi.getTargetDomainNameMapping().put(dirs[i],dirs[i]);
202                 /*if(new File(cmi.getSourceDomainRoot()).getPath().equals(new File(cmi.getTargetDomainRoot()).getPath()))
203                     cmi.getTargetDomainNameMapping().put(dirs[i],dirs[i]+"_8x");
204                 else
205                     cmi.getTargetDomainNameMapping().put(dirs[i],dirs[i]); */

206             }
207         }
208         return domainMapping;
209     }
210     public Hashtable enlistDomainsFromUserDefinedDirectories(List JavaDoc srcDomainDirs){
211         Hashtable domainMapping = null;
212         for(Iterator dIt = srcDomainDirs.iterator(); dIt.hasNext();){
213             if(domainMapping == null) domainMapping = new Hashtable();
214             String JavaDoc domainDirStr = (String JavaDoc)dIt.next();
215             Hashtable dMaps = this.enlistDomainsFromDomainsDirectory(this.common, domainDirStr);
216             if(dMaps != null){
217                 for(Enumeration domainsEnum = dMaps.keys();domainsEnum.hasMoreElements();){
218                     String JavaDoc dName = (String JavaDoc)domainsEnum.nextElement();
219                     domainMapping.put(dName, dMaps.get(dName));
220                 }
221             }
222         }
223         return domainMapping;
224     }
225     public String JavaDoc findLatestDomainDir(String JavaDoc domainRoot,String JavaDoc domainName) {
226         File backupDir = new File(domainRoot+File.separator+"backup");
227         String JavaDoc lastestDomainPath=null;
228         long latestTimestamp = 0;
229         if(backupDir.isDirectory()) {
230             String JavaDoc [] dirs = backupDir.list();
231             for(int i=0;i<dirs.length;i++) {
232                 String JavaDoc time = dirs[i].substring(dirs[i].lastIndexOf("_")+1);
233                 if(domainName != null )
234                     if(!(dirs[i].startsWith(domainName)))
235                         continue;
236                 long timestamp = Long.parseLong(time);
237                 if(timestamp > latestTimestamp) {
238                     latestTimestamp = timestamp;
239                     lastestDomainPath = domainRoot+File.separator+"backup"+File.separator+dirs[i];
240                 }
241             }
242         }
243         return lastestDomainPath;
244     }
245     
246     public boolean checkSourceInputAsDomain(){
247         //this supports 8.x domains only
248
//TODO 7.x support for single domains
249
File domainXML = new File(common.getSourceInstallDir() + File.separator + "config" + File.separator + "domain.xml");
250         if(domainXML.isFile()) {
251             return true;
252         }
253         return false;
254     }
255     
256     public boolean checkSourceInputAsDomainRoot(String JavaDoc source, CommonInfoModel cmi) {
257         if (source == null) {
258             return false;
259         }
260         File sourceDomain = new File(source);
261         File biDir = new File(source+File.separator+"bin");
262         //String osName = cmi.getOSName();
263
//if(!biDir.isDirectory() && sourceDomain.isDirectory() && osName.startsWith("SunOS")) {
264
if(!biDir.isDirectory() && sourceDomain.isDirectory()) {
265             // Now check wheather it is a domainRoot
266
//by checking the existence of domain/server1/config/server.xml file
267
String JavaDoc [] dirs = sourceDomain.list();
268             //sourceInputAsDomainRoot
269
if(dirs.length == 0) {
270                 cmi.setSourceDomainRootFlag(false);
271                 return false;
272             }
273             
274             if(new File(source + File.separator +"backup").exists()) {
275                 cmi.setSourceDomainRootFlag(true);
276                 return true;
277             }
278             // Checking If it contains 8.0 domain
279
for (int i=0;i<dirs.length;i++) {
280                 File domainXML = new File(source+File.separator+dirs[i]+File.separator+"config"+File.separator+"domain.xml");
281                 if(domainXML.isFile()) {
282                     cmi.setSourceDomainRootFlag(true);
283                     return true;
284                 }
285             }
286             
287             // Checking If it contains 7.0 domain
288
//if(cmi.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){
289
// Get any 7.0 domain
290
String JavaDoc domain = "";
291             boolean domain70=false;
292             for (int i=0;i<dirs.length;i++) {
293                 if(!(dirs[i].endsWith("8x")) && (new File(source +File.separator+ dirs[i]).isDirectory()) && this.isValid70Domain(source + File.separator + dirs[i])) {
294                     domain = dirs[i];
295                     domain70 =true;
296                     break;
297                 }
298             }
299             if(!domain70)
300                 return false;
301             File domainDir = new File(source+File.separator+domain);
302             String JavaDoc [] serverDirs = domainDir.list();
303             String JavaDoc serverInstanceName ="";
304             if(serverDirs[0].equals("admin-server"))
305                 serverInstanceName = serverDirs[1];
306             else
307                 serverInstanceName = serverDirs[0];
308             File serverXML = new File(source+File.separator+domain+File.separator+serverInstanceName+File.separator+"config"+File.separator+"server.xml");
309             if(serverXML.isFile())
310                 cmi.setSourceDomainRootFlag(true);
311             else
312                 cmi.setSourceDomainRootFlag(false);
313             /*}else {
314                 File domainXML = new File(source+File.separator+dirs[0]+File.separator+"config"+File.separator+"domain.xml");
315                 if(domainXML.isFile())
316                     cmi.setSourceDomainRootFlag(true);
317                 else
318                     cmi.setSourceDomainRootFlag(false);
319             } */

320             
321         }else {
322             cmi.setSourceDomainRootFlag(false);
323         }
324         return cmi.getSourceDomainRootFlag();
325     }
326     public boolean isValid70Domain(String JavaDoc domainPath) {
327         File domainDir = new File(domainPath);
328         boolean domain70 = false;
329         String JavaDoc [] serverDirs = domainDir.list();
330         String JavaDoc serverInstanceName ="";
331         if(serverDirs[0].equals("admin-server"))
332             serverInstanceName = serverDirs[1];
333         else
334             serverInstanceName = serverDirs[0];
335         File serverXML = new File(domainPath+File.separator+serverInstanceName+File.separator+"config"+File.separator+"server.xml");
336         if(serverXML.isFile())
337             domain70 = true;
338         else
339             domain70 = false;
340         return domain70;
341     }
342     
343     //Added by Prasad
344
public boolean isDomain(String JavaDoc domainPath) {
345         File domainXML = new File(domainPath+File.separator+
346                                     "config"+File.separator+
347                                      "domain.xml");
348         if(domainXML.exists()) {
349             return true;
350         } else {
351             return false;
352         }
353         
354     }
355     public String JavaDoc versionString(String JavaDoc versionEditionString){
356         if(UpgradeConstants.VERSION_AS7X_PE.equals(versionEditionString) ||
357                 UpgradeConstants.VERSION_AS7X_SE.equals(versionEditionString) ||
358                 UpgradeConstants.VERSION_AS7X_EE.equals(versionEditionString)){
359             return UpgradeConstants.VERSION_7X;
360         }
361         if(UpgradeConstants.VERSION_AS80_PE.equals(versionEditionString) ||
362                 UpgradeConstants.VERSION_AS80_SE.equals(versionEditionString) ||
363                 UpgradeConstants.VERSION_AS81_EE.equals(versionEditionString)){
364             return UpgradeConstants.VERSION_80;
365         }
366         if(UpgradeConstants.VERSION_AS81_PE.equals(versionEditionString) ||
367                 UpgradeConstants.VERSION_AS81_EE.equals(versionEditionString)){
368             return UpgradeConstants.VERSION_81;
369         }
370         if(UpgradeConstants.VERSION_AS90_PE.equals(versionEditionString) ||
371                 UpgradeConstants.VERSION_AS90_SE.equals(versionEditionString) ||
372                 UpgradeConstants.VERSION_AS90_EE.equals(versionEditionString)){
373             return UpgradeConstants.VERSION_90;
374         }
375         return "";
376     }
377     public String JavaDoc editionString(String JavaDoc versionEditionString){
378         if(UpgradeConstants.VERSION_AS7X_PE.equals(versionEditionString) ||
379                 UpgradeConstants.VERSION_AS80_PE.equals(versionEditionString) ||
380                 UpgradeConstants.VERSION_AS81_PE.equals(versionEditionString) ||
381                 UpgradeConstants.VERSION_AS90_PE.equals(versionEditionString)){
382             return UpgradeConstants.EDITION_PE;
383         }
384         if(UpgradeConstants.VERSION_AS7X_SE.equals(versionEditionString) ||
385                 UpgradeConstants.VERSION_AS80_SE.equals(versionEditionString)||
386                 UpgradeConstants.VERSION_AS90_SE.equals(versionEditionString)){
387             return UpgradeConstants.EDITION_SE;
388         }
389         if(UpgradeConstants.VERSION_AS7X_EE.equals(versionEditionString) ||
390                 UpgradeConstants.VERSION_AS81_EE.equals(versionEditionString)||
391                 UpgradeConstants.VERSION_AS81_EE.equals(versionEditionString)||
392                 UpgradeConstants.VERSION_AS90_EE.equals(versionEditionString)){
393             return UpgradeConstants.EDITION_EE;
394         }
395         return "";
396     }
397     public boolean isValidSourcePath(String JavaDoc sourcePath){
398         File biDir = new File(sourcePath+File.separator+"bin");
399         if(sourcePath.equals("") || !biDir.isDirectory()) {
400             if(!checkSourceInputAsDomainRoot(sourcePath, common)) {
401                 //check for 8.x domain dir as input
402
if(new File(sourcePath + File.separator + "config").isDirectory()){
403                     return true;
404                 }
405                 return false;
406             }
407             common.setSourceDomainRoot(sourcePath);
408         }
409         // First check if it is regular 7.x install directory.
410
String JavaDoc as7xInstallFileName = "admingui.ear"; //only exists in 7.x installations
411
File runtime70Jar = new File(sourcePath+File.separator+"lib"+File.separator+as7xInstallFileName);
412         if((runtime70Jar != null) && runtime70Jar.exists()) {
413             // Its a valid 7.x directory.
414
return true;
415         }
416         // Now check if it is AS8.x or solaris 10 installation or in-place upgrade.
417
String JavaDoc[] domainConfigDirs = new VersionExtracter(sourcePath,common).getDomainAndConfigDirs();
418         if(domainConfigDirs == null || domainConfigDirs.length == 0){
419             // if sourcePath is not valid, then getDomainAndConfigDirs returns null;
420
return false;
421         }
422         String JavaDoc domainsDir = domainConfigDirs[0];
423         
424         // If the domainsDir is <sourcePath>/domains then sourcePath is AS8.0.
425
// If the domainsDir is <sourcePath>_bak then sourcePath is AS8.0 in-place upgrade. (in-place upgrade only supported from 8.0PE onwards
426
// if the domainsDir is <sourcePath> then sourcePath is a domains directory given for solaris 10 integration.
427
if(domainsDir == null)
428             return false;
429         return true;
430     }
431     public boolean isValidTargetPath(String JavaDoc targetPath){
432         
433         /*
434         String osName = System.getProperty("os.name");
435         String asenv = null;
436         String asadmin = null;
437         if(osName.indexOf("Windows") != -1)
438             asadmin = targetPath + File.separator + "bin" + File.separator + "asupgrade.bat";
439         else
440             asadmin = targetPath + File.separator + "bin" + File.separator + "asupgrade";
441         try {
442             if(! new File(asadmin).exists()) return false;
443             BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(asadmin)));
444             String readString =reader.readLine();
445             while(readString != null) {
446                 if(readString.indexOf("targetVersion") != -1) {
447                     return true;
448                 }
449                 readString =reader.readLine();
450             }
451         }catch (Exception e) {
452             logger.log(Level.WARNING,this.stringManager.getString("enterprise.tools.upgrade.unknownError"),e);
453         }*/

454         
455         if(!new File(targetPath).isDirectory())
456             return false;
457         
458         return true;
459     }
460     public List JavaDoc getStandAloneInstances(Hashtable domainsMapping){
461         // This method is called while upgradeing 7.x EE upgrading. This comapres with servers listed in clusters with domains mapping.
462
List JavaDoc stdAloneList = new ArrayList();
463         for(Iterator domIt=domainsMapping.values().iterator(); domIt.hasNext();){
464             DomainInfo dInfo = (DomainInfo)domIt.next();
465             for(Iterator instIt = dInfo.getInstanceNames().iterator(); instIt.hasNext();){
466                 String JavaDoc instName = (String JavaDoc)instIt.next();
467                 if(this.isInstanceInCluster(instName)){
468                     continue;
469                 }
470                 if( !instName.equals("admin-server") ) {
471                     Vector instDInfo = new Vector();
472                     instDInfo.add(instName);
473                     instDInfo.add(dInfo);
474                     stdAloneList.add(instDInfo);
475                 }
476             }
477         }
478         return stdAloneList;
479     }
480     public boolean isInstanceInCluster(String JavaDoc instName){
481         // This method is a utility method to check for the instnce clustered or not.
482
List JavaDoc clList = ClustersInfoManager.getClusterInfoManager().getClusterInfoList();
483         if(clList == null) return false;
484         for(Iterator clIt = clList.iterator(); clIt.hasNext(); ){
485             ClusterInfo clInfo = (ClusterInfo)clIt.next();
486             for(Iterator instIt = clInfo.getClusteredInstanceList().iterator(); instIt.hasNext();){
487                 if(((ClusteredInstance)instIt.next()).getInstanceName().equals(instName)){
488                     return true;
489                 }
490             }
491         }
492         return false;
493     }
494     private Element JavaDoc getConfigElementFromDocumentElement(Element JavaDoc documentElement, String JavaDoc lookUpConfigName){
495         NodeList JavaDoc configEles = documentElement.getElementsByTagName("config");
496         Element JavaDoc configEle = null;
497         for(int lh =0; lh < configEles.getLength(); lh++){
498             String JavaDoc configName = ((Element JavaDoc)configEles.item(lh)).getAttribute("name");
499             if(configName.equals(lookUpConfigName)){
500                 configEle = (Element JavaDoc)configEles.item(lh);
501                 break;
502             }
503         }
504         return configEle;
505     }
506     public boolean switchedIIOPPorts(String JavaDoc serverID, String JavaDoc portValue, Element JavaDoc documentElement){
507         Element JavaDoc configEle = this.getConfigElementFromDocumentElement(documentElement, "server-config");
508         NodeList JavaDoc iiopService = configEle.getElementsByTagName("iiop-service");
509         NodeList JavaDoc iiopListeners = ((Element JavaDoc)iiopService.item(0)).getElementsByTagName("iiop-listener");
510         Element JavaDoc iiopListener = null;
511         for(int ii =0; ii < iiopListeners.getLength(); ii++){
512             if(((Element JavaDoc)iiopListeners.item(ii)).getAttribute("id").equals("orb-listener-1")){
513                 iiopListener = (Element JavaDoc)iiopListeners.item(ii);
514                 break;
515             }
516         }
517         if(iiopListener != null){
518             String JavaDoc serverIIOPPort = this.getIIOPListenerPortForServer(iiopListener.getAttribute("port"), "server", documentElement);
519             if(serverIIOPPort.equals("3700")){
520                 String JavaDoc portValueFromServer = this.getIIOPListenerPortForServer(portValue,serverID, documentElement);
521                 if(portValueFromServer != null)
522                     iiopListener.setAttribute("port", portValueFromServer);
523                 return true;
524             }else{
525                 return false;
526             }
527         }
528         return false;
529     }
530     private String JavaDoc getIIOPListenerPortForServer(String JavaDoc portValue, String JavaDoc serverName, Element JavaDoc documentElement){
531         try{
532             if(portValue != null){
533                 int portIntValue = Integer.parseInt(portValue);
534                 return String.valueOf(portIntValue);
535             }
536         }catch(java.lang.NumberFormatException JavaDoc ne){
537             // This shows the the portValue is not a string but ${IIOP_LISTENER_PORT}
538
}
539         NodeList JavaDoc servers = documentElement.getElementsByTagName("servers");
540         NodeList JavaDoc serverList = ((Element JavaDoc)servers.item(0)).getElementsByTagName("server");
541         String JavaDoc portValueFromServer = null;
542         for(int lh =0; lh < serverList.getLength(); lh++){
543             if(serverName.equals(((Element JavaDoc)serverList.item(lh)).getAttribute("name"))){
544                 Element JavaDoc serverElement = (Element JavaDoc)serverList.item(lh);
545                 Element JavaDoc sysProp = getSystemPropertyElement("IIOP_LISTENER_PORT",serverElement);
546                 if(sysProp != null){
547                     return sysProp.getAttribute("value");
548                 }else{
549                     Element JavaDoc configElement = this.getConfigElementFromDocumentElement(documentElement, serverElement.getAttribute("config-ref"));
550                     if(configElement != null){
551                         sysProp = getSystemPropertyElement("IIOP_LISTENER_PORT",configElement);
552                         if(sysProp != null)
553                             return sysProp.getAttribute("value");
554                     }
555                 }
556             }
557         }
558         return null;
559     }
560     private Element JavaDoc getSystemPropertyElement(String JavaDoc propertyName, Element JavaDoc parentForSysProp){
561         NodeList JavaDoc sysProps = parentForSysProp.getElementsByTagName("system-property");
562         for(int sh =0; sh < sysProps.getLength(); sh++){
563             if(propertyName.equals(((Element JavaDoc)sysProps.item(sh)).getAttribute("name"))){
564                 return ((Element JavaDoc)sysProps.item(sh));
565             }
566         }
567         return null;
568     }
569     public void updateDomainXMLWithIIOPCluster(CommonInfoModel cmnInfo, String JavaDoc domainXMLFile, IIOPCluster iiopCluster){
570         String JavaDoc clusterName = iiopCluster.getClusterName();
571         List JavaDoc servInstances = iiopCluster.getIIOPServerInstanceList();
572         boolean domainFileChanged = false;
573         Document JavaDoc domainDoc = this.getDomainDocumentElement(domainXMLFile);
574         for(int servIt = 0; servIt < servInstances.size(); servIt++){
575             List JavaDoc endPoints = ((IIOPServerInstance)servInstances.get(servIt)).getIIOPEndPoints();
576             String JavaDoc serverName = ((IIOPServerInstance)servInstances.get(servIt)).getName();
577             for(int endPt = 0; endPt < endPoints.size(); endPt++){
578                 IIOPEndPoint iiopEndPoint = (IIOPEndPoint)endPoints.get(endPt);
579                 // just use only one endpoint and transfer the port no.
580
if(domainDoc != null){
581                     String JavaDoc propertyName = "IIOP_LISTENER_PORT";
582                     String JavaDoc propValue = iiopEndPoint.getPort();
583                     NodeList JavaDoc servers = domainDoc.getDocumentElement().getElementsByTagName("servers");
584                     NodeList JavaDoc serverList = ((Element JavaDoc)servers.item(0)).getElementsByTagName("server");
585                     if(propValue.equals("3700")){
586                         this.switchedIIOPPorts(serverName, null, domainDoc.getDocumentElement());
587                         //logger.log(Level.WARNING,this.stringManager.getString("enterprise.tools.upgrade.iiop_port_defined"));
588
}
589                     // Passing baseElement as null. "IIOP_LISTENER_PORT" should definitely exist. Ssome times the create-instance does not add it.
590
// This base element is only a structural reference to invoke appendChild method.
591
BaseElement baseElement = null;
592                     try{
593                         baseElement = com.sun.enterprise.tools.upgrade.transform.ElementToObjectMapper.getMapper().getElementObject(domainDoc.getDocumentElement().getTagName());
594                     }catch(Exception JavaDoc ef){
595                     }
596                     this.addOrUpdateSystemPropertyToServer(serverName,serverList, propertyName, propValue, baseElement);
597                     domainFileChanged = true;
598                 }
599                 break;
600             }
601         }
602         if(domainFileChanged && (domainDoc != null)){
603             this.saveDocumentToDomainFile(domainXMLFile, domainDoc);
604         }
605     }
606     public java.util.List JavaDoc updateClusterList(String JavaDoc domainXMLFile, String JavaDoc domainName, java.util.List JavaDoc clList){
607         Document JavaDoc domainDoc = this.getDomainDocumentElement(domainXMLFile);
608         if(domainDoc != null){
609             NodeList JavaDoc clusters = domainDoc.getDocumentElement().getElementsByTagName("clusters");
610             if(clusters != null){
611                 NodeList JavaDoc clusterList = ((Element JavaDoc)clusters.item(0)).getElementsByTagName("cluster");
612                 if((clusterList != null) && (clusterList.getLength() >0)){
613                     for(int lh =0; lh < clusterList.getLength(); lh++){
614                         String JavaDoc clName = ((Element JavaDoc)clusterList.item(lh)).getAttribute("name");
615                         ClusterInfo clInfo = new ClusterInfo();
616                         clInfo.setClusterName(clName);
617                         clInfo.setDomainName(domainName);
618                         clList.add(clInfo);
619                     }
620                 }
621             }
622         }
623         return clList;
624     }
625     private Document JavaDoc getDomainDocumentElement(String JavaDoc domainFileName){
626         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
627         //factory.setValidating(true);
628
factory.setNamespaceAware(true);
629         Document JavaDoc resultDoc = null;
630         try {
631             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
632             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
633                     ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
634             resultDoc = builder.parse( new File(domainFileName));
635         }catch (Exception JavaDoc ex){
636             logger.log(Level.WARNING, stringManager.getString("enterprise.tools.upgrade.iiop_port_domain_doc"),ex);
637         }
638         return resultDoc;
639     }
640     private void saveDocumentToDomainFile(String JavaDoc domainFileName, Document JavaDoc resultDoc){
641         // Use a Transformer for output
642
try{
643             TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
644             Transformer JavaDoc transformer = tFactory.newTransformer();
645             if (resultDoc.getDoctype() != null){
646                 String JavaDoc systemValue = resultDoc.getDoctype().getSystemId();
647                 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);
648                 String JavaDoc pubValue = resultDoc.getDoctype().getPublicId();
649                 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubValue);
650             }
651             DOMSource JavaDoc source = new DOMSource JavaDoc(resultDoc);
652             StreamResult JavaDoc result = new StreamResult JavaDoc(new FileOutputStream(domainFileName));
653             transformer.transform(source, result);
654             result.getOutputStream().close();
655         }catch (Exception JavaDoc ex){
656             logger.log(Level.WARNING, stringManager.getString("enterprise.tools.upgrade.iiop_port_domain_save_fail"),ex);
657             //throw new HarnessException(stringManager.getString("upgrade.transform.startFailureMessage"));
658
}
659     }
660     
661     public void recover() {
662         List JavaDoc domainList = common.getDomainList();
663         String JavaDoc sourceDir = common.getSourceDomainRoot();
664         String JavaDoc targetDir = common.getTargetDomainRoot();
665         if(domainList != null && sourceDir != null && targetDir != null) {
666             Iterator itr = domainList.iterator();
667             while(itr.hasNext()) {
668                 String JavaDoc dname = (String JavaDoc)itr.next();
669                 boolean domainRootSame = new File(sourceDir).equals(new File(targetDir));
670                 if(domainRootSame) {
671                     if(new File(sourceDir + File.separator + "backup").isDirectory()) {
672                         common.setCurrentDomain(dname);
673                         String JavaDoc latestDomainPath=common.findLatestDomainDir(sourceDir);
674                         if(latestDomainPath != null) {
675                             new File(latestDomainPath).renameTo(new File(sourceDir + File.separator + dname));
676                             File backupdir = new File(sourceDir + File.separator + "backup");
677                             String JavaDoc[] dirs = backupdir.list();
678                             if(dirs.length == 0)
679                                 backupdir.delete();
680                         }
681                     }
682                 }
683                 
684             }//while loop
685
}//not null check of domainList
686
}
687     
688     public boolean validateUserDetails(String JavaDoc adminUserName, String JavaDoc adminPassword, String JavaDoc masterPassword){
689         try{
690             com.sun.enterprise.admin.servermgmt.RepositoryManager rManager = new com.sun.enterprise.admin.servermgmt.RepositoryManager();
691             String JavaDoc domainPath = this.common.getTargetDomainRoot();
692             if(!new File(domainPath+File.separator+"domain1").exists() ||
693                     ((common.getSourceInstallDir() != null && common.getTargetInstallDir() != null) && (common.getSourceInstallDir().equalsIgnoreCase(common.getTargetInstallDir() + File.separator + "domains") || (common.getSourceInstallDir().equalsIgnoreCase(common.getTargetInstallDir()))))){
694                 // domain1 should exist in target directory in order to validate user name and password.
695
// If this does not exist, then cannot validate user name and password. Just return true.
696
// In case of in-place upgrade domain1 is created by upgrade process. The time this method is called for user details validation
697
// the domain1 wouldnt have been created. Also, in case if in-place upgrade upgrade is invoked soon after installation.
698
// The upgrade tool uses com.sun.enterprise.admin.servermgmt.RepositoryConfig to validate passwords before the upgrade starts.
699
// The RepositoryConfig.validateAdminUserAndPassword() method doesn't work if an 8.1 domain hasn't been created.
700
// In the in-place upgrade scenario the installer doesn't create one and this method gets called before the upgrade tool
701
// creates one. When the upgrade tool does create a domain, it uses the passwords passed in by the installers to create the
702
// new domains, so we don't have to worry about checking for incorrect passwords in this case.
703
return true;
704             }
705             com.sun.enterprise.admin.servermgmt.RepositoryConfig repConf = new com.sun.enterprise.admin.servermgmt.RepositoryConfig("domain1",domainPath);
706             rManager.validateAdminUserAndPassword(repConf,adminUserName,adminPassword);
707             if((masterPassword != null) && (!this.common.getTargetVersionAndEdition().equals(UpgradeConstants.VERSION_AS81_PE)))
708                 rManager.validateMasterPassword(repConf,masterPassword);
709         }catch(com.sun.enterprise.admin.servermgmt.RepositoryException re){
710             logger.log(Level.SEVERE, stringManager.getString("enterprise.tools.upgrade.generalException", re.getMessage()), re);
711             return false;
712         }
713         return true;
714     }
715     public void updateDomainXMLWithPersistenceStoreProps(java.util.Properties JavaDoc props){
716         // TODO
717
// Read each property and add it to appropriate place in domain.xml
718
// Yet to hear back from Larry white.
719
}
720     public void updateListenerPortsForClusteredInstances(Element JavaDoc docElement, String JavaDoc propertyName, String JavaDoc portValue, BaseElement bElement){
721         NodeList JavaDoc servers = docElement.getElementsByTagName("servers");
722         NodeList JavaDoc serverList = ((Element JavaDoc)servers.item(0)).getElementsByTagName("server");
723         String JavaDoc clusterName = this.common.getCurrentCluster();
724         for(java.util.Iterator JavaDoc dItr = ClustersInfoManager.getClusterInfoManager().getClusterInfoList().iterator(); dItr.hasNext();){
725             ClusterInfo cInfo = (ClusterInfo)dItr.next();
726             if(cInfo.getClusterName().equals(clusterName)){
727                 for(java.util.Iterator JavaDoc clItr = cInfo.getClusteredInstanceList().iterator(); clItr.hasNext();){
728                     ClusteredInstance clInstance = (ClusteredInstance)clItr.next();
729                     if(portValue == null){
730                         this.addOrUpdateSystemPropertyToServer(clInstance.getInstanceName(), serverList, propertyName, clInstance.getInstancePort(),bElement);
731                     }else{
732                         this.addOrUpdateSystemPropertyToServer(clInstance.getInstanceName(), serverList, propertyName, portValue,bElement);
733                     }
734                 }
735             }
736         }
737     }
738     public void addOrUpdateSystemPropertyToServer(String JavaDoc serverName, NodeList JavaDoc serverList, String JavaDoc propertyName, String JavaDoc propertyValue, BaseElement bElement){
739         for(int lh =0; lh < serverList.getLength(); lh++){
740             if(serverName.equals(((Element JavaDoc)serverList.item(lh)).getAttribute("name"))){
741                 Element JavaDoc serverElement = (Element JavaDoc)serverList.item(lh);
742                 Element JavaDoc sysProp = this.getSystemPropertyElement(propertyName,serverElement);
743                 if(sysProp != null){
744                     sysProp.setAttribute("value", propertyValue);
745                     return;
746                 }
747                 if(sysProp == null){
748                     // bElement could be passed as null in case if this property need not be added.
749
if(bElement != null){
750                         sysProp = serverElement.getOwnerDocument().createElement("system-property");
751                         sysProp.setAttribute("name", propertyName);
752                         sysProp.setAttribute("value", propertyValue);
753                         bElement.appendElementToParent(serverElement,sysProp);
754                     }
755                 }
756                 return;
757             }
758         }
759     }
760     
761     public void copyFile(String JavaDoc source, String JavaDoc target) throws IOException {
762         copyFile(source, target, false);
763     }
764     
765     public void copyFile(String JavaDoc source, String JavaDoc target, boolean append) throws IOException{
766         //copy the file
767
//FileReader in = new FileReader(new File(source));
768
//FileWriter out = new FileWriter(new File(target), append);
769
//FileReader/FileWriter were corrupting keystore files.
770
FileInputStream in = new FileInputStream(new File(source));
771         FileOutputStream out = new FileOutputStream(new File(target), append);
772         int c;
773         while ((c = in.read()) != -1) {
774             out.write(c);
775         }
776         in.close();
777         out.close();
778     }
779     
780     //returns the value of the jvm-option or null if not found
781
public String JavaDoc getJvmOptionValueFromSourceConfig(String JavaDoc optionName) {
782         String JavaDoc sourceConfig = common.getSourceConfigXMLFile();
783         Document JavaDoc sourceDoc = this.getDomainDocumentElement(common.getSourceConfigXMLFile());
784         NodeList JavaDoc serverList = sourceDoc.getElementsByTagName("server");
785         String JavaDoc serverName = common.getCurrentSourceInstance();
786         if (serverName == null || serverName == "") {
787             serverName = "server";
788         }
789         String JavaDoc configName = null;
790         String JavaDoc password = null;
791         for(int i=0;i<serverList.getLength();i++){
792             Node JavaDoc serverNode = (Node JavaDoc)serverList.item(i);
793             NamedNodeMap JavaDoc attributes = serverNode.getAttributes();
794             String JavaDoc name = (attributes.getNamedItem("name")).getNodeValue();
795             if(serverName.equals(name)){
796                 configName = (attributes.getNamedItem("config-ref")).getNodeValue();
797             }
798         }
799         if (configName != null) {
800             NodeList JavaDoc jvmOptionList = sourceDoc.getElementsByTagName("jvm-options");
801             for(int j=0;j<jvmOptionList.getLength();j++) {
802                 Node JavaDoc jvmNode = jvmOptionList.item(j);
803                 String JavaDoc jvmOptions = getTextNodeData((Element JavaDoc)jvmNode);
804                 if (jvmOptions.indexOf(optionName) != -1) {
805                     password = jvmOptions.substring(jvmOptions.indexOf("=")+1, jvmOptions.length());
806                 }
807             }
808         } else {
809             logger.warning(stringManager.getString("enterprise.tools.upgrade.configNotFound", configName));
810         }
811         return password;
812         
813     }
814     
815     public String JavaDoc getTextNodeData(Element JavaDoc element){
816         NodeList JavaDoc children = element.getChildNodes();
817         for(int index=0; index < children.getLength(); index++){
818             if(children.item(index).getNodeType() == Node.TEXT_NODE){
819                 return children.item(index).getNodeValue();
820             }
821         }
822         return "";
823     }
824 }
825
Popular Tags