KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > miscconfig > SunACCTransfer


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  * SunACCTransfer.java
26  *
27  * Created on September 8, 2003, 2:08 PM
28  */

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

36 import java.io.*;
37 import java.util.logging.*;
38 import com.sun.enterprise.util.i18n.StringManager;
39 import com.sun.enterprise.tools.upgrade.logging.*;
40
41 public class SunACCTransfer {
42     
43     private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.miscconfig");
44     private Logger logger = com.sun.enterprise.tools.upgrade.common.CommonInfoModel.getDefaultLogger();
45     private String JavaDoc docType = null;
46     private String JavaDoc secPolicyProperty = null;
47     /** Creates a new instance of SunACCTransfer */
48     public SunACCTransfer() {
49     }
50     public void transform(String JavaDoc sourceFileName, String JavaDoc targetFileName){
51         logger.log(Level.INFO, stringManager.getString("upgrade.configTransfers.sunacc.startMessage"));
52         File sourceFile = new File(sourceFileName);
53         File targetFile = new File(targetFileName);
54         try {
55             docType = this.getDOCTYPEString(targetFile);
56             secPolicyProperty = this.getSecurityConfigValue(targetFile);
57         } catch(Exception JavaDoc e) {}
58         
59         boolean renamed = targetFile.renameTo(new File(targetFileName+".bak"));
60         if(!renamed){
61             // This is possible if user is running the upgrade again and .bak is already created.
62
renamed = targetFile.delete();
63         }
64         if(renamed){
65             try{
66                 targetFile = new File(targetFileName);
67                 targetFile.createNewFile();
68                 this.transferFileContents(sourceFile, targetFile);
69             }catch(Exception JavaDoc ex){
70                 // Log error message
71
logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.sunacc.startFailureMessage"),ex);
72             }
73         }else{
74             // Log error message
75
logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.sunacc.renameFailureMessage"));
76         }
77     }
78     private String JavaDoc getDOCTYPEString(File target) throws Exception JavaDoc{
79         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(target)));
80         String JavaDoc readLine = null;
81         while((readLine = reader.readLine()) != null){
82             if(readLine.indexOf("<!DOCTYPE") != -1){
83                 return readLine;
84             }
85         }
86         return null;
87     }
88
89     private String JavaDoc getSecurityConfigValue(File target) throws Exception JavaDoc{
90         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(target)));
91         String JavaDoc readLine = null;
92         while((readLine = reader.readLine()) != null){
93             if(readLine.indexOf("<property") != -1){
94                 if(readLine.indexOf("security.config") != -1) {
95                     return readLine;
96                 }
97             }
98         }
99         return null;
100     }
101
102     private void transferFileContents(File source, File target) throws Exception JavaDoc{
103         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(source)));
104         PrintWriter writer = new PrintWriter(new FileOutputStream(target));
105         String JavaDoc readLine = null;
106         while((readLine = reader.readLine()) != null){
107             if((readLine.indexOf("<!DOCTYPE") == -1) && (readLine.indexOf("<property") == -1)) {
108                 writer.println(readLine);
109             } else if(readLine.indexOf("<!DOCTYPE") != -1){
110                 if(docType != null)
111                     writer.println(docType);
112                 else
113                     writer.println();
114             } else if(readLine.indexOf("<property") != -1) {
115                 if(readLine.indexOf("security.config") != -1) {
116                     if(secPolicyProperty != null)
117                         writer.println(secPolicyProperty);
118                     else
119                         writer.println();
120                 } else {
121                     writer.println(readLine);
122                 }
123             }
124         }
125         writer.flush();
126         writer.close();
127         reader.close();
128     }
129 }
130
Popular Tags