KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.sun.enterprise.admin.servermgmt.SMFServiceHandler;
28 import com.sun.enterprise.admin.servermgmt.SMFService;
29 import com.sun.enterprise.util.SystemPropertyConstants;
30 import com.sun.enterprise.admin.servermgmt.RepositoryManager;
31 import com.sun.enterprise.admin.servermgmt.RepositoryException;
32 import com.sun.enterprise.admin.servermgmt.DomainsManager;
33 import com.sun.enterprise.admin.servermgmt.DomainConfig;
34 import java.io.File JavaDoc;
35
36 import java.util.Date JavaDoc;
37
38 public class CreateServiceCommand extends S1ASCommand
39 {
40     private final static String JavaDoc TYPE = "type";
41     private final static String JavaDoc NAME = "name";
42     private final static String JavaDoc SERVICE_PROPERTIES = "serviceproperties";
43     private final static String JavaDoc VALID_TYPES = "das|node-agent";
44     private final static String JavaDoc DAS_TYPE = "das";
45
46     /**
47      * A method that validates the options/operands
48      * @return boolean returns true if success else returns false
49      * @throws CommandValidationException
50      */

51     public boolean validateOptions() throws CommandValidationException
52     {
53         String JavaDoc passwordFile = getOption(PASSWORDFILE);
54         //check final the passwordfile is valid
55
if (! new File JavaDoc(passwordFile).exists())
56         {
57             final String JavaDoc msg = getLocalizedString("FileDoesNotExist",
58                                                    new Object JavaDoc[] {passwordFile});
59             throw new CommandValidationException(msg);
60         }
61         String JavaDoc typedirOperand = (String JavaDoc) getOperands().get(0);
62         File JavaDoc typeDir = new File JavaDoc(typedirOperand);
63         String JavaDoc type = getOption(TYPE);
64         if (!type.matches(VALID_TYPES))
65         {
66             throw new CommandValidationException(
67                             getLocalizedString("InvalideServiceType"));
68         }
69         if (!typeDir.exists() || !typeDir.canWrite() || !typeDir.isDirectory())
70         {
71             final String JavaDoc msg = getLocalizedString("InvalidDirectory",
72                                                     new Object JavaDoc[] {typeDir});
73             throw new CommandValidationException(msg);
74         }
75         return true;
76     }
77
78
79     protected void validateType() throws CommandException
80     {
81         String JavaDoc typedirOperand = (String JavaDoc) getOperands().get(0);
82         File JavaDoc typeDir = new File JavaDoc(typedirOperand);
83         String JavaDoc type = getOption(TYPE);
84         //Validate the domain directory
85
if (type.equals(DAS_TYPE))
86         {
87             String JavaDoc domainName = typeDir.getName();
88             String JavaDoc domainRoot = typeDir.getParent();
89             try {
90                 DomainConfig dc = new DomainConfig(domainName, domainRoot);
91                 RepositoryManager rm = new RepositoryManager();
92                 rm.checkRepository(dc, true);
93             }catch (RepositoryException re){
94                 throw new CommandException(re.getLocalizedMessage());
95             }
96         }
97         else // validate the node-agent directory
98
{
99             validateNodeAgent(typeDir);
100         }
101     }
102
103     
104     protected void validateNodeAgent(File JavaDoc typeDir) throws CommandException
105     {
106         throw new CommandException(getLocalizedString( "TypeNotSupported"));
107     }
108     
109     
110     /**
111      * Method that Executes the command
112      * @throws CommandException, CommandValidationException
113      */

114     public void runCommand() throws CommandException, CommandValidationException
115     {
116         validateOptions();
117
118         validateType();
119
120         String JavaDoc passwordFile = getOption(PASSWORDFILE);
121         String JavaDoc type = getOption(TYPE);
122         String JavaDoc typeDir = (String JavaDoc) getOperands().get(0);
123         final SMFServiceHandler sh = new SMFServiceHandler();
124         final SMFService service = new SMFService();
125         //configure service
126
service.setDate(new Date JavaDoc().toString());
127         final StringBuilder JavaDoc ap = new StringBuilder JavaDoc();
128         service.setName(getName(typeDir, ap));
129         service.setLocation(ap.toString());
130         service.setType(type.equals("das") ?
131             SMFService.AppserverServiceType.Domain
132                         : SMFService.AppserverServiceType.NodeAgent);
133         service.setFQSN();
134         service.setOSUser();
135         service.setAsadminPath(SystemPropertyConstants.getAsAdminScriptLocation());
136         service.setPasswordFilePath(passwordFile);
137         service.setServiceProperties(getOption(SERVICE_PROPERTIES));
138         service.isConfigValid();
139         sh.setTrace(CLILogger.isDebug());
140         sh.createService(service.tokensAndValues());
141         printSuccess(service);
142         CLILogger.getInstance().printMessage(getLocalizedString(
143                                                    "CommandSuccessful",
144                                                    new Object JavaDoc[] {name}));
145     }
146
147     
148     /**
149      * Retrieves the domain/nodeagent name from the given directory
150      * @return domain/nodeagent name
151      * @throws CommandValidationException
152      */

153     private String JavaDoc getName(String JavaDoc typeDir, final StringBuilder JavaDoc absolutePath) throws CommandException
154     {
155         String JavaDoc path = "";
156         try
157         {
158             //Already checked for the valid directory in validateOptions()
159
final File JavaDoc f = new File JavaDoc(typeDir);
160            String JavaDoc name = f.getName();
161            absolutePath.append(f.getAbsolutePath());
162            final String JavaDoc nameFromOption = getOption(NAME);
163            if (nameFromOption != null)
164                name = nameFromOption;
165            CLILogger.getInstance().printDebugMessage("service name = " + name);
166            return ( name );
167         }
168         catch (Exception JavaDoc e)
169         {
170             throw new CommandException(e.getLocalizedMessage());
171         }
172     }
173     private void printSuccess(final SMFService smf) {
174         final String JavaDoc[] params = new String JavaDoc[] {smf.getName(), smf.getType().toString(), smf.getLocation(), smf.getManifestFilePath()};
175         final String JavaDoc msg = getLocalizedString("SMFServiceCreated", params);
176         CLILogger.getInstance().printMessage(msg);
177     }
178 }
179
Popular Tags