1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
12 ******************************************************************************/
13 package org
.eclipse
.titan
.executorapi
;
17 import org
.eclipse
.titan
.executorapi
.exception
.JniExecutorIllegalArgumentException
;
20 * Contains the data describing a Host Controller.
22 public final class HostController
{
26 /** Used by the constructor */
27 private static final String EXCEPTION_TEXT_ILLEGAL_ARG_WORKINGDIR_NULL
= "Working directory is null";
28 /** Used by the constructor */
29 private static final String EXCEPTION_TEXT_ILLEGAL_ARG_EXECUTABLE_NULL
= "Executable is null";
30 /** Used by the constructor */
31 private static final String EXCEPTION_TEXT_ILLEGAL_ARG_WORKINGDIR_NOT_EXIST
= "Working directory does NOT exists";
32 /** Used by the constructor */
33 private static final String EXCEPTION_TEXT_ILLEGAL_ARG_EXECUTABLE_NOT_EXIST
= "Executable does NOT exists";
36 * The name of the host, it can be null, default: null (localhost)
38 private String mHost
= null;
41 * The working directory to use when executing commands
43 private String mWorkingDirectory
;
46 * The executable if the Host Controller.
47 * This executable is started (with 2 parameters: MC host, MC port)
48 * in the working directory when a HC is started.
50 private String mExecutable
;
54 * @param aHost the name of the host, it can be null (localhost)
55 * @param aWorkingdirectory the working directory to use when executing commands
56 * @param aExecutable the Host Controller's executable to start
57 * @throws JniExecutorIllegalArgumentException if aWorkingdirectory == null or aExecutable == null
59 public HostController( final String aHost
, final String aWorkingdirectory
, final String aExecutable
) throws JniExecutorIllegalArgumentException
{
60 if ( aWorkingdirectory
== null ) {
61 if ( aExecutable
== null ) {
62 throw new JniExecutorIllegalArgumentException( EXCEPTION_TEXT_ILLEGAL_ARG_WORKINGDIR_NULL
+ ", " + EXCEPTION_TEXT_ILLEGAL_ARG_EXECUTABLE_NULL
);
64 throw new JniExecutorIllegalArgumentException( EXCEPTION_TEXT_ILLEGAL_ARG_WORKINGDIR_NULL
);
66 } else if ( aExecutable
== null ) {
67 throw new JniExecutorIllegalArgumentException( EXCEPTION_TEXT_ILLEGAL_ARG_EXECUTABLE_NULL
);
70 if ( isLocalhost(aHost
) ) {
71 // if working directory directory is local, it must exist
72 final File workingDir
= new File(aWorkingdirectory
);
73 if ( !workingDir
.exists() || !workingDir
.isDirectory() ) {
74 throw new JniExecutorIllegalArgumentException( EXCEPTION_TEXT_ILLEGAL_ARG_WORKINGDIR_NOT_EXIST
);
77 // also the executable
78 final String execFullPath
= aWorkingdirectory
+ ( aWorkingdirectory
.endsWith( File
.separator
) ?
"" : File
.separator
) + aExecutable
;
79 final File executable
= new File( execFullPath
);
80 if ( !executable
.exists() || !executable
.isFile() ) {
81 throw new JniExecutorIllegalArgumentException( EXCEPTION_TEXT_ILLEGAL_ARG_EXECUTABLE_NOT_EXIST
);
86 this.mWorkingDirectory
= aWorkingdirectory
;
87 this.mExecutable
= aExecutable
;
90 public String
getHost() {
94 public String
getWorkingDirectory() {
95 return mWorkingDirectory
;
98 public String
getExecutable() {
103 * Builds a command to start the HC
104 * @param aMcHost MainController host
105 * @param aMcPort MainController port
106 * @return the actual command to execute in a shell to start up the Host Controller fully configured
108 public String
getCommand( final String aMcHost
, final int aMcPort
) {
109 final StringBuilder commandSb
= new StringBuilder();
111 if ( !isLocalhost( mHost
) ) {
113 // ssh %Host cd %Workingdirectory; %Executable %MCHost %MCPort
114 commandSb
.append( "ssh " );
115 commandSb
.append( mHost
);
116 commandSb
.append( " " );
120 // cd %Workingdirectory; %Executable %MCHost %MCPort
122 commandSb
.append( "cd " );
123 commandSb
.append( mWorkingDirectory
);
124 commandSb
.append( "; ./" );
125 commandSb
.append( mExecutable
);
126 commandSb
.append( " " );
127 commandSb
.append( "NULL".equals( aMcHost
) ?
"0.0.0.0" : aMcHost
);
128 commandSb
.append( " " );
129 commandSb
.append( aMcPort
);
130 return commandSb
.toString();
134 * Checks if the host name is localhost
135 * @param aHostName the host name, it can be IP address, it can be null
136 * @return true if the host name is localhost, false otherwise
138 private static boolean isLocalhost( final String aHostName
) {
141 "".equalsIgnoreCase( aHostName
) ||
142 "localhost".equalsIgnoreCase( aHostName
) ||
143 "0.0.0.0".equalsIgnoreCase( aHostName
) ||
144 "NULL".equalsIgnoreCase( aHostName
)