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