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.registry;
021:
022: /**
023: * <p>
024: * RegistryManager implements a "straw-man" registry interface.
025: * </p>
026: *
027: * @author Massachusetts Institute of Technology
028: * @version $Id: PresenceTool.java 632 2005-07-14 21:22:50Z jeffkahn@mit.edu $
029: */
030: public class RegistryManager implements
031: edu.mit.osid.registry.RegistryManager {
032: private static final String REGISTRY_XML_FILENAME = "OSIDProviderRegistry.xml";
033: private static final String ID_MANAGER_IMPLEMENTATION = "org.sakaiproject.component.osid.id";
034: private static final String FILE_NOT_FOUND_MESSAGE = "Cannot find or open ";
035:
036: private static final String REGISTRY_TAG = "registry";
037: private static final String PROVIDER_RECORD_TAG = "record";
038: private static final String OSID_SERVICE_TAG = "oki:osidservice";
039: private static final String OSID_VERSION_TAG = "oki:osidversion";
040: private static final String OSID_LOAD_KEY_TAG = "oki:osidloadkey";
041: private static final String DISPLAY_NAME_TAG = "dc:title";
042: private static final String DESCRIPTION_TAG = "dc:description";
043: private static final String ID_TAG = "dc:identifier";
044: private static final String CREATOR_TAG = "dc:creator";
045: private static final String PUBLISHER_TAG = "dc:publisher";
046: private static final String REGISTRATION_DATE_TAG = "dc:registrationDate";
047: private static final String RIGHTS_TAG = "dc:rights";
048: private static final String DC_NAMESPACE = "xmlns:dc";
049: private static final String DC_NAMESPACE_URL = "http://purl.org/dc/elements/1.1/";
050: private static final String OKI_NAMESPACE = "xmlns:oki";
051: private static final String OKI_NAMESPACE_URL = "http://www.okiproject.org/registry/elements/1.0/";
052: private static final String DATE_FORMAT = "yyyy-MM-dd kk:mm:ss:SSS zz";
053:
054: private org.osid.OsidContext passedInContext = null;
055: private org.osid.OsidContext emptyContext = new org.osid.OsidContext();
056: private java.util.Properties configuration = null;
057: private java.util.Properties managerProperties = new java.util.Properties();
058: private org.osid.id.IdManager idManager = null;
059:
060: /**
061: * Return the OsidContext assigned earlier.
062: */
063: public org.osid.OsidContext getOsidContext()
064: throws edu.mit.osid.registry.RegistryException {
065: return this .passedInContext;
066: }
067:
068: /**
069: * Store away an OsidContext from the consumer.
070: */
071: public void assignOsidContext(org.osid.OsidContext context)
072: throws org.osid.repository.RepositoryException {
073: this .passedInContext = context;
074: }
075:
076: /**
077: * Simple getter for IdManager with error checking
078: */
079: private org.osid.id.IdManager getIdManager() {
080: if (this .idManager != null)
081: return this .idManager;
082: try {
083: this .idManager = (org.osid.id.IdManager) org.sakaiproject.component.cover.ComponentManager
084: .get(org.osid.id.IdManager.class);
085:
086: } catch (Throwable t) {
087: log(t);
088: }
089: return this .idManager;
090: }
091:
092: /**
093: * Store the configuration from the consumer and perform other intialization.
094: * This method should be called after assignOsidContext() and before any others.
095: * The default OsidLoader does this automatically.
096: */
097: public void assignConfiguration(java.util.Properties configuration)
098: throws org.osid.repository.RepositoryException {
099: this .configuration = configuration;
100: }
101:
102: /**
103: * Examine the registry XML for information and maker Providers
104: */
105: public edu.mit.osid.registry.ProviderIterator getProviders()
106: throws edu.mit.osid.registry.RegistryException {
107: java.util.Vector result = new java.util.Vector();
108:
109: java.io.InputStream istream = org.sakaiproject.component.osid.loader.OsidLoader
110: .getConfigStream(REGISTRY_XML_FILENAME, getClass());
111: if (istream == null) {
112: log(FILE_NOT_FOUND_MESSAGE + REGISTRY_XML_FILENAME);
113: throw new edu.mit.osid.registry.RegistryException(
114: org.osid.OsidException.CONFIGURATION_ERROR);
115: }
116:
117: try {
118: javax.xml.parsers.DocumentBuilderFactory dbf = null;
119: javax.xml.parsers.DocumentBuilder db = null;
120: org.w3c.dom.Document document = null;
121:
122: dbf = javax.xml.parsers.DocumentBuilderFactory
123: .newInstance();
124: db = dbf.newDocumentBuilder();
125: document = db.parse(istream);
126:
127: org.w3c.dom.NodeList records = document
128: .getElementsByTagName(PROVIDER_RECORD_TAG);
129: int numRecords = records.getLength();
130: for (int i = 0; i < numRecords; i++) {
131: String osidService = null;
132: String osidVersion = null;
133: String osidLoadKey = null;
134: String title = null;
135: String description = null;
136: String creator = null;
137: String publisher = null;
138: String registrationDate = null;
139: String identifier = null;
140: String rights = null;
141:
142: org.w3c.dom.Element record = (org.w3c.dom.Element) records
143: .item(i);
144: org.w3c.dom.NodeList nodeList = record
145: .getElementsByTagName(OSID_SERVICE_TAG);
146: int numNodes = nodeList.getLength();
147: for (int k = 0; k < numNodes; k++) {
148: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
149: .item(k);
150: if (e.hasChildNodes()) {
151: osidService = e.getFirstChild().getNodeValue();
152: }
153: }
154:
155: record = (org.w3c.dom.Element) records.item(i);
156: nodeList = record
157: .getElementsByTagName(OSID_VERSION_TAG);
158: numNodes = nodeList.getLength();
159: for (int k = 0; k < numNodes; k++) {
160: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
161: .item(k);
162: if (e.hasChildNodes()) {
163: osidVersion = e.getFirstChild().getNodeValue();
164: }
165: }
166:
167: record = (org.w3c.dom.Element) records.item(i);
168: nodeList = record
169: .getElementsByTagName(OSID_LOAD_KEY_TAG);
170: numNodes = nodeList.getLength();
171: for (int k = 0; k < numNodes; k++) {
172: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
173: .item(k);
174: if (e.hasChildNodes()) {
175: osidLoadKey = e.getFirstChild().getNodeValue();
176: }
177: }
178:
179: record = (org.w3c.dom.Element) records.item(i);
180: nodeList = record
181: .getElementsByTagName(DISPLAY_NAME_TAG);
182: numNodes = nodeList.getLength();
183: for (int k = 0; k < numNodes; k++) {
184: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
185: .item(k);
186: if (e.hasChildNodes()) {
187: title = e.getFirstChild().getNodeValue();
188: }
189: }
190:
191: record = (org.w3c.dom.Element) records.item(i);
192: nodeList = record.getElementsByTagName(DESCRIPTION_TAG);
193: numNodes = nodeList.getLength();
194: for (int k = 0; k < numNodes; k++) {
195: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
196: .item(k);
197: if (e.hasChildNodes()) {
198: description = e.getFirstChild().getNodeValue();
199: }
200: }
201:
202: record = (org.w3c.dom.Element) records.item(i);
203: nodeList = record.getElementsByTagName(ID_TAG);
204: numNodes = nodeList.getLength();
205: for (int k = 0; k < numNodes; k++) {
206: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
207: .item(k);
208: if (e.hasChildNodes()) {
209: identifier = e.getFirstChild().getNodeValue();
210: }
211: }
212:
213: record = (org.w3c.dom.Element) records.item(i);
214: nodeList = record.getElementsByTagName(CREATOR_TAG);
215: numNodes = nodeList.getLength();
216: for (int k = 0; k < numNodes; k++) {
217: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
218: .item(k);
219: if (e.hasChildNodes()) {
220: creator = e.getFirstChild().getNodeValue();
221: }
222: }
223:
224: record = (org.w3c.dom.Element) records.item(i);
225: nodeList = record.getElementsByTagName(PUBLISHER_TAG);
226: numNodes = nodeList.getLength();
227: for (int k = 0; k < numNodes; k++) {
228: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
229: .item(k);
230: if (e.hasChildNodes()) {
231: publisher = e.getFirstChild().getNodeValue();
232: }
233: }
234:
235: record = (org.w3c.dom.Element) records.item(i);
236: nodeList = record
237: .getElementsByTagName(REGISTRATION_DATE_TAG);
238: numNodes = nodeList.getLength();
239: for (int k = 0; k < numNodes; k++) {
240: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
241: .item(k);
242: if (e.hasChildNodes()) {
243: registrationDate = e.getFirstChild()
244: .getNodeValue();
245: }
246: }
247:
248: record = (org.w3c.dom.Element) records.item(i);
249: nodeList = record.getElementsByTagName(RIGHTS_TAG);
250: numNodes = nodeList.getLength();
251: for (int k = 0; k < numNodes; k++) {
252: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
253: .item(k);
254: if (e.hasChildNodes()) {
255: rights = e.getFirstChild().getNodeValue();
256: }
257: }
258:
259: /*
260: System.out.println(osidVersion);
261: System.out.println(osidLoadKey);
262: System.out.println(title);
263: System.out.println(description);
264: System.out.println(creator);
265: System.out.println(publisher);
266: System.out.println(registrationDate);
267: System.out.println(identifier);
268: System.out.println(rights);
269: */
270: result.addElement(new Provider(this , osidService,
271: osidVersion, osidLoadKey, title, description,
272: getIdManager().getId(identifier), creator,
273: publisher, registrationDate, rights));
274: //System.out.println("Element Added...");
275: }
276: } catch (Throwable t) {
277: log(t);
278: }
279: return new ProviderIterator(result);
280: }
281:
282: /**
283: * Unimplemented method. We have no Provider Types for the community at this time.
284: */
285: public edu.mit.osid.registry.ProviderIterator getProvidersByType(
286: org.osid.shared.Type providerType)
287: throws edu.mit.osid.registry.RegistryException {
288: throw new edu.mit.osid.registry.RegistryException(
289: org.osid.OsidException.UNIMPLEMENTED);
290: }
291:
292: /**
293: * Return a Provider from the content in the registry XML file
294: */
295: public edu.mit.osid.registry.Provider getProvider(
296: org.osid.shared.Id providerId)
297: throws edu.mit.osid.registry.RegistryException {
298: edu.mit.osid.registry.ProviderIterator providerIterator = getProviders();
299: try {
300: while (providerIterator.hasNextProvider()) {
301: edu.mit.osid.registry.Provider provider = providerIterator
302: .nextProvider();
303: if (provider.getId().isEqual(providerId)) {
304: return provider;
305: }
306: }
307: } catch (Throwable t) {
308: log(t);
309: throw new edu.mit.osid.registry.RegistryException(
310: org.osid.OsidException.OPERATION_FAILED);
311: }
312: throw new edu.mit.osid.registry.RegistryException(
313: org.osid.shared.SharedException.UNKNOWN_ID);
314: }
315:
316: /**
317: * Append a new record element to registry XML file. Omit any nodes where the input argument is
318: * null, except for the Registration Date. If that is null, insert the current date and time.
319: */
320: public edu.mit.osid.registry.Provider createProvider(
321: String osidService, String osidVersion, String osidLoadKey,
322: String displayName, String description,
323: org.osid.shared.Id id, String creator, String publisher,
324: String registrationDate, String rights)
325: throws edu.mit.osid.registry.RegistryException {
326: try {
327: String now = null;
328: java.io.InputStream istream = getClass().getClassLoader()
329: .getResourceAsStream(REGISTRY_XML_FILENAME);
330: if (istream == null) {
331: log(FILE_NOT_FOUND_MESSAGE + REGISTRY_XML_FILENAME);
332: throw new edu.mit.osid.registry.RegistryException(
333: org.osid.OsidException.CONFIGURATION_ERROR);
334: }
335:
336: javax.xml.parsers.DocumentBuilderFactory dbf = null;
337: javax.xml.parsers.DocumentBuilder db = null;
338: org.w3c.dom.Document document = null;
339:
340: dbf = javax.xml.parsers.DocumentBuilderFactory
341: .newInstance();
342: db = dbf.newDocumentBuilder();
343: document = db.parse(istream);
344:
345: org.w3c.dom.NodeList nodeList = document
346: .getElementsByTagName(REGISTRY_TAG);
347: int numNodes = nodeList.getLength();
348: org.w3c.dom.Element records = (org.w3c.dom.Element) nodeList
349: .item(numNodes - 1);
350: org.w3c.dom.Element record = document
351: .createElement(PROVIDER_RECORD_TAG);
352: record.setAttribute(DC_NAMESPACE, DC_NAMESPACE_URL);
353: record.setAttribute(OKI_NAMESPACE, OKI_NAMESPACE_URL);
354:
355: org.w3c.dom.Element e;
356: if (osidService != null) {
357: e = document.createElement(OSID_SERVICE_TAG);
358: e.appendChild(document.createTextNode(osidService));
359: record.appendChild(e);
360: }
361:
362: if (osidVersion != null) {
363: e = document.createElement(OSID_VERSION_TAG);
364: e.appendChild(document.createTextNode(osidVersion));
365: record.appendChild(e);
366: }
367:
368: if (osidLoadKey != null) {
369: e = document.createElement(OSID_LOAD_KEY_TAG);
370: e.appendChild(document.createTextNode(osidLoadKey));
371: record.appendChild(e);
372: }
373:
374: if (displayName != null) {
375: e = document.createElement(DISPLAY_NAME_TAG);
376: e.appendChild(document.createTextNode(displayName));
377: record.appendChild(e);
378: }
379:
380: if (description != null) {
381: e = document.createElement(DESCRIPTION_TAG);
382: e.appendChild(document.createTextNode(description));
383: record.appendChild(e);
384: }
385:
386: if (id != null) {
387: e = document.createElement(ID_TAG);
388: e
389: .appendChild(document.createTextNode(id
390: .getIdString()));
391: record.appendChild(e);
392: }
393:
394: if (creator != null) {
395: e = document.createElement(CREATOR_TAG);
396: e.appendChild(document.createTextNode(creator));
397: record.appendChild(e);
398: }
399:
400: if (publisher != null) {
401: e = document.createElement(PUBLISHER_TAG);
402: e.appendChild(document.createTextNode(publisher));
403: record.appendChild(e);
404: }
405:
406: if (registrationDate != null) {
407: now = registrationDate;
408: } else {
409: java.util.Calendar calendar = java.util.Calendar
410: .getInstance();
411: java.util.Date date = calendar.getTime();
412: java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
413: DATE_FORMAT);
414: StringBuffer sb = sdf.format(date, new StringBuffer(),
415: new java.text.FieldPosition(0));
416: now = sb.toString();
417: }
418: e = document.createElement(REGISTRATION_DATE_TAG);
419: e.appendChild(document.createTextNode(now));
420: record.appendChild(e);
421:
422: if (rights != null) {
423: e = document.createElement(RIGHTS_TAG);
424: e.appendChild(document.createTextNode(rights));
425: record.appendChild(e);
426: }
427:
428: records.appendChild(record);
429:
430: javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory
431: .newInstance();
432: javax.xml.transform.Transformer transformer = tf
433: .newTransformer();
434: java.util.Properties properties = new java.util.Properties();
435: properties.put("indent", "yes");
436: transformer.setOutputProperties(properties);
437: javax.xml.transform.dom.DOMSource domSource = new javax.xml.transform.dom.DOMSource(
438: document);
439: javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(
440: REGISTRY_XML_FILENAME);
441: transformer.transform(domSource, result);
442:
443: return new Provider(this , osidService, osidVersion,
444: osidLoadKey, displayName, description, id, creator,
445: publisher, now, rights);
446: } catch (Throwable t) {
447: log(t);
448: }
449: throw new edu.mit.osid.registry.RegistryException(
450: org.osid.OsidException.OPERATION_FAILED);
451: }
452:
453: /**
454: * Find the record element in the registry XML file whose identifier node matches the
455: * input. Remove that record and re-save the XML.
456: */
457: public void deleteProvider(org.osid.shared.Id providerId)
458: throws edu.mit.osid.registry.RegistryException {
459: if (providerId == null) {
460: throw new edu.mit.osid.registry.RegistryException(
461: org.osid.shared.SharedException.NULL_ARGUMENT);
462: }
463:
464: java.io.InputStream istream = getClass().getClassLoader()
465: .getResourceAsStream(REGISTRY_XML_FILENAME);
466: if (istream == null) {
467: log(FILE_NOT_FOUND_MESSAGE + REGISTRY_XML_FILENAME);
468: throw new edu.mit.osid.registry.RegistryException(
469: org.osid.OsidException.CONFIGURATION_ERROR);
470: }
471:
472: try {
473: javax.xml.parsers.DocumentBuilderFactory dbf = null;
474: javax.xml.parsers.DocumentBuilder db = null;
475: org.w3c.dom.Document document = null;
476:
477: dbf = javax.xml.parsers.DocumentBuilderFactory
478: .newInstance();
479: db = dbf.newDocumentBuilder();
480: document = db.parse(istream);
481:
482: org.w3c.dom.NodeList registryEntries = document
483: .getElementsByTagName(REGISTRY_TAG);
484: org.w3c.dom.Element registry = (org.w3c.dom.Element) registryEntries
485: .item(0);
486:
487: org.w3c.dom.NodeList records = document
488: .getElementsByTagName(PROVIDER_RECORD_TAG);
489: int numRecords = records.getLength();
490: for (int i = 0; i < numRecords; i++) {
491: org.w3c.dom.Element record = (org.w3c.dom.Element) records
492: .item(i);
493: org.w3c.dom.NodeList nodeList = record
494: .getElementsByTagName(ID_TAG);
495: int numNodes = nodeList.getLength();
496: for (int k = 0; k < numNodes; k++) {
497: org.w3c.dom.Element e = (org.w3c.dom.Element) nodeList
498: .item(k);
499: if (e.hasChildNodes()) {
500: String idString = e.getFirstChild()
501: .getNodeValue();
502: org.osid.shared.Id id = getIdManager().getId(
503: idString);
504: if (id.isEqual(providerId)) {
505: registry.removeChild(record);
506:
507: javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory
508: .newInstance();
509: javax.xml.transform.Transformer transformer = tf
510: .newTransformer();
511: java.util.Properties properties = new java.util.Properties();
512: properties.put("indent", "yes");
513: transformer.setOutputProperties(properties);
514: javax.xml.transform.dom.DOMSource domSource = new javax.xml.transform.dom.DOMSource(
515: document);
516: javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(
517: REGISTRY_XML_FILENAME);
518: transformer.transform(domSource, result);
519:
520: return;
521: }
522: }
523: }
524: }
525: } catch (Throwable t) {
526: log(t);
527: throw new edu.mit.osid.registry.RegistryException(
528: org.osid.OsidException.OPERATION_FAILED);
529: }
530: throw new edu.mit.osid.registry.RegistryException(
531: org.osid.shared.SharedException.UNKNOWN_ID);
532: }
533:
534: /**
535: * Checked by the org.osid.OsidLoader.getManager() method
536: */
537: public void osidVersion_2_0()
538: throws edu.mit.osid.registry.RegistryException {
539: }
540:
541: private void log(Throwable t) {
542: t.printStackTrace();
543: }
544:
545: private void log(String message) {
546: System.out.println(message);
547: }
548: }
|