1 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 ; 35 36 import java.util.Date ; 37 38 public class CreateServiceCommand extends S1ASCommand 39 { 40 private final static String TYPE = "type"; 41 private final static String NAME = "name"; 42 private final static String SERVICE_PROPERTIES = "serviceproperties"; 43 private final static String VALID_TYPES = "das|node-agent"; 44 private final static String DAS_TYPE = "das"; 45 46 51 public boolean validateOptions() throws CommandValidationException 52 { 53 String passwordFile = getOption(PASSWORDFILE); 54 if (! new File (passwordFile).exists()) 56 { 57 final String msg = getLocalizedString("FileDoesNotExist", 58 new Object [] {passwordFile}); 59 throw new CommandValidationException(msg); 60 } 61 String typedirOperand = (String ) getOperands().get(0); 62 File typeDir = new File (typedirOperand); 63 String 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 msg = getLocalizedString("InvalidDirectory", 72 new Object [] {typeDir}); 73 throw new CommandValidationException(msg); 74 } 75 return true; 76 } 77 78 79 protected void validateType() throws CommandException 80 { 81 String typedirOperand = (String ) getOperands().get(0); 82 File typeDir = new File (typedirOperand); 83 String type = getOption(TYPE); 84 if (type.equals(DAS_TYPE)) 86 { 87 String domainName = typeDir.getName(); 88 String 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 { 99 validateNodeAgent(typeDir); 100 } 101 } 102 103 104 protected void validateNodeAgent(File typeDir) throws CommandException 105 { 106 throw new CommandException(getLocalizedString( "TypeNotSupported")); 107 } 108 109 110 114 public void runCommand() throws CommandException, CommandValidationException 115 { 116 validateOptions(); 117 118 validateType(); 119 120 String passwordFile = getOption(PASSWORDFILE); 121 String type = getOption(TYPE); 122 String typeDir = (String ) getOperands().get(0); 123 final SMFServiceHandler sh = new SMFServiceHandler(); 124 final SMFService service = new SMFService(); 125 service.setDate(new Date ().toString()); 127 final StringBuilder ap = new StringBuilder (); 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 [] {name})); 145 } 146 147 148 153 private String getName(String typeDir, final StringBuilder absolutePath) throws CommandException 154 { 155 String path = ""; 156 try 157 { 158 final File f = new File (typeDir); 160 String name = f.getName(); 161 absolutePath.append(f.getAbsolutePath()); 162 final String nameFromOption = getOption(NAME); 163 if (nameFromOption != null) 164 name = nameFromOption; 165 CLILogger.getInstance().printDebugMessage("service name = " + name); 166 return ( name ); 167 } 168 catch (Exception e) 169 { 170 throw new CommandException(e.getLocalizedMessage()); 171 } 172 } 173 private void printSuccess(final SMFService smf) { 174 final String [] params = new String [] {smf.getName(), smf.getType().toString(), smf.getLocation(), smf.getManifestFilePath()}; 175 final String msg = getLocalizedString("SMFServiceCreated", params); 176 CLILogger.getInstance().printMessage(msg); 177 } 178 } 179 | Popular Tags |