Project

General

Profile

1
package eu.dnetlib.enabling.is.store;
2

    
3
import java.io.IOException;
4
import java.io.StringWriter;
5
import java.net.URL;
6

    
7
import javax.xml.parsers.ParserConfigurationException;
8
import javax.xml.xpath.XPathExpressionException;
9

    
10
import org.apache.commons.io.FilenameUtils;
11
import org.apache.commons.io.IOUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.springframework.beans.factory.annotation.Required;
15
import org.xml.sax.SAXException;
16

    
17
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
18
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
19
import eu.dnetlib.enabling.tools.ServiceLocator;
20
import eu.dnetlib.enabling.tools.StreamOpaqueResource;
21

    
22
/**
23
 * Abstract resource loading code.
24
 * 
25
 * @author marko, michele
26
 * 
27
 */
28
public class AbstractContentInitializer {
29

    
30
	/**
31
	 * logger.
32
	 */
33
	private static final Log log = LogFactory.getLog(AbstractContentInitializer.class); // NOPMD by marko on 11/24/08 5:02 PM
34
	/**
35
	 * store service locator.
36
	 */
37
	private ServiceLocator<ISRegistryService> registryLocator;
38
	/**
39
	 * helper class used to bypass the registry and import resources as-is from a backup dump.
40
	 */
41
	private BulkResourceImporter bulkImporter;
42

    
43
	private int timeToSleep;
44

    
45
	/**
46
	 * register a schema from a local resource.
47
	 * 
48
	 * @param url
49
	 *            url
50
	 * @throws IOException
51
	 *             happens
52
	 * @throws ISRegistryException
53
	 *             could happen
54
	 */
55
	protected void registerSchema(final URL url) throws IOException, ISRegistryException {
56
		final String resourceType = FilenameUtils.getBaseName(url.getPath());
57
		log.debug("registering schema: " + resourceType);
58

    
59
		final StringWriter writer = new StringWriter();
60
		IOUtils.copy(url.openStream(), writer);
61
		ISRegistryService service = null;
62
		while (service == null) {
63
			try {
64
				service = registryLocator.getService();
65
				service.addResourceType(resourceType, writer.getBuffer().toString());
66
				log.info("The is registry service is ready ");
67
			} catch (Exception e) {
68
				log.fatal("The is registry service is not ready ", e);
69
				try {
70
					Thread.sleep(timeToSleep);
71
				} catch (InterruptedException e1) {
72
					log.error(e1);
73
				}
74
			}
75
		}
76

    
77
	}
78

    
79
	/**
80
	 * register a profile from a local resource.
81
	 * 
82
	 * @param url
83
	 *            url
84
	 * @throws IOException
85
	 *             could happen
86
	 * @throws ISRegistryException
87
	 *             could happen
88
	 * @throws ParserConfigurationException
89
	 *             could happen
90
	 * @throws SAXException
91
	 *             could happen
92
	 * @throws XPathExpressionException
93
	 *             could happen
94
	 */
95
	protected void registerProfile(final URL url) throws IOException, ISRegistryException, XPathExpressionException, SAXException, ParserConfigurationException {
96
		log.debug("registering profile: " + url);
97

    
98
		bulkImporter.importResource(new StreamOpaqueResource(url.openStream()));
99
	}
100

    
101
	public void setRegistryLocator(final ServiceLocator<ISRegistryService> registryLocator) {
102
		this.registryLocator = registryLocator;
103
	}
104

    
105
	public ServiceLocator<ISRegistryService> getRegistryLocator() {
106
		return registryLocator;
107
	}
108

    
109
	@Required
110
	public void setBulkImporter(final BulkResourceImporter bulkImporter) {
111
		this.bulkImporter = bulkImporter;
112
	}
113

    
114
	public BulkResourceImporter getBulkImporter() {
115
		return bulkImporter;
116
	}
117

    
118
	/**
119
	 * @return the timeToSleep
120
	 */
121
	public int getTimeToSleep() {
122
		return timeToSleep;
123
	}
124

    
125
	/**
126
	 * @param timeToSleep
127
	 *            the timeToSleep to set
128
	 */
129
	@Required
130
	public void setTimeToSleep(final int timeToSleep) {
131
		this.timeToSleep = timeToSleep;
132
	}
133

    
134
}
(1-1/2)