source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/OAICollection.java@ 28984

Last change on this file since 28984 was 28984, checked in by kjdon, 10 years ago

made a new OAICollection class. Inherits from Collection. ALll OAI stuff is taken out of Collection and put in new class. OAICollection is a much simpler collection as it only loads in OAIPMH service rack, not any of the others. This will be used by new OAIMessageRouter instead of Collection

File size: 6.1 KB
Line 
1/*
2 * Collection.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.collection;
20
21import java.io.BufferedReader;
22import java.io.BufferedWriter;
23import java.io.File;
24import java.io.FileReader;
25import java.io.FileWriter;
26import java.io.IOException;
27import java.io.PrintWriter;
28import java.io.StringWriter;
29import java.util.ArrayList;
30import java.util.HashMap;
31
32import org.apache.commons.lang3.StringUtils;
33import org.apache.log4j.Logger;
34import org.greenstone.gsdl3.core.ModuleInterface;
35import org.greenstone.gsdl3.service.ServiceRack;
36import org.greenstone.gsdl3.util.GSFile;
37import org.greenstone.gsdl3.util.GSXML;
38import org.greenstone.gsdl3.util.GSXSLT;
39import org.greenstone.gsdl3.util.OAIXML;
40import org.greenstone.gsdl3.util.SimpleMacroResolver;
41import org.greenstone.gsdl3.util.UserContext;
42import org.greenstone.gsdl3.util.XMLConverter;
43import org.greenstone.gsdl3.util.XMLTransformer;
44import org.w3c.dom.Document;
45import org.w3c.dom.Element;
46import org.w3c.dom.Node;
47import org.w3c.dom.NodeList;
48
49/**
50 * Represents a collection for the OAI server. This is a cut down version of Collection, as we
51 * only want to load the OAIPMH service rack, not any of the others
52 *
53 * @author Katherine Don
54 * @see ModuleInterface
55 */
56public class OAICollection extends Collection
57{
58
59 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.OAICollection.class.getName());
60
61 /** does this collection provide the OAI service */
62 protected boolean has_oai = false;
63
64 /**
65 * Configures the collection.
66 *
67 * site_home and cluster_name must be set before configure is called.
68 *
69 * collection metadata is obtained, and services loaded.
70 *
71 * @return true/false on success/fail
72 */
73 public boolean configure()
74 {
75 if (this.site_home == null || this.cluster_name == null)
76 {
77 logger.error("Collection: site_home and collection_name must be set before configure called!");
78 return false;
79 }
80
81 macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
82
83 Element coll_config_xml = loadCollConfigFile();
84 GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
85 Element build_config_xml = loadBuildConfigFile();
86
87 if (coll_config_xml == null || build_config_xml == null)
88 {
89 return false;
90 }
91
92 // process the metadata and display items and default library params
93 super.configureLocalData(coll_config_xml);
94 super.configureLocalData(build_config_xml);
95 // get extra collection specific stuff
96 findAndLoadInfo(coll_config_xml, build_config_xml);
97
98 // load up the OAIPMH serviceRack
99 configureServiceRacks(coll_config_xml, build_config_xml);
100
101 return true;
102
103 }
104
105
106 /**
107 * whether this collection has OAIPMH services
108 */
109 public boolean hasOAI()
110 {
111 return has_oai;
112 }
113
114
115
116 /** override this to only load up OAIPMH serviceRack - don't need all the rest of them for oai*/
117 protected boolean configureServiceRackList(Element service_rack_list, Element extra_info)
118 {
119
120 // find the OAIPMH service
121 Element oai_service_xml = GSXML.getNamedElement(service_rack_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, OAIXML.OAI_SERVICE_RACK);
122 if (oai_service_xml == null) {
123 return false;
124 }
125
126 // the xml request to send to the serviceRack to query what services it provides
127 Document doc = XMLConverter.newDOM();
128 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
129 Element request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
130 message.appendChild(request);
131
132 ServiceRack s = null;
133
134 try {
135
136 // try for a default service in standard package
137 s = (ServiceRack) Class.forName("org.greenstone.gsdl3.service." + OAIXML.OAI_SERVICE_RACK).newInstance();
138
139 } catch (Exception e) {}
140
141 if (s == null) {
142
143 try {
144 // name as is, in case package is already specified
145 s = (ServiceRack) Class.forName(OAIXML.OAI_SERVICE_RACK).newInstance();
146 } catch (Exception e) {}
147 }
148
149 if (s == null) {
150
151 logger.error("Couldn't get an instance of class " + OAIXML.OAI_SERVICE_RACK + ", or org.greenstone.gsdl3.service." + OAIXML.OAI_SERVICE_RACK);
152 return false;
153 }
154
155
156 s.setSiteHome(this.site_home);
157 s.setSiteAddress(this.site_http_address);
158 s.setClusterName(this.cluster_name);
159 s.setServiceCluster(this);
160 s.setMessageRouter(this.router);
161 // pass the xml node to the service for configuration
162 if (s.configure(oai_service_xml, extra_info)) {
163
164 // find out the supported service types for this service module
165 Node types = s.process(message);
166 NodeList typenodes = ((Element) types).getElementsByTagName(GSXML.SERVICE_ELEM);
167
168 for (int j = 0; j < typenodes.getLength(); j++)
169 {
170 String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);
171
172 if (service_map.get(service) != null)
173 {
174 char extra = '0';
175 String new_service = service + extra;
176
177 while (service_map.get(new_service) != null)
178 {
179 extra++;
180 new_service = service + extra;
181 }
182 this.service_name_map.put(new_service, service);
183 service = new_service;
184 ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
185 }
186 this.service_map.put(service, s);
187 // also add info to the ServiceInfo XML element
188 this.service_list.appendChild(this.desc_doc.importNode(typenodes.item(j), true));
189 }
190 has_oai = true;
191 return true;
192 }
193
194
195 return false;
196 }
197
198}
Note: See TracBrowser for help on using the repository browser.