001    /* 
002     * Copyright 2004 Niclas Hedhman.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at 
007     * 
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     * 
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.transit.monitor;
020    
021    import java.net.URI;
022    import java.net.URL;
023    import java.lang.reflect.Constructor;
024     
025    /**
026     * A repository monitor router handles mutlicast distribution of monitor events to 
027     * a set of subscribed monitors.
028     *
029     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
030     * @version 1.1.0
031     */
032    public class RepositoryMonitorRouter extends AbstractMonitorRouter
033        implements RepositoryMonitor, Router
034    {   
035        //--------------------------------------------------------------------
036        // RepositoryMonitor
037        //--------------------------------------------------------------------
038    
039       /**
040        * Notify all subscribed monitors of a info message event.
041        * @param info the information message
042        */ 
043        public void sequenceInfo( String info )
044        {
045            Monitor[] monitors = getMonitors();
046            for( int i=0; i < monitors.length; i++ )
047            {
048                RepositoryMonitor monitor = (RepositoryMonitor) monitors[i];
049                monitor.sequenceInfo( info );
050            }
051        }
052        
053       /**
054        * Notify all monitors of a request for the establishment of a plugin.
055        * @param parent the parent classloader
056        * @param uri the requested plugin uri
057        * @param args the supplied constructor arguments
058        */
059        public void getPluginRequested( ClassLoader parent, URI uri, Object[] args )
060        {
061            Monitor[] monitors = getMonitors();
062            for( int i=0; i < monitors.length; i++ )
063            {
064                RepositoryMonitor monitor = (RepositoryMonitor) monitors[i];
065                monitor.getPluginRequested( parent, uri, args );
066            }
067        }
068        
069       /**
070        * Notify all monitorrs of the establishment of a plugin class.
071        * @param pluginClass the plugin class
072        */
073        public void establishedPluginClass( Class pluginClass )
074        {
075            Monitor[] monitors = getMonitors();
076            for( int i=0; i < monitors.length; i++ )
077            {
078                RepositoryMonitor monitor = (RepositoryMonitor) monitors[i];
079                monitor.establishedPluginClass( pluginClass );
080            }
081        }
082        
083       /**
084        * Notify all monitors of an exception related to plugin establishment.
085        * @param methodname the method raising the exception
086        * @param e the causal exception
087        */
088        public void exceptionOccurred( String methodname, Exception e )
089        {
090            Monitor[] monitors = getMonitors();
091            for( int i=0; i < monitors.length; i++ )
092            {
093                RepositoryMonitor monitor = (RepositoryMonitor) monitors[i];
094                monitor.exceptionOccurred( methodname, e );
095            }
096        }
097        
098       /**
099        * Notify all monitors of the discovery of a plugin constructor.
100        * @param constructor the constructor
101        * @param args the constructor args
102        */
103        public void pluginConstructorFound( Constructor constructor, Object[] args )
104        {
105            Monitor[] monitors = getMonitors();
106            for( int i=0; i < monitors.length; i++ )
107            {
108                RepositoryMonitor monitor = (RepositoryMonitor) monitors[i];
109                monitor.pluginConstructorFound( constructor, args );
110            }
111        }
112        
113       /**
114        * Notify all monitors of the instantiation of a plugin.
115        * @param pluginInstance the plugin instance
116        */
117        public void pluginInstantiated( Object pluginInstance )
118        {
119            Monitor[] monitors = getMonitors();
120            for( int i=0; i < monitors.length; i++ )
121            {
122                RepositoryMonitor monitor = (RepositoryMonitor) monitors[i];
123                monitor.pluginInstantiated( pluginInstance );
124            }
125        }
126    
127       /**
128        * Notify all monitors of the creation of a new classloader.
129        * @param type the type of classloader (api, spi or impl)
130        * @param classloader the new classloader 
131        */    
132        public void classloaderConstructed( String type, ClassLoader classloader )
133        {
134            Monitor[] monitors = getMonitors();
135            for( int i=0; i < monitors.length; i++ )
136            {
137                RepositoryMonitor monitor = (RepositoryMonitor) monitors[i];
138                monitor.classloaderConstructed( type, classloader );
139            }
140        }
141    
142       /**
143        * Handle notification of system classloader expansion.
144        * @param plugin the uri of the plugin requesting system classloader expansion
145        * @param urls the array of urls added to the system classloader
146        */
147        public void systemExpanded( URI plugin, URL[] urls )
148        {
149            Monitor[] monitors = getMonitors();
150            for( int i=0; i < monitors.length; i++ )
151            {
152                RepositoryMonitor monitor = (RepositoryMonitor) monitors[i];
153                monitor.systemExpanded( plugin, urls );
154            }
155        }
156    
157       /**
158        * Add a monitor to the set of monitors managed by this router.
159        * @param monitor the monitor to add
160        * @exception IllegalArgumentException if the supplied monitor does not
161        *   implement the RepositoryMonitor interface
162        */
163        public void addMonitor( Monitor monitor ) throws IllegalArgumentException
164        {
165            if( !( monitor instanceof RepositoryMonitor ) )
166            {
167                throw new IllegalArgumentException( "monitor must be RepositoryMonitor type." );
168            }
169            else
170            {
171                super.addMonitor( monitor );
172            }
173        }
174    }
175