1 | /*
|
---|
2 | * OAICollection.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 | */
|
---|
19 | package org.greenstone.gsdl3.collection;
|
---|
20 |
|
---|
21 | import java.io.BufferedReader;
|
---|
22 | import java.io.BufferedWriter;
|
---|
23 | import java.io.File;
|
---|
24 | import java.io.FileReader;
|
---|
25 | import java.io.FileWriter;
|
---|
26 | import java.io.IOException;
|
---|
27 | import java.io.PrintWriter;
|
---|
28 | import java.io.StringWriter;
|
---|
29 | import java.util.ArrayList;
|
---|
30 | import java.util.HashMap;
|
---|
31 |
|
---|
32 | import org.apache.commons.lang3.StringUtils;
|
---|
33 | import org.apache.log4j.Logger;
|
---|
34 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
35 | import org.greenstone.gsdl3.service.ServiceRack;
|
---|
36 | import org.greenstone.gsdl3.service.OAIPMH;
|
---|
37 | import org.greenstone.gsdl3.util.GSFile;
|
---|
38 | import org.greenstone.gsdl3.util.GSXML;
|
---|
39 | import org.greenstone.gsdl3.util.GSXSLT;
|
---|
40 | import org.greenstone.gsdl3.util.OAIXML;
|
---|
41 | import org.greenstone.gsdl3.util.SimpleMacroResolver;
|
---|
42 | import org.greenstone.gsdl3.util.UserContext;
|
---|
43 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
44 | import org.greenstone.gsdl3.util.XMLTransformer;
|
---|
45 | import org.w3c.dom.Document;
|
---|
46 | import org.w3c.dom.Element;
|
---|
47 | import org.w3c.dom.Node;
|
---|
48 | import org.w3c.dom.NodeList;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Represents a collection for the OAI server. This is a cut down version of Collection, as we
|
---|
52 | * only want to load the OAIPMH service rack, not any of the others
|
---|
53 | *
|
---|
54 | */
|
---|
55 | public class OAICollection extends Collection
|
---|
56 | {
|
---|
57 |
|
---|
58 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.OAICollection.class.getName());
|
---|
59 |
|
---|
60 | /** does this collection provide the OAI service */
|
---|
61 | protected boolean has_oai = false;
|
---|
62 |
|
---|
63 | /** earliest datestamp of an OAI collection. Also used to work out the earliest datetimestamp of the entire OAI repository */
|
---|
64 | protected long earliestOAIDatestamp = 0;
|
---|
65 |
|
---|
66 | /** a reference to the OAIPMH service rack */
|
---|
67 | protected OAIPMH oai_service_rack = null;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Configures the collection.
|
---|
71 | *
|
---|
72 | * site_home and cluster_name must be set before configure is called.
|
---|
73 | *
|
---|
74 | * collection metadata is obtained, and services loaded.
|
---|
75 | *
|
---|
76 | * @return true/false on success/fail
|
---|
77 | */
|
---|
78 | public boolean configure()
|
---|
79 | {
|
---|
80 | if (this.site_home == null || this.cluster_name == null)
|
---|
81 | {
|
---|
82 | logger.error("Collection: site_home and collection_name must be set before configure called!");
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 |
|
---|
86 | macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
|
---|
87 |
|
---|
88 | Element coll_config_xml = loadCollConfigFile();
|
---|
89 | if (coll_config_xml == null) {
|
---|
90 | logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load collection config file");
|
---|
91 |
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 | Element build_config_xml = loadBuildConfigFile();
|
---|
95 | if (build_config_xml == null)
|
---|
96 | {
|
---|
97 | logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load build config file");
|
---|
98 |
|
---|
99 | return false;
|
---|
100 | }
|
---|
101 | GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
|
---|
102 |
|
---|
103 | // process the metadata and display items and default library params
|
---|
104 | super.configureLocalData(coll_config_xml);
|
---|
105 | super.configureLocalData(build_config_xml);
|
---|
106 | // get extra collection specific stuff
|
---|
107 | findAndLoadInfo(coll_config_xml, build_config_xml);
|
---|
108 |
|
---|
109 | // load up the OAIPMH serviceRack
|
---|
110 | configureServiceRacks(coll_config_xml, build_config_xml);
|
---|
111 |
|
---|
112 | return true;
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 | // NOTE:
|
---|
117 | // Calling cleanUp() on OAIPMH object oai_service_rack will close any open dbs handles on module deactivation by MESSAGEROUTER (no deactivate yet called by OAIMESSAGEROUTER)
|
---|
118 | // But the OAIPMH object's cleanUp() is already called by superclass ServiceCluster, which goes around calling cleanUp() on all services/ServiceRacks.
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * whether this collection has OAIPMH services
|
---|
122 | */
|
---|
123 | public boolean hasOAI()
|
---|
124 | {
|
---|
125 | return has_oai;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * The earliesttimestamp entry in the oai-inf.db representing when the collection was created.
|
---|
130 | * Used by the OAIReceptionist
|
---|
131 | */
|
---|
132 | public long getEarliestOAIDatestamp()
|
---|
133 | {
|
---|
134 | return earliestOAIDatestamp;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /** add any extra info for collection from OAIConfig.xml */
|
---|
138 | public boolean configureOAI(Element oai_config) {
|
---|
139 | // just pass the element to each service - should only be one
|
---|
140 | return this.oai_service_rack.configureOAI(oai_config);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** override this to only load up OAIPMH serviceRack - don't need all the rest of them for oai*/
|
---|
144 | protected boolean configureServiceRackList(Element service_rack_list, Element extra_info)
|
---|
145 | {
|
---|
146 |
|
---|
147 | // find the OAIPMH service
|
---|
148 | Element oai_service_xml = GSXML.getNamedElement(service_rack_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, "OAIPMH");
|
---|
149 | if (oai_service_xml == null) {
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 |
|
---|
153 | // the xml request to send to the serviceRack to query what services it provides
|
---|
154 | Document doc = XMLConverter.newDOM();
|
---|
155 | Element message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
156 | Element request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
|
---|
157 | message.appendChild(request);
|
---|
158 |
|
---|
159 | if(this.oai_service_rack == null) {
|
---|
160 | this.oai_service_rack = new OAIPMH();
|
---|
161 | }
|
---|
162 | this.oai_service_rack.setSiteHome(this.site_home);
|
---|
163 | this.oai_service_rack.setSiteAddress(this.site_http_address);
|
---|
164 | this.oai_service_rack.setClusterName(this.cluster_name);
|
---|
165 | this.oai_service_rack.setServiceCluster(this);
|
---|
166 | this.oai_service_rack.setMessageRouter(this.router);
|
---|
167 | // pass the xml node to the service for configuration
|
---|
168 | if (this.oai_service_rack.configure(oai_service_xml, extra_info)) {
|
---|
169 |
|
---|
170 | // once we've configured the OAIPMH service, we can use the OAIPMH service to
|
---|
171 | // retrieve the earliest timestamp of this OAI collection from the oai-inf db
|
---|
172 | long earliestTimestamp = this.oai_service_rack.getEarliestTimestamp();
|
---|
173 | if(earliestTimestamp == -1) {
|
---|
174 | this.earliestOAIDatestamp = -1;
|
---|
175 | logger.warn("No OAI timestamp for collection " + this.cluster_name);
|
---|
176 | } else {
|
---|
177 | this.earliestOAIDatestamp = earliestTimestamp; // milliseconds
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | // find out the supported service types for this service module
|
---|
182 | Node types = this.oai_service_rack.process(message);
|
---|
183 | NodeList typenodes = ((Element) types).getElementsByTagName(GSXML.SERVICE_ELEM);
|
---|
184 |
|
---|
185 | for (int j = 0; j < typenodes.getLength(); j++)
|
---|
186 | {
|
---|
187 | String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);
|
---|
188 |
|
---|
189 | if (service_map.get(service) != null)
|
---|
190 | {
|
---|
191 | char extra = '0';
|
---|
192 | String new_service = service + extra;
|
---|
193 |
|
---|
194 | while (service_map.get(new_service) != null)
|
---|
195 | {
|
---|
196 | extra++;
|
---|
197 | new_service = service + extra;
|
---|
198 | }
|
---|
199 | this.service_name_map.put(new_service, service);
|
---|
200 | service = new_service;
|
---|
201 | ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
|
---|
202 | }
|
---|
203 | this.service_map.put(service, this.oai_service_rack);
|
---|
204 | // also add info to the ServiceInfo XML element
|
---|
205 | this.service_list.appendChild(this.desc_doc.importNode(typenodes.item(j), true));
|
---|
206 | }
|
---|
207 | has_oai = true;
|
---|
208 | return true;
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | return false;
|
---|
213 | }
|
---|
214 |
|
---|
215 | }
|
---|