Project

General

Profile

1
package eu.dnetlib.index.utils;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.util.Map;
6
import java.util.Map.Entry;
7

    
8
import org.apache.commons.io.IOUtils;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.apache.solr.common.cloud.SolrZkClient;
12
import org.apache.zookeeper.CreateMode;
13
import org.apache.zookeeper.KeeperException;
14
import org.dom4j.Document;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Required;
17
import org.springframework.core.io.Resource;
18
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
19

    
20
import com.google.common.collect.Maps;
21

    
22
import eu.dnetlib.rmi.provision.IndexServiceException;
23

    
24

    
25
/**
26
 * The Class ZkUtils.
27
 */
28
public class ZkUtils {
29

    
30
	/**
31
	 * The log.
32
	 */
33
	private static final Log log = LogFactory.getLog(ZkUtils.class); // NOPMD by marko on 11/24/08 5:02 PM
34

    
35
	/** The Constant CONFIGS_PATH. */
36
	private static final String CONFIGS_PATH = "/configs";
37

    
38
	/** The schema factory. */
39
	@Autowired
40
	private IndexSchemaFactory schemaFactory;
41

    
42
	/** The config factory. */
43
	private IndexConfigFactory configFactory;
44

    
45
	/** The static configuration classpath. */
46
	private String staticConfigurationClasspath;
47

    
48
	/**
49
	 * Upload zookeper config.
50
	 * 
51
	 * @param zkClient
52
	 *            the zk client
53
	 * @param coreName
54
	 *            the core name
55
	 * @param fields
56
	 *            the fields
57
	 * @param params
58
	 *            the params
59
	 * @param overwrite
60
	 *            the overwrite
61
	 */
62
	public void uploadZookeperConfig(final SolrZkClient zkClient,
63
			final String coreName,
64
			final Document fields,
65
			final Map<String, String> params,
66
			final boolean overwrite) {
67

    
68
		final String basepath = CONFIGS_PATH + "/" + coreName;
69

    
70
		log.info("uploading solr configuration to ZK for index collection: " + coreName);
71

    
72
		try {
73
			if (overwrite && zkClient.getSolrZooKeeper().exists(basepath, false) != null) {
74
				log.info("cleanup ZK configuration: " + coreName);
75
				for (String child : zkClient.getSolrZooKeeper().getChildren(basepath, false)) {
76
					final String path = basepath + "/" + child;
77
					log.debug("cleanup ZK file: " + path);
78
					zkClient.delete(path, -1, true);
79
				}
80
				zkClient.delete(basepath, -1, true);
81
			}
82
			if (!zkClient.exists(basepath, true)) {
83
				log.info("upload ZK configuration: " + coreName);
84
				zkClient.makePath(basepath, true);
85
				uploadConfiguration(zkClient, basepath, buildConfiguration(coreName, fields, params));
86
			}
87
			log.info("upload ZK configuration complete");
88
		} catch (Exception e) {
89
			log.error("unable to upload solr configuration", e);
90
		}
91
	}
92

    
93
	/**
94
	 * Builds the configuration.
95
	 * 
96
	 * @param indexName
97
	 *            the index name
98
	 * @param fields
99
	 *            the fields
100
	 * @param params
101
	 *            the params
102
	 * @return the map
103
	 * @throws IndexServiceException
104
	 *             the index service exception
105
	 */
106
	private Map<String, byte[]> buildConfiguration(final String indexName, final Document fields, final Map<String, String> params)
107
			throws IndexServiceException {
108

    
109
		Map<String, byte[]> res = Maps.newHashMap();
110

    
111
		try {
112
			log.debug("adding schema.xml to the resource map");
113
			res.put("schema.xml", schemaFactory.getSchema(fields).getBytes());
114
			
115
			res.put("solrconfig.xml", configFactory.getConfig(params).getBytes());
116
			log.debug("adding solrconfig.xml to the resource map");
117
			PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
118
			Resource[] resources = resolver.getResources(getStaticConfigurationClasspath());
119

    
120
			for (Resource r : resources) {
121
				InputStream is = r.getInputStream();
122
				if ((r.getFilename() != null) && !r.getFilename().isEmpty()) {
123
				    res.put(r.getFilename(), IOUtils.toByteArray(is));
124
				    log.debug("adding " + r.getFilename() +" to the resource map");
125
				    is.close();
126
				}
127
			}
128
			return res;
129
		} catch (Throwable e) {
130
			throw new IndexServiceException("failed to build configuration", e);
131
		}
132
	}
133

    
134
	/**
135
	 * Upload configuration.
136
	 * 
137
	 * @param zkClient
138
	 *            the zk client
139
	 * @param basePath
140
	 *            the base path
141
	 * @param resources
142
	 *            the resources
143
	 * @throws KeeperException
144
	 *             the keeper exception
145
	 * @throws InterruptedException
146
	 *             the interrupted exception
147
	 * @throws IOException
148
	 *             Signals that an I/O exception has occurred.
149
	 */
150
	private void uploadConfiguration(final SolrZkClient zkClient, final String basePath, final Map<String, byte[]> resources) throws KeeperException,
151
			InterruptedException, IOException {
152

    
153
		if (!zkClient.exists(basePath, true)) {
154
			zkClient.makePath(basePath, true);
155
		}
156

    
157
		for (final Entry<String, byte[]> e : resources.entrySet()) {
158
			String path = basePath + "/" + e.getKey();
159
			log.debug("upload ZK configuration: " + path);
160
			zkClient.create(path, e.getValue(), CreateMode.PERSISTENT, true);
161
		}
162
	}
163

    
164
	/**
165
	 * Gets the config factory.
166
	 * 
167
	 * @return the config factory
168
	 */
169
	public IndexConfigFactory getConfigFactory() {
170
		return configFactory;
171
	}
172

    
173
	/**
174
	 * Sets the config factory.
175
	 * 
176
	 * @param configFactory
177
	 *            the new config factory
178
	 */
179
	@Required
180
	public void setConfigFactory(final IndexConfigFactory configFactory) {
181
		this.configFactory = configFactory;
182
	}
183

    
184
	/**
185
	 * Gets the static configuration classpath.
186
	 * 
187
	 * @return the static configuration classpath
188
	 */
189
	public String getStaticConfigurationClasspath() {
190
		return staticConfigurationClasspath;
191
	}
192

    
193
	/**
194
	 * Sets the static configuration classpath.
195
	 * 
196
	 * @param staticConfigurationClasspath
197
	 *            the new static configuration classpath
198
	 */
199
	@Required
200
	public void setStaticConfigurationClasspath(final String staticConfigurationClasspath) {
201
		this.staticConfigurationClasspath = staticConfigurationClasspath;
202
	}
203

    
204
}
(9-9/9)