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 | */
|
---|
19 | package org.greenstone.gsdl3.collection;
|
---|
20 |
|
---|
21 | import org.greenstone.gsdl3.util.*;
|
---|
22 | import org.greenstone.gsdl3.core.*;
|
---|
23 | import org.greenstone.gsdl3.service.*;
|
---|
24 |
|
---|
25 |
|
---|
26 | // java XML classes we're using
|
---|
27 | import org.w3c.dom.Document;
|
---|
28 | import org.w3c.dom.Node;
|
---|
29 | import org.w3c.dom.Element;
|
---|
30 | import org.w3c.dom.NodeList;
|
---|
31 |
|
---|
32 | import java.io.File;
|
---|
33 | import java.util.HashMap;
|
---|
34 |
|
---|
35 | import org.apache.log4j.*;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Represents a collection in Greenstone. A collection is an extension of
|
---|
39 | * a ServiceCluster - it has local data that the services use.
|
---|
40 | *
|
---|
41 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
42 | * @version $Revision: 13860 $
|
---|
43 | * @see ModuleInterface
|
---|
44 | */
|
---|
45 | public class Collection
|
---|
46 | extends ServiceCluster {
|
---|
47 |
|
---|
48 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
|
---|
49 |
|
---|
50 | /** is this collection public or private */
|
---|
51 | protected boolean is_public = true;
|
---|
52 | protected XMLTransformer transformer = null;
|
---|
53 | /** same as setClusterName */
|
---|
54 | public void setCollectionName(String name) {
|
---|
55 | setClusterName(name);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public Collection() {
|
---|
59 | super();
|
---|
60 | this.description = this.doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Configures the collection.
|
---|
66 | *
|
---|
67 | * gsdlHome and collectionName must be set before configure is called.
|
---|
68 | *
|
---|
69 | * the file buildcfg.xml is located in gsdlHome/collect/collectionName
|
---|
70 | * collection metadata is obtained, and services loaded.
|
---|
71 | *
|
---|
72 | * @return true/false on success/fail
|
---|
73 | */
|
---|
74 | public boolean configure() {
|
---|
75 |
|
---|
76 | if (this.site_home == null || this.cluster_name== null) {
|
---|
77 | logger.error("Collection: site_home and collection_name must be set before configure called!");
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | Element coll_config_xml = loadCollConfigFile();
|
---|
82 | Element build_config_xml = loadBuildConfigFile();
|
---|
83 |
|
---|
84 | if (coll_config_xml==null||build_config_xml==null) {
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 | // process the metadata and display items
|
---|
88 | findAndLoadInfo(coll_config_xml, build_config_xml);
|
---|
89 |
|
---|
90 | // now do the services
|
---|
91 | Element service_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
|
---|
92 | configureServiceRack(service_list, coll_config_xml);
|
---|
93 |
|
---|
94 | return true;
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | public boolean isPublic() {
|
---|
99 | return is_public;
|
---|
100 | }
|
---|
101 | /**
|
---|
102 | * load in the collection config file into a DOM Element
|
---|
103 | */
|
---|
104 | protected Element loadCollConfigFile() {
|
---|
105 |
|
---|
106 | File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
|
---|
107 |
|
---|
108 | if (!coll_config_file.exists()) {
|
---|
109 | logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+coll_config_file+" does not exist");
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 | // get the xml for both files
|
---|
113 | Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
|
---|
114 | Element coll_config_elem = null;
|
---|
115 | if (coll_config_doc != null) {
|
---|
116 | coll_config_elem = coll_config_doc.getDocumentElement();
|
---|
117 | }
|
---|
118 | return coll_config_elem;
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * load in the collection build config file into a DOM Element
|
---|
124 | */
|
---|
125 | protected Element loadBuildConfigFile() {
|
---|
126 |
|
---|
127 | File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
|
---|
128 | if (!build_config_file.exists()) {
|
---|
129 | logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+build_config_file+" does not exist");
|
---|
130 | return null;
|
---|
131 | }
|
---|
132 | Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
|
---|
133 | Element build_config_elem = null;
|
---|
134 | if (build_config_doc != null) {
|
---|
135 | build_config_elem = build_config_doc.getDocumentElement();
|
---|
136 | }
|
---|
137 | return build_config_elem;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * find the metadata and display elems from the two config files and add it to the appropriate lists
|
---|
142 | */
|
---|
143 | protected boolean findAndLoadInfo(Element coll_config_xml,
|
---|
144 | Element build_config_xml){
|
---|
145 |
|
---|
146 | // metadata
|
---|
147 | Element meta_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
148 | addMetadata(meta_list);
|
---|
149 | meta_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
150 | addMetadata(meta_list);
|
---|
151 |
|
---|
152 | meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
153 | GSXML.addMetadata(this.doc, meta_list, "httpPath", this.site_http_address+"/collect/"+this.cluster_name);
|
---|
154 | addMetadata(meta_list);
|
---|
155 |
|
---|
156 | // display stuff
|
---|
157 | Element display_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
|
---|
158 | if (display_list != null) {
|
---|
159 | resolveMacros(display_list);
|
---|
160 | addDisplayItems(display_list);
|
---|
161 | }
|
---|
162 |
|
---|
163 | // check whether we are public or not
|
---|
164 | if (meta_list != null) {
|
---|
165 | Element meta_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
|
---|
166 | if (meta_elem != null) {
|
---|
167 | String value = GSXML.getValue(meta_elem).toLowerCase().trim();
|
---|
168 | if (value.equals("false")) {
|
---|
169 | is_public = false;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | return true;
|
---|
174 |
|
---|
175 | }
|
---|
176 |
|
---|
177 | protected boolean resolveMacros(Element display_list) {
|
---|
178 | if (display_list==null) return false;
|
---|
179 | NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
|
---|
180 | if (displaynodes.getLength()>0) {
|
---|
181 | String http_site = this.site_http_address;
|
---|
182 | String http_collection = this.site_http_address +"/collect/"+this.cluster_name;
|
---|
183 | for(int k=0; k<displaynodes.getLength(); k++) {
|
---|
184 | Element d = (Element) displaynodes.item(k);
|
---|
185 | String text = GSXML.getNodeText(d);
|
---|
186 | text = text.replaceAll("_httpsite_", http_site);
|
---|
187 | text = text.replaceAll("_httpcollection_", http_collection);
|
---|
188 | GSXML.setNodeText(d, text);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 | /**
|
---|
194 | * do a configure on only part of the collection
|
---|
195 | */
|
---|
196 | protected boolean configureSubset(String subset) {
|
---|
197 |
|
---|
198 | // need the coll config files
|
---|
199 | Element coll_config_elem = loadCollConfigFile();
|
---|
200 | Element build_config_elem = loadBuildConfigFile();
|
---|
201 | if (coll_config_elem == null||build_config_elem == null) {
|
---|
202 | // wont be able to do any of teh requests
|
---|
203 | return false;
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
207 | Element service_rack_list = (Element)GSXML.getChildByTagName(build_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
|
---|
208 |
|
---|
209 | return configureServiceRack(service_rack_list, coll_config_elem);
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (subset.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
213 | return findAndLoadInfo(coll_config_elem, build_config_elem);
|
---|
214 |
|
---|
215 | }
|
---|
216 |
|
---|
217 | logger.error("Collection: cant process system request, configure "+subset);
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 |
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 |
|
---|
225 |
|
---|