KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DomainsProcessor.java
26  *
27  * Created on November 20, 2003, 10:45 AM
28  */

29
30 package com.sun.enterprise.tools.upgrade.common;
31
32 import java.util.*;
33 import java.io.*;
34 import java.util.logging.*;
35 import java.net.InetAddress JavaDoc;
36 import java.net.UnknownHostException JavaDoc;
37
38 import com.sun.enterprise.util.ASenvPropertyReader;
39 import com.sun.enterprise.util.SystemPropertyConstants;
40
41 import com.sun.enterprise.tools.upgrade.logging.*;
42 import com.sun.enterprise.cli.framework.*;
43 import com.sun.enterprise.util.i18n.StringManager;
44
45 import javax.xml.parsers.DocumentBuilder JavaDoc;
46 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
47 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
48 import javax.xml.parsers.ParserConfigurationException JavaDoc;
49
50 import javax.xml.transform.Transformer JavaDoc;
51 import javax.xml.transform.TransformerFactory JavaDoc;
52 import javax.xml.transform.TransformerException JavaDoc;
53 import javax.xml.transform.TransformerConfigurationException JavaDoc;
54
55 import javax.xml.transform.dom.DOMSource JavaDoc;
56 import javax.xml.transform.stream.StreamResult JavaDoc;
57 import javax.xml.transform.OutputKeys JavaDoc;
58
59 import org.xml.sax.SAXException JavaDoc;
60 import org.xml.sax.SAXParseException JavaDoc;
61 import org.w3c.dom.Document JavaDoc;
62 import org.w3c.dom.DOMException JavaDoc;
63 import org.w3c.dom.Element JavaDoc;
64 import org.w3c.dom.Node JavaDoc;
65 import org.w3c.dom.NodeList JavaDoc;
66
67 import com.sun.enterprise.tools.upgrade.cluster.*;
68
69 /**
70  *
71  * @author prakash
72  * @author hans
73  */

