1919import com .cloud .agent .Agent .ExitStatus ;
2020import com .cloud .agent .dao .StorageComponent ;
2121import com .cloud .agent .dao .impl .PropertiesStorage ;
22+ import com .cloud .agent .properties .AgentProperties ;
23+ import com .cloud .agent .properties .AgentPropertiesFileHandler ;
2224import com .cloud .resource .ServerResource ;
2325import com .cloud .utils .LogUtils ;
24- import com .cloud .utils .NumbersUtil ;
2526import com .cloud .utils .ProcessUtil ;
2627import com .cloud .utils .PropertiesUtil ;
2728import com .cloud .utils .backoff .BackoffAlgorithm ;
3132import org .apache .commons .daemon .DaemonContext ;
3233import org .apache .commons .daemon .DaemonInitException ;
3334import org .apache .commons .lang .math .NumberUtils ;
35+ import org .apache .commons .lang3 .BooleanUtils ;
3436import org .apache .commons .lang3 .StringUtils ;
3537import org .apache .log4j .Logger ;
3638import org .apache .log4j .xml .DOMConfigurator ;
@@ -74,6 +76,7 @@ public class AgentShell implements IAgentShell, Daemon {
7476 private String hostToConnect ;
7577 private String connectedHost ;
7678 private Long preferredHostCheckInterval ;
79+ protected AgentProperties agentProperties = new AgentProperties ();
7780
7881 public AgentShell () {
7982 }
@@ -265,75 +268,83 @@ protected boolean parseCommand(final String[] args) throws ConfigurationExceptio
265268 }
266269 }
267270
268- if (port == null ) {
269- port = getProperty (null , "port" );
270- }
271+ setHost (host );
271272
272- _port = NumberUtils .toInt (port , 8250 );
273+ _guid = getGuid (guid );
274+ _properties .setProperty (AgentProperties .GUID .getName (), _guid );
273275
274- _proxyPort = NumberUtils .toInt (getProperty (null , "consoleproxy.httpListenPort" ), 443 );
276+ _port = getPortOrWorkers (port , AgentProperties .PORT );
277+ _workers = getWorkers (workers );
278+ _zone = getZoneOrPod (zone , AgentProperties .ZONE );
279+ _pod = getZoneOrPod (pod , AgentProperties .POD );
275280
276- if ( workers == null ) {
277- workers = getProperty ( null , "workers" );
278- }
281+ _proxyPort = AgentPropertiesFileHandler . getPropertyValue ( AgentProperties . CONSOLEPROXY_HTTPLISTENPORT );
282+ _pingRetries = AgentPropertiesFileHandler . getPropertyValue ( AgentProperties . PING_RETRIES );
283+ preferredHostCheckInterval = AgentPropertiesFileHandler . getPropertyValue ( AgentProperties . HOST_LB_CHECK_INTERVAL );
279284
280- _workers = NumberUtils .toInt (workers , 5 );
281- if (_workers <= 0 ) {
282- _workers = 5 ;
283- }
285+ return true ;
286+ }
284287
288+ protected void setHost (String host ) throws ConfigurationException {
285289 if (host == null ) {
286- host = getProperty ( null , "host" );
290+ host = AgentPropertiesFileHandler . getPropertyValue ( AgentProperties . HOST );
287291 }
288292
289- if (host == null ) {
290- host = "localhost" ;
293+ if (isValueStartingAndEndingWithAtSign ( host ) ) {
294+ throw new ConfigurationException ( String . format ( "Host [%s] is not configured correctly." , host )) ;
291295 }
292296
293297 setHosts (host );
298+ }
294299
295- if (zone != null )
296- _zone = zone ;
297- else
298- _zone = getProperty (null , "zone" );
299- if (_zone == null || (_zone .startsWith ("@" ) && _zone .endsWith ("@" ))) {
300- _zone = "default" ;
300+ protected boolean isValueStartingAndEndingWithAtSign (String value ) {
301+ return value .startsWith ("@" ) && value .endsWith ("@" );
302+ }
303+
304+ protected String getGuid (String guid ) throws ConfigurationException {
305+ guid = StringUtils .defaultString (guid , AgentPropertiesFileHandler .getPropertyValue (AgentProperties .GUID ));
306+ if (guid != null ) {
307+ return guid ;
301308 }
302309
303- if (pod != null )
304- _pod = pod ;
305- else
306- _pod = getProperty (null , "pod" );
307- if (_pod == null || (_pod .startsWith ("@" ) && _pod .endsWith ("@" ))) {
308- _pod = "default" ;
310+ if (BooleanUtils .isFalse (AgentPropertiesFileHandler .getPropertyValue (AgentProperties .DEVELOPER ))) {
311+ throw new ConfigurationException ("Unable to find the guid" );
312+ }
313+
314+ return UUID .randomUUID ().toString ();
315+ }
316+
317+ protected String getZoneOrPod (String zoneOrPod , AgentProperties .Property <String > property ) {
318+ String value = zoneOrPod ;
319+
320+ if (value == null ) {
321+ value = AgentPropertiesFileHandler .getPropertyValue (property );
309322 }
310323
311- if (_host == null || ( _host . startsWith ( "@" ) && _host . endsWith ( "@" ) )) {
312- throw new ConfigurationException ( "Host is not configured correctly: " + _host );
324+ if (isValueStartingAndEndingWithAtSign ( value )) {
325+ value = property . getDefaultValue ( );
313326 }
314327
315- final String retries = getProperty ( null , "ping.retries" ) ;
316- _pingRetries = NumbersUtil . parseInt ( retries , 5 );
328+ return value ;
329+ }
317330
318- String value = getProperty (null , "developer" );
319- boolean developer = Boolean .parseBoolean (value );
331+ protected int getWorkers (String workersString ) {
332+ AgentProperties .Property <Integer > propertyWorkers = agentProperties .getWorkers ();
333+ int workers = getPortOrWorkers (workersString , propertyWorkers );
320334
321- if (guid != null )
322- _guid = guid ;
323- else
324- _guid = getProperty (null , "guid" );
325- if (_guid == null ) {
326- if (!developer ) {
327- throw new ConfigurationException ("Unable to find the guid" );
328- }
329- _guid = UUID .randomUUID ().toString ();
330- _properties .setProperty ("guid" , _guid );
335+ if (workers <= 0 ) {
336+ workers = propertyWorkers .getDefaultValue ();
331337 }
332338
333- String val = getProperty ( null , preferredHostIntervalKey ) ;
334- preferredHostCheckInterval = StringUtils . isEmpty ( val ) ? null : Long . valueOf ( val );
339+ return workers ;
340+ }
335341
336- return true ;
342+ protected int getPortOrWorkers (String portOrWorkers , AgentProperties .Property <Integer > property ) {
343+ if (portOrWorkers == null ) {
344+ return AgentPropertiesFileHandler .getPropertyValue (property );
345+ }
346+
347+ return NumberUtils .toInt (portOrWorkers , property .getDefaultValue ());
337348 }
338349
339350 @ Override
@@ -398,7 +409,7 @@ public void init(String[] args) throws ConfigurationException {
398409 }
399410
400411 private void launchAgent () throws ConfigurationException {
401- String resourceClassNames = getProperty ( null , "resource" );
412+ String resourceClassNames = AgentPropertiesFileHandler . getPropertyValue ( AgentProperties . RESOURCE );
402413 s_logger .trace ("resource=" + resourceClassNames );
403414 if (resourceClassNames != null ) {
404415 launchAgentFromClassInfo (resourceClassNames );
@@ -414,25 +425,15 @@ private void launchAgentFromClassInfo(String resourceClassNames) throws Configur
414425 Class <?> impl ;
415426 try {
416427 impl = Class .forName (name );
417- final Constructor <?> constructor = impl .getDeclaredConstructor ();
428+ Constructor <?> constructor = impl .getDeclaredConstructor ();
418429 constructor .setAccessible (true );
419430 ServerResource resource = (ServerResource )constructor .newInstance ();
420431 launchNewAgent (resource );
421- } catch (final ClassNotFoundException e ) {
422- throw new ConfigurationException ("Resource class not found: " + name + " due to: " + e .toString ());
423- } catch (final SecurityException e ) {
424- throw new ConfigurationException ("Security exception when loading resource: " + name + " due to: " + e .toString ());
425- } catch (final NoSuchMethodException e ) {
426- throw new ConfigurationException ("Method not found exception when loading resource: " + name + " due to: " + e .toString ());
427- } catch (final IllegalArgumentException e ) {
428- throw new ConfigurationException ("Illegal argument exception when loading resource: " + name + " due to: " + e .toString ());
429- } catch (final InstantiationException e ) {
430- throw new ConfigurationException ("Instantiation exception when loading resource: " + name + " due to: " + e .toString ());
431- } catch (final IllegalAccessException e ) {
432- throw new ConfigurationException ("Illegal access exception when loading resource: " + name + " due to: " + e .toString ());
433- } catch (final InvocationTargetException e ) {
434- throw new ConfigurationException ("Invocation target exception when loading resource: " + name + " due to: " + e .toString ());
435- }
432+ } catch (final ClassNotFoundException | SecurityException | NoSuchMethodException | IllegalArgumentException | InstantiationException |
433+ IllegalAccessException | InvocationTargetException e ) {
434+ ConfigurationException configurationException = new ConfigurationException (String .format ("Error while creating Agent with class [%s]." , name ));
435+ configurationException .setRootCause (e );
436+ throw configurationException ; }
436437 }
437438 }
438439
@@ -492,7 +493,7 @@ public void start() {
492493
493494 String instance = getProperty (null , "instance" );
494495 if (instance == null ) {
495- if (Boolean . parseBoolean ( getProperty ( null , "developer" ))) {
496+ if (BooleanUtils . isTrue ( AgentPropertiesFileHandler . getPropertyValue ( AgentProperties . DEVELOPER ))) {
496497 instance = UUID .randomUUID ().toString ();
497498 } else {
498499 instance = "" ;
@@ -516,13 +517,8 @@ public void start() {
516517 s_logger .debug ("[ignored] AgentShell was interrupted." );
517518 }
518519
519- } catch (final ConfigurationException e ) {
520- s_logger .error ("Unable to start agent: " + e .getMessage ());
521- System .out .println ("Unable to start agent: " + e .getMessage ());
522- System .exit (ExitStatus .Configuration .value ());
523520 } catch (final Exception e ) {
524521 s_logger .error ("Unable to start agent: " , e );
525- System .out .println ("Unable to start agent: " + e .getMessage ());
526522 System .exit (ExitStatus .Error .value ());
527523 }
528524 }
0 commit comments