View Javadoc

1   package net.sourceforge.basher.internal.impl;
2   
3   import java.util.*;
4   import java.util.concurrent.CountDownLatch;
5   import java.beans.XMLDecoder;
6   import java.io.ByteArrayInputStream;
7   
8   import net.sourceforge.basher.*;
9   import net.sourceforge.basher.events.*;
10  import net.sourceforge.basher.impl.TaskContribution;
11  import net.sourceforge.basher.internal.TaskCreator;
12  import org.apache.commons.logging.LogFactory;
13  import org.ops4j.gaderian.Registry;
14  import org.ops4j.gaderian.impl.*;
15  
16  /**
17   * @author Johan Lindquist
18   * @version $Revision$
19   */
20  public class BasherBootHelper implements BasherEventListener
21  {
22      private Registry _registry;
23      private CountDownLatch countDownLatch = new CountDownLatch( 1 );
24      private boolean _manuallyControlled = false;
25  
26      public int run( final Properties properties ) throws Exception
27      {
28          // Extract the active basher context
29          final String activeBasherContext = properties.getProperty( "activeBasherContext", "default" );
30  
31          // Construct the registry
32          final RegistryBuilder registryBuilder = new RegistryBuilder( new StrictErrorHandler() );
33          registryBuilder.addDefaultModuleDescriptorProvider();
34  
35          _registry = registryBuilder.constructRegistry( Locale.getDefault() );
36  
37          // Handle initialisation of Basher contexts provided from the plug-ins
38          // This will also ensure that the selected contexts reporting directory
39          // is correctly setup with the directory specified by the plug-ins
40          final BasherContext basherContext = initializeBasherContexts( activeBasherContext, properties );
41  
42          // Lookup the event manager and register for events
43          EventManager eventManager = ( EventManager ) _registry.getService( EventManager.class );
44          eventManager.addBasherEventListener( this );
45  
46          // Retrieve tasks added as part of the source tree
47          final List<Class> includedClasses = initializeIncludedClasses( properties );
48  
49          if ( includedClasses != null && !includedClasses.isEmpty() )
50          {
51              // There are classes added - register them with the task manager
52              final TaskCreator taskCreator = ( TaskCreator ) _registry.getService( TaskCreator.class );
53              final TaskManager taskManager = ( TaskManager ) _registry.getService( TaskManager.class );
54  
55              for ( Class includedClass : includedClasses )
56              {
57                  // FIXME: do not just yet yse this!!!
58                  // taskManager.addTask( includedClass );
59              }
60          }
61  
62          //
63          _manuallyControlled = basherContext.isManuallyControlled();
64  
65          if ( !_manuallyControlled )
66          {
67              // Find the scheduler and start the run
68              final Scheduler scheduler = ( Scheduler ) _registry.getService( Scheduler.class );
69              scheduler.start( activeBasherContext );
70          }
71  
72          // Now, wait for the complete call back
73          try
74          {
75              countDownLatch.await();
76          }
77          catch ( InterruptedException e )
78          {
79              e.printStackTrace();
80          }
81  
82          // Ok, we have now received an end event - it is safe to shutdown the registry and exit
83  
84          // For safety, we sleep for just a bit
85          try
86          {
87              Thread.sleep( 3000 );
88          }
89          catch ( InterruptedException e )
90          {
91              e.printStackTrace();
92          }
93  
94          // FIXME: This should process the results to check whether
95          // FIXME: - any tasks failed
96          // FIXME: - any tasks ran for too long
97          int resultCode = 0;
98  
99  
100 
101         try
102         {
103             _registry.shutdown();
104         }
105         catch ( Exception e )
106         {
107             e.printStackTrace();
108         }
109         finally
110         {
111             return resultCode;
112         }
113 
114 
115     }
116 
117     private List<Class> initializeIncludedClasses( final Properties properties ) throws ClassNotFoundException
118     {
119         List<Class> includedFiles = new ArrayList<Class>();
120         for ( Object key : properties.keySet() )
121         {
122             String name = ( String ) key;
123 
124             if ( name.startsWith( "includedFile." ) )
125             {
126                 final String className = makeClassName( properties.getProperty( name ) );
127                 includedFiles.add( Thread.currentThread().getContextClassLoader().loadClass( className ) );
128             }
129         }
130 
131         return includedFiles;
132     }
133 
134     private String makeClassName( final String sourceName )
135     {
136         String sourceClassName = sourceName.substring( 0, sourceName.length() - ".java".length() );
137         sourceClassName = sourceClassName.replaceAll( "/", "." );
138         sourceClassName = sourceClassName.replaceAll( "\\\\", "." );
139         return sourceClassName;
140     }
141 
142     private BasherContext initializeBasherContexts( final String activeBasherContext, final Properties properties )
143     {
144         // First deserialize the basher contexts provided to use from plugins
145         final List<BasherContext> basherContexts = deserializeBasherContexts( properties );
146 
147         // Find the context manager and add all provided contexts
148         final ContextManager contextManager = ( ContextManager ) _registry.getService( ContextManager.class );
149         for ( final BasherContext basherContext : basherContexts )
150         {
151             contextManager.addBasherContext( basherContext );
152         }
153 
154         final BasherContext context = contextManager.getBasherContext( activeBasherContext );
155         setupReportsDirectory( context, properties );
156         return context;
157     }
158 
159     private void setupReportsDirectory( final BasherContext basherContext, Properties properties )
160     {
161         basherContext.setReportDirectory( properties.getProperty( "reportingDirectory", "target/basher-reports" ) );
162     }
163 
164     private List<BasherContext> deserializeBasherContexts( final Properties properties )
165     {
166         List<BasherContext> basherContexts = new ArrayList<BasherContext>();
167         for ( Object o : properties.keySet() )
168         {
169             String key = ( String ) o;
170             if ( key.startsWith( "basherContext." ) )
171             {
172                 XMLDecoder xmlDecoder = new XMLDecoder( new ByteArrayInputStream( properties.getProperty( key ).getBytes() ) );
173                 final BasherContext basherContext = ( BasherContext ) xmlDecoder.readObject();
174                 basherContexts.add( basherContext );
175             }
176         }
177 
178         return basherContexts;
179 
180     }
181 
182     public void basherEvent( final BasherEvent basherEvent )
183     {
184         if ( basherEvent instanceof PhaseTransitionEvent )
185         {
186             if ( !_manuallyControlled )
187             {
188                 if ( ( ( PhaseTransitionEvent ) basherEvent ).getNewPhase() == Phase.END )
189                 {
190                     countDownLatch.countDown();
191                 }
192             }
193         }
194         else if ( basherEvent instanceof BasherShutdownEvent )
195         {
196             if ( _manuallyControlled )
197             {
198                 countDownLatch.countDown();
199             }
200         }
201 //        else if (basherEvent instanceof AverageCollectedEvent )
202 //        {
203 //            Average average = ((AverageCollectedEvent)basherEvent).getAverage();
204 //            // FIXME: Collect the averages for later check
205 //        }
206     }
207 }