Project

General

Profile

1 6187 leinfelder
/**
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 6366 leinfelder
import org.dataone.service.util.TypeMarshaller;
32 6187 leinfelder
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 6366 leinfelder
import org.dataone.service.types.v1.ObjectFormat;
38
import org.dataone.service.types.v1.ObjectFormatIdentifier;
39
import org.dataone.service.types.v1.ObjectFormatList;
40 6187 leinfelder
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 6306 leinfelder
	public static final String OBJECT_FORMAT_DOCID = "OBJECT_FORMAT_LIST.1";
60 6187 leinfelder
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 6803 leinfelder
			throws ServiceFailure, NotFound, NotImplemented {
101 6187 leinfelder
102
		logMetacat.debug("CNCoreImpl.getFormat() called.");
103
104
		ObjectFormat objectFormat = null;
105
106
		// look up the format in the object format map
107
		objectFormat = getObjectFormatMap().get(fmtid.getValue());
108
109
		// refresh the object format list if the format is null
110
		if (objectFormat == null) {
111
			getCachedList();
112
			objectFormat = getObjectFormatMap().get(fmtid.getValue());
113
114
			// the object format isn't registered
115
			if (objectFormat == null) {
116
				throw new NotFound("4848", "The format specified by "
117
						+ fmtid.getValue() + " does not exist at this node.");
118
119
			}
120
121
		}
122
123
		return objectFormat;
124
	}
125
126
	/**
127
	 * Return the list of object formats registered from the Coordinating Node.
128
	 *
129
	 * @return objectFormatList - the list of object formats
130
	 */
131 6803 leinfelder
	public ObjectFormatList listFormats() throws ServiceFailure, NotImplemented {
132 6187 leinfelder
133 6304 leinfelder
		objectFormatList = getCachedList();
134 6187 leinfelder
135
		return objectFormatList;
136
	}
137
138
	/*
139
	 * Return the hash containing the fmtid and format mapping
140
	 *
141
	 * @return objectFormatMap - the hash of fmtid/format pairs
142
	 */
143
	private HashMap<String, ObjectFormat> getObjectFormatMap() {
144
145
		if (objectFormatMap == null) {
146
			objectFormatMap = new HashMap<String, ObjectFormat>();
147
148
		}
149
		return objectFormatMap;
150
151
	}
152
153
	/**
154
	 * Get the object format list cached in Metacat
155
	 */
156 6622 leinfelder
	private ObjectFormatList getCachedList() throws ServiceFailure {
157 6187 leinfelder
158
		ObjectFormatList objectFormatList = null;
159
160
		try {
161
162
			// reset the accession number separator in case it is
163
			// different than the default
164
			try {
165
166
				separator = PropertyService
167
						.getProperty("document.accNumSeparator");
168
169
			} catch (PropertyNotFoundException pnfe) {
170
171
				// use the default separator, but log the issue
172
				logMetacat.debug("There was a problem finding the document "
173
						+ "separator property. The error message was: "
174
						+ pnfe.getMessage());
175
			}
176
177
			// get the latest accession number if it is in Metacat
178
			this.rev = DBUtil
179
					.getLatestRevisionInDocumentTable(this.OBJECT_FORMAT_DOCID);
180
181
			if (this.rev != -1) {
182
				this.accNumber = this.OBJECT_FORMAT_DOCID + this.separator
183
						+ this.rev;
184
				DocumentImpl objectFormatsDocument = new DocumentImpl(
185
						accNumber, false);
186
				ByteArrayInputStream bais = new ByteArrayInputStream(
187 6304 leinfelder
						objectFormatsDocument.getBytes());
188 6187 leinfelder
189
				// deserialize the object format list
190
				try {
191
192
					objectFormatList = TypeMarshaller.unmarshalTypeFromStream(
193
							ObjectFormatList.class, bais);
194
195
				} catch (IOException e) {
196
					throw new ServiceFailure("4841",
197
							"Unexpected exception from the service - "
198
									+ e.getClass() + ": " + e.getMessage());
199
200
				} catch (InstantiationException e) {
201
					throw new ServiceFailure("4841",
202
							"Unexpected exception from the service - "
203
									+ e.getClass() + ": " + e.getMessage());
204
205
				} catch (IllegalAccessException e) {
206
					throw new ServiceFailure("4841",
207
							"Unexpected exception from the service - "
208
									+ e.getClass() + ": " + e.getMessage());
209
210
				} catch (JiBXException e) {
211
					throw new ServiceFailure("4841",
212
							"Unexpected exception from the service - "
213
									+ e.getClass() + ": " + e.getMessage());
214
215
				}
216
217
			} else {
218 6622 leinfelder
				throw new ServiceFailure("4841",
219 6187 leinfelder
						"The object formats collection could not "
220
								+ "be found at this node.");
221
			}
222
223
		} catch (SQLException sqle) {
224
			throw new ServiceFailure("4841",
225
					"Unexpected exception from the service - "
226
							+ sqle.getClass() + ": " + sqle.getMessage());
227
228
		} catch (McdbException mcdbe) {
229
			throw new ServiceFailure("4841",
230
					"Unexpected exception from the service - "
231
							+ mcdbe.getClass() + ": " + mcdbe.getMessage());
232
233
		}
234
235
		// index the object format list based on the format identifier string
236 6366 leinfelder
		int listSize = objectFormatList.sizeObjectFormatList();
237 6187 leinfelder
238
		for (int i = 0; i < listSize; i++) {
239
240
			ObjectFormat objectFormat = objectFormatList.getObjectFormat(i);
241 6561 leinfelder
			String identifier = objectFormat.getFormatId().getValue();
242 6187 leinfelder
			getObjectFormatMap().put(identifier, objectFormat);
243
244
		}
245
246
		return objectFormatList;
247
248
	}
249
250
}