View Javadoc

1   package com.atlassian.jira.imports;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.io.FileReader;
6   import java.io.InputStream;
7   import java.io.InputStreamReader;
8   import java.io.Reader;
9   
10  import org.apache.log4j.Category;
11  
12  import com.atlassian.core.util.ClassLoaderUtils;
13  
14  /**
15   * Abstract Class for a generic ImportManager.
16   * @created 27 Juillet 2006
17   * @version 1.0
18   */
19  public abstract class AImportManager implements IImportManager {
20  
21  	/**
22  	 * Logger permettant aux Class héritant de AImportManager de tracer certains
23  	 * évenements
24  	 */
25  	protected static Category log = Category.getInstance(AImportManager.class);
26  
27  	/** Fichier d'import */
28  	protected File file;
29  
30  	/** Uri permettant de chercher le fichier d'import */
31  	protected String uri;
32  
33  	/** Reader permettant de lire le flux de caractere du fichier d'import */
34  	private Reader reader;
35  
36  	public File getFile() {
37  		return this.file;
38  	}
39  
40  	public String getUri() {
41  		return this.uri;
42  	}
43  
44  	public void setFile(File _file) {
45  		this.file = _file;
46  	}
47  
48  	public void setUri(String _uri) {
49  		this.uri = _uri;
50  	}
51  
52  	/**
53  	 * Execution de la lecture du fichier en passant par quelques verifications
54  	 * de validités concernant le fichier d'import.
55  	 */
56  	public void doImport() throws Exception {
57  
58  		if (file == null && uri == null) {
59  			throw new Exception(
60  					"This object must have at least a propertie 'file' or 'uri' specified");
61  		}
62  
63  		if (file != null) {
64  			if (!file.exists() || !file.canRead()) {
65  				throw new Exception("The file: " + file
66  						+ " does not exist or cannot be read");
67  			}
68  
69  			try {
70  				reader = new FileReader(file);
71  			} catch (FileNotFoundException e) {
72  				throw new Exception("could not find the file", e);
73  			}
74  		} else {
75  			InputStream in = ClassLoaderUtils.getResourceAsStream(uri, this
76  					.getClass());
77  			if (in == null) {
78  				throw new Exception("Could not find uri: " + uri);
79  			}
80  
81  			reader = new InputStreamReader(in);
82  		}
83  
84  		log.info(super.getClass().getName() + " - Starting import with '"
85  				+ file.getName() + "' file ...");
86  		importWorker(reader);
87  		log.info(super.getClass().getName() + " - Import terminated");
88  		reader.close();
89  	};
90  
91  	/**
92  	 * Worker d'import : doit permettre au developpeur de parser le reader et
93  	 * d'effectuer les traitements nescessaires
94  	 */
95  	protected abstract void importWorker(Reader _reader);
96  
97  	/** Retourne une instance de type String */
98  	protected String getString(Object _obj) {
99  		if (_obj == null)
100 			return "";
101 		return String.valueOf(_obj);
102 	}
103 }