74
75
76 public class DomainsProcessor {
77     private static CommonInfoModel commonInfo;
78     private java.util.Vector JavaDoc httpSSLPorts;
79     private java.util.Vector JavaDoc iiopSSLPorts;
80     private java.util.Vector JavaDoc iiopMutualAuthPorts;
81     private java.util.Vector JavaDoc sourceXMLCorePorts;
82     //start CR 6407292
83
private java.util.Vector JavaDoc adminJMXPorts;
84     //end CR 6407292
85

86     private static int adminPortToStartWith = 4858;
87     
88     private static Logger logger = LogService.getLogger(LogService.UPGRADE_LOGGER);
89     private static StringManager stringManager = StringManager.getManager(LogService.UPGRADE_LOGGER);
90     
91     /** Creates a new instance of DomainsProcessor */
92     public DomainsProcessor(CommonInfoModel ci) {
93         this.commonInfo = ci;
94         httpSSLPorts = new java.util.Vector JavaDoc();
95         iiopSSLPorts = new java.util.Vector JavaDoc();
96         iiopMutualAuthPorts = new java.util.Vector JavaDoc();
97         sourceXMLCorePorts = new java.util.Vector JavaDoc();
98         //start CR 6407292
99
adminJMXPorts = new java.util.Vector JavaDoc();
100         //end CR 6407292
101

102         // Prefill with ports used in default domain and samples domain.
103
// default domain xml file ports:
104
File domainDir = new File(ci.getTargetDomainRoot() + File.separator + "domain1");
105         if(!domainDir.exists()){
106             // This could be due to the reason that its inplace upgrade. In this case upgrade tool should create the default domain too.
107
}
108         //start CR 6407292
109
else {
110         //end CR 6407292
111
String JavaDoc targetDomainFile = ci.getTargetDomainRoot() + File.separator + "domain1"+File.separator+
112                     "config"+File.separator+"domain.xml" ;
113             if(domainDir.isDirectory() &&
114                     !(ci.isValid70Domain(ci.getTargetDomainRoot() + File.separator + "domain1")) &&
115                     (new File(targetDomainFile)).exists()) {
116                 String JavaDoc htSSP = getPortFromXML(targetDomainFile, "http-listener", "http-listener-2");
117                 if(htSSP != null) httpSSLPorts.add(htSSP);
118                 String JavaDoc iiSSP = getPortFromXML(targetDomainFile, "iiop-listener", "SSL");
119                 if(iiSSP != null) iiopSSLPorts.add(iiSSP);
120                 String JavaDoc iiMAP = getPortFromXML(targetDomainFile, "iiop-listener", "SSL_MUTUALAUTH");
121                 if(iiMAP != null) iiopMutualAuthPorts.add(iiMAP);
122                 //start CR 6407292
123
String JavaDoc adminJMX = getJMXPortFromXML(targetDomainFile, "jmx-connector", "system");
124                 if(adminJMX != null) adminJMXPorts.add(adminJMX);
125                 //end CR 6407292
126
}
127         //start CR 6407292
128
}
129         //end CR 6407292
130
File samplesDir = new File(ci.getTargetDomainRoot() + File.separator + "samples");
131         if(samplesDir.isDirectory() && !(ci.isValid70Domain(ci.getTargetDomainRoot() + File.separator + "samples")) ) {
132             // Now add sample domain ports if there is one
133
String JavaDoc sampleDomainFile = ci.getTargetDomainRoot() + File.separator + "samples"+File.separator+"config"+File.separator+"domain.xml" ;
134             if(new File(sampleDomainFile).exists()){
135                 String JavaDoc shtSSP = getPortFromXML(sampleDomainFile, "http-listener", "http-listener-2");
136                 if(shtSSP != null) httpSSLPorts.add(shtSSP);
137                 String JavaDoc siiSSP = getPortFromXML(sampleDomainFile, "iiop-listener", "SSL");
138                 if(siiSSP != null) iiopSSLPorts.add(siiSSP);
139                 String JavaDoc siiMAP = getPortFromXML(sampleDomainFile, "iiop-listener", "SSL_MUTUALAUTH");
140                 if(siiMAP != null) iiopMutualAuthPorts.add(siiMAP);
141                 //start CR 6407292
142
String JavaDoc sadminJMX = getJMXPortFromXML(sampleDomainFile, "jmx-connector", "system");
143                 if(sadminJMX != null) adminJMXPorts.add(sadminJMX);
144                     //end CR 6407292
145
}
146             
147         }
148         
149         // Store source xml ports core ports: http,iiop,jms
150
// TO-DO collect these ports from all source domains iteratively and store them in sourceXMLCorePorts vector
151
// Only for testing purposes.....
152
}
153     private DomainsProcessor(CommonInfoModel ci, boolean nothing) {
154         
155     }
156     
157     public void processTargetDomains() throws HarnessException{
158         List JavaDoc domainNames = commonInfo.getDomainList();
159         String JavaDoc target = commonInfo.getTargetInstallDir();
160         
161         //Following lines are copied from IasAdminCommand
162
ASenvPropertyReader reader = new ASenvPropertyReader(System.getProperty(SystemPropertyConstants.CONFIG_ROOT_PROPERTY));
163         reader.setSystemProperties();
164         //initGlobalsManager(null);
165
//IasAdminCommand end
166
for(int i=0; i<domainNames.size();i++) {
167             String JavaDoc sourceDomainName = (String JavaDoc)domainNames.get(i);
168             String JavaDoc targetDomainName=sourceDomainName;
169             if(!new File(commonInfo.getTargetDomainRoot()).isDirectory()) {
170                 logger.log(Level.INFO,stringManager.getString("enterprise.tools.upgrade.not_valid_target_install"));
171                 return;
172             }
173             File targetDomainUpgrade = new File(commonInfo.getTargetDomainRoot()+File.separator+targetDomainName);
174             // Added by Prasad
175
// We need to check if there is a domain , if not we create it.
176
// if the domain exists then we do not create it.
177
if( !commonInfo.isDomain(targetDomainUpgrade.getAbsolutePath()) ){
178                 
179                 /*
180                 File targetDomainUpgrade80=new File(commonInfo.getTargetDomainRoot()+File.separator+targetDomainName);
181                 if(targetDomainUpgrade80.isDirectory() ) {
182                     this.setAdminPortAndSecurity(sourceDomainName,targetDomainName);
183                     continue;
184                 } */

185                 String JavaDoc[] createDomainCommand = this.getCreateDomainCommand(sourceDomainName,targetDomainName);
186                 boolean canContinue = executeCommand(createDomainCommand);
187                 UpdateProgressManager.getProgressManager().setContinueUpgrade(canContinue);
188                 if(!canContinue) {
189                     throw new HarnessException(stringManager.getString("enterprise.tools.upgrade.domain_creation_error", targetDomainName));
190                 }
191                 UpdateProgressManager.getProgressManager().processUpgradeUpdateEvent((i*30)/domainNames.size());
192             }
193             if(commonInfo.isDomain(targetDomainUpgrade.getAbsolutePath()))
194                 this.setAdminPortAndSecurity(sourceDomainName,targetDomainName);
195         }
196         //this.processDomainInstances();
197
}
198     
199     private String JavaDoc[] getCreateDomainCommand(String JavaDoc domainName, String JavaDoc domainName80) {
200         /**
201          * Port type Appserver 8.0 Appserver 7.0
202          * 1. Http 8080 http-listener-1 80 or 1024- http-listener-1
203          * 2. http_ssl 1043 - http-listener-2 -------- dont know --------
204          * 3. JMS 7676 - default_jms_host 7676 - jms-service
205          * 4. IIOP 3700 - orb-listener-1 3700 - orb-listener-1
206          * 5. iiop_ssl 1060 - ssl ---------- dont know -------
207          * 6. iiop_mutual_auth 1061 -ssl-math ---------- dont know -----
208          * 7. admin port 4848 - admin-listener (httplistener) 4848 - stored in admin-server server.xml
209          *
210          * http, jms, iiop, and admin ports are transferred from appserver 7.0.
211          * Need to pick up appropriate ports for http_ssl, iiop_ssl and iiop_mutual_auth.
212          */

213         String JavaDoc target = commonInfo.getTargetInstallDir();
214         String JavaDoc adminPort = getSourceAdminPort(((DomainInfo)this.commonInfo.getDomainMapping().get(domainName)).getDomainPath());
215         // http, iiop, jms ports are replaced by the migration anyway...
216
String JavaDoc httpPort = "8080";
217         String JavaDoc jmsPort = "7676";
218         String JavaDoc iiopPort = "3700";
219         
220         // Now I need a unique ports for http-ssl,iiop-ssl,iiop-mutual-auth
221
String JavaDoc httpSSLPort = getAFreePort(1044,10);
222         String JavaDoc iiopSSLPort = getAFreePort(1090,10);
223         String JavaDoc iiopSSLMutualAuth = getAFreePort(1091,10);
224         //START CR 6397215
225
String JavaDoc adminJMXPort = getAFreePort(8687,10);
226         //Start CR 6407292
227
adminJMXPorts.add(adminJMXPort);
228         //end CR 6407292
229
//END CR 6397215
230

231         httpSSLPorts.add(httpSSLPort);
232         iiopSSLPorts.add(iiopSSLPort);
233         iiopMutualAuthPorts.add(iiopSSLMutualAuth);
234         String JavaDoc propertiesString = "http.ssl.port="+httpSSLPort+":orb.ssl.port="+iiopSSLPort+":orb.mutualauth.port="+iiopSSLMutualAuth+
235             ":jms.port="+jmsPort+":orb.listener.port="+iiopPort;
236         
237         //START CR 6397215
238
// if we are upgrading from 8.x PE to 9.0 PE we need to specify a
239
// JMX connector port
240
if(commonInfo.checkUpgradefrom8xpeto90pe()) {
241             propertiesString = propertiesString+":domain.jmxPort="+adminJMXPort;
242         }
243         //END CR 6397215
244
String JavaDoc [] createDomainCommand = {"create-domain",
245          "--domaindir",
246             "\"" + commonInfo.getTargetDomainRoot() +"\"",
247             "--adminport",
248             adminPort,
249             "--adminuser",
250             commonInfo.getAdminUserName(),
251             "--passwordfile ",
252             "\"" + commonInfo.getPasswordFile() +"\"",
253             "--domainproperties",
254             propertiesString,
255             domainName80
256             };
257            return createDomainCommand;
258  
259     }
260     
261     public static String JavaDoc getSourceAdminPort() {
262         return getSourceAdminPort(commonInfo.getSourceDomainPath());
263     }
264     
265     // This method returns admin port from admin-server server.xml
266
public static String JavaDoc getSourceAdminPort(String JavaDoc domainPath) {
267         // Get the admin port from server.xml of admin-server
268
if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)) {
269             String JavaDoc adminServerXmlFile = domainPath+File.separator+"admin-server"+File.separator+"config"+File.separator+"server.xml";
270             String JavaDoc port = getPortFromXML(adminServerXmlFile,"http-listener", "http-listener-1");
271             if(port != null)
272                 return port;
273         }else{
274             // For 8.x get it from domain.xml
275
String JavaDoc domainXmlFile = domainPath+File.separator+"config"+File.separator+"domain.xml";
276             if(new File(domainXmlFile).exists()){
277                 String JavaDoc port = getPortFromXML(domainXmlFile,"http-listener", "admin-listener");
278                 if(port != null)
279                     return port;
280             }
281         }
282         // return some default no....user should change it later.
283
adminPortToStartWith =+10;
284         return String.valueOf(adminPortToStartWith);
285     }
286     public static String JavaDoc getSourceAdminSecurity(String JavaDoc domainPath) {
287         // Get the admin security-enabled from server.xml of admin-server
288
if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)) {
289             String JavaDoc adminServerXmlFile = domainPath+File.separator+"admin-server"+File.separator+"config"+File.separator+"server.xml";
290             String JavaDoc securityEnabled = getSecurityEnabledFromXML(adminServerXmlFile,"http-listener", "http-listener-1");
291             if(securityEnabled != null)
292                 return securityEnabled;
293         }else{
294             // For 8.x get it from domain.xml
295
String JavaDoc domainXmlFile = domainPath+File.separator+"config"+File.separator+"domain.xml";
296             if(new File(domainXmlFile).exists()){
297                 String JavaDoc securityEnabled = getSecurityEnabledFromXML(domainXmlFile,"http-listener", "admin-listener");
298                 if(securityEnabled != null)
299                     return securityEnabled;
300             }
301         }
302         return "true"; // default to true
303
}
304     
305     public static String JavaDoc getSourceAdminSecurity() {
306         String JavaDoc domainPath = commonInfo.getSourceDomainPath();
307         return getSourceAdminSecurity(domainPath);
308     }
309     private void setAdminPortAndSecurity(String JavaDoc sourceDomainName, String JavaDoc targetDomainName){
310         String JavaDoc domainPath = ((DomainInfo)this.commonInfo.getDomainMapping().get(sourceDomainName)).getDomainPath();
311         String JavaDoc adminPort = getSourceAdminPort(domainPath);
312         String JavaDoc securityEnabled = getSourceAdminSecurity(domainPath);
313         
314         String JavaDoc domainFileName = commonInfo.getTargetDomainRoot()+File.separator+targetDomainName+File.separator+"config"+File.separator+"domain.xml";
315         
316         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
317         //factory.setValidating(true);
318
factory.setNamespaceAware(true);
319         try {
320             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
321             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
322             ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
323             Document JavaDoc resultDoc = builder.parse( new File(domainFileName));
324             NodeList JavaDoc taggedElements = resultDoc.getDocumentElement().getElementsByTagName("http-listener");
325             for(int lh =0; lh < taggedElements.getLength(); lh++){
326                 Element JavaDoc element = (Element JavaDoc)taggedElements.item(lh);
327                 // Compare id attribute of http-listener elements.
328
if((element.getAttribute("id")).equals("admin-listener")){
329                     element.setAttribute("port",adminPort);
330                     element.setAttribute("security-enabled", securityEnabled);
331                     break;
332                 }
333             }
334             
335           // Use a Transformer for output
336
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
337           Transformer JavaDoc transformer = tFactory.newTransformer();
338           if (resultDoc.getDoctype() != null){
339                 String JavaDoc systemValue = resultDoc.getDoctype().getSystemId();
340                 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);
341                 String JavaDoc pubValue = resultDoc.getDoctype().getPublicId();
342                 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubValue);
343           }
344
345           DOMSource JavaDoc source = new DOMSource JavaDoc(resultDoc);
346           StreamResult JavaDoc result = new StreamResult JavaDoc(new FileOutputStream(domainFileName));
347           transformer.transform(source, result);
348           result.getOutputStream().close();
349           
350         }catch (Exception JavaDoc ex){
351             logger.log(Level.WARNING, stringManager.getString("domainProcessor.portFromXML"),ex);
352             //throw new HarnessException(stringManager.getString("upgrade.transform.startFailureMessage"));
353
}
354         
355     }
356     private static String JavaDoc getPortFromXML(String JavaDoc fileName, String JavaDoc tagName, String JavaDoc id) {
357         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
358         //factory.setValidating(true);
359
factory.setNamespaceAware(true);
360         try {
361             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
362             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
363             ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
364             Document JavaDoc adminServerDoc = builder.parse( new File(fileName));
365             NodeList JavaDoc taggedElements = adminServerDoc.getDocumentElement().getElementsByTagName(tagName);
366             for(int lh =0; lh < taggedElements.getLength(); lh++){
367                 Element JavaDoc element = (Element JavaDoc)taggedElements.item(lh);
368                 // Compare id attribute of http-listener elements.
369
if((element.getAttribute("id")).equals(id)){
370                     return element.getAttribute("port");
371                 }
372             }
373         }catch (Exception JavaDoc ex){
374             logger.log(Level.WARNING, stringManager.getString("domainProcessor.portFromXML"),ex);
375             //throw new HarnessException(stringManager.getString("upgrade.transform.startFailureMessage"));
376
}
377         return null;
378     }
379     
380     private static String JavaDoc getSecurityEnabledFromXML(String JavaDoc fileName, String JavaDoc tagName, String JavaDoc id) {
381         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
382         //factory.setValidating(true);
383
factory.setNamespaceAware(true);
384         try {
385             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
386             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
387             ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
388             Document JavaDoc adminServerDoc = builder.parse( new File(fileName));
389             NodeList JavaDoc taggedElements = adminServerDoc.getDocumentElement().getElementsByTagName(tagName);
390             for(int lh =0; lh < taggedElements.getLength(); lh++){
391                 Element JavaDoc element = (Element JavaDoc)taggedElements.item(lh);
392                 // Compare id attribute of http-listener elements.
393
if((element.getAttribute("id")).equals(id)){
394                     return element.getAttribute("security-enabled");
395                 }
396             }
397         }catch (Exception JavaDoc ex){
398             logger.log(Level.WARNING, stringManager.getString("domainProcessor.adminSecureFromXML"),ex);
399             //throw new HarnessException(stringManager.getString("upgrade.transform.startFailureMessage"));
400
}
401         return null;
402     }
403     
404     private String JavaDoc getTargetNodeAgentName(String JavaDoc domainName, CommonInfoModel commonInfoMod){
405         String JavaDoc configFileName = commonInfoMod.getTargetDomainRoot()+File.separator+domainName+File.separator+"config"+File.separator+"domain.xml";
406         return getNodeAgentNameFromXML(configFileName);
407     }
408     
409     private String JavaDoc getNodeAgentNameFromXML(String JavaDoc fileName) {
410         if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X) || ! new File(fileName).exists()) {
411             return null;
412         }
413         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
414         //factory.setValidating(true);
415
factory.setNamespaceAware(true);
416         try {
417             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
418             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
419             ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
420             Document JavaDoc adminServerDoc = builder.parse( new File(fileName));
421             NodeList JavaDoc taggedElements = adminServerDoc.getDocumentElement().getElementsByTagName("node-agent");
422             Element JavaDoc element = (Element JavaDoc)taggedElements.item(0);
423             return element.getAttribute("name");
424         }catch (Exception JavaDoc ex){
425             logger.log(Level.WARNING, stringManager.getString("domainProcessor.nodeAgentFromXML"),ex);
426             //throw new HarnessException(stringManager.getString("upgrade.transform.startFailureMessage"));
427
}
428         return null;
429     }
430     private boolean isNodeAgentExists(String JavaDoc agentName, String JavaDoc fileName){
431         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
432         //factory.setValidating(true);
433
factory.setNamespaceAware(true);
434         try {
435             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
436             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
437             ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
438             Document JavaDoc adminServerDoc = builder.parse( new File(fileName));
439             NodeList JavaDoc taggedElements = adminServerDoc.getDocumentElement().getElementsByTagName("node-agent");
440             for(int lh =0; lh < taggedElements.getLength(); lh++){
441                 Element JavaDoc itElement = ((Element JavaDoc)taggedElements.item(lh));
442                 String JavaDoc attrName = itElement.getAttribute("name");
443                 if(attrName.equals(agentName))
444                     return true;
445                 // If the agentName contains . example beacon.east, then should only compare to beacon.
446
if(agentName.indexOf(".") != -1){
447                     if(agentName.substring(0,agentName.indexOf(".")).equals(attrName)){
448                         return true;
449                     }
450                 }
451             }
452         }catch (Exception JavaDoc ex){
453             logger.log(Level.WARNING, stringManager.getString("domainProcessor.nodeAgentFromXML"),ex);
454             //throw new HarnessException(stringManager.getString("upgrade.transform.startFailureMessage"));
455
}
456         return false;
457     }
458     
459     public static String JavaDoc getTargetDomainPort(String JavaDoc domainName, CommonInfoModel commonInfoMod){
460         String JavaDoc configFileName = commonInfoMod.getTargetDomainRoot()+File.separator+domainName+File.separator+"config"+File.separator+"domain.xml";
461         return getPortFromXML(configFileName, "http-listener", "admin-listener");
462     }
463     
464     public static String JavaDoc getTargetDomainSecurity(String JavaDoc domainName, CommonInfoModel commonInfoMod){
465         String JavaDoc configFileName = commonInfoMod.getTargetDomainRoot()+File.separator+domainName+File.separator+"config"+File.separator+"domain.xml";
466         return getSecurityEnabledFromXML(configFileName, "http-listener", "admin-listener");
467     }
468     
469     private String JavaDoc getAFreePort(int initPortNumber, int increment){
470         // Do this only for 20 iterations. If you cant find one in 10 iterations, then just assign some random number.
471
int portNumber = initPortNumber;
472         for(int i=0; i<20; i++){
473             if(isPortNumberUsable(portNumber))
474                 return Integer.toString(portNumber);
475             portNumber +=increment;
476         }
477         // In 20 iterations, if we cant find a free port, just return the last port number itereated.
478
return Integer.toString(portNumber);
479     }
480     private boolean isPortNumberUsable(int portNumber){
481         // First check if this port exists in any of the lists we have
482
String JavaDoc portString = Integer.toString(portNumber);
483         for(int i=0; i<httpSSLPorts.size(); i++){
484             if(((String JavaDoc)httpSSLPorts.get(i)).equals(portString))
485                 return false;
486         }
487         for(int i=0; i<iiopSSLPorts.size(); i++){
488             if(((String JavaDoc)iiopSSLPorts.get(i)).equals(portString))
489                 return false;
490         }
491         for(int i=0; i<iiopMutualAuthPorts.size(); i++){
492             if(((String JavaDoc)iiopMutualAuthPorts.get(i)).equals(portString))
493                 return false;
494         }
495         for(int i=0; i<sourceXMLCorePorts.size(); i++){
496             if(((String JavaDoc)sourceXMLCorePorts.get(i)).equals(portString))
497                 return false;
498         }
499         //start CR 6407292
500
for(int i=0; i<adminJMXPorts.size(); i++){
501             if(((String JavaDoc)adminJMXPorts.get(i)).equals(portString))
502                 return false;
503         }
504         //end CR 6407292
505
// This NetUtils class is in appserv-commons module.
506
return com.sun.enterprise.util.net.NetUtils.isPortFree(portNumber);
507     }
508     private java.util.List JavaDoc getInstanceNamesWithoutAdminServerAndServer1(java.util.List JavaDoc instanceList){
509         java.util.List JavaDoc newList = new java.util.ArrayList JavaDoc();
510         for(java.util.Iterator JavaDoc it = instanceList.iterator(); it.hasNext();){
511             String JavaDoc serverName = (String JavaDoc)it.next();
512             if(serverName.equals("admin-server")) //|| serverName.equals("server1"))
513
continue;
514             newList.add(serverName);
515         }
516         return newList;
517     }
518     public void processDomainInstances() throws HarnessException {
519         // This method creates instances in target installation for every server instance existing in source.
520
java.util.Hashtable JavaDoc domainMapping = commonInfo.getDomainMapping();
521         
522         // Make sure that this is not called on PE
523
// Safety check
524
if(commonInfo.getTargetEdition().equals(UpgradeConstants.EDITION_PE)){
525             return;
526         }
527         for(java.util.Enumeration JavaDoc domains = domainMapping.keys();domains.hasMoreElements();){
528             DomainInfo dInfo = (DomainInfo)domainMapping.get((String JavaDoc)domains.nextElement());
529             // creation of server instances required only if there more than 2 servers
530
// Is this is valid? admin-server and server1 are default ones.
531
// Fixed: Many a time when domains are created for 7.x appservers, it does create only admin-server and may not create server1
532
//if(dInfo.getInstanceNames().size() > 2){
533
boolean domainStarted = false;
534             //if(this.getInstanceNamesWithoutAdminServer(dInfo.getInstanceNames()).size() > 1){
535
if(this.getInstanceNamesWithoutAdminServerAndServer1(dInfo.getInstanceNames()).size() > 0){
536                 // get adminport and adminhost name for the domain.
537
String JavaDoc adminPort = this.getTargetDomainPort(dInfo.getDomainName(),this.commonInfo);
538                 String JavaDoc adminSecurity = this.getTargetDomainSecurity(dInfo.getDomainName(),this.commonInfo);
539                 String JavaDoc agentName = getTargetNodeAgentName(dInfo.getDomainName(),this.commonInfo);
540                 //if agentName==null the installer did not create a node agent, so we do
541
if ( agentName == null ) {
542                     String JavaDoc hostName;
543                     try {
544                         hostName = (java.net.InetAddress.getLocalHost()).getHostName();
545                         agentName = hostName + "_" + dInfo.getDomainName();
546                     } catch (java.net.UnknownHostException JavaDoc uhe) {
547                         hostName = "localhost";
548                         agentName = dInfo.getDomainName() + "-agent";
549                     }
550                     createNodeAgent(agentName, hostName, adminPort, commonInfo.getAdminUserName(), commonInfo.getAdminPassword(),dInfo.getDomainName());
551                 }
552                 for(java.util.Iterator JavaDoc instanceNames = dInfo.getInstanceNames().iterator(); instanceNames.hasNext();){
553                     String JavaDoc serverName = (String JavaDoc)instanceNames.next();
554                     if(!serverName.equals("admin-server") &&
555                     !commonInfo.getTargetEdition().equals(UpgradeConstants.EDITION_PE)) { //&& !serverName.equals("server1")){
556
if(!domainStarted){
557                             if(!this.startDomain(dInfo.getDomainName())){
558                                 throw new HarnessException(stringManager.getString("domainProcessor.domainStartFailed",dInfo.getDomainName()));
559                             }
560                             domainStarted = true;
561                         }
562                         boolean status = this.createServerInstance(serverName, agentName, null, commonInfo.getAdminUserName(), commonInfo.getAdminPassword(), adminPort, adminSecurity);
563                         if(!instanceNames.hasNext()){
564                             if(domainStarted)
565                                 this.stopDomain(dInfo.getDomainName());
566                         }
567                     }
568                 }
569             }
570         }
571     }
572     
573     public boolean createNodeAgent(String JavaDoc agentName, String JavaDoc dasHost,String JavaDoc dasPort, String JavaDoc dasuser, String JavaDoc daspwd, String JavaDoc domainName){
574         String JavaDoc adminSecurity = DomainsProcessor.getTargetDomainSecurity(domainName, commonInfo);
575         String JavaDoc[] command = {"create-node-agent", "--host",dasHost,"--port", dasPort, "--secure="+adminSecurity, "--user", dasuser, "--passwordfile ", "\"" + commonInfo.getPasswordFile() +"\"",
576         agentName };
577         return this.executeCommand(command);
578     }
579     
580     public boolean createConfig(String JavaDoc configName){
581         // Still dont know when to use. will fill in whenever it is required
582
return false;
583     }
584     
585     public boolean createServerInstance(String JavaDoc serverName, String JavaDoc agentName, String JavaDoc configName, String JavaDoc userid, String JavaDoc pwd, String JavaDoc adminPort, String JavaDoc adminSecurity){
586         // if configName is null, then the command creates serverName_config
587
// if nodeAgent does not exist, will create nodeAgent implicitely
588
if(configName != null){
589             String JavaDoc[] command = {"create-instance","--nodeagent",agentName,"--port",adminPort, "--secure="+adminSecurity, "--config", configName,
590             "--user", userid, "--passwordfile ", "\"" + commonInfo.getPasswordFile() +"\"", serverName };
591             return this.executeCommand(command);
592         }else{
593             //String[] command = {"create-instance","--nodeagent",agentName,
594
// "--user", userid, "--password", pwd, serverName };
595
String JavaDoc[] command = {"create-instance","--nodeagent",agentName,
596             "--user", userid, "--passwordfile ", "\"" + commonInfo.getPasswordFile() +"\"", "--port",adminPort,"--secure="+adminSecurity ,serverName };
597             return this.executeCommand(command);
598         }
599     }
600     
601     private boolean startDomain(String JavaDoc domainName) {
602         return startDomain(domainName, this.commonInfo);
603     }
604     
605     public boolean startDomain(String JavaDoc domainName, CommonInfoModel commonInfo){
606         return Commands.startDomain(domainName, commonInfo);
607     }
608     
609     public boolean stopDomain(String JavaDoc domainName){
610         return Commands.stopDomain(domainName, commonInfo);
611     }
612     
613     private boolean executeCommand(String JavaDoc[] commandStrings){
614         try {
615             return Commands.executeCommand(commandStrings);
616         } catch (com.sun.enterprise.cli.framework.CommandException ce) {
617             //e.printStackTrace();
618
Throwable JavaDoc t = ce.getCause();
619             CommonInfoModel.getDefaultLogger().log(Level.SEVERE, stringManager.getString("enterprise.tools.upgrade.generalException", ce.getMessage()),ce);
620             if (t != null) {
621                 CommonInfoModel.getDefaultLogger().severe(stringManager.getString("enterprise.tools.upgrade.generalException", t.getMessage()));
622             }
623         }
624         return false;
625     }
626     public boolean processClusters() throws HarnessException {
627         java.util.List JavaDoc clInfoList = ClustersInfoManager.getClusterInfoManager().getClusterInfoList();
628         if((clInfoList == null) || (clInfoList.isEmpty()))
629             return false;
630         // Create cluster for each clInfoList.
631
int clusterNo = 0;
632         for(Iterator it = clInfoList.iterator(); it.hasNext(); ){
633             ClusterInfo clInfo = (ClusterInfo)it.next();
634             String JavaDoc clusterName = clInfo.getClusterName();
635             if(clusterName == null)
636                 clusterName = "cluster_"+Integer.toString(clusterNo++);
637             clInfo.setClusterName(clusterName);
638             //this.createCluster(clusterName,clInfo.getMasterInstance().getDomain());
639
this.createCluster(clusterName,clInfo.getDomainName());
640             if(!this.commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){
641                 continue;
642             }
643             List JavaDoc clInstances = clInfo.getClusteredInstanceList();
644             ClusteredInstance masterInstance = clInfo.getMasterInstance();
645             boolean domainStarted = false;
646             String JavaDoc startedDomainName = null;
647             String JavaDoc configName = configName = clusterName+"_config";
648             if(masterInstance == null){
649                 // Is it guaranteed that there will be one master instance always?
650
String JavaDoc adminPort = null;
651                 String JavaDoc adminSecurity = null;
652                 for(Iterator clInstIt = clInstances.iterator(); clInstIt.hasNext();){
653                     ClusteredInstance clusteredInstance = (ClusteredInstance)clInstIt.next();
654                     if(!domainStarted){
655                         startedDomainName = clusteredInstance.getDomain();
656                         if(!this.startDomain(startedDomainName)){
657                             // Could not start domain. Report the problem and break.
658
domainStarted = false;
659                             throw new HarnessException(stringManager.getString("domainProcessor.domainStartFailed",startedDomainName));
660                         }
661                         adminPort = this.getTargetDomainPort(startedDomainName,this.commonInfo);
662                         adminSecurity = this.getTargetDomainSecurity(startedDomainName,this.commonInfo);
663                         domainStarted = true;
664                     }
665                     this.createClusteredInstance(clusteredInstance,clusterName,startedDomainName,configName,adminPort, adminSecurity);
666                     if(!clInstIt.hasNext()){
667                         if(domainStarted)
668                             this.stopDomain(startedDomainName);
669                     }
670                 }
671             }else{
672                 if(this.startDomain(masterInstance.getDomain())){
673                     domainStarted = true;
674                     String JavaDoc adminPort = this.getTargetDomainPort(masterInstance.getDomain(),this.commonInfo);
675                     String JavaDoc adminSecurity = this.getTargetDomainSecurity(masterInstance.getDomain(),this.commonInfo);
676                     this.createClusteredInstance(masterInstance,clusterName,masterInstance.getDomain(),configName,adminPort, adminSecurity);
677                     for(Iterator clInstIt = clInstances.iterator(); clInstIt.hasNext();){
678                         ClusteredInstance clInst = (ClusteredInstance)clInstIt.next();
679                         if(!clInst.isMaster())
680                             this.createClusteredInstance(clInst,clusterName,masterInstance.getDomain(),configName,adminPort, adminSecurity);
681                         if(!clInstIt.hasNext()){
682                             if(domainStarted)
683                                 this.stopDomain(masterInstance.getDomain());
684                         }
685                     }
686                 }else{
687                     throw new HarnessException(stringManager.getString("domainProcessor.domainStartFailed",masterInstance.getDomain()));
688                 }
689             }
690         }
691         // Set currentCluster to null;
692
this.commonInfo.setCurrentCluster(null);
693         this.processStandAloneInstances();
694         return true;
695     }
696     private boolean createClusteredInstance(ClusteredInstance clInstance, String JavaDoc clusterName, String JavaDoc domainName, String JavaDoc configName, String JavaDoc adminPort, String JavaDoc adminSecurity){
697         String JavaDoc nodeAgentName = clInstance.getHost();
698         String JavaDoc dasHostName = null;
699         try {
700             dasHostName = (java.net.InetAddress.getLocalHost()).getHostName();
701         } catch (java.net.UnknownHostException JavaDoc uhe) {
702             dasHostName = domainName;
703         }
704         if(nodeAgentName.equals("localhost")){
705             nodeAgentName = dasHostName + "_" + domainName;
706         } else {
707             nodeAgentName = nodeAgentName + "_" + domainName;
708         }
709         String JavaDoc configFileName = commonInfo.getTargetDomainRoot()+File.separator+domainName+File.separator+"config"+File.separator+"domain.xml";
710         if(!this.isNodeAgentExists(nodeAgentName,configFileName)){
711             createNodeAgent(nodeAgentName, dasHostName, clInstance.getPort(), commonInfo.getAdminUserName(), commonInfo.getAdminPassword(),domainName);
712         }
713         //String property = http-listener-1-port=8078:HTTP_SSL_LISTENER_PORT=1041:IIOP_LISTENER_PORT=3698:IIOP_SSL_LISTENER_PORT=1051:IIOP_SSL_MUTUALAUTH_PORT=1052:JMX_SYSTEM_CONNECTOR_PORT=8688
714
// I must use the data stored in clInstance to create the server instance by setting property.
715
// Config name and cluster name cannot both be specified.
716
String JavaDoc user = commonInfo.getAdminUserName();
717         String JavaDoc password = commonInfo.getAdminPassword();
718         String JavaDoc[] command = {"create-instance","--user", user, "--passwordfile ", "\"" + commonInfo.getPasswordFile() +"\"","--nodeagent",nodeAgentName,"--port",adminPort, "--secure="+adminSecurity,"--cluster",clusterName,clInstance.getInstanceName()};
719         return this.executeCommand(command);
720     }
721     private boolean createCluster(String JavaDoc clusterName, String JavaDoc domainName){
722         String JavaDoc adminSecurity = null;
723         String JavaDoc adminPort = null;
724         if(domainName != null){
725             this.startDomain(domainName);
726             adminPort = this.getTargetDomainPort(domainName, this.commonInfo);
727             adminSecurity = this.getTargetDomainSecurity(domainName, this.commonInfo);
728         }else{
729             // This should be fixed. This is not typical. Domain name is a must.
730
}
731         String JavaDoc[] command = {"create-cluster","--port",adminPort, "--secure="+adminSecurity,"--user",commonInfo.getAdminUserName(),"--passwordfile ", "\"" + commonInfo.getPasswordFile() +"\"",clusterName};
732         boolean returnStatus = this.executeCommand(command);
733         this.stopDomain(domainName);
734         return returnStatus;
735     }
736     public void processStandAloneInstances() throws HarnessException {
737         if(!this.commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){
738             // If the source is AS8.xEE then there server elements are migrated during transformation.
739
return;
740         }
741         List JavaDoc stdAloneInsts = UpgradeUtils.getUpgradeUtils(this.commonInfo).getStandAloneInstances(this.commonInfo.getDomainMapping());
742         for(Iterator it = stdAloneInsts.iterator(); it.hasNext();){
743             Vector instDInfo = (Vector)it.next();
744             String JavaDoc serverName = (String JavaDoc)instDInfo.elementAt(0);
745             DomainInfo dInfo = (DomainInfo)instDInfo.elementAt(1);
746             String JavaDoc adminPort = this.getTargetDomainPort(dInfo.getDomainName(),this.commonInfo);
747             String JavaDoc adminSecurity = this.getTargetDomainSecurity(dInfo.getDomainName(),this.commonInfo);
748             String JavaDoc agentName = getTargetNodeAgentName(dInfo.getDomainName(),this.commonInfo);
749             if(!this.startDomain(dInfo.getDomainName())){
750                 throw new HarnessException(stringManager.getString("domainProcessor.domainStartFailed",dInfo.getDomainName()));
751             }
752             if ( agentName == null ) {
753                 String JavaDoc hostName;
754                 try {
755                     hostName = (java.net.InetAddress.getLocalHost()).getHostName();
756                     agentName = hostName + "_" + dInfo.getDomainName();
757                 } catch (java.net.UnknownHostException JavaDoc uhe) {
758                     hostName = "localhost";
759                     agentName = dInfo.getDomainName() + "-agent";
760                 }
761                 createNodeAgent(agentName, hostName, adminPort, commonInfo.getAdminUserName(), commonInfo.getAdminPassword(),dInfo.getDomainName());
762             }
763             if(!serverName.equals("admin-server")) { // && !serverName.equals("server1")){
764
boolean status = this.createServerInstance(serverName, agentName, null, commonInfo.getAdminUserName(), commonInfo.getAdminPassword(),adminPort, adminSecurity);
765             }
766             this.stopDomain(dInfo.getDomainName());
767         }
768     }
769
770     //start CR 6407292
771
private static String JavaDoc getJMXPortFromXML(String JavaDoc fileName, String JavaDoc tagName, String JavaDoc name) {
772         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
773         factory.setNamespaceAware(true);
774         try {
775             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
776             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
777             ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
778             Document JavaDoc adminServerDoc = builder.parse( new File(fileName));
779             NodeList JavaDoc taggedElements = adminServerDoc.getDocumentElement().getElementsByTagName(tagName);
780             for(int lh =0; lh < taggedElements.getLength(); lh++){
781                 Element JavaDoc element = (Element JavaDoc)taggedElements.item(lh);
782                 // Compare id attribute of http-listener elements.
783
if((element.getAttribute("name")).equals(name)){
784                     return element.getAttribute("port");
785                 }
786             }
787         }catch (Exception JavaDoc ex){
788             logger.log(Level.WARNING, stringManager.getString("domainProcessor.portFromXML"),ex);
789             //throw new HarnessException(stringManager.getString("upgrade.transform.startFailureMessage"));
790
}
791         return null;
792     }
793     //end CR 6407292
794

795 }
796
Popular Tags