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.ogcwebservices.csw.manager;
038    
039    import java.io.IOException;
040    import java.net.URL;
041    import java.util.Arrays;
042    import java.util.Collection;
043    import java.util.Collections;
044    import java.util.HashMap;
045    import java.util.Iterator;
046    import java.util.List;
047    import java.util.Map;
048    
049    import org.deegree.framework.log.ILogger;
050    import org.deegree.framework.log.LoggerFactory;
051    import org.deegree.framework.xml.XSLTDocument;
052    import org.deegree.ogcwebservices.MissingParameterValueException;
053    import org.deegree.ogcwebservices.csw.configuration.CatalogueConfiguration;
054    import org.deegree.ogcwebservices.csw.configuration.CatalogueDeegreeParams;
055    import org.deegree.ogcwebservices.csw.manager.HarvestRepository.ResourceType;
056    import org.deegree.ogcwebservices.wfs.WFService;
057    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
058    import org.xml.sax.SAXException;
059    
060    /**
061     * 
062     * 
063     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064     * @author last edited by: $Author: poth $
065     * 
066     * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
067     */
068    abstract class AbstractManager implements Manager {
069    
070        private static final ILogger LOG = LoggerFactory.getLogger( AbstractManager.class );
071    
072        protected static Map<ResourceType, AbstractHarvester> harvester = null;
073    
074        protected XSLTDocument IN_XSL = null;
075    
076        protected XSLTDocument OUT_XSL = null;
077    
078        protected WFService wfsService;
079    
080        /**
081         * starts all known/registered harvester. This method can be used to start harvesting using
082         * requests e.g. if a server has been shutdown and restarted.
083         * 
084         * @param version
085         *            the version of the  CSW
086         */
087        public static void startAllHarvester( String version ) {
088            initHarvester( version );
089            Collection<AbstractHarvester> con = harvester.values();
090            for ( Iterator<AbstractHarvester> iter = con.iterator(); iter.hasNext(); ) {
091                AbstractHarvester h = iter.next();
092                if ( !h.isRunning() ) {
093                    h.startHarvesting();
094                }
095            }
096        }
097    
098        /**
099         * stpos all known/registered harvester. This method can be used to stop harvesting using
100         * requests e.g. if a server has shall be shut down.
101         * 
102         */
103        public static void stopAllHarvester() {
104            if ( harvester != null ) {
105                Collection<AbstractHarvester> con = harvester.values();
106                for ( Iterator<AbstractHarvester> iter = con.iterator(); iter.hasNext(); ) {
107                    AbstractHarvester h = iter.next();
108                    if ( h.isRunning() ) {
109                        LOG.logInfo( "stop harvesting for: " + h.getClass().getName() );
110                        h.stopHarvesting();
111                    }
112                }
113            }
114        }
115    
116        /*
117         * (non-Javadoc)
118         * 
119         * @see org.deegree.ogcwebservices.csw.manager.Manager#getWfsService()
120         */
121        public WFService getWFService() {
122            return wfsService;
123        }
124    
125        /*
126         * (non-Javadoc)
127         * 
128         * @see org.deegree.ogcwebservices.csw.manager.Manager#init(org.deegree.ogcwebservices.wfs.WFService,
129         * org.deegree.ogcwebservices.csw.configuration.CatalogueConfiguration)
130         */
131        public void init( WFService wfsService, CatalogueConfiguration catalogueConfiguration )
132                                throws MissingParameterValueException {
133            this.wfsService = wfsService;
134    
135            // try {
136            CatalogueDeegreeParams cdp = catalogueConfiguration.getDeegreeParams();
137            URL url = cdp.getTransformationInputXSLT().getLinkage().getHref();
138            IN_XSL = new XSLTDocument();
139            try {
140                IN_XSL.load( url );
141            } catch ( IOException e ) {
142                String s = "If a CS-W is defined to handle Transaction and/or Harvest requests, XSLT scripts for request transformations (e.g. mapping the input schema to gml) must be defined in the deegreeParams section of the capabilities document. While trying to read an xslt script from: '"
143                           + url.toString()
144                           + "' (which was defined for the transformation of incoming request), the following error occurred: "
145                           + e.getMessage();
146                LOG.logError( s, e );
147                throw new MissingParameterValueException( getClass().getName(), s );
148            } catch ( SAXException e ) {
149                String s = "The xslt script (transforming incoming requests) read from the location: '" + url
150                           + "' could not be parsed because: " + e.getMessage();
151                LOG.logError( s, e );
152                throw new MissingParameterValueException( getClass().getName(), s );
153            }
154            url = cdp.getTransformationOutputXSLT().getLinkage().getHref();
155            OUT_XSL = new XSLTDocument();
156            try {
157                OUT_XSL.load( url );
158            } catch ( IOException e ) {
159                String s = "If a CS-W is defined to handle Transaction and/or Harvest requests, XSLT scripts for request transformations (e.g. mapping the input schema to gml) must be defined in the deegreeParams section of the capabilities document. While trying to read an xslt script from: '"
160                           + url.toString()
161                           + "' (which was defined for the transformation of the response), the following error occurred: "
162                           + e.getMessage();
163                LOG.logError( s, e );
164                throw new MissingParameterValueException( getClass().getName(), s );
165            } catch ( SAXException e ) {
166                String s = "The xslt script (transforming the response) read from the location: '" + url
167                           + "' could not be parsed because: " + e.getMessage();
168                LOG.logError( s, e );
169                throw new MissingParameterValueException( getClass().getName(), s );
170            }
171            WFSCapabilities capa = wfsService.getCapabilities();
172    
173            List<String> versions = Arrays.asList( catalogueConfiguration.getServiceIdentification().getServiceTypeVersions() );
174            Collections.sort( versions );
175    
176            initHarvester( versions.get( versions.size() - 1 ) );
177    
178            LOG.logInfo( "CSW Manager initialized with WFS resource, wfs version:" + capa.getVersion() );
179    
180        }
181    
182        /**
183         * initializes a static Map containing a harvester for other coatalogues, for services and for
184         * single CSW-profile documents.
185         * 
186         * @param version
187         *            the version of the CSW
188         */
189        static void initHarvester( String version ) {
190            if ( harvester == null ) {
191                harvester = new HashMap<ResourceType, AbstractHarvester>();
192                harvester.put( ResourceType.catalogue, CatalogueHarvester.getInstance( version ) );
193                harvester.put( ResourceType.service, ServiceHarvester.getInstance( version ) );
194                harvester.put( ResourceType.csw_profile, CSWProfileHarvester.getInstance( version ) );
195            }
196        }
197    
198    }