KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > deployment > DeploymentUpgrade


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  * DeploymentUpgrade.java
26  *
27  * Created on August 15, 2003, 11:46 AM
28  */

29 package com.sun.enterprise.tools.upgrade.deployment;
30
31 import java.io.*;
32 import java.util.*;
33 import java.util.jar.*;
34 import java.util.logging.*;
35
36 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
37 import com.sun.enterprise.deployment.deploy.shared.FileArchiveFactory;
38 import com.sun.enterprise.deployment.deploy.shared.JarArchiveFactory;
39 import com.sun.enterprise.deployment.deploy.shared.OutputJarArchive;
40 import com.sun.enterprise.tools.upgrade.common.*;
41 import com.sun.enterprise.util.i18n.StringManager;
42
43 import javax.xml.parsers.DocumentBuilder JavaDoc;
44 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
45
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48 import org.w3c.dom.NodeList JavaDoc;
49 import org.w3c.dom.NamedNodeMap JavaDoc;
50
51 import javax.xml.transform.Transformer JavaDoc;
52 import javax.xml.transform.TransformerFactory JavaDoc;
53
54 import javax.xml.transform.dom.DOMSource JavaDoc;
55 import javax.xml.transform.stream.StreamResult JavaDoc;
56 import javax.xml.transform.OutputKeys JavaDoc;
57 import com.sun.enterprise.cli.framework.CommandException;
58 import com.sun.enterprise.deployment.WebBundleDescriptor;
59 import com.sun.enterprise.deployment.io.runtime.WebRuntimeDDFile;
60 import com.sun.enterprise.deployment.node.SaxParserHandler;
61 import org.w3c.dom.Element JavaDoc;
62
63
64 /**
65  * This class transfers the deployed application ears, jars, wars, lifecycle modules and libraries
66  * @author Hans Hrasna
67  */

