001    //$HeadURL$
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.portal.owswatch;
038    
039    import java.io.Serializable;
040    import java.util.Calendar;
041    
042    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
043    import org.apache.commons.httpclient.HttpClient;
044    import org.apache.commons.httpclient.HttpMethodBase;
045    import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
046    import org.apache.commons.httpclient.params.HttpMethodParams;
047    import org.deegree.ogcwebservices.OGCWebServiceException;
048    
049    /**
050     * Executes the tests on a certain service. It also calls the validator and logs the result in the protocol file This
051     * class extends Thread. On call of start() will execute the functionality of the class as described above
052     *
053     * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a>
054     * @author last edited by: $Author: elmasry $
055     *
056     * @version $Revision: 1.2 $, $Date: 2008-03-20 16:33:27 $
057     */
058    public class ServiceInvoker extends Thread implements Serializable {
059    
060        /**
061         *
062         */
063        private static final long serialVersionUID = 8445955567617597483L;
064    
065        private ServiceConfiguration serviceConfig = null;
066    
067        private ServiceLog serviceLog = null;
068    
069        /**
070         * @param serviceconfig
071         * @param serviceLog
072         */
073        public ServiceInvoker( ServiceConfiguration serviceconfig, ServiceLog serviceLog ) {
074            this.serviceConfig = serviceconfig;
075            this.serviceLog = serviceLog;
076        }
077    
078        /**
079         * Executes the test in a separate thread
080         *
081         */
082        public void executeTestThreaded() {
083            start();
084        }
085    
086        /*
087         * (non-Javadoc)
088         *
089         * @see java.lang.Thread#run()
090         */
091        @Override
092        public void run() {
093            executeTest();
094        }
095    
096        /**
097         * Executes the test in the main thread
098         *
099         */
100        public void executeTest() {
101    
102            ValidatorResponse tmpResponse = null;
103            try {
104                HttpMethodBase method = serviceConfig.getHttpMethodBase();
105                tmpResponse = executeHttpMethod( method );
106                method.releaseConnection();
107            } catch ( OGCWebServiceException e ) {
108                tmpResponse = new ValidatorResponse( "Page Unavailable: " + e.getLocalizedMessage(),
109                                                     Status.RESULT_STATE_PAGE_UNAVAILABLE );
110                tmpResponse.setLastLapse( -1 );
111                tmpResponse.setLastTest( Calendar.getInstance().getTime() );
112            }
113    
114            serviceLog.addMessage( tmpResponse, serviceConfig );
115        }
116    
117        /**
118         * @param method
119         * @return The response OGCWebServiceResponseData
120         * @throws OGCWebServiceException
121         */
122        protected ValidatorResponse executeHttpMethod( HttpMethodBase method )
123                                throws OGCWebServiceException {
124    
125            HttpClient client = new HttpClient();
126            HttpConnectionManagerParams cmParams = client.getHttpConnectionManager().getParams();
127            cmParams.setConnectionTimeout( serviceConfig.getTimeout() * 1000 );
128            client.getHttpConnectionManager().setParams( cmParams );
129            // Provide custom retry handler is necessary
130            method.getParams().setParameter( HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler( 2, false ) );
131            ValidatorResponse response = null;
132            try {
133                long startTime = System.currentTimeMillis();
134                int statusCode = client.executeMethod( method );
135                long lapse = System.currentTimeMillis() - startTime;
136                response = serviceConfig.getValidator().validateAnswer( method, statusCode );
137    
138                response.setLastLapse( lapse );
139                Calendar date = Calendar.getInstance();
140                date.setTimeInMillis( startTime );
141                response.setLastTest( date.getTime() );
142            } catch ( Exception e ) {
143                throw new OGCWebServiceException( e.getLocalizedMessage() );
144            } finally {
145                method.releaseConnection();
146            }
147            return response;
148        }
149    
150        /**
151         * @return ServiceConfiguration
152         */
153        public ServiceConfiguration getServiceConfig() {
154            return serviceConfig;
155        }
156    
157        /**
158         * @return ServiceLog
159         */
160        public ServiceLog getServiceLog() {
161            return serviceLog;
162        }
163    }