001: /**********************************************************************************
002: * $URL: $
003: * $Id: $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.component.osid.repository.srw;
021:
022: /**
023: * @author Massachusetts Institute of Techbology, Sakai Software Development Team
024: * @version
025: */
026: public class RepositoryManager implements
027: org.osid.repository.RepositoryManager {
028: private org.osid.repository.Repository repository = null;
029: private org.osid.id.IdManager idManager = null;
030: private org.osid.logging.LoggingManager loggingManager = null;
031: private org.osid.logging.WritableLog log = null;
032: private org.osid.OsidContext context = null;
033: private java.util.Properties configuration = null;
034: private java.util.Vector repositoryVector = new java.util.Vector();
035: private java.util.Vector searchTypeVector = new java.util.Vector();
036:
037: public org.osid.OsidContext getOsidContext()
038: throws org.osid.repository.RepositoryException {
039: return context;
040: }
041:
042: private void log(String entry)
043: throws org.osid.repository.RepositoryException {
044: if (log != null) {
045: try {
046: log.appendLog(entry);
047: } catch (org.osid.logging.LoggingException lex) {
048: // swallow exception since logging is a best attempt to log an exception anyway
049: }
050: }
051: }
052:
053: public void assignOsidContext(org.osid.OsidContext context)
054: throws org.osid.repository.RepositoryException {
055: this .context = context;
056: }
057:
058: public void assignConfiguration(java.util.Properties configuration)
059: throws org.osid.repository.RepositoryException {
060: this .configuration = configuration;
061: //System.out.println(this+" Configuration = "+configuration);
062: try {
063: String idImplementation = (String) configuration
064: .get("osid_20_Id_Implementation");
065: String loggingImplementation = (String) configuration
066: .get("osid_20_Logging_Implementation");
067: String logFilename = (String) configuration
068: .get("log_filename");
069: String loggingFormatTypeDomain = (String) configuration
070: .get("loggingFormatType_domain");
071: String loggingFormatTypeAuthority = (String) configuration
072: .get("loggingFormatType_authority");
073: String loggingFormatTypeKeyword = (String) configuration
074: .get("loggingFormatType_keyword");
075: String loggingPriorityTypeDomain = (String) configuration
076: .get("loggingPriorityType_domain");
077: String loggingPriorityTypeAuthority = (String) configuration
078: .get("loggingPriorityType_authority");
079: String loggingPriorityTypeKeyword = (String) configuration
080: .get("loggingPriorityType_keyword");
081:
082: if ((logFilename == null)
083: || (loggingFormatTypeDomain == null)
084: || (loggingFormatTypeAuthority == null)
085: || (loggingFormatTypeKeyword == null)
086: || (loggingPriorityTypeDomain == null)
087: || (loggingPriorityTypeAuthority == null)
088: || (loggingPriorityTypeKeyword == null)) {
089: throw new org.osid.repository.RepositoryException(
090: org.osid.OsidException.CONFIGURATION_ERROR);
091: } else {
092: loggingManager = (org.osid.logging.LoggingManager) org.sakaiproject.component.osid.loader.OsidLoader
093: .getManager("org.osid.logging.LoggingManager",
094: loggingImplementation, this .context,
095: new java.util.Properties());
096: try {
097: log = loggingManager.getLogForWriting(logFilename);
098: } catch (org.osid.logging.LoggingException lex) {
099: log = loggingManager.createLog(logFilename);
100: }
101: log.assignFormatType(new Type(
102: loggingFormatTypeAuthority,
103: loggingFormatTypeDomain,
104: loggingFormatTypeKeyword, ""));
105: log.assignPriorityType(new Type(
106: loggingPriorityTypeAuthority,
107: loggingPriorityTypeDomain,
108: loggingPriorityTypeKeyword, ""));
109: }
110:
111: if (idImplementation == null) {
112: log("no Id Implementation configuration");
113: throw new org.osid.repository.RepositoryException(
114: org.osid.OsidException.CONFIGURATION_ERROR);
115: }
116: this .idManager = (org.osid.id.IdManager) org.sakaiproject.component.osid.loader.OsidLoader
117: .getManager("org.osid.id.IdManager",
118: idImplementation, this .context,
119: new java.util.Properties());
120:
121: Managers.setIdManager(this .idManager);
122:
123: // load repositories from configuration
124: int repositoryNum = 0;
125: String displayName = null;
126: String description = null;
127: String id = null;
128:
129: while ((displayName = (String) configuration
130: .get("repository_" + repositoryNum + "_displayName")) != null) {
131: description = (String) configuration.get("repository_"
132: + repositoryNum + "_description");
133: id = (String) configuration.get("repository_"
134: + repositoryNum + "_id");
135:
136: int searchTypeNum = 0;
137: java.util.Vector searchTypeVector = new java.util.Vector();
138: java.util.Vector searchQueryVector = new java.util.Vector();
139:
140: String authority = null;
141: String domain = null;
142: String keyword = null;
143: String typeDescription = null;
144:
145: while ((authority = (String) configuration
146: .get("repository_" + repositoryNum
147: + "_searchtype_" + searchTypeNum
148: + "_authority")) != null) {
149: domain = (String) configuration.get("repository_"
150: + repositoryNum + "_searchtype_"
151: + searchTypeNum + "_domain");
152: keyword = (String) configuration.get("repository_"
153: + repositoryNum + "_searchtype_"
154: + searchTypeNum + "_keyword");
155: typeDescription = (String) configuration
156: .get("repository_" + repositoryNum
157: + "_searchtype_" + searchTypeNum
158: + "_typeDescription");
159: searchQueryVector.addElement((String) configuration
160: .get("repository_" + repositoryNum
161: + "_searchtype_" + searchTypeNum
162: + "_query"));
163: searchTypeVector.addElement(new Type(authority,
164: domain, keyword, typeDescription));
165: searchTypeNum++;
166: }
167:
168: this .repositoryVector.addElement(new Repository(
169: displayName, description, id, searchTypeVector,
170: searchQueryVector, this .idManager, this .log));
171: repositoryNum++;
172: }
173: } catch (Throwable t) {
174: t.printStackTrace();
175: log(t.getMessage());
176: if (t instanceof org.osid.repository.RepositoryException) {
177: throw new org.osid.repository.RepositoryException(t
178: .getMessage());
179: } else {
180: throw new org.osid.repository.RepositoryException(
181: org.osid.OsidException.OPERATION_FAILED);
182: }
183: }
184: }
185:
186: public org.osid.repository.Repository createRepository(
187: String displayName, String description,
188: org.osid.shared.Type repositoryType)
189: throws org.osid.repository.RepositoryException {
190: throw new org.osid.repository.RepositoryException(
191: org.osid.OsidException.UNIMPLEMENTED);
192: }
193:
194: public void deleteRepository(org.osid.shared.Id repositoryId)
195: throws org.osid.repository.RepositoryException {
196: if (repositoryId == null) {
197: throw new org.osid.repository.RepositoryException(
198: org.osid.shared.SharedException.NULL_ARGUMENT);
199: }
200: throw new org.osid.repository.RepositoryException(
201: org.osid.OsidException.UNIMPLEMENTED);
202: }
203:
204: public org.osid.repository.RepositoryIterator getRepositories()
205: throws org.osid.repository.RepositoryException {
206: return new RepositoryIterator(this .repositoryVector);
207: }
208:
209: public org.osid.repository.RepositoryIterator getRepositoriesByType(
210: org.osid.shared.Type repositoryType)
211: throws org.osid.repository.RepositoryException {
212: if (repositoryType == null) {
213: throw new org.osid.repository.RepositoryException(
214: org.osid.shared.SharedException.NULL_ARGUMENT);
215: }
216: java.util.Vector result = new java.util.Vector();
217: org.osid.repository.RepositoryIterator repositoryIterator = getRepositories();
218: while (repositoryIterator.hasNextRepository()) {
219: org.osid.repository.Repository nextRepository = repositoryIterator
220: .nextRepository();
221: if (nextRepository.getType().isEqual(repositoryType)) {
222: result.addElement(nextRepository);
223: }
224: }
225: return new RepositoryIterator(result);
226: }
227:
228: public org.osid.repository.Repository getRepository(
229: org.osid.shared.Id repositoryId)
230: throws org.osid.repository.RepositoryException {
231: if (repositoryId == null) {
232: throw new org.osid.repository.RepositoryException(
233: org.osid.shared.SharedException.NULL_ARGUMENT);
234: }
235: try {
236: org.osid.repository.RepositoryIterator repositoryIterator = getRepositories();
237: while (repositoryIterator.hasNextRepository()) {
238: org.osid.repository.Repository nextRepository = repositoryIterator
239: .nextRepository();
240: if (nextRepository.getId().isEqual(repositoryId)) {
241: return nextRepository;
242: }
243: }
244: throw new org.osid.repository.RepositoryException(
245: org.osid.shared.SharedException.UNKNOWN_ID);
246: } catch (Throwable t) {
247: log(t.getMessage());
248: throw new org.osid.repository.RepositoryException(
249: org.osid.OsidException.OPERATION_FAILED);
250: }
251: }
252:
253: public org.osid.repository.Asset getAsset(org.osid.shared.Id assetId)
254: throws org.osid.repository.RepositoryException {
255: if (assetId == null) {
256: throw new org.osid.repository.RepositoryException(
257: org.osid.shared.SharedException.NULL_ARGUMENT);
258: }
259: try {
260: org.osid.repository.RepositoryIterator repositoryIterator = getRepositories();
261: while (repositoryIterator.hasNextRepository()) {
262: org.osid.repository.Repository nextRepository = repositoryIterator
263: .nextRepository();
264: try {
265: org.osid.repository.Asset asset = nextRepository
266: .getAsset(assetId);
267: return asset;
268: } catch (Throwable t) {
269: }
270: }
271: } catch (Throwable t) {
272: log(t.getMessage());
273: throw new org.osid.repository.RepositoryException(
274: org.osid.OsidException.OPERATION_FAILED);
275: }
276: throw new org.osid.repository.RepositoryException(
277: org.osid.shared.SharedException.UNKNOWN_ID);
278: }
279:
280: public org.osid.repository.Asset getAssetByDate(
281: org.osid.shared.Id assetId, long date)
282: throws org.osid.repository.RepositoryException {
283: if (assetId == null) {
284: throw new org.osid.repository.RepositoryException(
285: org.osid.shared.SharedException.NULL_ARGUMENT);
286: }
287: try {
288: org.osid.repository.RepositoryIterator repositoryIterator = getRepositories();
289: while (repositoryIterator.hasNextRepository()) {
290: org.osid.repository.Repository nextRepository = repositoryIterator
291: .nextRepository();
292: try {
293: org.osid.repository.Asset asset = nextRepository
294: .getAssetByDate(assetId, date);
295: return asset;
296: } catch (Throwable t) {
297: }
298: }
299: } catch (Throwable t) {
300: log(t.getMessage());
301: throw new org.osid.repository.RepositoryException(
302: org.osid.OsidException.OPERATION_FAILED);
303: }
304: throw new org.osid.repository.RepositoryException(
305: org.osid.shared.SharedException.UNKNOWN_ID);
306: }
307:
308: public org.osid.shared.LongValueIterator getAssetDates(
309: org.osid.shared.Id assetId)
310: throws org.osid.repository.RepositoryException {
311: if (assetId == null) {
312: throw new org.osid.repository.RepositoryException(
313: org.osid.shared.SharedException.NULL_ARGUMENT);
314: }
315: java.util.Vector result = new java.util.Vector();
316: try {
317: org.osid.repository.RepositoryIterator repositoryIterator = getRepositories();
318: while (repositoryIterator.hasNextRepository()) {
319: org.osid.repository.Repository nextRepository = repositoryIterator
320: .nextRepository();
321: org.osid.shared.LongValueIterator longValueIterator = repository
322: .getAssetDates(assetId);
323: while (longValueIterator.hasNextLongValue()) {
324: result.addElement(new Long(longValueIterator
325: .nextLongValue()));
326: }
327: }
328: return new LongValueIterator(result);
329: } catch (Throwable t) {
330: log(t.getMessage());
331: throw new org.osid.repository.RepositoryException(
332: org.osid.OsidException.OPERATION_FAILED);
333: }
334: }
335:
336: public org.osid.repository.AssetIterator getAssetsBySearch(
337: org.osid.repository.Repository[] repositories,
338: java.io.Serializable searchCriteria,
339: org.osid.shared.Type searchType,
340: org.osid.shared.Properties searchProperties)
341: throws org.osid.repository.RepositoryException {
342: if (repositories == null) {
343: throw new org.osid.repository.RepositoryException(
344: org.osid.shared.SharedException.NULL_ARGUMENT);
345: }
346: try {
347: java.util.Vector results = new java.util.Vector();
348: for (int j = 0; j < repositories.length; j++) {
349: org.osid.repository.Repository nextRepository = repositories[j];
350: //optionally add a separate thread here
351: try {
352: org.osid.repository.AssetIterator assetIterator = nextRepository
353: .getAssetsBySearch(searchCriteria,
354: searchType, searchProperties);
355: while (assetIterator.hasNextAsset()) {
356: results.addElement(assetIterator.nextAsset());
357: }
358: } catch (Throwable t) {
359: // log exceptions but don't stop searching
360: log(t.getMessage());
361: }
362: }
363: return new AssetIterator(results);
364: } catch (Throwable t) {
365: log(t.getMessage());
366: throw new org.osid.repository.RepositoryException(
367: org.osid.OsidException.OPERATION_FAILED);
368: }
369: }
370:
371: public org.osid.shared.Id copyAsset(
372: org.osid.repository.Repository repository,
373: org.osid.shared.Id assetId)
374: throws org.osid.repository.RepositoryException {
375: if ((repository == null) || (assetId == null)) {
376: throw new org.osid.repository.RepositoryException(
377: org.osid.shared.SharedException.NULL_ARGUMENT);
378: }
379: throw new org.osid.repository.RepositoryException(
380: org.osid.OsidException.UNIMPLEMENTED);
381: }
382:
383: public org.osid.shared.TypeIterator getRepositoryTypes()
384: throws org.osid.repository.RepositoryException {
385: java.util.Vector results = new java.util.Vector();
386: try {
387: results.addElement(new Type("mit.edu", "repository",
388: "library_content"));
389: return new TypeIterator(results);
390: } catch (Throwable t) {
391: log(t.getMessage());
392: throw new org.osid.repository.RepositoryException(
393: org.osid.OsidException.OPERATION_FAILED);
394: }
395: }
396:
397: public void osidVersion_2_0()
398: throws org.osid.repository.RepositoryException {
399: }
400: }
|