View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
3    ~
4    ~ Licensed under the Apache License, Version 2.0 (the "License");
5    ~ you may not use this file except in compliance with the License.
6    ~ You may obtain a copy of the License at
7    ~
8    ~      http://www.apache.org/licenses/LICENSE-2.0
9    ~
10   ~ Unless required by applicable law or agreed to in writing, software
11   ~ distributed under the License is distributed on an "AS IS" BASIS,
12   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   ~ See the License for the specific language governing permissions and
14   ~ limitations under the License.
15   */
16  package org.jmonit.monitors;
17  
18  import java.util.Collection;
19  
20  import org.jmonit.Repository;
21  import org.jmonit.log.Log;
22  import org.jmonit.spi.Plugin;
23  import org.jmonit.spi.PluginManager;
24  import org.jmonit.spi.PluginManagerAware;
25  import org.jmonit.spi.RepositoryAware;
26  
27  /**
28   * Default <code>Monitor</code> implementation. Uses the FeatureManager to
29   * automatically register features when the monitor is tagged.
30   * 
31   * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
32   */
33  public class DefaultMonitor
34      extends AbstractMonitor
35      implements PluginManagerAware
36  {
37      /** logger */
38      private static Log log = Log.getLog( DefaultMonitor.class );
39  
40      private PluginManager pluginManager;
41  
42      private Repository repository;
43  
44      public DefaultMonitor( String name, Repository repository )
45      {
46          super( name );
47          this.repository = repository;
48      }
49  
50      /**
51       * {@inheritDoc}
52       * 
53       * @see org.jmonit.spi.PluginManagerAware#setPluginManager(org.jmonit.spi.PluginManager)
54       */
55      public void setPluginManager( PluginManager manager )
56      {
57          this.pluginManager = manager;
58      }
59  
60      /**
61       * {@inheritDoc}
62       * <p>
63       * Add the ability to automagically register new features on-demand using
64       * factories provided by the <code>FeatureManager</code>
65       * 
66       * @see org.jmonit.Monitor#getFeature(java.lang.Class)
67       */
68      @Override
69      public <T> T getFeature( Class<T> role )
70      {
71          T feature = super.getFeature( role );
72          if ( feature != null )
73          {
74              return feature;
75          }
76          // Try to enhance the monitor on-demand
77          if ( pluginManager != null )
78          {
79              if ( log.isDebugEnabled() )
80              {
81                  log.debug( "enhance monitor " + getName() + " with feature " + role.getName() );
82              }
83              Plugin<T> plugin = pluginManager.getFactory( role ).newPluginInstance();
84              if ( plugin instanceof RepositoryAware )
85              {
86                  ( (RepositoryAware) plugin ).setRepository( repository );
87              }
88              register( plugin, role );
89              return plugin.getFeature();
90          }
91          return null;
92      }
93  
94      /**
95       * {@inheritDoc}
96       * 
97       * @see org.jmonit.monitors.AbstractMonitor#onMonitorTaggedEvent(java.lang.String)
98       */
99      protected void onMonitorTaggedEvent( String tag )
100     {
101         if ( pluginManager != null )
102         {
103             Collection<Class> plugins = pluginManager.getFeatures( tag );
104             for ( Class feature : plugins )
105             {
106                 getFeature( feature );
107             }
108         }
109     }
110 }