protected void doMain ( String ... args ) { properties = new MergeableProperties ( ) ; properties . putAll ( System . getProperties ( ) ) ; for ( String arg : args ) { properties . merge ( arg ) ; } // Read event handling configuration Map < String , List < EventHandler > > eventHandlers = new LinkedHashMap <> ( ) ; MergeableProperties allProperties = properties . getPrefixed ( "willow-event-handler.handlers" ) ; int i = 0 ; while ( true ) { MergeableProperties handlerProperties = allProperties . getPrefixed ( "[" + i ++ + "]" ) ; if ( handlerProperties . isEmpty ( ) ) { break ; } String handlerClassName = handlerProperties . getProperty ( "handler" ) ; String eventClassName = handlerProperties . getProperty ( "event" ) ; try { EventHandler eventHandler = ( EventHandler ) injector . getInstance ( Class . forName ( handlerClassName ) ) ; MergeableProperties beanProperties = handlerProperties . getPrefixed ( "properties" ) ; for ( String propertyName : beanProperties . stringPropertyNames ( ) ) { String propertyValue = beanProperties . getProperty ( propertyName ) ; BeanUtils . setProperty ( eventHandler , propertyName , propertyValue ) ; } List < EventHandler > eventHandlersForType = eventHandlers . get ( eventClassName ) ; if ( eventHandlersForType == null ) { eventHandlersForType = new ArrayList <> ( ) ; eventHandlers . put ( eventClassName , eventHandlersForType ) ; } eventHandlersForType . add ( eventHandler ) ; } catch ( ClassNotFoundException | IllegalAccessException | InvocationTargetException e ) { logger . log ( Level . SEVERE , "Error instantiating handler" , e ) ; System . exit ( 1 ) ; } } if ( eventHandlers . isEmpty ( ) ) { logger . info ( "No event handlers configured. Exiting." ) ; System . exit ( 0 ) ; } Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( ) { @ Override public void run ( ) { Main . this . stop ( ) ; } } ) ; // Start polling events String uri = properties . getProperty ( "willow-event-handler.eventsUri" ) ; try { logger . info ( "Initializing event poller" ) ; eventPoller . initialize ( eventHandlers , new URI ( uri ) ) ; } catch ( URISyntaxException e ) { logger . log ( Level . SEVERE , "Invalid URI: " + uri , e ) ; System . exit ( 1 ) ; } }
Actual main implementation .