001    /*
002     * Copyright 2005 Stephen J. McConnell.
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.test.state;
020    
021    import java.rmi.Remote;
022    import java.rmi.RemoteException;
023    
024    import net.dpml.logging.Logger;
025    
026    /**
027     * Component implementation that exposes an active management operation.
028     *
029     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
030     * @version 1.2.0
031     */
032    public class ManagedComponent implements Service
033    {
034        //------------------------------------------------------------------
035        // state
036        //------------------------------------------------------------------
037    
038       /**
039        * The logging channel.
040        */
041        private final Logger m_logger;
042        
043        private int m_count = 0;
044        
045        private DefaultMonitor m_monitor;
046    
047        //------------------------------------------------------------------
048        // constructor
049        //------------------------------------------------------------------
050    
051       /**
052        * Creation of a new <tt>ManagedComponent</tt>.
053        * 
054        * @param logger the assigned logging channel
055        */
056        public ManagedComponent( final Logger logger )
057        {
058            m_logger = logger;
059            
060            m_monitor = new DefaultMonitor();
061        }
062        
063        //------------------------------------------------------------------
064        // Service
065        //------------------------------------------------------------------
066    
067       /**
068        * Ping the object.
069        */
070        public void ping()
071        {
072            m_count++;
073        }
074        
075        //------------------------------------------------------------------
076        // operations
077        //------------------------------------------------------------------
078        
079       /**
080        * Return a monitor.
081        * @return the monitor
082        */
083        public Monitor getMonitor()
084        {
085            return m_monitor;
086        }
087        
088        //------------------------------------------------------------------
089        // internal
090        //------------------------------------------------------------------
091        
092       /**
093        * Return the assigned logging channel.
094        * @return the logging channel
095        */
096        private Logger getLogger()
097        {
098            return m_logger;
099        }
100        
101       /**
102        * Interface implemented by an internal monitor returned
103        * by the getMonitor operation.
104        */
105        public interface Monitor extends Remote
106        {
107           /**
108            * Monitor the number of times the component ping operation 
109            * has been invoked.
110            * @return the access count
111            * @exception RemoteException if a remote I/O error occurs
112            */
113            int getAccessCount() throws RemoteException;
114        }
115        
116       /**
117        * The default monitor implementation.
118        */
119        class DefaultMonitor implements Monitor
120        {
121           /**
122            * Return the access count.
123            * @return the access count
124            */
125            public int getAccessCount()
126            {
127                return m_count;
128            }
129        }
130    }