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 9833 tao
import org.dataone.exceptions.MarshallingException;
32 6366 leinfelder
import org.dataone.service.util.TypeMarshaller;
33 6187 leinfelder
import org.dataone.service.exceptions.NotFound;
34
import org.dataone.service.exceptions.NotImplemented;
35
import org.dataone.service.exceptions.ServiceFailure;
36 8810 leinfelder
import org.dataone.service.types.v2.ObjectFormat;
37 6366 leinfelder
import org.dataone.service.types.v1.ObjectFormatIdentifier;
38 8810 leinfelder
import org.dataone.service.types.v2.ObjectFormatList;
39 6187 leinfelder
40
import edu.ucsb.nceas.metacat.DBUtil;
41
import edu.ucsb.nceas.metacat.DocumentImpl;
42
import edu.ucsb.nceas.metacat.McdbException;
43
import edu.ucsb.nceas.metacat.properties.PropertyService;
44
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
45
46
/**
47
 *
48
 * Implements a subset of the DataONE CNCore services in Metacat.
49
 *
50
 * @author jones, leinfelder
51
 */
52
public class ObjectFormatService {
53
54
	private Logger logMetacat = Logger.getLogger(ObjectFormatService.class);
55
56
	/* The scope of the object formats docid used as the metacat identifier */
57 6306 leinfelder
	public static final String OBJECT_FORMAT_DOCID = "OBJECT_FORMAT_LIST.1";
58 6187 leinfelder
59
	/* The revision of the object formats document */
60
	private int rev;
61
62
	/* The separator of the object formats document */
63
	private String separator = ".";
64
65
	/* The accession number of the object formats document */
66
	private String accNumber = null;
67
68
	/* The list of object formats */
69
	private ObjectFormatList objectFormatList = null;
70
71
	/* the searchable map of object formats */
72
	private static HashMap<String, ObjectFormat> objectFormatMap;
73
74
	private static ObjectFormatService instance = null;
75
76
	public static ObjectFormatService getInstance() {
77
		if (instance == null) {
78
			instance = new ObjectFormatService();
79
		}
80
		return instance;
81
	}
82
83
	/**
84
	 * Constructor, private for singleton access
85
	 */
86
	private ObjectFormatService() {
87
88
	}
89
90
	/**
91
	 * Return the object format based on the given object format identifier
92
	 *
93
	 * @param fmtid
94
	 *            - the object format identifier to look up
95
	 * @return objectFormat - the desired object format
96
	 */
97
	public ObjectFormat getFormat(ObjectFormatIdentifier fmtid)
98 6803 leinfelder
			throws ServiceFailure, NotFound, NotImplemented {
99 6187 leinfelder
100
		logMetacat.debug("CNCoreImpl.getFormat() called.");
101
102
		ObjectFormat objectFormat = null;
103
104
		// look up the format in the object format map
105
		objectFormat = getObjectFormatMap().get(fmtid.getValue());
106
107
		// refresh the object format list if the format is null
108
		if (objectFormat == null) {
109
			getCachedList();
110
			objectFormat = getObjectFormatMap().get(fmtid.getValue());
111
112
			// the object format isn't registered
113
			if (objectFormat == null) {
114
				throw new NotFound("4848", "The format specified by "
115
						+ fmtid.getValue() + " does not exist at this node.");
116
117
			}
118
119
		}
120
121
		return objectFormat;
122
	}
123
124
	/**
125
	 * Return the list of object formats registered from the Coordinating Node.
126
	 *
127
	 * @return objectFormatList - the list of object formats
128
	 */
129 6803 leinfelder
	public ObjectFormatList listFormats() throws ServiceFailure, NotImplemented {
130 6187 leinfelder
131 6304 leinfelder
		objectFormatList = getCachedList();
132 6187 leinfelder
133
		return objectFormatList;
134
	}
135
136
	/*
137
	 * Return the hash containing the fmtid and format mapping
138
	 *
139
	 * @return objectFormatMap - the hash of fmtid/format pairs
140
	 */
141
	private HashMap<String, ObjectFormat> getObjectFormatMap() {
142
143
		if (objectFormatMap == null) {
144
			objectFormatMap = new HashMap<String, ObjectFormat>();
145
146
		}
147
		return objectFormatMap;
148
149
	}
150
151
	/**
152
	 * Get the object format list cached in Metacat
153
	 */
154 6622 leinfelder
	private ObjectFormatList getCachedList() throws ServiceFailure {
155 6187 leinfelder
156
		ObjectFormatList objectFormatList = null;
157
158
		try {
159
160
			// reset the accession number separator in case it is
161
			// different than the default
162
			try {
163
164
				separator = PropertyService
165
						.getProperty("document.accNumSeparator");
166
167
			} catch (PropertyNotFoundException pnfe) {
168
169
				// use the default separator, but log the issue
170
				logMetacat.debug("There was a problem finding the document "
171
						+ "separator property. The error message was: "
172
						+ pnfe.getMessage());
173
			}
174
175
			// get the latest accession number if it is in Metacat
176
			this.rev = DBUtil
177
					.getLatestRevisionInDocumentTable(this.OBJECT_FORMAT_DOCID);
178
179
			if (this.rev != -1) {
180
				this.accNumber = this.OBJECT_FORMAT_DOCID + this.separator
181
						+ this.rev;
182
				DocumentImpl objectFormatsDocument = new DocumentImpl(
183
						accNumber, false);
184
				ByteArrayInputStream bais = new ByteArrayInputStream(
185 6304 leinfelder
						objectFormatsDocument.getBytes());
186 6187 leinfelder
187
				// deserialize the object format list
188
				try {
189
190
					objectFormatList = TypeMarshaller.unmarshalTypeFromStream(
191
							ObjectFormatList.class, bais);
192
193
				} catch (IOException e) {
194
					throw new ServiceFailure("4841",
195
							"Unexpected exception from the service - "
196
									+ e.getClass() + ": " + e.getMessage());
197
198
				} catch (InstantiationException e) {
199
					throw new ServiceFailure("4841",
200
							"Unexpected exception from the service - "
201
									+ e.getClass() + ": " + e.getMessage());
202
203
				} catch (IllegalAccessException e) {
204
					throw new ServiceFailure("4841",
205
							"Unexpected exception from the service - "
206
									+ e.getClass() + ": " + e.getMessage());
207
208 9833 tao
				} catch (MarshallingException e) {
209 6187 leinfelder
					throw new ServiceFailure("4841",
210
							"Unexpected exception from the service - "
211
									+ e.getClass() + ": " + e.getMessage());
212
213
				}
214
215
			} else {
216 6622 leinfelder
				throw new ServiceFailure("4841",
217 6187 leinfelder
						"The object formats collection could not "
218
								+ "be found at this node.");
219
			}
220
221
		} catch (SQLException sqle) {
222
			throw new ServiceFailure("4841",
223
					"Unexpected exception from the service - "
224
							+ sqle.getClass() + ": " + sqle.getMessage());
225
226
		} catch (McdbException mcdbe) {
227
			throw new ServiceFailure("4841",
228
					"Unexpected exception from the service - "
229
							+ mcdbe.getClass() + ": " + mcdbe.getMessage());
230
231
		}
232
233
		// index the object format list based on the format identifier string
234 6366 leinfelder
		int listSize = objectFormatList.sizeObjectFormatList();
235 6187 leinfelder
236
		for (int i = 0; i < listSize; i++) {
237
238
			ObjectFormat objectFormat = objectFormatList.getObjectFormat(i);
239 6561 leinfelder
			String identifier = objectFormat.getFormatId().getValue();
240 6187 leinfelder
			getObjectFormatMap().put(identifier, objectFormat);
241
242
		}
243
244
		return objectFormatList;
245
246
	}
247
248
}