KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > BackupCommands


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 package com.sun.enterprise.cli.commands;
25
26 import java.io.*;
27 import java.util.*;
28
29 import com.sun.enterprise.cli.framework.CommandValidationException;
30 import com.sun.enterprise.cli.framework.CommandException;
31 import com.sun.enterprise.cli.framework.CLILogger;
32 import com.sun.enterprise.util.diagnostics.ObjectAnalyzer;
33 //import com.sun.enterprise.util.diagnostics.SystemProps;
34
import com.sun.enterprise.util.SystemPropertyConstants;
35 import com.sun.enterprise.config.backup.BackupException;
36 import com.sun.enterprise.config.backup.BackupManager;
37 import com.sun.enterprise.config.backup.RestoreManager;
38 import com.sun.enterprise.config.backup.BackupRequest;
39 import com.sun.enterprise.config.backup.BackupWarningException;
40 import com.sun.enterprise.config.backup.ListManager;
41
42 import com.sun.enterprise.admin.servermgmt.DomainsManager;
43 import com.sun.enterprise.admin.servermgmt.DomainConfig;
44 import com.sun.enterprise.admin.servermgmt.InstancesManager;
45 import com.sun.enterprise.admin.common.Status;
46 import com.sun.enterprise.admin.pluggable.ClientPluggableFeatureFactory;
47 /**
48  * This is a local command for backing-up domains.
49  * @version $Revision: 1.3 $
50  * The Options:
51     <ul>
52     <li>domaindir
53     </ul>
54  * The Operand:
55     <ul>
56     <li>domain
57     </ul>
58  */

59
60 public class BackupCommands extends BaseLifeCycleCommand
61 {
62     public String JavaDoc toString()
63     {
64         return super.toString() + "\n" + ObjectAnalyzer.toString(this);
65     }
66     
67     /**
68      * An abstract method that validates the options
69      * on the specification in the xml properties file
70      * @return true if successfull
71      */

72     public boolean validateOptions() throws CommandValidationException
73     {
74         super.validateOptions();
75         setOptions();
76         checkOptions();
77         prepareRequest();
78         
79         // if anything went wrong, an Exception would have been thrown
80
// and we'd never get to this return statement...
81
return true;
82     }
83     /**
84      * An abstract method that executes the command
85      * @throws CommandException
86      */

87     public void runCommand() throws CommandException, CommandValidationException
88     {
89         validateOptions();
90         
91         try
92         {
93             if(command == CmdType.BACKUP)
94             {
95                 BackupManager mgr = new BackupManager(request);
96                 CLILogger.getInstance().printMessage(mgr.backup());
97             }
98             else if(command == CmdType.RESTORE)
99             {
100                 RestoreManager mgr = new RestoreManager(request);
101                 CLILogger.getInstance().printMessage(mgr.restore());
102             }
103             else if(command == CmdType.LIST)
104             {
105                 ListManager mgr = new ListManager(request);
106                 CLILogger.getInstance().printMessage(mgr.list());
107             }
108             else
109             {
110                 // IMPOSSIBLE!!!
111
throw new CommandException("Internal Error");
112             }
113         }
114         catch(BackupWarningException bwe)
115         {
116             CLILogger.getInstance().printMessage(bwe.getMessage());
117         }
118         catch(BackupException be)
119         {
120             throw new CommandException(be);
121         }
122     }
123     
124
125     /**
126      * A method that sets the options and operand that the user supplied.
127      */

128     private void setOptions() throws CommandValidationException
129     {
130         setCommand();
131         setDomainsDir();
132         setDomainName();
133         setBackupFilename();
134         setDescription();
135         setVerbosity();
136     }
137     
138     ///////////////////////////////////////////////////////////////////////////
139

140     private void setCommand() throws CommandValidationException
141     {
142         String JavaDoc cmd = getName();
143         command = CmdType.valueOf(cmd);
144         
145         if(command == null)
146         {
147             // This shouldn't happen unless somebody erred editing CLIDescriptor.xml
148
throw new CommandValidationException(
149                 getLocalizedString("NoUsageText", new String JavaDoc[] {cmd}) );
150         }
151     }
152     
153     ///////////////////////////////////////////////////////////////////////////
154

155     private void setDomainName() throws CommandValidationException
156     {
157         try
158         {
159             domainName = getDomainName();
160         }
161         catch(CommandException ce)
162         {
163             throw new CommandValidationException(ce);
164         }
165         //domainName = (String)operands.firstElement();
166
}
167
168     ///////////////////////////////////////////////////////////////////////////
169

170     private void setDomainsDir() throws CommandValidationException
171     {
172         domainsDir = getOption(DOMAINSDIR);
173
174         if(domainsDir == null || domainsDir.length() <= 0)
175             domainsDir = System.getProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY);
176     }
177     
178     ///////////////////////////////////////////////////////////////////////////
179

180     private void setBackupFilename()
181     {
182         // this option is only used for restore operations
183
backupFilename = getOption(FILENAME);
184     }
185     
186     ///////////////////////////////////////////////////////////////////////////
187

188     private void setVerbosity() throws CommandValidationException
189     {
190         if(getBooleanOption("terse"))
191             terse = true;
192
193         if(getBooleanOption("verbose"))
194             verbose = true;
195         
196         // it is an error for both to be true (duh!)
197

198         if(verbose && terse)
199             throw new CommandValidationException(getLocalizedString("NoVerboseAndTerseAtTheSameTime"));
200     }
201     
202     ///////////////////////////////////////////////////////////////////////////
203