68 public class DeploymentUpgrade implements com.sun.enterprise.tools.upgrade.common.BaseModule {
69     
70     private static String JavaDoc EAR_DIR = "j2ee-apps";
71     private static String JavaDoc MODULE_DIR = "j2ee-modules";
72     private static String JavaDoc SOURCE_LIBRARY_DIR = "lib";
73     private static String JavaDoc TARGET_LIBRARY_DIR = "lib";
74     private static String JavaDoc MEJB_APP = "MEjbApp";
75     private static String JavaDoc TIMER_APP = "__ejb_container_timer";
76     private static Hashtable deployedModules = new Hashtable();
77     
78     File sourceDir;
79     String JavaDoc targetDir;
80     boolean success = true;
81     boolean domainRunning = false; //assume domain is not running
82
CommonInfoModel commonInfo;
83     
84     private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.deployment");
85     private Logger logger = CommonInfoModel.getDefaultLogger();
86     
87     /** Creates a new instance of DeploymentUpgrade */
88     public DeploymentUpgrade() {
89     }
90     
91     public boolean upgrade(com.sun.enterprise.tools.upgrade.common.CommonInfoModel commonInfo) {
92         this.commonInfo = commonInfo;
93         //start CR 6396940
94
DomainInfo dInfo = new DomainInfo(commonInfo.getCurrentDomain(),
95         commonInfo.getSourceDomainPath());
96         String JavaDoc applicationRoot = dInfo.getDomainApplicationRoot();
97         //end CR 6396940
98

99         if (deployedModules.get(commonInfo.getCurrentDomain()) == null) {
100             deployedModules.put(commonInfo.getCurrentDomain(), new ArrayList());
101         }
102         logger.log(Level.INFO, stringManager.getString("upgrade.deployment.startMessage"));
103     //start CR 6396940
104
String JavaDoc sourceInstance = commonInfo.getSourceDomainPath();
105         //String sourceInstance = commonInfo.getSourceInstancePath();
106
//end CR 6396940
107
String JavaDoc targetInstance = commonInfo.getDestinationDomainPath();
108         domainRunning = false;
109         
110         processLibraries(sourceInstance, targetInstance);
111     //start CR 6396940
112
//sourceDir = new File(sourceInstance, "applications");
113
sourceDir = new File(sourceInstance, applicationRoot);
114     //end CR 6396940
115

116         //targetDir = targetInstance + File.separatorChar + "autodeploy";
117
try {
118             File tmp = File.createTempFile("upgrade", null);
119             targetDir = tmp.getParent();
120             tmp.delete();
121         } catch (IOException ioe) {
122             logger.severe(stringManager.getString("upgrade.deployment.ioExceptionMsg")+ioe.getMessage());
123             return false;
124         }
125         
126         // process lifecycle modules
127
processLifecycles();
128         
129         
130         
131         // process apps
132
File srcEarDir = new File(sourceDir, EAR_DIR);
133         //start CR 6396940
134
if(srcEarDir != null) {
135         //end CR 6396940
136
if (srcEarDir.listFiles() != null && srcEarDir.listFiles().length > 0) {
137                 domainRunning = startDomain(commonInfo.getCurrentDomain());
138                 if (domainRunning) {
139                     processApplications(srcEarDir);
140                 } else {
141                     return false;
142                 }
143             }
144     }
145         
146         // process standalone modules
147
File srcJarDir = new File(sourceDir, MODULE_DIR);
148         //start CR 6396940
149
if(srcJarDir != null) {
150             if(srcJarDir.listFiles() != null && srcJarDir.listFiles().length > 0) {
151         //end CR 6396940
152
//if (srcJarDir.listFiles().length > 0) {
153
if (!domainRunning) {
154                     domainRunning = startDomain(commonInfo.getCurrentDomain());
155                 }
156                 if (domainRunning) {
157                     processStandaloneModules(srcJarDir);
158                 } else {
159                     return false;
160                 }
161             }
162     }
163         
164         if (domainRunning) {
165             stopDomain(commonInfo.getCurrentDomain());
166         }
167         return success;
168         
169         
170     }
171     
172     public void recovery(CommonInfoModel commonInfo) {
173         commonInfo.deletePasswordFile();
174     }
175     
176     /* Builds an ear out of each deployed application
177        and deploys it on the target */

178     private void processApplications(File srcDir) {
179         
180         File [] earDirs = srcDir.listFiles();
181         for (int i=0;i<earDirs.length;i++) {
182             String JavaDoc earDirName = earDirs[i].getName();
183             String JavaDoc earName;
184             // START CR 6406682
185
if (earDirName.lastIndexOf("_ear") > -1) {
186                 earName = earDirName.substring(0,earDirName.lastIndexOf("_"));
187             // END CR 6406682
188
} else {
189                 earName = earDirName;
190             }
191             String JavaDoc appName = earName;
192             if (appName.startsWith(MEJB_APP) || appName.startsWith(TIMER_APP)) {
193                 continue;
194             }
195             earName=earName+".ear";
196             try {
197                 JarArchiveFactory jaf = new JarArchiveFactory();
198                 OutputJarArchive targetJar = (OutputJarArchive)jaf.createArchive(new File(targetDir, earName).getAbsolutePath());
199                 FileArchiveFactory faf = new FileArchiveFactory();
200                 FileArchive farc = (FileArchive)faf.openArchive((new File(srcDir, earDirName)).getAbsolutePath());
201                 Enumeration e = farc.entries();
202                 String JavaDoc lastModuleProcessed = "";
203                 while(e.hasMoreElements()) {
204                     String JavaDoc s = (String JavaDoc)e.nextElement();
205                     String JavaDoc moduleDir;
206                     try {
207                         moduleDir = s.substring(0, s.lastIndexOf('_')+4);
208                     } catch (StringIndexOutOfBoundsException JavaDoc sob) {
209                         moduleDir = "";
210                     }
211                     
212                     FileInputStream fis = null;
213                     OutputStream out = null;
214                     try {
215                         if (moduleDir.endsWith("_jar") || moduleDir.endsWith("_war") || moduleDir.endsWith("_rar")) {
216                             if(lastModuleProcessed.equals(moduleDir)) {
217                                 continue;
218                             }
219                             File jar = processModule(EAR_DIR, earDirName, moduleDir);
220                             lastModuleProcessed = moduleDir;
221                             out = targetJar.putNextEntry(jar.getName());
222                             fis = new FileInputStream(jar);
223                         } else {
224                             if (!s.endsWith("Client.jar")) { // don't include *Client.jars generated by server
225
out = targetJar.putNextEntry(s);
226                                 fis = new FileInputStream(new File(new File(srcDir, earDirName), s));
227                             } else {
228                                 continue;
229                             }
230                             
231                         }
232                         
233                         while(fis.available() > 0) {
234                             int ix = fis.read();
235                             out.write(ix);
236                         }
237                         
238                     } catch(java.util.zip.ZipException JavaDoc z) {
239                         logger.warning(stringManager.getString("upgrade.deployment.zipExceptionMsg")+z.getMessage());
240                     } catch (IOException ioe) {
241                         logger.severe(stringManager.getString("upgrade.deployment.ioExceptionMsg")+ioe.getMessage());
242                     }
243                     targetJar.closeEntry();
244                 }
245                 targetJar.close();
246                 if(deploy((new File(targetJar.getArchiveUri())).getAbsolutePath())) {
247                     logger.info(stringManager.getString("upgrade.deployment.finishedProcessingMsg") + appName);
248                 } else {
249                     logger.warning(appName + " " + stringManager.getString("upgrade.deployment.errorProcessingMsg"));
250                 }
251             } catch(Exception JavaDoc ex) {
252                 logger.severe(stringManager.getString("upgrade.deployment.generalExceptionMsg")+ ex.toString() + ": " + ex.getMessage());
253             }
254         }
255     }
256     
257     private void processStandaloneModules(File srcModuleDir) {
258         
259         File [] moduleDirs = srcModuleDir.listFiles();
260         for (int i=0;i<moduleDirs.length;i++) {
261             String JavaDoc moduleDirName = moduleDirs[i].getName();
262             File jarFile = processModule(MODULE_DIR, "", moduleDirName);
263             String JavaDoc moduleName;
264             if (moduleDirName.lastIndexOf('_') > -1 ) {
265                 moduleName = moduleDirName.substring(0,moduleDirName.lastIndexOf('_'));
266             } else {
267                 moduleName = moduleDirName;
268             }
269             //if (jarFile.renameTo(new File(targetDir, jarFile.getName()))) {
270
if(deploy(jarFile.getAbsolutePath())) {
271                 logger.info(stringManager.getString("upgrade.deployment.finishedProcessingMsg") + moduleName );
272             } else {
273                 logger.warning(moduleName + " " + stringManager.getString("upgrade.deployment.errorProcessingMsg"));
274             }
275         }
276     }
277     
278     //process a module and return it as a jar file
279
private File processModule(String JavaDoc appDir, String JavaDoc earDirName, String JavaDoc moduleDirName) {
280         String JavaDoc moduleName;
281         // START CR6406682
282
if ((moduleDirName.lastIndexOf("_war") > -1) ||
283               (moduleDirName.lastIndexOf("_rar") > -1) ||
284                (moduleDirName.lastIndexOf("_jar") > -1)){
285         // END CR6406682
286
moduleName = moduleDirName.substring(0,moduleDirName.lastIndexOf('_'));
287         } else {
288             moduleName = moduleDirName;
289         }
290         
291         try {
292             
293             JarArchiveFactory jaf = new JarArchiveFactory();
294             FileArchiveFactory faf = new FileArchiveFactory();
295             FileArchive farc = (FileArchive)faf.openArchive(new File(new File(new File(sourceDir, appDir), earDirName), moduleDirName).getAbsolutePath());
296             String JavaDoc suffix = ".jar"; //default to .jar
297
//File temp;
298
Enumeration e = farc.entries();
299             //figure out what type of module this is by the existance of the standard dd's
300
while(e.hasMoreElements()) {
301                 String JavaDoc entry = (String JavaDoc)e.nextElement();
302                 if (entry.equalsIgnoreCase("WEB-INF/web.xml")) {
303                     suffix = ".war";
304                 } else if (entry.equalsIgnoreCase("META-INF/ra.xml")) {
305                     suffix = ".rar";
306                 }
307             }
308             //temp = File.createTempFile(moduleName, suffix);
309
File tempJar = new File(targetDir, moduleName + suffix);
310             String JavaDoc path = tempJar.getAbsolutePath();
311             //temp.delete();
312
OutputJarArchive targetModule = (OutputJarArchive)jaf.createArchive(path);
313             logger.fine(stringManager.getString("upgrade.deployment.addingInfoMsg") + targetModule.getArchiveUri());
314             e = farc.entries();
315             while(e.hasMoreElements()) {
316                 String JavaDoc entry = (String JavaDoc)e.nextElement();
317                 InputStream in = farc.getEntry(entry);
318                 if (entry.equals("WEB-INF/web.xml")) {
319                     InputStream fixedDescriptor = fixWebServiceDescriptor(farc);
320                     if(fixedDescriptor != null) {
321                         in = fixedDescriptor;
322                     }
323                 }
324         //start RFE 6389864
325
if(entry.equals("WEB-INF/sun-web.xml")) {
326             checkDescriptors(farc, "sun-web.xml", "WEB-INF");
327                 }
328                 if(entry.equals("META-INF/sun-ejb-jar.xml")) {
329                     checkDescriptors(farc, "sun-ejb-jar.xml", "META-INF");
330                 }
331         //end RFE 6389864
332
OutputStream out = null;
333                 try {
334                     out = targetModule.putNextEntry(entry);
335                     int i = in.read();
336                     do {
337                         out.write(i);
338                         i = in.read();
339                     } while (i > -1);
340                 } catch(java.util.zip.ZipException JavaDoc z) {
341                     logger.warning(stringManager.getString("upgrade.deployment.zipExceptionMsg")+z.getMessage());
342                 }catch (IOException ioe) {
343                     logger.severe(stringManager.getString("upgrade.deployment.ioExceptionMsg")+ioe.getMessage());
344                 }
345                 targetModule.closeEntry();
346             }
347             InputStream in = farc.getEntry(JarFile.MANIFEST_NAME);
348             OutputStream out = null;
349             try {
350                 out = targetModule.putNextEntry(JarFile.MANIFEST_NAME);
351                 int i = in.read();
352                 do {
353                     out.write(i);
354                     i = in.read();
355                 } while (i > -1);
356             } catch(java.util.zip.ZipException JavaDoc z) {
357                 logger.warning(stringManager.getString("upgrade.deployment.zipExceptionMsg")+z.getMessage());
358             }catch (IOException ioe) {
359                 logger.severe(stringManager.getString("upgrade.deployment.ioExceptionMsg")+ioe.getMessage());
360             }
361             targetModule.closeEntry();
362             
363             targetModule.close();
364             return tempJar;
365         } catch(Exception JavaDoc ex) {
366             logger.severe(stringManager.getString("upgrade.deployment.generalExceptionMsg")+ ex.toString() + ": " + ex.getMessage());
367         }
368         return null;
369     }
370     
371     /* Lifecycle modules are processed based on the configuration entry
372      */

373     private void processLifecycles(){
374         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
375         //factory.setValidating(true);
376
factory.setNamespaceAware(true);
377         if(commonInfo.getSourceDomainRootFlag()) {
378             factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",Boolean.FALSE);
379         }
380         try {
381             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
382             builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
383                     ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
384             Document JavaDoc sourceDoc = builder.parse( new File(commonInfo.getSourceConfigXMLFile()) );
385             Document JavaDoc targetDoc = builder.parse( new File(commonInfo.getTargetConfigXMLFile()) );
386             
387             NodeList JavaDoc nl = sourceDoc.getElementsByTagName("lifecycle-module");
388             for(int i =0; i < nl.getLength(); i++){
389                 Node JavaDoc node = nl.item(i);
390                 Node JavaDoc newNode = targetDoc.importNode(node, true);
391                 NamedNodeMap JavaDoc attributes = newNode.getAttributes();
392                 String JavaDoc lcname = attributes.getNamedItem("name").getNodeValue();
393                 Node JavaDoc classpathNode = attributes.getNamedItem("classpath");
394                 try {
395                     String JavaDoc classpath = null;
396                     if (classpathNode != null) {
397                         classpath = classpathNode.getNodeValue();
398                         File testPath = new File(classpath);
399                         if(!testPath.exists()) {
400                             logger.warning( stringManager.getString("upgrade.deployment.lifecycleErrorMsg") + lcname );
401                             logger.warning( stringManager.getString("upgrade.deployment.lifecycleClasspathMsg" + classpath, lcname) );
402                             continue;
403                         }
404                     }
405                     NodeList JavaDoc appNodeList = targetDoc.getElementsByTagName("applications");
406                     Node JavaDoc applicationsNode = appNodeList.item(0); // there is only one
407
//check for pre-existing Lifecycle module entry in the target with the same name
408
NodeList JavaDoc applicationsList = applicationsNode.getChildNodes();
409                     boolean foundDup = false;
410                     for (int n=0; n < applicationsList.getLength(); n++) {
411                         Node JavaDoc appNode = (Node JavaDoc)applicationsList.item(n);
412                         if ( appNode.getNodeName().equals(newNode.getNodeName()) ) {
413                             NamedNodeMap JavaDoc appNodeAttrs = appNode.getAttributes();
414                             NamedNodeMap JavaDoc newNodeAttrs = newNode.getAttributes();
415                             Node JavaDoc appNodeName = appNodeAttrs.getNamedItem("name");
416                             Node JavaDoc newNodeName = appNodeAttrs.getNamedItem("name");
417                             String JavaDoc newNodeNameString = newNodeName.getNodeValue();
418                             if (newNodeNameString.equals(appNodeName.getNodeValue())) {
419                                 logger.warning(stringManager.getString("upgrade.deployment.lifecycleExistsMsg", newNodeNameString ));
420                                 foundDup = true;
421                                 break;
422                             }
423                         }
424                     }
425                     if (!foundDup) {
426                         applicationsNode.appendChild(newNode);
427                     }
428                     
429                 } catch (SecurityException JavaDoc se) {
430                     logger.warning(stringManager.getString("upgrade.deployment.lifecycleClasspathMsg", node.getNodeName()) + se.getMessage());
431                 }
432                 
433                 
434                 
435             }
436             TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
437             Transformer JavaDoc transformer = tFactory.newTransformer();
438             if (targetDoc.getDoctype() != null){
439                 String JavaDoc systemValue = targetDoc.getDoctype().getSystemId();
440                 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);
441                 String JavaDoc pubValue = targetDoc.getDoctype().getPublicId();
442                 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubValue);
443             }
444             DOMSource JavaDoc source = new DOMSource JavaDoc(targetDoc);
445             StreamResult JavaDoc result = new StreamResult JavaDoc(new FileOutputStream(commonInfo.getTargetConfigXMLFile()));
446             transformer.transform(source, result);
447             
448         } catch (Exception JavaDoc ex){
449             logger.log(Level.SEVERE, stringManager.getString("upgrade.deployment.generalExceptionMsg"), ex);
450         }
451     }
452     
453     /* recursively copy the contents of sourceInstance/lib -> targetInstance/lib
454      */

