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.io.File;
022    
023    import java.net.URL;
024    
025    import net.dpml.transit.Artifact;
026    
027    /**
028     * The cache monitor router is responsible for the dispatiching of cache monitor 
029     * events to registered monitors.
030     *
031     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
032     * @version 1.0.3
033     */
034    public class CacheMonitorRouter extends AbstractMonitorRouter
035        implements CacheMonitor, Router
036    {
037        //--------------------------------------------------------------------
038        // CacheMonitor
039        //--------------------------------------------------------------------
040    
041       /**
042        * Notify all monitors that a request for an artifact has been received.
043        * @param artifact the requested artifact
044        */ 
045        public void resourceRequested( Artifact artifact )
046        {
047            Monitor[] monitors = getMonitors();
048            for( int i=0; i < monitors.length; i++ )
049            {
050                CacheMonitor monitor = (CacheMonitor) monitors[i];
051                monitor.resourceRequested( artifact );
052            }
053        }
054        
055       /**
056        * Notify all monitors that a request artifact has been added to the local cache.
057        * @param resource the url of the remote resource
058        * @param localFile the local file added to the cache
059        */ 
060        public void addedToLocalCache( URL resource, File localFile )
061        {
062            Monitor[] monitors = getMonitors();
063            for( int i=0; i < monitors.length; i++ )
064            {
065                CacheMonitor monitor = (CacheMonitor) monitors[i];
066                monitor.addedToLocalCache( resource, localFile );
067            }
068        }
069        
070       /**
071        * Notify all monitors that an artifact in the local cache has been updated.
072        * @param resource the url of the remote resource 
073        * @param localFile the local file that was modified
074        */ 
075        public void updatedLocalCache( URL resource, File localFile )
076        {
077            Monitor[] monitors = getMonitors();
078            for( int i=0; i < monitors.length; i++ )
079            {
080                CacheMonitor monitor = (CacheMonitor) monitors[i];
081                monitor.addedToLocalCache( resource, localFile );
082            }
083        }
084        
085       /**
086        * Notify all monitors that an artifact in the local cache was removed.
087        * @param resource the url of the remote resource 
088        * @param localFile the local file that was removed
089        */ 
090        public void removedFromLocalCache( URL resource, File localFile )
091        {
092            Monitor[] monitors = getMonitors();
093            for( int i=0; i < monitors.length; i++ )
094            {
095                CacheMonitor monitor = (CacheMonitor) monitors[i];
096                monitor.removedFromLocalCache( resource, localFile );
097            }
098        }
099        
100       /**
101        * Notify all monitors that an artifact request on a named host failed.
102        * @param host the remote host
103        * @param artifact the requested artifact
104        * @param cause the cause of the failure
105        */ 
106        public void failedDownloadFromHost( String host, Artifact artifact, Throwable cause )
107        {
108            Monitor[] monitors = getMonitors();
109            for( int i=0; i < monitors.length; i++ )
110            {
111                CacheMonitor monitor = (CacheMonitor) monitors[i];
112                monitor.failedDownloadFromHost( host, artifact, cause );
113            }
114        }
115        
116       /**
117        * Notify all monitors that an artifact request failed.
118        * @param artifact the requested artifact
119        */ 
120        public void failedDownload( Artifact artifact )
121        {
122            Monitor[] monitors = getMonitors();
123            for( int i=0; i < monitors.length; i++ )
124            {
125                CacheMonitor monitor = (CacheMonitor) monitors[i];
126                monitor.failedDownload( artifact );
127            }
128        }
129    
130       /**
131        * Addition of a monitor to the list of monitors maintained by this router.
132        * @param monitor the monitor to add
133        * @exception IllegalArgumentException if the monitor does not implement CacheMonitor
134        */ 
135        public void addMonitor( Monitor monitor ) throws IllegalArgumentException
136        {
137            if( !( monitor instanceof CacheMonitor ) )
138            {
139                throw new IllegalArgumentException( "monitor must be CacheMonitor type." );
140            }
141            else
142            {
143                super.addMonitor( monitor );
144            }
145        }
146    }
147