    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/owswatch/ServiceLog.java $
    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.File;
    040    import java.io.FileWriter;
    041    import java.io.IOException;
    042    import java.io.Serializable;
    043    import java.util.Calendar;
    044
    045    import org.deegree.framework.log.ILogger;
    046    import org.deegree.framework.log.LoggerFactory;
    047    import org.deegree.framework.xml.XMLException;
    048    import org.deegree.framework.xml.XMLFragment;
    049    import org.deegree.framework.xml.XMLTools;
    050    import org.w3c.dom.Element;
    051
    052    /**
    053     * A class to log the testings results into a protocol file, i.e. a logger class
    054     *
    055     * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a>
    056     * @author last edited by: $Author: jmays $
    057     *
    058     * @version $Revision: 20271 $, $Date: 2009-10-21 13:07:15 +0200 (Mi, 21. Okt 2009) $
    059     */
    060    public class ServiceLog implements Serializable {
    061
    062        /**
    063         *
    064         */
    065        private static final long serialVersionUID = -2010721267404089317L;
    066
    067        private static final ILogger LOG = LoggerFactory.getLogger( ServiceLog.class );
    068
    069        private int serviceId = -1;
    070
    071        private String serviceName = null;
    072
    073        private String serviceType = null;
    074
    075        private String protocolURI;
    076
    077        private String protDirPath = null;
    078
    079        private String protHttpAddr = null;
    080
    081        private EmailSender emailSender = null;
    082
    083        private ValidatorResponse response = null;
    084
    085        /**
    086         * @param serviceId
    087         * @param serviceName
    088         * @param serviceType
    089         * @param protDirPath
    090         * @param servletAddr
    091         * @param emailSender
    092         *            The class responsible for sending emails
    093         * @throws IOException
    094         *             if the given paths are invalid
    095         * @throws XMLException
    096         *             if the xmlDocument(s) are mal formed
    097         */
    098        public ServiceLog( String protDirPath, int serviceId, String serviceName, String serviceType, String servletAddr,
    099                           EmailSender emailSender ) throws IOException {
    100            this.protDirPath = protDirPath;
    101            this.serviceId = serviceId;
    102            this.serviceName = serviceName;
    103            this.serviceType = serviceType;
    104            this.emailSender = emailSender;
    105
    106            StringBuilder builder = new StringBuilder( servletAddr );
    107            if ( !( builder.lastIndexOf( "?" ) == builder.length() - 1 ) ) {
    108                builder.append( "?" );
    109            }
    110            protHttpAddr = builder.append( "action=serviceProtocol&serviceId=" ).append( serviceId ).toString();
    111
    112            this.response = new ValidatorResponse( "Not yet Tested", Status.RESULT_STATE_WAITING );
    113            response.setLastLapse( -1 );
    114        }
    115
    116        /**
    117         * creates an xml protocol file for this Protocol Object if it does not already exist
    118         */
    119        protected Element init()
    120                                throws IOException {
    121
    122            protocolURI = buildProtocolURI();
    123
    124            Element root = null;
    125            try {
    126                File file = new File( protocolURI );
    127
    128                if ( !file.exists() || file.length() == 0 ) {
    129                    root = XMLTools.create().createElement( "owsWatchProtocol" );
    130
    131                    root.setAttribute( "serviceName", serviceName );
    132                    root.setAttribute( "serviceType", serviceType );
    133                    XMLFragment frag = new XMLFragment( root );
    134                    frag.write( new FileWriter( protocolURI ) );
    135                } else {
    136                    XMLFragment xmlFrag = new XMLFragment( new File( protocolURI ) );
    137                    root = xmlFrag.getRootElement();
    138                }
    139            } catch ( Exception e ) {
    140                throw new IOException( Messages.getMessage( "ERROR_CONFS_EXCEPTION", protocolURI )
    141                                       + e.getLocalizedMessage() );
    142            }
    143            return root;
    144        }
    145
    146        /**
    147         * adds a message to protocol file
    148         *
    149         * @param response
    150         * @param serviceconfig
    151         *
    152         */
    153        public void addMessage( ValidatorResponse response, ServiceConfiguration serviceconfig ) {
    154
    155            try {
    156
    157                Element root = init();
    158                if ( root != null ) {
    159
    160                    Element entry = XMLTools.appendElement( root, null, "Entry" );
    161                    XMLTools.appendElement( entry, null, "TimeStamp", response.getLastTest().toString() );
    162                    XMLTools.appendElement( entry, null, "Message", response.getMessage() );
    163                    XMLTools.appendElement( entry, null, "Lapse", String.valueOf( response.getLastLapse() ) );
    164
    165                    XMLFragment frag = new XMLFragment( root );
    166                    FileWriter writer = new FileWriter( new File( protocolURI ) );
    167                    writer.write( frag.getAsPrettyString() );
    168                    writer.close();
    169                } else {
    170                    LOG.logError( "Exception while creating the Protocol" );
    171                }
    172            } catch ( Exception e ) {
    173                LOG.logError( "Error building the xml document in addMessage()", e );
    174            }
    175
    176            if ( ( this.response.getStatus().isAvailable() || this.response.getStatus().isWaiting() )
    177                 && !response.getStatus().isAvailable() && !response.getStatus().isWaiting() ) {
    178                emailSender.createAndSendMail( serviceconfig, response, getProtHttpAddr() );
    179            }
    180            this.response = response;
    181        }
    182
    183        /**
    184         * @return String the uri of this Protocol Object
    185         */
    186        public String getProtocolURI() {
    187            return buildProtocolURI();
    188        }
    189
    190        /**
    191         * @return protocol path
    192         */
    193        public String getProtPath() {
    194            return protocolURI;
    195        }
    196
    197        /**
    198         * @return String
    199         */
    200        public String getProtDirPath() {
    201            return protDirPath;
    202        }
    203
    204        /**
    205         * @param protDirPath
    206         */
    207        public void setProtDirPath( String protDirPath ) {
    208            this.protDirPath = protDirPath;
    209            protocolURI = buildProtocolURI();
    210        }
    211
    212        private String buildProtocolURI() {
    213
    214            if ( protocolURI != null ) {
    215                return protocolURI;
    216            }
    217            // Writing the file name
    218            StringBuffer buffer = new StringBuffer( 100 );
    219            buffer.append( protDirPath );
    220            if ( !protDirPath.endsWith( "/" ) ) {
    221                buffer.append( "/" );
    222            }
    223            buffer.append( "protocol_ID" ).append( "_" ).append( serviceId ).append( "_" );
    224            Calendar cal = Calendar.getInstance();
    225            // I added 1 to the month since the returned month index is always -1 that the true,
    226            // ex. january returns 0, feb 1, etc...
    227            buffer.append( cal.get( Calendar.YEAR ) ).append( "_" );
    228            int month = cal.get( Calendar.MONTH ) + 1;
    229            buffer.append( month > 9 ? month : "0" + month );
    230            buffer.append( ".xml" );
    231
    232            return buffer.toString();
    233        }
    234
    235        /**
    236         * @return http address that forwards you to the protocol page
    237         */
    238        public String getProtHttpAddr() {
    239            return protHttpAddr;
    240        }
    241
    242        /**
    243         * @return {@link EmailSender}
    244         */
    245        public EmailSender getEmailSender() {
    246            return emailSender;
    247        }
    248
    249        /**
    250         * @return {@link EmailSender}
    251         */
    252        public ValidatorResponse getResponse() {
    253            return response;
    254        }
    255    }