455     private void processLibraries(String JavaDoc sourceInstance, String JavaDoc targetInstance){
456         File sourceDir = new File(sourceInstance, SOURCE_LIBRARY_DIR);
457         File targetDir = new File(targetInstance, TARGET_LIBRARY_DIR);
458         try {
459             copyDir(sourceDir, targetDir);
460         } catch(FileNotFoundException fnf) {
461             logger.severe(stringManager.getString("upgrade.deployment.generalExceptionMsg")+ fnf.toString() + ": " + fnf.getMessage());
462         } catch(IOException ioe) {
463             logger.severe(stringManager.getString("upgrade.deployment.ioExceptionMsg")+ioe.getMessage());
464         }
465     }
466     
467     private void copyDir(File inputDir, File outputDir) throws FileNotFoundException, IOException {
468         File [] srcFiles = inputDir.listFiles();
469         if (srcFiles != null) {
470             for(int i=0; i< srcFiles.length; i++) {
471                 File dest = new File(outputDir, srcFiles[i].getName());
472                 if( srcFiles[i].isDirectory() ) {
473                     if (!dest.exists()) {
474                         dest.mkdir();
475                     }
476                     copyDir(srcFiles[i], dest);
477                 } else {
478                     if (!dest.exists()) {
479                         dest.createNewFile();
480                     }
481                     copyFile(srcFiles[i], new File(outputDir, srcFiles[i].getName()));
482                     
483                 }
484             }
485         }
486     }
487     
488     private void copyFile(File inputFile, File outputFile) throws FileNotFoundException, IOException {
489         FileReader in = new FileReader(inputFile);
490         FileWriter out = new FileWriter(outputFile);
491         int c;
492         
493         while ((c = in.read()) != -1) {
494             out.write(c);
495         }
496         
497         in.close();
498         out.close();
499     }
500     
501     private boolean deploy(String JavaDoc modulePath) {
502         if (commonInfo.getTargetEdition().equals(UpgradeConstants.EDITION_EE)) {
503             ArrayList mods = (ArrayList)deployedModules.get(commonInfo.getCurrentDomain());
504             String JavaDoc fileName = new File(modulePath).getName();
505             String JavaDoc moduleName = fileName.substring(0, fileName.lastIndexOf('.'));
506             if (mods.contains(moduleName)) {
507                 String JavaDoc currentDomain = commonInfo.getCurrentDomain();
508                 String JavaDoc adminPort = DomainsProcessor.getTargetDomainPort(currentDomain, commonInfo);
509                 String JavaDoc adminSecurity = DomainsProcessor.getTargetDomainSecurity(currentDomain, commonInfo);
510                 String JavaDoc[] createAppRefCommand = {
511                     "create-application-ref",
512                             "--user", commonInfo.getAdminUserName(),
513                             "--passwordfile ", "\"" + commonInfo.getPasswordFile()+ "\"",
514                             "--port",adminPort,
515                             "--secure=" + adminSecurity,
516                             "--target", commonInfo.getCurrentSourceInstance(),
517                             moduleName
518                 };
519                 try {
520                     return Commands.executeCommand(createAppRefCommand);
521                 } catch(CommandException ce) {
522                     logger.log(Level.SEVERE, stringManager.getString("upgrade.deployment.generalExceptionMsg"), ce);
523                     return false;
524                 }
525             } else {
526                 if (Commands.deploy(modulePath, commonInfo)) {
527                     mods.add(moduleName);
528                     return true;
529                 }
530                 return false;
531             }
532         }
533         return Commands.deploy(modulePath, commonInfo);
534     }
535     
536     private boolean startDomain(String JavaDoc domainName) {
537         return Commands.startDomain(domainName, commonInfo);
538     }
539     
540     private boolean stopDomain(String JavaDoc domainName) {
541         return Commands.stopDomain(domainName, commonInfo);
542     }
543     
544     
545     public static void main(String JavaDoc [] args) {
546         try{
547             com.sun.enterprise.tools.upgrade.logging.LogService.initialize("upgradetest.log");
548         }catch(Exception JavaDoc e){
549             e.printStackTrace();
550         }
551         CommonInfoModel cim = new CommonInfoModel();
552         cim.setSourceInstallDir("C:\\Sun\\AppServer80");
553         cim.setTargetInstallDir("C:\\Sun\\AppServer");
554         cim.setCurrentDomain("domain1");
555         //cim.setCurrentSourceInstance("domain1");
556
cim.setTargetDomainRoot("C:\\Sun\\AppServer\\domains");
557         cim.setSourceDomainRoot("C:\\Sun\\AppServer80\\domains");
558         java.util.Hashtable JavaDoc ht = new java.util.Hashtable JavaDoc();
559         ht.put("domain1", "C:\\Sun\\AppServer80\\domains\\domain1");
560         cim.setDomainMapping(ht);
561         cim.enlistDomainsFromSource();
562         cim.setAdminUserName("admin");
563         cim.setAdminPassword("adminadmin");
564         new DeploymentUpgrade().upgrade(cim);
565     }
566     
567     
568     public String JavaDoc getName() {
569         return stringManager.getString("upgrade.deployment.moduleName");
570     }
571    
572     //start RFE 6389864
573
private void checkDescriptors(FileArchive farc, String JavaDoc fileName, String JavaDoc dirName) throws IOException {
574         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
575         factory.setValidating(false);
576         try {
577             DocumentBuilder JavaDoc docBuilder = factory.newDocumentBuilder();
578             docBuilder.setEntityResolver(new SaxParserHandler());
579             String JavaDoc dirPath = farc.getArchiveUri();
580             Document JavaDoc document = docBuilder.parse(dirPath + File.separatorChar + dirName + File.separatorChar + fileName);
581             Element JavaDoc docEle = document.getDocumentElement();
582             NodeList JavaDoc securityBindingList = docEle.getElementsByTagName("message-security-binding");
583             for(int i=0; i<securityBindingList.getLength();i++) {
584                 Element JavaDoc element = (Element JavaDoc) securityBindingList.item(i);
585                 if(element != null) {
586                     if(element.getAttribute("provider-id") != null) {
587             if(element.getAttribute("provider-id").equals("ClientProvider"))
588                 logger.log(Level.WARNING, stringManager.getString("upgrade.deployment.messageSecurityConfig",
589                 fileName, "ClientProvider"));
590             else if(element.getAttribute("provider-id").equals("ServerProvider"))
591                     logger.log(Level.WARNING, stringManager.getString("upgrade.deployment.messageSecurityConfig",
592                 fileName, "ServerProvider"));
593                 }
594         }
595             }
596         } catch(IOException ioe) {
597             logger.log(Level.WARNING, stringManager.getString("upgrade.deployment.ioExceptionMsg") + ioe.getLocalizedMessage());
598         } catch (Exception JavaDoc e) {
599             logger.log(Level.WARNING, stringManager.getString("upgrade.deployment.generalExceptionMsg") + e.getLocalizedMessage());
600         }
601
602     }
603
604     //end RFE 6389864
605

