forked from ome/openmicroscopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportException.java
216 lines (188 loc) · 7.45 KB
/
ImportException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* org.openmicroscopy.shoola.env.data.ImportException
*
*------------------------------------------------------------------------------
* Copyright (C) 2006-2013 University of Dundee. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*------------------------------------------------------------------------------
*/
package org.openmicroscopy.shoola.env.data;
//Java imports
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import ome.conditions.ResourceError;
import omero.ChecksumValidationException;
//Third-party libraries
import loci.formats.FormatException;
import loci.formats.UnsupportedCompressionException;
//Application-internal dependencies
/**
* Reports an error occurred while importing an image.
*
* @author Jean-Marie Burel
* <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
* @author Donald MacDonald
* <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a>
* @author Blazej Pindelski, bpindelski at dundee.ac.uk
* @version 3.0
* @since 3.0-Beta4
*/
public class ImportException
extends Exception
{
/** Text to indicate that the file, after scanning is not valid. */
public static final String FILE_NOT_VALID_TEXT = "File Not Valid";
/** Text to indicate that the file format is not supported. */
public static final String UNKNOWN_FORMAT_TEXT = "Unknown format";
/** Text to indicate a library is missing. */
public static final String MISSING_LIBRARY_TEXT = "Missing library to "+
"decode the file.";
/** Text to indicate the file is on tape. */
private static final String NETWORK_NAME_TEXT =
"The specified network name is no longer available";
/** Text to indicate the file is on tape. */
private static final String SPACE_TEXT = "No space left on device";
/** Indicates that the compression is not supported.*/
public static int COMPRESSION = 0;
/** Indicates that a library is missing.*/
public static int MISSING_LIBRARY = 1;
/** Indicates that a library is missing.*/
public static int FILE_ON_TAPE = 2;
/** Indicates that there is no space left.*/
public static int NO_SPACE = 3;
/** Indicates that there was a checksum mismatch.*/
public static int CHECKSUM_MISMATCH = 4;
/** Indicates that the file is not valid.*/
public static int NOT_VALID = 5;
/** Indicates that the file format is not supported.*/
public static int UNKNOWN_FORMAT = 6;
/** The status associated to the exception.*/
private int status;
/**
* Returns the message corresponding to the error thrown while importing the
* files.
*
* @param t The exception to handle.
* @return See above.
*/
public static String getImportFailureMessage(Throwable t)
{
String message;
Throwable cause = t.getCause();
if (cause instanceof FormatException) {
message = cause.getMessage();
cause.printStackTrace();
if (message == null) return null;
if (message.contains("ome-xml.jar"))
return "Missing ome-xml.jar required to read OME-TIFF files";
String[] s = message.split(":");
if (s.length > 0) return s[0];
} else if (cause instanceof IOException) {
} else if (cause instanceof omero.ChecksumValidationException) {
return ((omero.ChecksumValidationException) cause).getMessage();
}
return null;
}
/**
* Constructs a new exception with the specified detail message.
*
* @param message Short explanation of the problem.
* @param readerType The type of reader used while trying to import an image.
*/
public ImportException(String message)
{
this(message, null);
if (FILE_NOT_VALID_TEXT.equals(message)) status = NOT_VALID;
else if (UNKNOWN_FORMAT_TEXT.equals(message)) status = UNKNOWN_FORMAT;
else if (MISSING_LIBRARY_TEXT.equals(message))
status = MISSING_LIBRARY;
}
/**
* Constructs a new exception with the specified detail message and cause.
*
* @param message Short explanation of the problem.
* @param cause The exception that caused this one to be risen.
* @param readerType The type of reader used while trying to import an mage.
*/
public ImportException(Throwable cause)
{
this((String) getImportFailureMessage(cause), cause);
}
/**
* Constructs a new exception with the specified detail message and cause.
*
* @param message Short explanation of the problem.
* @param cause The exception that caused this one to be risen.
* @param readerType The type of reader used while trying to import an image.
*/
public ImportException(String message, Throwable cause)
{
super(message, cause);
if (FILE_NOT_VALID_TEXT.equals(message)) status = NOT_VALID;
else if (UNKNOWN_FORMAT_TEXT.equals(message)) status = UNKNOWN_FORMAT;
else if (MISSING_LIBRARY_TEXT.equals(message)) status = MISSING_LIBRARY;
else status = -1;
}
/**
* Returns one of the constant defined by this class.
*
* @return See above.
*/
public int getStatus()
{
Throwable cause = getCause();
if (cause == null) return status;
if (cause instanceof UnsupportedCompressionException) {
return COMPRESSION;
} else if (cause instanceof FormatException) {
String message = cause.getMessage();
if (message.contains(MISSING_LIBRARY_TEXT.toLowerCase()))
return MISSING_LIBRARY;
} else if (cause instanceof IOException) {
String message = cause.getMessage();
if (message.contains(NETWORK_NAME_TEXT))
return FILE_ON_TAPE;
} else if (cause.getCause() instanceof IOException) {
String message = cause.getCause().getMessage();
if (message.contains(NETWORK_NAME_TEXT))
return FILE_ON_TAPE;
} else if (cause instanceof ResourceError) {
String message = cause.getMessage();
if (message.contains(SPACE_TEXT))
return NO_SPACE;
} else if (cause instanceof ChecksumValidationException) {
return CHECKSUM_MISMATCH;
}
return status;
}
/**
* Overridden to return the cause of the problem.
* @see Exception#toString()
*/
public String toString()
{
Throwable cause = getCause();
if (cause != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
cause.printStackTrace(pw);
return sw.toString();
}
return super.toString();
}
}