View Javadoc

1   /*
2    * Created on May 18, 2005
3    *
4    * Default implementation of TargetLauncher
5    */
6   package net.sf.tlc.core.impl;
7   
8   import java.util.logging.Level;
9   import java.util.logging.Logger;
10  
11  import net.sf.tlc.core.LifecycleAware;
12  import net.sf.tlc.core.TargetFinder;
13  import net.sf.tlc.core.TargetLauncher;
14  import net.sf.tlc.model.Target;
15  import net.sf.tlc.util.ClassUtils;
16  
17  /***
18   * Default implementation of TargetLauncher
19   * 
20   * @author aisrael
21   */
22  public class DefaultLauncher implements TargetLauncher {
23  
24      private static final Logger logger = Logger.getLogger(DefaultLauncher.class.getName());
25  
26      private final ClassLoader classLoader;
27  
28      private final TargetFinder finder;
29  
30      private final ThreadGroup threads = new ThreadGroup(this.getClass().getName());
31  
32      /***
33       * @param classLoader
34       *            ClassLoader
35       * @param finder
36       *            TargetFinder
37       */
38      public DefaultLauncher(final ClassLoader classLoader, final TargetFinder finder) {
39          if (null == classLoader) {
40              final RuntimeException r = new IllegalArgumentException("classLoader is null");
41              logger.throwing(this.getClass().getName(), "DefaultLauncher", r);
42              throw r;
43          }
44          this.classLoader = classLoader;
45          if (null == finder) {
46              final RuntimeException r = new IllegalArgumentException("finder is null");
47              logger.throwing(this.getClass().getName(), "DefaultLauncher", r);
48              throw r;
49          }
50          this.finder = finder;
51      }
52  
53      /***
54       * (non-Javadoc)
55       * 
56       * @see net.sf.tlc.core.TargetLauncher#loadTargets()
57       */
58      public final void loadTargets() {
59          final Target[] targets = finder.listTargets();
60          for (int i = 0; i < targets.length; ++i) {
61              System.out.println(targets[i]);
62              try {
63                  final Class theClass = classLoader.loadClass(targets[i].getName());
64                  if (ClassUtils.isRunnable(theClass)) {
65                      final Runnable runnable = (Runnable) theClass.newInstance();
66                      final Thread thread = new Thread(threads, runnable, theClass.getName());
67                      thread.start();
68                  } else if (LifecycleAware.class.isAssignableFrom(theClass)) {
69                      // TODO implement this!
70                  }
71              } catch (ClassNotFoundException e) {
72                  logger.log(Level.WARNING, "ClassNotFoundException: " + targets[i].getName(), e);
73              } catch (InstantiationException e) {
74                  logger.log(Level.WARNING, "InstantiationException: " + targets[i].getName(), e);
75              } catch (IllegalAccessException e) {
76                  logger.log(Level.WARNING, "IllegalAccessException: " + targets[i].getName(), e);
77              }
78          }
79          while (threads.activeCount() > 0) {
80              try {
81                  Thread.sleep(1000);
82              } catch (InterruptedException e) {
83                  logger.log(Level.FINE, "Interrupted!", e);
84              }
85          }
86      }
87  
88  }