001    //$HeadURL$
002    /*----------------    FILE HEADER  ------------------------------------------
003     This file is part of deegree.
004     Copyright (C) 2001-2008 by:
005     Department of Geography, University of Bonn
006     http://www.giub.uni-bonn.de/deegree/
007     lat/lon GmbH
008     http://www.lat-lon.de
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014     This library is distributed in the hope that it will be useful,
015     but WITHOUT ANY WARRANTY; without even the implied warranty of
016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     Lesser General Public License for more details.
018     You should have received a copy of the GNU Lesser General Public
019     License along with this library; if not, write to the Free Software
020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     Contact:
022    
023     Andreas Poth
024     lat/lon GmbH
025     Aennchenstr. 19
026     53177 Bonn
027     Germany
028     E-Mail: poth@lat-lon.de
029    
030     Prof. Dr. Klaus Greve
031     Department of Geography
032     University of Bonn
033     Meckenheimer Allee 166
034     53115 Bonn
035     Germany
036     E-Mail: greve@giub.uni-bonn.de
037     ---------------------------------------------------------------------------*/
038    
039    
040    package org.deegree.ogcwebservices.csw.manager;
041    
042    import java.io.IOException;
043    import java.net.URL;
044    import java.util.Collection;
045    import java.util.HashMap;
046    import java.util.Iterator;
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        public static void startAllHarvester() {
085            initHarvester();
086            Collection con = harvester.values();
087            for ( Iterator iter = con.iterator(); iter.hasNext(); ) {
088                AbstractHarvester h = (AbstractHarvester) iter.next();
089                if ( !h.isRunning() ) {
090                    h.startHarvesting();
091                }
092            }
093        }
094    
095        /**
096         * stpos all known/registered harvester. This method can be used to stop harvesting using
097         * requests e.g. if a server has shall be shut down.
098         * 
099         */
100        public static void stopAllHarvester() {
101            if ( harvester != null ) {
102                Collection con = harvester.values();
103                for ( Iterator iter = con.iterator(); iter.hasNext(); ) {
104                    AbstractHarvester h = (AbstractHarvester) iter.next();
105                    if ( h.isRunning() ) {
106                        LOG.logInfo( "stop harvesting for: " + h.getClass().getName() );
107                        h.stopHarvesting();
108                    }
109                }
110            }
111        }
112    
113        /* (non-Javadoc)
114         * @see org.deegree.ogcwebservices.csw.manager.Manager#getWfsService()
115         */
116        public WFService getWFService() {
117            return wfsService;
118        }
119    
120        /* (non-Javadoc)
121         * @see org.deegree.ogcwebservices.csw.manager.Manager#init(org.deegree.ogcwebservices.wfs.WFService, org.deegree.ogcwebservices.csw.configuration.CatalogueConfiguration)
122         */
123        public void init( WFService wfsService, CatalogueConfiguration catalogueConfiguration ) throws MissingParameterValueException {
124            this.wfsService = wfsService;
125    
126            // try {
127            CatalogueDeegreeParams cdp = catalogueConfiguration.getDeegreeParams();
128            URL url = cdp.getTransformationInputXSLT().getLinkage().getHref();
129            IN_XSL = new XSLTDocument();
130            try {
131                IN_XSL.load( url );
132            } catch ( IOException e ) {
133                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: '" + url.toString()
134                           + "' (which was defined for the transformation of incoming request), the following error occurred: "
135                           + e.getMessage();
136                LOG.logError( s, e );
137                throw new MissingParameterValueException( getClass().getName(), s );
138            } catch ( SAXException e ) {
139                String s = "The xslt script (transforming incoming requests) read from the location: '" + url
140                           + "' could not be parsed because: "
141                           + e.getMessage();
142                LOG.logError( s, e );
143                throw new MissingParameterValueException( getClass().getName(), s );
144            }
145            url = cdp.getTransformationOutputXSLT().getLinkage().getHref();
146            OUT_XSL = new XSLTDocument();
147            try {
148                OUT_XSL.load( url );
149            } catch ( IOException e ) {
150                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: '" + url.toString()
151                           + "' (which was defined for the transformation of the response), the following error occurred: "
152                           + e.getMessage();
153                LOG.logError( s, e );
154                throw new MissingParameterValueException( getClass().getName(), s );
155            } catch ( SAXException e ) {
156                String s = "The xslt script (transforming the response) read from the location: '" + url
157                           + "' could not be parsed because: "
158                           + e.getMessage();
159                LOG.logError( s, e );
160                throw new MissingParameterValueException( getClass().getName(), s );
161            }
162            WFSCapabilities capa = wfsService.getCapabilities();
163    
164            initHarvester();
165    
166            LOG.logInfo( "CSW Manager initialized with WFS resource, wfs version:" + capa.getVersion() );
167            
168        }
169    
170       
171        /**
172         * initializes a static Map containing a harvester for other coatalogues, for services and for single CSW-profile
173         * documents.
174         */
175        static void initHarvester() {
176            if ( harvester == null ) {
177                harvester = new HashMap<ResourceType, AbstractHarvester>();
178                harvester.put( ResourceType.catalogue, CatalogueHarvester.getInstance() );
179                harvester.put( ResourceType.service, ServiceHarvester.getInstance() );
180                harvester.put( ResourceType.csw_profile, CSWProfileHarvester.getInstance() );
181            }
182        }
183    
184        
185        
186    }