1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: leinfelder $'
|
7
|
* '$Date: 2011-11-08 15:49:41 -0800 (Tue, 08 Nov 2011) $'
|
8
|
*
|
9
|
* This program is free software; you can redistribute it and/or modify
|
10
|
* it under the terms of the GNU General Public License as published by
|
11
|
* the Free Software Foundation; either version 2 of the License, or
|
12
|
* (at your option) any later version.
|
13
|
*
|
14
|
* This program is distributed in the hope that it will be useful,
|
15
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
* GNU General Public License for more details.
|
18
|
*
|
19
|
* You should have received a copy of the GNU General Public License
|
20
|
* along with this program; if not, write to the Free Software
|
21
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
22
|
*/
|
23
|
package edu.ucsb.nceas.metacat.dataone;
|
24
|
|
25
|
import java.io.ByteArrayInputStream;
|
26
|
import java.io.IOException;
|
27
|
import java.sql.SQLException;
|
28
|
import java.util.HashMap;
|
29
|
|
30
|
import org.apache.log4j.Logger;
|
31
|
import org.dataone.service.util.TypeMarshaller;
|
32
|
import org.dataone.service.exceptions.InsufficientResources;
|
33
|
import org.dataone.service.exceptions.InvalidRequest;
|
34
|
import org.dataone.service.exceptions.NotFound;
|
35
|
import org.dataone.service.exceptions.NotImplemented;
|
36
|
import org.dataone.service.exceptions.ServiceFailure;
|
37
|
import org.dataone.service.types.v1.ObjectFormat;
|
38
|
import org.dataone.service.types.v1.ObjectFormatIdentifier;
|
39
|
import org.dataone.service.types.v1.ObjectFormatList;
|
40
|
import org.jibx.runtime.JiBXException;
|
41
|
|
42
|
import edu.ucsb.nceas.metacat.DBUtil;
|
43
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
44
|
import edu.ucsb.nceas.metacat.McdbException;
|
45
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
46
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
47
|
|
48
|
/**
|
49
|
*
|
50
|
* Implements a subset of the DataONE CNCore services in Metacat.
|
51
|
*
|
52
|
* @author jones, leinfelder
|
53
|
*/
|
54
|
public class ObjectFormatService {
|
55
|
|
56
|
private Logger logMetacat = Logger.getLogger(ObjectFormatService.class);
|
57
|
|
58
|
/* The scope of the object formats docid used as the metacat identifier */
|
59
|
public static final String OBJECT_FORMAT_DOCID = "OBJECT_FORMAT_LIST.1";
|
60
|
|
61
|
/* The revision of the object formats document */
|
62
|
private int rev;
|
63
|
|
64
|
/* The separator of the object formats document */
|
65
|
private String separator = ".";
|
66
|
|
67
|
/* The accession number of the object formats document */
|
68
|
private String accNumber = null;
|
69
|
|
70
|
/* The list of object formats */
|
71
|
private ObjectFormatList objectFormatList = null;
|
72
|
|
73
|
/* the searchable map of object formats */
|
74
|
private static HashMap<String, ObjectFormat> objectFormatMap;
|
75
|
|
76
|
private static ObjectFormatService instance = null;
|
77
|
|
78
|
public static ObjectFormatService getInstance() {
|
79
|
if (instance == null) {
|
80
|
instance = new ObjectFormatService();
|
81
|
}
|
82
|
return instance;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Constructor, private for singleton access
|
87
|
*/
|
88
|
private ObjectFormatService() {
|
89
|
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Return the object format based on the given object format identifier
|
94
|
*
|
95
|
* @param fmtid
|
96
|
* - the object format identifier to look up
|
97
|
* @return objectFormat - the desired object format
|
98
|
*/
|
99
|
public ObjectFormat getFormat(ObjectFormatIdentifier fmtid)
|
100
|
throws ServiceFailure, NotFound,
|
101
|
InsufficientResources, NotImplemented {
|
102
|
|
103
|
logMetacat.debug("CNCoreImpl.getFormat() called.");
|
104
|
|
105
|
ObjectFormat objectFormat = null;
|
106
|
|
107
|
// look up the format in the object format map
|
108
|
objectFormat = getObjectFormatMap().get(fmtid.getValue());
|
109
|
|
110
|
// refresh the object format list if the format is null
|
111
|
if (objectFormat == null) {
|
112
|
getCachedList();
|
113
|
objectFormat = getObjectFormatMap().get(fmtid.getValue());
|
114
|
|
115
|
// the object format isn't registered
|
116
|
if (objectFormat == null) {
|
117
|
throw new NotFound("4848", "The format specified by "
|
118
|
+ fmtid.getValue() + " does not exist at this node.");
|
119
|
|
120
|
}
|
121
|
|
122
|
}
|
123
|
|
124
|
return objectFormat;
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Return the list of object formats registered from the Coordinating Node.
|
129
|
*
|
130
|
* @return objectFormatList - the list of object formats
|
131
|
*/
|
132
|
public ObjectFormatList listFormats() throws ServiceFailure, InsufficientResources, NotImplemented {
|
133
|
|
134
|
objectFormatList = getCachedList();
|
135
|
|
136
|
return objectFormatList;
|
137
|
}
|
138
|
|
139
|
/*
|
140
|
* Return the hash containing the fmtid and format mapping
|
141
|
*
|
142
|
* @return objectFormatMap - the hash of fmtid/format pairs
|
143
|
*/
|
144
|
private HashMap<String, ObjectFormat> getObjectFormatMap() {
|
145
|
|
146
|
if (objectFormatMap == null) {
|
147
|
objectFormatMap = new HashMap<String, ObjectFormat>();
|
148
|
|
149
|
}
|
150
|
return objectFormatMap;
|
151
|
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* Get the object format list cached in Metacat
|
156
|
*/
|
157
|
private ObjectFormatList getCachedList() throws ServiceFailure {
|
158
|
|
159
|
ObjectFormatList objectFormatList = null;
|
160
|
|
161
|
try {
|
162
|
|
163
|
// reset the accession number separator in case it is
|
164
|
// different than the default
|
165
|
try {
|
166
|
|
167
|
separator = PropertyService
|
168
|
.getProperty("document.accNumSeparator");
|
169
|
|
170
|
} catch (PropertyNotFoundException pnfe) {
|
171
|
|
172
|
// use the default separator, but log the issue
|
173
|
logMetacat.debug("There was a problem finding the document "
|
174
|
+ "separator property. The error message was: "
|
175
|
+ pnfe.getMessage());
|
176
|
}
|
177
|
|
178
|
// get the latest accession number if it is in Metacat
|
179
|
this.rev = DBUtil
|
180
|
.getLatestRevisionInDocumentTable(this.OBJECT_FORMAT_DOCID);
|
181
|
|
182
|
if (this.rev != -1) {
|
183
|
this.accNumber = this.OBJECT_FORMAT_DOCID + this.separator
|
184
|
+ this.rev;
|
185
|
DocumentImpl objectFormatsDocument = new DocumentImpl(
|
186
|
accNumber, false);
|
187
|
ByteArrayInputStream bais = new ByteArrayInputStream(
|
188
|
objectFormatsDocument.getBytes());
|
189
|
|
190
|
// deserialize the object format list
|
191
|
try {
|
192
|
|
193
|
objectFormatList = TypeMarshaller.unmarshalTypeFromStream(
|
194
|
ObjectFormatList.class, bais);
|
195
|
|
196
|
} catch (IOException e) {
|
197
|
throw new ServiceFailure("4841",
|
198
|
"Unexpected exception from the service - "
|
199
|
+ e.getClass() + ": " + e.getMessage());
|
200
|
|
201
|
} catch (InstantiationException e) {
|
202
|
throw new ServiceFailure("4841",
|
203
|
"Unexpected exception from the service - "
|
204
|
+ e.getClass() + ": " + e.getMessage());
|
205
|
|
206
|
} catch (IllegalAccessException e) {
|
207
|
throw new ServiceFailure("4841",
|
208
|
"Unexpected exception from the service - "
|
209
|
+ e.getClass() + ": " + e.getMessage());
|
210
|
|
211
|
} catch (JiBXException e) {
|
212
|
throw new ServiceFailure("4841",
|
213
|
"Unexpected exception from the service - "
|
214
|
+ e.getClass() + ": " + e.getMessage());
|
215
|
|
216
|
}
|
217
|
|
218
|
} else {
|
219
|
throw new ServiceFailure("4841",
|
220
|
"The object formats collection could not "
|
221
|
+ "be found at this node.");
|
222
|
}
|
223
|
|
224
|
} catch (SQLException sqle) {
|
225
|
throw new ServiceFailure("4841",
|
226
|
"Unexpected exception from the service - "
|
227
|
+ sqle.getClass() + ": " + sqle.getMessage());
|
228
|
|
229
|
} catch (McdbException mcdbe) {
|
230
|
throw new ServiceFailure("4841",
|
231
|
"Unexpected exception from the service - "
|
232
|
+ mcdbe.getClass() + ": " + mcdbe.getMessage());
|
233
|
|
234
|
}
|
235
|
|
236
|
// index the object format list based on the format identifier string
|
237
|
int listSize = objectFormatList.sizeObjectFormatList();
|
238
|
|
239
|
for (int i = 0; i < listSize; i++) {
|
240
|
|
241
|
ObjectFormat objectFormat = objectFormatList.getObjectFormat(i);
|
242
|
String identifier = objectFormat.getFormatId().getValue();
|
243
|
getObjectFormatMap().put(identifier, objectFormat);
|
244
|
|
245
|
}
|
246
|
|
247
|
return objectFormatList;
|
248
|
|
249
|
}
|
250
|
|
251
|
}
|