KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ConfigTransfers.java
26  *
27  * Created on September 8, 2003, 9:05 AM
28  */

29
30 package com.sun.enterprise.tools.upgrade.miscconfig;
31
32 /**
33  *
34  * @author prakash
35  * @author hans hrasna
36  * This class is used to transfer config files
37  * server.policy, sun-acc.xml, default-web.xml, secmod.db
38  * with minor modifications wherever necessary.
39  */

40 import com.sun.enterprise.tools.upgrade.common.*;
41 import java.io.*;
42 import com.sun.enterprise.util.i18n.StringManager;
43 import com.sun.enterprise.tools.upgrade.logging.*;
44 import java.util.logging.*;
45 import java.util.Enumeration JavaDoc;
46 import java.util.Collections JavaDoc;
47 import java.util.LinkedList JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.Vector JavaDoc;
50
51 public class ConfigTransfers implements BaseModule{
52     
53     private static final String JavaDoc SECMODDB = "secmod.db";
54     
55     private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.miscconfig");
56     private Logger logger = CommonInfoModel.getDefaultLogger();
57     private Vector JavaDoc recoveryList = new Vector JavaDoc();
58     
59     /** Creates a new instance of ConfigTransfers
60      *
61      */

62     public ConfigTransfers() {
63         
64     }
65     
66     public boolean upgrade(CommonInfoModel commonInfo) {
67         transferServerPolicy(commonInfo.getSourceServerPolicyFileName(), commonInfo.getTargetServerPolicyFileName());
68         //transferSecModDb(commonInfo);
69
new DefaultWebXMLTransfer().transform(commonInfo.getSourceDefaultWebXMLFileName(), commonInfo.getTargetDefaultWebXMLFileName());
70         new SunACCTransfer().transform(commonInfo.getSourceSunACCFileName(), commonInfo.getTargetSunACCFileName());
71         
72         if(commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){
73             new InitConfTransfer(commonInfo).transform();
74             
75         }else{
76             // For 8.x still need to figure out what need to be transferred.
77
// FIXME
78
}
79         return true;
80     }
81     
82     public void recovery(CommonInfoModel commonInfo) {
83         Enumeration JavaDoc e = recoveryList.elements();
84         while(e.hasMoreElements()){
85             String JavaDoc recoverPath = (String JavaDoc)e.nextElement();
86             String JavaDoc backupPath = recoverPath + ".bak";
87             try {
88                 UpgradeUtils.getUpgradeUtils(commonInfo).copyFile(backupPath, recoverPath);
89                 new File(backupPath).delete();
90             } catch (IOException ioe) {
91                 logger.log(Level.SEVERE, stringManager.getString("upgrade.realm.recoveryFailureMessage",ioe.getMessage()),new Object JavaDoc[]{recoverPath,ioe});
92             }
93         }
94     }
95     
96     /* transferSeverPolicy uses sun.security.provider.PolicyParser from the jdk to parse the source and
97      * target server.policy files and transfer user added grants and permissions
98      * @author hans
99      *
100      */

101     private void transferServerPolicy(String JavaDoc sourcePolicyFileName, String JavaDoc destPolicyFileName){
102         if (!backup(destPolicyFileName)) {
103             logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.backupFailureMessage"));
104             logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage"));
105             return;
106         }
107         logger.log(Level.INFO, stringManager.getString("upgrade.configTransfers.serverPolicy.startMessage"));
108         PolicyParser sourcePolicy = new PolicyParser();
109         PolicyParser targetPolicy = new PolicyParser();
110         try {
111             sourcePolicy.read(new FileReader(sourcePolicyFileName));
112             targetPolicy.read(new FileReader(destPolicyFileName));
113         } catch (PolicyParser.ParsingException pe) {
114             logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage") + pe.getLocalizedMessage());
115             return;
116         } catch (IOException ioe) {
117             logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage"),ioe.getMessage());
118             return;
119         }
120         Enumeration JavaDoc sourceElements = sourcePolicy.grantElements();
121         Enumeration JavaDoc targetElements = targetPolicy.grantElements();
122         
123         while(sourceElements.hasMoreElements()) {
124             PolicyParser.GrantEntry sourceGrantEntry = (PolicyParser.GrantEntry)sourceElements.nextElement();
125             boolean matchedGrantEntry = false;
126             while (targetElements.hasMoreElements()) {
127                 PolicyParser.GrantEntry targetGrantEntry = (PolicyParser.GrantEntry)targetElements.nextElement();
128                 if(targetGrantEntry.codeBase == null && sourceGrantEntry.codeBase == null) {
129                     matchedGrantEntry = true;
130                 } else if (targetGrantEntry.codeBase != null && sourceGrantEntry.codeBase != null) {
131                     if (targetGrantEntry.codeBase.equals(sourceGrantEntry.codeBase)) {
132                         //found a matched codeBase
133
matchedGrantEntry = true;
134                     }
135                 }
136                 if(matchedGrantEntry) {
137                     //does the target have all the permissions of the source? if not, add the missing permissions
138
Enumeration JavaDoc sourcePermissions = sourceGrantEntry.permissionElements();
139                     while(sourcePermissions.hasMoreElements()) {
140                         boolean matchedPermission = false;
141                         PolicyParser.PermissionEntry sourcePermission = (PolicyParser.PermissionEntry)sourcePermissions.nextElement();
142                         /*if(!targetGrantEntry.contains(sourcePermission)){
143                             targetGrantEntry.add(sourcePermission);
144                         }*/

145                         Enumeration JavaDoc targetPermissions = targetGrantEntry.permissionElements();
146                         while(targetPermissions.hasMoreElements()) {
147                             PolicyParser.PermissionEntry targetPermission = (PolicyParser.PermissionEntry)targetPermissions.nextElement();
148                             if(targetPermission.equals(sourcePermission)) {
149                                 matchedPermission = true;
150                                 break;
151                             }
152                         }
153                         if(!matchedPermission){
154                             targetGrantEntry.add(sourcePermission);
155                         }
156                     }
157                     //does the target have all the principals of the source? if not, add the missing principals
158
Iterator JavaDoc sourcePrincipalIterator = sourceGrantEntry.principals.iterator();
159                     while(sourcePrincipalIterator.hasNext()) {
160                         boolean matchedPrincipal = false;
161                         PolicyParser.PrincipalEntry sourcePrincipalEntry = (PolicyParser.PrincipalEntry)sourcePrincipalIterator.next();
162                         /*if(!targetGrantEntry.contains(sourcePrincipalEntry)) {
163                             targetGrantEntry.principals.add(sourcePrincipalEntry);
164                         }*/

165                         Iterator JavaDoc targetPrincipalIterator = targetGrantEntry.principals.iterator();
166                         while(targetPrincipalIterator.hasNext()) {
167                             PolicyParser.PrincipalEntry targetPrincipalEntry = (PolicyParser.PrincipalEntry)targetPrincipalIterator.next();
168                             if(targetPrincipalEntry.equals(sourcePrincipalEntry)) {
169                                 matchedPrincipal = true;
170                                 break;
171                             }
172                         }
173                         if(!matchedPrincipal) {
174                             targetGrantEntry.principals.add(sourcePrincipalEntry);
175                         }
176                     }
177                     break;
178                 }
179             }
180             if (!matchedGrantEntry) {
181                 targetPolicy.add(sourceGrantEntry);
182             }
183         }
184         try {
185             targetPolicy.write(new FileWriter(destPolicyFileName));
186         } catch (IOException ioe) {
187             logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage"),ioe.getMessage());
188             return;
189         }
190     }
191     
192     private void transferSecModDb(CommonInfoModel commonInfo) {
193         String JavaDoc sourcePath = commonInfo.getSourceDomainPath();
194         String JavaDoc targetPath = commonInfo.getDestinationDomainPath();
195         File sourceFile = new File(sourcePath + File.separator + "config" + File.separator + SECMODDB);
196         File targetFile = new File(targetPath + File.separator + "config" + File.separator + SECMODDB);
197         if(!sourceFile.exists()) return;
198         if(targetFile.exists()) {
199             backup(targetFile.getAbsolutePath());
200         }
201         try {
202             UpgradeUtils.getUpgradeUtils(commonInfo).copyFile(sourceFile.getAbsolutePath(), targetFile.getAbsolutePath());
203         } catch(IOException e) {
204             logger.log(Level.WARNING, stringManager.getString("upgrade.configTransfers.secModDb.failureMessage") + e.getLocalizedMessage());
205         }
206     }
207     
208     private boolean backup(String JavaDoc filename) {
209         try{
210             File targetFile = new File(filename);
211             boolean renamed = targetFile.renameTo(new File(filename +".bak"));
212             if(!renamed){
213                 // This is possible if user is running the upgrade again and .bak is already created.
214
renamed = targetFile.delete();
215             }
216             if(renamed){
217                 targetFile = new File(filename);
218                 targetFile.createNewFile();
219                 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename + ".bak")));
220                 PrintWriter writer = new PrintWriter(new FileOutputStream(targetFile));
221                 String JavaDoc readLine = null;
222                 while((readLine = reader.readLine()) != null){
223                     writer.println(readLine);
224                 }
225                 writer.flush();
226                 writer.close();
227                 reader.close();
228                 return true;
229                 //permission javax.security.auth.PrivateCredentialPermission "javax.resource.spi.security.PasswordCredential * \"*\"","read
230
// The above line differs from source. Still need to be modified.
231
}else{
232                 // Log a error message
233
logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.renameFailureMessage"));
234             }
235         }catch(Exception JavaDoc ex){
236             // Log a error message
237
logger.log(Level.SEVERE, stringManager.getString("upgrade.configTransfers.serverPolicy.startFailureMessage"),ex);
238         }
239         return false;
240     }
241     
242     /*
243     public static void main(String[] args){
244         try{
245             com.sun.enterprise.tools.upgrade.logging.LogService.initialize("upgradetest.log");
246         }catch(Exception e){
247             e.printStackTrace();
248         }
249         CommonInfoModel cim = new CommonInfoModel();
250         cim.setSourceInstallDir("C:\\Sun\\AppServer80PE");
251         cim.setTargetInstallDir("C:\\Sun\\AppServer81");
252         cim.setCurrentDomain("domain1");
253         cim.setCurrentSourceInstance("server1");
254         cim.setTargetDomainRoot("C:\\Sun\\AppServer81\\domains");
255         java.util.Hashtable ht = new java.util.Hashtable();
256         ht.put("domain1", "C:\\Sun\\AppServer7\\domains\\domain1");
257         cim.setDomainMapping(ht);
258         cim.enlistDomainsFromSource();
259         cim.setAdminPassword("adminadmin");
260         new ConfigTransfers().upgrade(cim);
261     }
262      */

263     
264     public String JavaDoc getName() {
265         return stringManager.getString("upgrade.configTransfers.moduleName");
266     }
267     
268 }
269
Popular Tags