204     private void setDescription()
205     {
206         description = getOption(DESCRIPTION);
207     }
208     
209     /**
210      * A method that checks the options and operand that the user supplied.
211      * These tests are slightly different for different CLI commands
212      */

213     private void checkOptions() throws CommandValidationException
214     {
215         // disallow backup & restore if server is running. list-backups is OK anytime...
216
if(command == CmdType.BACKUP || command == CmdType.RESTORE)
217         {
218             if(!isNotRunning())
219             {
220                 throw new CommandValidationException(getLocalizedString("DomainIsNotStopped",
221                     new String JavaDoc[] {command.name} ));
222             }
223         }
224         // make sure we have a domainsDir
225
if(domainsDir == null || domainsDir.length() <= 0)
226         {
227             throw new CommandValidationException(getLocalizedString("InvalidDomainPath",
228                 new String JavaDoc[] {domainsDir}) );
229         }
230
231         File domainsDirFile = new File(domainsDir);
232
233         // make sure domainsDir exists and is a directory
234
if(!domainsDirFile.isDirectory())
235         {
236             throw new CommandValidationException(getLocalizedString("InvalidDomainPath",
237                 new String JavaDoc[] {domainsDir}) );
238         }
239
240         File domainFile = new File(domainsDirFile, domainName);
241
242         // BACKUP, LIST: make sure the domain dir exists and is
243
// a directory and is writable
244
// RESTORE: It must exist if backupFilename isn't set.
245
boolean domainDirDoesNotHaveToExist =
246             (command == CmdType.RESTORE) && backupFilename != null;
247                             
248         if(!domainDirDoesNotHaveToExist)
249         {
250             if(!domainFile.isDirectory() || !domainFile.canWrite())
251             {
252                 throw new CommandValidationException(getLocalizedString("InvalidDirectory",
253                     new String JavaDoc[] {domainFile.getPath()}) );
254             }
255         }
256         
257         if(backupFilename != null)
258         {
259             File f = new File(backupFilename);
260             
261             if(!f.exists() || !f.canRead())
262             {
263                 throw new CommandValidationException(getLocalizedString("FileDoesNotExist",
264                     new String JavaDoc[] { backupFilename } ));
265             }
266         }
267     }
268     
269     ///////////////////////////////////////////////////////////////////////////
270

271     private void prepareRequest() throws CommandValidationException
272     {
273         if(backupFilename == null)
274             request = new BackupRequest(domainsDir, domainName, description);
275         else
276             request = new BackupRequest(domainsDir, domainName, description, backupFilename);
277         
278         request.setTerse(terse);
279         request.setVerbose(verbose);
280     }
281     
282     ///////////////////////////////////////////////////////////////////////////
283

284     private boolean isNotRunning() throws CommandValidationException
285     {
286         try
287         {
288             ClientPluggableFeatureFactory cpff = getFeatureFactory();
289             DomainsManager dm = cpff.getDomainsManager();
290             DomainConfig dc = getDomainConfig(domainName);
291             InstancesManager im = dm.getInstancesManager(dc);
292             final int state = im.getInstanceStatus();
293
294             return state == Status.kInstanceNotRunningCode;
295         }
296         catch(Exception JavaDoc e)
297         {
298             throw new CommandValidationException(e);
299         }
300     }
301     
302     ///////////////////////////////////////////////////////////////////////////
303

304     /* temp debug code TBD FIXME
305     private void dumpProps()
306     {
307         List list = SystemProps.get();
308         
309         for(Iterator it = list.iterator(); it.hasNext(); )
310         {
311             Map.Entry entry = (Map.Entry)it.next();
312             
313             if(((String)entry.getKey()).startsWith("com.sun."))
314                 System.out.println((String)entry.getKey() + "=" + (String)entry.getValue());
315         }
316     }
317     */

318     ///////////////////////////////////////////////////////////////////////////
319

320     private static final String JavaDoc DOMAINSDIR = "domaindir";
321     private static final String JavaDoc FILENAME = "filename";
322     private static final String JavaDoc DESCRIPTION = "description";
323     private BackupRequest request;
324     private String JavaDoc domainName;
325     private String JavaDoc domainsDir;
326     private String JavaDoc backupFilename;
327     private String JavaDoc description;
328     private CmdType command;
329     private boolean terse = false;
330     private boolean verbose = false;
331     
332     private static class CmdType
333     {
334         private CmdType(String JavaDoc name)
335         {
336             this.name = name;
337         }
338         private static CmdType valueOf(String JavaDoc aName)
339         {
340             if(aName.equals(BACKUP.name))
341                 return BACKUP;
342
343             if(aName.equals(RESTORE.name))
344                 return RESTORE;
345             
346             if(aName.equals(LIST.name))
347                 return LIST;
348
349             return null;
350         }
351         
352         private static final CmdType BACKUP = new CmdType("backup-domain");
353         private static final CmdType RESTORE = new CmdType("restore-domain");
354         private static final CmdType LIST = new CmdType("list-backups");
355         private final String JavaDoc name;
356     }
357 }
358
Popular Tags