KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.cli.framework.*;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MBeanServerConnection JavaDoc;
30 import com.sun.enterprise.deployment.util.DeploymentProperties;
31 import com.sun.enterprise.server.Constants;
32 import com.sun.enterprise.admin.common.JMXFileTransfer;
33 import com.sun.enterprise.util.FileUtil;
34 import com.sun.enterprise.util.io.FileUtils;
35 import com.sun.enterprise.admin.common.constant.DeploymentConstants;
36
37
38 // jdk imports
39
import java.io.File JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Properties JavaDoc;
42
43
44 /**
45  * This is the GetClientStubs command
46  */

47 public class GetClientStubsCommand extends S1ASCommand
48 {
49     private String JavaDoc downloadDir;
50     private String JavaDoc appName;
51     public static final String JavaDoc APP_NAME = "appname";
52
53     /**
54      * An abstract method that Executes the command
55      * @throws CommandException
56      */

57     public void runCommand()
58             throws CommandException, CommandValidationException
59     {
60         validateOptions();
61
62     //use http connector
63
final MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(),
64                                     getPort(),
65                                     getUser(),
66                                     getPassword());
67
68         String JavaDoc path = getClientStub(mbsc);
69
70     // getClientStub(mbsc,retrievePath, type);
71

72         CLILogger.getInstance().printDetailMessage(getLocalizedString(
73                              "CommandSuccessful",
74                          new Object JavaDoc[] {name} ) );
75     }
76
77
78     /**
79      * An abstract method that validates the options
80      * on the specification in the xml properties file
81      * This method verifies for the correctness of number of
82      * operands and if all the required options are supplied by the client.
83      * @return boolean returns true if success else returns false
84      */

85     public boolean validateOptions() throws CommandValidationException
86     {
87         super.validateOptions();
88         downloadDir = (String JavaDoc) getOperands().get(0);
89     validateDirectory();
90         appName = getAppName();
91     return true;
92     }
93
94
95     /**
96      * check if file path exist on the local file system
97      * @throws CommandValidationExcetion if file not found
98      */

99     private void validateDirectory() throws CommandValidationException
100     {
101         File JavaDoc dlDir = new File JavaDoc(downloadDir);
102     if (!dlDir.exists() )
103         {
104             dlDir.mkdirs();
105         }
106         if(!dlDir.exists() || !dlDir.canWrite() || !dlDir.isDirectory() ) {
107             throw new CommandValidationException(getLocalizedString(
108                          "InvalidDirectory",new Object JavaDoc [] {downloadDir}));
109         }
110     }
111     
112     private String JavaDoc getAppName() throws CommandValidationException
113     {
114         String JavaDoc name = getOption(APP_NAME);
115         return name;
116     }
117
118
119
120     
121     /**
122      * retrieve client stub from server
123      */

124     private String JavaDoc getClientStub(MBeanServerConnection JavaDoc mbsc)
125     throws CommandException
126     {
127     try
128     {
129
130         final String JavaDoc fileName = new JMXFileTransfer(mbsc).downloadClientStubs(
131                              appName,
132                              downloadDir);
133         CLILogger.getInstance().printDebugMessage("Downloaded client stubs to: " + fileName);
134         return fileName;
135     }
136     catch (Exception JavaDoc e)
137     {
138             Throwable JavaDoc t = e.getCause();
139             while(t!=null && t.getCause()!=null)
140                 t=t.getCause();
141             if(t==null)
142                 t=e;
143         throw new CommandException(t.getLocalizedMessage(),t);
144     }
145     }
146
147
148 }
149
Popular Tags