|
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$'
|
|
7 |
* '$Date$'
|
|
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.cn.batch.utils.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.ObjectFormat;
|
|
38 |
import org.dataone.service.types.ObjectFormatIdentifier;
|
|
39 |
import org.dataone.service.types.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 |
private 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 InvalidRequest, 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 InvalidRequest,
|
|
133 |
ServiceFailure, NotFound, InsufficientResources, NotImplemented {
|
|
134 |
|
|
135 |
try {
|
|
136 |
objectFormatList = getCachedList();
|
|
137 |
|
|
138 |
} catch (ServiceFailure sfe) {
|
|
139 |
throw sfe;
|
|
140 |
|
|
141 |
} catch (NotFound nfe) {
|
|
142 |
throw nfe;
|
|
143 |
|
|
144 |
}
|
|
145 |
|
|
146 |
return objectFormatList;
|
|
147 |
}
|
|
148 |
|
|
149 |
/*
|
|
150 |
* Return the hash containing the fmtid and format mapping
|
|
151 |
*
|
|
152 |
* @return objectFormatMap - the hash of fmtid/format pairs
|
|
153 |
*/
|
|
154 |
private HashMap<String, ObjectFormat> getObjectFormatMap() {
|
|
155 |
|
|
156 |
if (objectFormatMap == null) {
|
|
157 |
objectFormatMap = new HashMap<String, ObjectFormat>();
|
|
158 |
|
|
159 |
}
|
|
160 |
return objectFormatMap;
|
|
161 |
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Get the object format list cached in Metacat
|
|
166 |
*/
|
|
167 |
private ObjectFormatList getCachedList() throws NotFound, ServiceFailure {
|
|
168 |
|
|
169 |
ObjectFormatList objectFormatList = null;
|
|
170 |
|
|
171 |
try {
|
|
172 |
|
|
173 |
// reset the accession number separator in case it is
|
|
174 |
// different than the default
|
|
175 |
try {
|
|
176 |
|
|
177 |
separator = PropertyService
|
|
178 |
.getProperty("document.accNumSeparator");
|
|
179 |
|
|
180 |
} catch (PropertyNotFoundException pnfe) {
|
|
181 |
|
|
182 |
// use the default separator, but log the issue
|
|
183 |
logMetacat.debug("There was a problem finding the document "
|
|
184 |
+ "separator property. The error message was: "
|
|
185 |
+ pnfe.getMessage());
|
|
186 |
}
|
|
187 |
|
|
188 |
// get the latest accession number if it is in Metacat
|
|
189 |
this.rev = DBUtil
|
|
190 |
.getLatestRevisionInDocumentTable(this.OBJECT_FORMAT_DOCID);
|
|
191 |
|
|
192 |
if (this.rev != -1) {
|
|
193 |
this.accNumber = this.OBJECT_FORMAT_DOCID + this.separator
|
|
194 |
+ this.rev;
|
|
195 |
DocumentImpl objectFormatsDocument = new DocumentImpl(
|
|
196 |
accNumber, false);
|
|
197 |
ByteArrayInputStream bais = new ByteArrayInputStream(
|
|
198 |
objectFormatsDocument.toString().getBytes());
|
|
199 |
|
|
200 |
// deserialize the object format list
|
|
201 |
try {
|
|
202 |
|
|
203 |
objectFormatList = TypeMarshaller.unmarshalTypeFromStream(
|
|
204 |
ObjectFormatList.class, bais);
|
|
205 |
|
|
206 |
} catch (IOException e) {
|
|
207 |
throw new ServiceFailure("4841",
|
|
208 |
"Unexpected exception from the service - "
|
|
209 |
+ e.getClass() + ": " + e.getMessage());
|
|
210 |
|
|
211 |
} catch (InstantiationException e) {
|
|
212 |
throw new ServiceFailure("4841",
|
|
213 |
"Unexpected exception from the service - "
|
|
214 |
+ e.getClass() + ": " + e.getMessage());
|
|
215 |
|
|
216 |
} catch (IllegalAccessException e) {
|
|
217 |
throw new ServiceFailure("4841",
|
|
218 |
"Unexpected exception from the service - "
|
|
219 |
+ e.getClass() + ": " + e.getMessage());
|
|
220 |
|
|
221 |
} catch (JiBXException e) {
|
|
222 |
throw new ServiceFailure("4841",
|
|
223 |
"Unexpected exception from the service - "
|
|
224 |
+ e.getClass() + ": " + e.getMessage());
|
|
225 |
|
|
226 |
}
|
|
227 |
|
|
228 |
} else {
|
|
229 |
throw new NotFound("4843",
|
|
230 |
"The object formats collection could not "
|
|
231 |
+ "be found at this node.");
|
|
232 |
}
|
|
233 |
|
|
234 |
} catch (SQLException sqle) {
|
|
235 |
throw new ServiceFailure("4841",
|
|
236 |
"Unexpected exception from the service - "
|
|
237 |
+ sqle.getClass() + ": " + sqle.getMessage());
|
|
238 |
|
|
239 |
} catch (McdbException mcdbe) {
|
|
240 |
throw new ServiceFailure("4841",
|
|
241 |
"Unexpected exception from the service - "
|
|
242 |
+ mcdbe.getClass() + ": " + mcdbe.getMessage());
|
|
243 |
|
|
244 |
}
|
|
245 |
|
|
246 |
// index the object format list based on the format identifier string
|
|
247 |
int listSize = objectFormatList.sizeObjectFormats();
|
|
248 |
|
|
249 |
for (int i = 0; i < listSize; i++) {
|
|
250 |
|
|
251 |
ObjectFormat objectFormat = objectFormatList.getObjectFormat(i);
|
|
252 |
String identifier = objectFormat.getFmtid().getValue();
|
|
253 |
getObjectFormatMap().put(identifier, objectFormat);
|
|
254 |
|
|
255 |
}
|
|
256 |
|
|
257 |
return objectFormatList;
|
|
258 |
|
|
259 |
}
|
|
260 |
|
|
261 |
}
|
0 |
262 |
|
implement object format methods - using a separate class to do the actual metacat lookup/caching so that teh CN implementation looks cleaner