1 23 24 package com.sun.enterprise.instance; 25 26 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 32 import com.sun.enterprise.util.ProcessExecutor; 34 import com.sun.enterprise.util.ExecException; 35 import com.sun.enterprise.config.ConfigException; 36 import com.sun.enterprise.server.Constants; 37 import com.sun.enterprise.util.StringUtils; 38 import com.sun.enterprise.util.io.FileUtils; 39 import com.sun.enterprise.util.OS; 40 41 import com.sun.enterprise.util.net.NetUtils; 42 import com.sun.enterprise.admin.common.constant.AdminConstants; 43 import com.sun.enterprise.config.ConfigContext; 44 import com.sun.enterprise.config.ConfigFactory; 45 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 46 import com.sun.enterprise.config.serverbeans.Server; 47 import com.sun.enterprise.config.serverbeans.Config; 48 import com.sun.enterprise.config.serverbeans.HttpService; 49 import com.sun.enterprise.config.serverbeans.HttpListener; 50 import com.sun.enterprise.config.serverbeans.IiopService; 51 import com.sun.enterprise.config.serverbeans.IiopListener; 52 import com.sun.enterprise.config.serverbeans.JmsService; 53 import com.sun.enterprise.config.serverbeans.JmsHost; 54 import com.sun.enterprise.admin.common.InitConfFileBean; 55 56 64 65 public class ServerManager { 66 67 public static final String INSTALL_ROOT = 68 System.getProperty(Constants.INSTALL_ROOT); 69 public static final String INSTANCE_CFG_ROOT = 70 System.getProperty(Constants.IAS_ROOT); 71 public static final String DOC_DIR_NAME = "docroot"; 72 public static final String ADMINSERVER_ID = "admin-server"; 73 74 private static final ServerManager SHARED_INSTANCE = new ServerManager(); 75 private static Logger sLogger = 76 Logger.getLogger(AdminConstants.kLoggerName); 77 78 79 80 private ServerManager() { 81 } 82 83 86 87 public static ServerManager instance(){ 88 return SHARED_INSTANCE; 89 } 90 91 102 103 public boolean instanceExists(String instanceID) { 104 if (instanceID.equals("server")) { 107 return true; 108 } 109 return false; 110 } 111 112 123 124 public String [] getInstanceNames(boolean countAdmin) { 125 return new String [] {"server"}; 127 } 128 129 135 136 public int getNumInstances(boolean countAdmin) { 137 return ( getInstanceNames(countAdmin).length ); 138 } 139 140 153 154 public void createServerInstance(InstanceDefinition instance) throws 155 IOException , ConfigException 156 { 157 163 164 throw new UnsupportedOperationException ("createServerInstance - not in PE"); 165 } 166 167 177 public void deleteServerInstance(String instanceName) throws 178 ConfigException { 179 180 throw new UnsupportedOperationException ("deleteServerInstance - not in PE"); 181 } 182 193 194 public void startServerInstance(InstanceDefinition instance) throws 195 ConfigException, RuntimeException { 196 startServerInstance(instance, null); 197 } 198 199 public void startServerInstance(InstanceDefinition instance, String [] passwords) throws 200 ConfigException, RuntimeException { 201 if (instance == null) { 202 throw new IllegalArgumentException (); 203 } 204 String [] startCommand = instance.getStartCommand(); 205 String [] inputLines = null; 206 if(passwords!=null) 207 inputLines = passwords; 208 else 209 inputLines = new String []{}; try { 212 sLogger.log(Level.FINE, "general.exec_cmd", startCommand[0]); 213 ProcessExecutor executor = new ProcessExecutor(startCommand, inputLines); 214 executor.execute(); 215 } 216 catch (ExecException ee) { 217 sLogger.log(Level.WARNING, "general.exec_cmd", ee); 218 throw new RuntimeException (Localizer.getValue(ExceptionType.SERVER_NO_START)); 219 } 220 catch (Exception e) { 221 throw new ConfigException(e.getMessage()); 222 } 223 } 224 225 236 237 public void stopServerInstance(InstanceDefinition instance) throws 238 ConfigException { 239 if (instance == null) { 240 throw new IllegalArgumentException (); 241 } 242 String stopCommand[] = instance.getStopCommand(); 243 try { 244 sLogger.log(Level.FINE, "general.exec_cmd", stopCommand[0]); 245 ProcessExecutor executor = new ProcessExecutor(stopCommand); 246 executor.execute(); 247 } 248 catch (Exception e) { 249 throw new ConfigException(e.getMessage()); 250 } 251 } 252 253 255 public void restartServerInstance(InstanceDefinition instance) 256 throws ConfigException { 257 if (instance == null) { 258 throw new IllegalArgumentException (); 259 } 260 String [] restartCommand = instance.getRestartCommand(); 261 try { 262 sLogger.log(Level.FINE, "general.exec_cmd", restartCommand[0]); 263 ProcessExecutor executor = new ProcessExecutor(restartCommand); 264 executor.execute(); 265 } 266 catch (Exception e) { 267 throw new ConfigException(e.getMessage()); 268 } 269 } 270 271 public String [] getSecurityTokensForInstance(InstanceDefinition instance) throws 272 ConfigException, RuntimeException { 273 if (instance == null) { 274 throw new IllegalArgumentException (); 275 } 276 String [] command = instance.getGetSecurityTokensCommand(); 277 String [] inputLines = null; 278 try { 279 sLogger.log(Level.FINE, "general.gettokens_cmd", command[0]); 280 ProcessExecutor executor = new ProcessExecutor(command); 281 return executor.execute(true); 282 } 283 catch (ExecException ee) { 284 throw new RuntimeException (Localizer.getValue(ExceptionType.NO_RECEIVE_TOKENS)); 285 } 286 catch (Exception e) { 287 throw new ConfigException(e.getMessage()); 288 } 289 } 290 291 296 public String getMimeTypesTemplateFilePath() { 297 final String libDirName = "lib"; 298 final String installDirName = "install"; 299 final String templateDirName = "templates"; 300 final String mimeTemplateFileName = "mime.types.template"; 301 302 String [] fileNames = new String [] {INSTALL_ROOT, libDirName, 303 installDirName, templateDirName, mimeTemplateFileName}; 304 return ( StringUtils.makeFilePath(fileNames, false) ); 305 } 306 307 313 private int getFreePort(int defaultPort) { 314 int port = NetUtils.getFreePort(); 315 if (port == 0) { 316 317 sLogger.log(Level.SEVERE, "general.free_port_failed"); 318 return defaultPort; 319 } 320 else { 321 Integer portInteger = new Integer (port); 322 sLogger.log(Level.INFO, "general.free_port", portInteger); 323 return port; 324 } 325 } 326 327 330 331 private boolean portTakenByHTTP(int port) { 332 boolean portTaken = true; 333 String portString = "" + port; 334 String [] instances = getInstanceNames(true); 335 336 try { 337 for (int i = 0 ; i < instances.length ; i++) { 338 String instanceName = instances[i]; 339 InstanceEnvironment inst = 340 new InstanceEnvironment(instanceName); 341 String backURL = inst.getConfigFilePath(); 342 ConfigContext context = ConfigFactory. 343 createConfigContext(backURL); 344 Config rootElement = ServerBeansFactory.getConfigBean(context); 345 346 HttpService httpService = rootElement.getHttpService(); 347 HttpListener[] httpListeners = 348 httpService.getHttpListener(); 349 for (int j = 0 ; j < httpListeners.length ; j++) { 350 String aPort = httpListeners[j].getPort(); 351 aPort = aPort.trim(); 352 sLogger.log(Level.FINE, "port = " + aPort); 353 if (aPort.equals(portString)) { 354 sLogger.log(Level.WARNING, 355 "general.port_occupied", instanceName); 356 return portTaken; 357 } 358 } 359 } 360 } 361 catch (Exception e) { 362 sLogger.log(Level.WARNING, "general.port_derivation_failed", e); 363 } 364 return ( false ); 365 } 366 367 370 private boolean portTakenByORB(int port) { 371 boolean portTaken = true; 372 String portString = "" + port; 373 String [] instances = getInstanceNames(true); 374 375 try { 376 for (int i = 0 ; i < instances.length ; i++) { 377 String instanceName = instances[i]; 378 InstanceEnvironment inst = 379 new InstanceEnvironment(instanceName); 380 String backURL = inst.getConfigFilePath(); 381 ConfigContext context = ConfigFactory. 382 createConfigContext(backURL); 383 384 Config rootElement = ServerBeansFactory.getConfigBean(context); 385 386 IiopService iiopService = rootElement.getIiopService(); 387 IiopListener[] iiopListeners = iiopService.getIiopListener(); 388 for (int j = 0 ; j < iiopListeners.length ; j++) { 389 String aPort = iiopListeners[j].getPort(); 390 aPort = aPort.trim(); 391 sLogger.log(Level.FINE, "port = " + aPort); 392 if (aPort.equals(portString)) { 393 sLogger.log(Level.WARNING, 394 "general.port_occupied", instanceName); 395 return portTaken; 396 } 397 } 398 } 399 } 400 catch (Exception e) { 401 sLogger.log(Level.WARNING, "general.port_derivation_failed", e); 402 } 403 return ( false ); 404 } 405 406 407 409 private boolean portTakenByJMS(int port) { 410 boolean portTaken = true; 411 String portString = "" + port; 412 String [] instances = getInstanceNames(true); 413 414 try { 415 for (int i = 0 ; i < instances.length ; i++) { 416 String instanceName = instances[i]; 417 InstanceEnvironment inst = 418 new InstanceEnvironment(instanceName); 419 String backURL = inst.getConfigFilePath(); 420 ConfigContext context = ConfigFactory. 421 createConfigContext(backURL); 422 JmsHost jmsHost = ServerBeansFactory.getJmsHostBean(context); 423 String aPort = jmsHost.getPort(); 424 aPort = aPort.trim(); 425 sLogger.log(Level.FINE, "port = " + aPort); 426 if (aPort.equals(portString)) { 427 sLogger.log(Level.WARNING, 428 "general.port_occupied", instanceName); 429 return portTaken; 430 } 431 } 432 } 433 catch (Exception e) { 434 sLogger.log(Level.WARNING, "general.port_derivation_failed", e); 435 } 436 return ( false ); 437 } 438 441 public String getDomainName(){ 442 File domainRoot = new File (INSTANCE_CFG_ROOT); 443 return domainRoot.getName(); 444 } 445 446 public String getInstanceUser(InstanceEnvironment env) throws IOException 447 { 448 452 462 return System.getProperty("user.name"); 463 } 464 465 466 491 492 } 493 | Popular Tags |