606     private InputStream fixWebServiceDescriptor(FileArchive farc) throws IOException {
607         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
608         factory.setValidating(false);
609         try {
610             DocumentBuilder JavaDoc docBuilder = factory.newDocumentBuilder();
611             docBuilder.setEntityResolver(new SaxParserHandler());
612             String JavaDoc dirPath = farc.getArchiveUri();
613             Document JavaDoc document = docBuilder.parse(dirPath + File.separatorChar + "WEB-INF" + File.separatorChar + "web.xml");
614             Element JavaDoc docEle = document.getDocumentElement();
615             NodeList JavaDoc servletList = docEle.getElementsByTagName("servlet");
616             for(int i=0; i<servletList.getLength();i++) {
617                 Node JavaDoc currentServletNode = servletList.item(i);
618                 NodeList JavaDoc nodeList = ((Element JavaDoc)currentServletNode).getElementsByTagName("servlet-name");
619                 String JavaDoc servletName = getTextNodeData(nodeList.item(0));
620                 nodeList = ((Element JavaDoc)currentServletNode).getElementsByTagName("servlet-class");
621                 Node JavaDoc servletClassNode = nodeList.item(0); //there is only one servlet-class element
622
if(servletClassNode == null) {
623                     return null;
624                 }
625                 String JavaDoc servletClass = getTextNodeData(servletClassNode);
626                 if(servletClass.equals("com.sun.enterprise.webservice.JAXRPCServlet")) {
627                     //DocumentBuilder docBuilder2 = factory.newDocumentBuilder();
628
//docBuilder2.setEntityResolver(new SaxParserHandler());
629
Document JavaDoc sunWebXml = docBuilder.parse(dirPath + File.separatorChar + "WEB-INF" + File.separatorChar + "sun-web.xml");
630                     Element JavaDoc de = sunWebXml.getDocumentElement();
631                     NodeList JavaDoc sunServletList = de.getElementsByTagName("servlet");
632                     for(int x=0;x<sunServletList.getLength();x++){
633                         Node JavaDoc sunServletNode = sunServletList.item(x);
634                         NodeList JavaDoc list = ((Element JavaDoc)sunServletNode).getElementsByTagName("servlet-name");
635                         String JavaDoc sunServletName = getTextNodeData(list.item(0));
636                         if(sunServletName.equals(servletName)) {
637                             NodeList JavaDoc nList = ((Element JavaDoc)sunServletNode).getElementsByTagName("servlet-impl-class");
638                             Node JavaDoc servletImplNode = nList.item(0);
639                             String JavaDoc origServletClass = getTextNodeData(servletImplNode);
640                             setTextNodeData(servletClassNode, origServletClass);
641                         }
642                     }
643                 }
644             }
645             // write out the document to a temporary file.
646
// Use a Transformer for output
647
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
648             Transformer JavaDoc transformer = tFactory.newTransformer();
649             if (document.getDoctype() != null){
650                 String JavaDoc systemValue = document.getDoctype().getSystemId();
651                 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);
652                 String JavaDoc pubValue = document.getDoctype().getPublicId();
653                 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubValue);
654             }
655             DOMSource JavaDoc source = new DOMSource JavaDoc(document);
656             File webTempFile = File.createTempFile("web","xml");
657             StreamResult JavaDoc result = new StreamResult JavaDoc(new FileOutputStream(webTempFile));
658             transformer.transform(source, result);
659             return new FileInputStream(webTempFile);
660         } catch (IOException ioe) {
661             logger.log(Level.WARNING, stringManager.getString("upgrade.deployment.ioExceptionMsg") + ioe.getLocalizedMessage());
662         } catch (Exception JavaDoc e) {
663             logger.log(Level.WARNING, stringManager.getString("upgrade.deployment.generalExceptionMsg") + e.getLocalizedMessage());
664         }
665         
666         return farc.getEntry("WEB-INF/web.xml");
667     }
668     
669     private String JavaDoc getTextNodeData(Node JavaDoc node){
670         NodeList JavaDoc children = ((Element JavaDoc)node).getChildNodes();
671         for(int index=0; index < children.getLength(); index++){
672             if(children.item(index).getNodeType() == Node.TEXT_NODE){
673                 return children.item(index).getNodeValue();
674             }
675         }
676         return "";
677     }
678     
679     private void setTextNodeData(Node JavaDoc node, String JavaDoc text){
680         NodeList JavaDoc children = ((Element JavaDoc)node).getChildNodes();
681         for(int index=0; index < children.getLength(); index++){
682             if(children.item(index).getNodeType() == Node.TEXT_NODE){
683                 children.item(index).setNodeValue(text);
684             }
685         }
686     }
687 }
688
Popular Tags