Last sync 2016.04.01
[deliverable/titan.core.git] / titan_executor_api / TITAN_Executor_API / src / org / eclipse / titan / executorapi / HostController.java
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
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Lovassy, Arpad
11 *
12 ******************************************************************************/
13 package org.eclipse.titan.executorapi;
14
15 import java.io.File;
16
17 import org.eclipse.titan.executorapi.exception.JniExecutorIllegalArgumentException;
18
19 /**
20 * Contains the data describing a Host Controller.
21 */
22 public final class HostController {
23
24 // Exception texts
25
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";
34
35 /**
36 * The name of the host, it can be null, default: null (localhost)
37 */
38 private String mHost = null;
39
40 /**
41 * The working directory to use when executing commands
42 */
43 private String mWorkingDirectory;
44
45 /**
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.
49 */
50 private String mExecutable;
51
52 /**
53 * Constructor
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
58 */
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 );
63 } else {
64 throw new JniExecutorIllegalArgumentException( EXCEPTION_TEXT_ILLEGAL_ARG_WORKINGDIR_NULL );
65 }
66 } else if ( aExecutable == null ) {
67 throw new JniExecutorIllegalArgumentException( EXCEPTION_TEXT_ILLEGAL_ARG_EXECUTABLE_NULL );
68 }
69
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 );
75 }
76
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 );
82 }
83 }
84
85 this.mHost = aHost;
86 this.mWorkingDirectory = aWorkingdirectory;
87 this.mExecutable = aExecutable;
88 }
89
90 public String getHost() {
91 return mHost;
92 }
93
94 public String getWorkingDirectory() {
95 return mWorkingDirectory;
96 }
97
98 public String getExecutable() {
99 return mExecutable;
100 }
101
102 /**
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
107 */
108 public String getCommand( final String aMcHost, final int aMcPort ) {
109 final StringBuilder commandSb = new StringBuilder();
110
111 if ( !isLocalhost( mHost ) ) {
112 // remote host
113 // ssh %Host cd %Workingdirectory; %Executable %MCHost %MCPort
114 commandSb.append( "ssh " );
115 commandSb.append( mHost );
116 commandSb.append( " " );
117 }
118
119 // local host
120 // cd %Workingdirectory; %Executable %MCHost %MCPort
121
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();
131 }
132
133 /**
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
137 */
138 private static boolean isLocalhost( final String aHostName ) {
139 return (
140 aHostName == null ||
141 "".equalsIgnoreCase( aHostName ) ||
142 "localhost".equalsIgnoreCase( aHostName ) ||
143 "0.0.0.0".equalsIgnoreCase( aHostName ) ||
144 "NULL".equalsIgnoreCase( aHostName )
145 );
146 }
147 }
148
This page took 0.035659 seconds and 5 git commands to generate.