001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wps/configuration/WPSConfiguration.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     * asdfasd
038     */
039    package org.deegree.ogcwebservices.wps.configuration;
040    
041    import java.io.File;
042    import java.io.IOException;
043    import java.lang.reflect.Constructor;
044    import java.lang.reflect.InvocationTargetException;
045    import java.net.URL;
046    import java.util.ArrayList;
047    import java.util.HashMap;
048    import java.util.Iterator;
049    import java.util.List;
050    import java.util.Map;
051    
052    import org.deegree.framework.log.ILogger;
053    import org.deegree.framework.log.LoggerFactory;
054    import org.deegree.framework.util.ConvenienceFileFilter;
055    import org.deegree.framework.xml.InvalidConfigurationException;
056    import org.deegree.framework.xml.XMLParsingException;
057    import org.deegree.ogcwebservices.getcapabilities.Contents;
058    import org.deegree.ogcwebservices.getcapabilities.OperationsMetadata;
059    import org.deegree.ogcwebservices.getcapabilities.ServiceIdentification;
060    import org.deegree.ogcwebservices.getcapabilities.ServiceProvider;
061    import org.deegree.ogcwebservices.wps.ProcessBrief;
062    import org.deegree.ogcwebservices.wps.capabilities.ProcessOfferings;
063    import org.deegree.ogcwebservices.wps.capabilities.WPSCapabilities;
064    import org.deegree.ogcwebservices.wps.describeprocess.ProcessDescription;
065    import org.deegree.ogcwebservices.wps.describeprocess.ProcessDescriptionDocument;
066    import org.deegree.ogcwebservices.wps.execute.Process;
067    import org.xml.sax.SAXException;
068    
069    /**
070     * WPSConfiguration.java
071     *
072     * Created on 08.03.2006. 17:58:55h
073     *
074     * @author <a href="mailto:christian@kiehle.org">Christian Kiehle</a>
075     * @author <a href="mailto:christian.heier@gmx.de">Christian Heier</a>
076     * @author last edited by: $Author: mschneider $
077     *
078     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
079     */
080    public class WPSConfiguration extends WPSCapabilities {
081    
082        private static final ILogger LOG = LoggerFactory.getLogger( WPSConfiguration.class );
083    
084        private WPSDeegreeParams deegreeParams = null;
085    
086        private Map<String, Process> registeredProcesses;
087    
088        private static final String SPEC_PROC_DIR = "Specified process directory '";
089    
090        /**
091         * Constructor initializes WPSConfiguration from passed parameters
092         *
093         * @param version
094         * @param updateSequence
095         * @param serviceIdentification
096         * @param serviceProvider
097         * @param operationsMetadata
098         * @param contents
099         * @throws InvalidConfigurationException
100         */
101        protected WPSConfiguration( String version, String updateSequence, ServiceIdentification serviceIdentification,
102                                    ServiceProvider serviceProvider, OperationsMetadata operationsMetadata,
103                                    Contents contents, WPSDeegreeParams wpsDeegreeParams )
104                                throws InvalidConfigurationException {
105            super( version, updateSequence, serviceIdentification, serviceProvider, operationsMetadata, contents );
106            this.deegreeParams = wpsDeegreeParams;
107            try {
108                loadProcessConfigs();
109                setConfiguredProcessOfferings();
110            } catch ( InvalidConfigurationException e ) {
111                LOG.logError( e.getMessage(), e );
112                throw e;
113            }
114        }
115    
116        private void setConfiguredProcessOfferings() {
117    
118            List<ProcessBrief> processBriefList = new ArrayList<ProcessBrief>();
119    
120            Iterator<Process> registeredProcessesValuesIterator = registeredProcesses.values().iterator();
121    
122            while ( registeredProcessesValuesIterator.hasNext() ) {
123                Process process = registeredProcessesValuesIterator.next();
124    
125                ProcessDescription processDescription = process.getProcessDescription();
126    
127                processBriefList.add( new ProcessBrief( processDescription.getIdentifier(), processDescription.getTitle(),
128                                                        processDescription.getAbstract(),
129                                                        processDescription.getProcessVersion(),
130                                                        processDescription.getMetadata() ) );
131            }
132    
133            ProcessOfferings processOfferings = new ProcessOfferings( processBriefList );
134    
135            super.setProcessOfferings( processOfferings );
136        }
137    
138        /**
139         * @return Returns the registeredProcesses.
140         */
141        public Map<String, Process> getRegisteredProcesses() {
142            return registeredProcesses;
143        }
144    
145        /**
146         * loads and initializes processes configured in the process directory.
147         *
148         * @throws InvalidConfigurationException
149         */
150        private void loadProcessConfigs()
151                                throws InvalidConfigurationException {
152            this.registeredProcesses = scanForRegisteredProcesses();
153    
154        }
155    
156        /**
157         * Scans for process configuration documents located in process directory of current WPS configuration.
158         *
159         * @return Map<String, Process>
160         * @throws InvalidConfigurationException
161         */
162        private Map<String, Process> scanForRegisteredProcesses()
163                                throws InvalidConfigurationException {
164            List<String> fileNameList = new ArrayList<String>();
165            String[] processDirectories = getDeegreeParams().getProcessDirectories();
166    
167            for ( int i = 0; i < processDirectories.length; i++ ) {
168                File file = new File( processDirectories[i] );
169                LOG.logInfo( "Directory '" + file.getAbsolutePath() + "' will be scanned for process configuration files." );
170                String[] list = file.list( new ConvenienceFileFilter( false, "XML" ) );
171                if ( list != null ) {
172                    if ( list.length == 0 ) {
173                        String msg = SPEC_PROC_DIR + processDirectories[i] + "' does not contain any '.xml' files.";
174                        LOG.logError( msg );
175                        throw new InvalidConfigurationException( msg );
176                    }
177                    for ( int j = 0; j < list.length; j++ ) {
178                        fileNameList.add( processDirectories[i] + '/' + list[j] );
179                    }
180                } else {
181                    String msg = SPEC_PROC_DIR + processDirectories[i] + "' does not denote a directory.";
182                    LOG.logError( msg );
183                    throw new InvalidConfigurationException( msg );
184                }
185            }
186            String[] fileNames = fileNameList.toArray( new String[fileNameList.size()] );
187            return extractProcessDescriptions( fileNames );
188        }
189    
190        /**
191         * Extracts a <code>ProcessDescription</code> for each provided files in <code>String[]</code> fileNames.
192         *
193         * @param fileNames
194         * @return
195         * @throws InvalidConfigurationException
196         */
197        private Map<String, Process> extractProcessDescriptions( String[] fileNames )
198                                throws InvalidConfigurationException {
199            int size = fileNames.length;
200            Map<String, Process> processMap = new HashMap<String, Process>( size );
201            for ( int i = 0; i < size; i++ ) {
202                LOG.logInfo( "Parsing process configuration file: '" + fileNames[i] + "'." );
203                Process process = null;
204                try {
205                    URL fileURL = new File( fileNames[i] ).toURL();
206                    ProcessDescriptionDocument processDescriptionDocument = new ProcessDescriptionDocument();
207                    processDescriptionDocument.load( fileURL );
208                    ProcessDescription processDescription = processDescriptionDocument.parseProcessDescription();
209    
210                    String className = processDescription.getResponsibleClass();
211                    try {
212                        Class<?> processClass = Class.forName( className );
213                        Constructor<?> con = processClass.getConstructor( ProcessDescription.class );
214                        process = (Process) con.newInstance( processDescription );
215                    } catch ( ClassNotFoundException cnfEx ) {
216                        String msg = "Responsible class for process execution: '" + className + "' not found.";
217                        // LOG.logError( msg, cnfEx );
218                        throw new XMLParsingException( msg, cnfEx );
219                    } catch ( NoSuchMethodException nsmEx ) {
220                        String msg = "Responsible class for process execution: '" + className
221                                     + "' can not be instantiated.";
222                        // LOG.logError( msg, nsmEx );
223                        throw new XMLParsingException( msg, nsmEx );
224                    } catch ( InstantiationException iEx ) {
225                        String msg = "Responsible class for process execution: '" + className
226                                     + "' can not be instantiated.";
227                        // LOG.logError( msg, iEx );
228                        throw new XMLParsingException( msg, iEx );
229                    } catch ( InvocationTargetException itEx ) {
230                        String msg = "Responsible class for process execution: '" + className
231                                     + "' can not be instantiated.";
232                        // LOG.logError( msg, itEx );
233                        throw new XMLParsingException( msg, itEx );
234                    } catch ( IllegalAccessException iaEx ) {
235                        String msg = "Responsible class for process execution: '" + className
236                                     + "' can not be instantiated.";
237                        // LOG.logError( msg, iaEx );
238                        throw new XMLParsingException( msg, iaEx );
239                    }
240    
241                    String processKey = processDescription.getIdentifier().getCode().toUpperCase();
242                    if ( !processMap.containsKey( processKey ) ) {
243                        processMap.put( processKey, process );
244                        LOG.logDebug( "Process '" + processKey + "' registered to server." );
245                    } else {
246                        String msg = "Multiple definition of process '" + processKey + "' not allowed! Process '"
247                                     + processKey + "' is already defined.";
248                        LOG.logError( msg );
249                        throw new InvalidConfigurationException( msg );
250                    }
251    
252                } catch ( IOException ioe ) {
253                    String msg = "Error loading '" + fileNames[i] + "': " + ioe.getMessage();
254                    LOG.logError( msg );
255                    throw new InvalidConfigurationException( msg, ioe );
256                } catch ( XMLParsingException e ) {
257                    String msg = "Error parsing '" + fileNames[i] + "': " + e.getMessage();
258                    LOG.logError( msg, e );
259                    throw new InvalidConfigurationException( msg, e );
260                } catch ( SAXException e ) {
261                    String msg = "Error parsing '" + fileNames[i] + "': " + e.getMessage();
262                    LOG.logError( msg, e );
263                    throw new InvalidConfigurationException( msg, e );
264                }
265    
266            }
267    
268            return processMap;
269        }
270    
271        /**
272         * @return Returns the deegreeParams.
273         */
274        public WPSDeegreeParams getDeegreeParams() {
275            return deegreeParams;
276        }
277    
278        /**
279         * @param deegreeParams
280         *            The deegreeParams to set.
281         */
282        public void setDeegreeParams( WPSDeegreeParams deegreeParams ) {
283            this.deegreeParams = deegreeParams;
284        }
285    
286        /**
287         * Returns a {@link WPSConfiguration} created from the configuration document that is loaded from the given URL.
288         *
289         * @param url
290         * @return {@link WPSConfiguration} created from the specified configuration document
291         * @throws IOException
292         * @throws SAXException
293         * @throws InvalidConfigurationException
294         */
295        public static WPSConfiguration createConfiguration( URL url )
296                                throws IOException, SAXException, InvalidConfigurationException {
297            WPSConfigurationDocument confDoc = new WPSConfigurationDocument();
298            confDoc.load( url );
299            WPSConfiguration configuration = confDoc.getConfiguration();
300    
301            return configuration;
302        }
303    
304    }