001 /*---------------- FILE HEADER ------------------------------------------
002
003 This file is part of deegree.
004 Copyright (C) 2001-2008 by:
005 EXSE, 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
015 This library is distributed in the hope that it will be useful,
016 but WITHOUT ANY WARRANTY; without even the implied warranty of
017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 Lesser General Public License for more details.
019
020 You should have received a copy of the GNU Lesser General Public
021 License along with this library; if not, write to the Free Software
022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023
024 Contact:
025
026 Andreas Poth
027 lat/lon GmbH
028 Aennchenstr. 19
029 53115 Bonn
030 Germany
031 E-Mail: poth@lat-lon.de
032
033 Prof. Dr. Klaus Greve
034 Department of Geography
035 University of Bonn
036 Meckenheimer Allee 166
037 53115 Bonn
038 Germany
039 E-Mail: greve@giub.uni-bonn.de
040
041
042 ---------------------------------------------------------------------------*/
043 package org.deegree.tools.importer;
044
045 import java.io.BufferedReader;
046 import java.io.IOException;
047 import java.io.InputStream;
048 import java.io.InputStreamReader;
049 import java.util.Iterator;
050 import java.util.List;
051 import java.util.Properties;
052
053 import org.deegree.framework.log.ILogger;
054 import org.deegree.framework.log.LoggerFactory;
055 import org.deegree.framework.util.StringTools;
056
057
058 /**
059 *
060 * This generic importer handles the way of inserting data. First data are loaded from source
061 * container, followed by structural validation, transformation and content validation. Finally the
062 * exporter insert data in target container. The classes the loader needed for this five steps must
063 * be specified in 'classes.properties'.
064 *
065 * @author <a href="mailto:buesching@lat-lon.de">Lyn Buesching</a>
066 * @author last edited by: $Author: buesching $
067 *
068 * @version $Revision: 1.3 $, $Date: 2007-10-30 14:34:54 $
069 *
070 */
071 public class Importer {
072
073 private static final ILogger LOG = LoggerFactory.getLogger( Importer.class );
074
075 private Loader loader;
076
077 private StructValidator structValidator;
078
079 private ContentValidator contentValidator;
080
081 private Transformer transformer;
082
083 private Exporter exporter;
084
085 /**
086 * The constructor reads the classes which should be used during import process from the
087 * property file 'classes.properties' and instantiate them. Make sure that the desired classes
088 * are configured in this file!
089 */
090 public Importer() {
091 this( "classes.properties" );
092 }
093
094 public Importer( String propertiesFile ) {
095
096 LOG.logInfo( Messages.getString( "Importer.BEGIN_INSTANTIATION" ) );
097 // read properties file
098 Properties classes = new Properties();
099 try {
100 InputStream is = Importer.class.getResourceAsStream( propertiesFile );
101 InputStreamReader isr = new InputStreamReader( is );
102 BufferedReader br = new BufferedReader( isr );
103 String line = null;
104 while ( ( line = br.readLine() ) != null ) {
105 if ( !line.trim().startsWith( "#" ) ) {
106 String[] tmp = StringTools.toArray( line.trim(), "=", false );
107 classes.put( tmp[0], tmp[1] );
108 }
109 }
110 // instantiate classes essential for importer
111 String loaderClass = classes.getProperty( "loader" );
112 LOG.logInfo( Messages.getString( "Importer.LOADER_CLASS", loaderClass ) );
113 loader = (Loader) Class.forName( loaderClass ).newInstance();
114
115 String structValidatorClass = classes.getProperty( "structValidator" );
116 LOG.logInfo( Messages.getString( "Importer.STRUCTVALIDATOR_CLASS", structValidatorClass ) );
117 structValidator = (StructValidator) Class.forName( structValidatorClass ).newInstance();
118
119 String transformerClass = classes.getProperty( "transformer" );
120 LOG.logInfo( Messages.getString( "Importer.TRANSFORMER_CLASS", transformerClass ) );
121 transformer = (Transformer) Class.forName( transformerClass ).newInstance();
122
123 String contentValidatorClass = classes.getProperty( "contentValidator" );
124 LOG.logInfo( Messages.getString( "Importer.CONTENTVALIDATOR_CLASS", contentValidatorClass ) );
125 contentValidator = (ContentValidator) Class.forName( contentValidatorClass ).newInstance();
126
127 String exporterClass = classes.getProperty( "exporter" );
128 LOG.logInfo( Messages.getString( "Importer.EXPORTER_CLASS", exporterClass ) );
129 exporter = (Exporter) Class.forName( exporterClass ).newInstance();
130
131 LOG.logInfo( Messages.getString( "Importer.END_INSTANTIATION" ) );
132
133 } catch ( IOException e ) {
134 LOG.logError( Messages.getString( "Importer.ERROR_READING_CLASSES_PROPERTIES", e.getMessage() ) );
135 } catch ( ClassNotFoundException e ) {
136 LOG.logError( Messages.getString( "Importer.ERROR_FIND_CLASSES", e.getMessage() ) );
137 } catch ( InstantiationException e ) {
138 LOG.logError( Messages.getString( "Importer.ERROR_INSTANTIATION", e.getMessage() ) );
139 } catch ( IllegalAccessException e ) {
140 LOG.logError( Messages.getString( "Importer.ERROR_ACCESS", e.getMessage() ) );
141 }
142
143 }
144
145 /**
146 * handles the import
147 *
148 * @param importObjects
149 * List of objects to import
150 */
151 public void handleImport( List<Object> importObjects ) {
152 int numberObjects = importObjects.size();
153 LOG.logInfo( Messages.getString( "Importer.BEGIN_IMPORT", numberObjects ) );
154 long startTimeAll = System.currentTimeMillis();
155 int successCounter = 0;
156 int counter = 1;
157 for ( Iterator iter = importObjects.iterator(); iter.hasNext(); ) {
158 long startTime = System.currentTimeMillis();
159 Object element = (Object) iter.next();
160 Object loadedObject = load( element );
161 if ( loadedObject != null ) {
162 LOG.logInfo( Messages.getString( "Importer.OBJECT_LOADED", counter ) );
163 if ( validateStructure( loadedObject ) ) {
164 Object transformedObject = transform( loadedObject );
165 if ( transformedObject != null ) {
166 LOG.logInfo( Messages.getString( "Importer.OBJECT_TRANSFORMED", counter ) );
167 if ( validateContent( transformedObject ) ) {
168 if ( export( transformedObject ) ) {
169 LOG.logInfo( Messages.getString( "Importer.OBJECT_EXPORTED", counter ) );
170 ++successCounter;
171 } else {
172 LOG.logInfo( Messages.getString( "Importer.ERROR_EXPORT" ) );
173 }
174 }
175 }
176 }
177 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
178 long endTime = System.currentTimeMillis();
179 LOG.logDebug( "Time needed to import one object: ", endTime - startTime );
180 }
181 }
182 LOG.logInfo( Messages.getString( "Importer.IMPORT_ONE_OBJECT_END" ) );
183 counter++;
184 }
185 long endTimeAll = System.currentTimeMillis();
186 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
187 String msg = StringTools.concat( 200, "Time needed to import ", successCounter, " object(s): " );
188 LOG.logDebug( msg, endTimeAll - startTimeAll );
189 }
190 LOG.logInfo( Messages.getString( "Importer.IMPORT_END", successCounter, numberObjects ) );
191 }
192
193 private Object load( Object importObject ) {
194 return loader.loadObject( importObject );
195 }
196
197 private boolean validateStructure( Object importObject ) {
198 return structValidator.validate( importObject );
199 }
200
201 private Object transform( Object importObject ) {
202 return transformer.transform( importObject );
203 }
204
205 private boolean validateContent( Object importObject ) {
206 return contentValidator.validate( importObject );
207 }
208
209 private boolean export( Object importObject ) {
210 return exporter.export( importObject );
211 }
212
213 }