This repository has been archived by the owner on Jan 6, 2018. It is now read-only.
/
SchemaService.java
161 lines (137 loc) · 5.64 KB
/
SchemaService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Copyright (c) 2010 Yahoo! Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. See accompanying LICENSE file.
*/
package org.apache.oozie.service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.oozie.ErrorCode;
import org.apache.oozie.util.IOUtils;
import org.xml.sax.SAXException;
/**
* Service that loads Oozie workflow definition schema and registered extension
* schemas.
*/
public class SchemaService implements Service {
public static final String CONF_PREFIX = Service.CONF_PREFIX + "SchemaService.";
public static final String WF_CONF_EXT_SCHEMAS = CONF_PREFIX + "wf.ext.schemas";
public static final String COORD_CONF_EXT_SCHEMAS = CONF_PREFIX + "coord.ext.schemas";
public static final String BUNDLE_CONF_EXT_SCHEMAS = CONF_PREFIX + "bundle.ext.schemas";
public static final String SLA_CONF_EXT_SCHEMAS = CONF_PREFIX + "sla.ext.schemas";
public static final String SLA_NAME_SPACE_URI = "uri:oozie:sla:0.1";
public static final String COORDINATOR_NAMESPACE_URI_1 = "uri:oozie:coordinator:0.1";
private Schema wfSchema;
private Schema coordSchema;
private Schema bundleSchema;
private Schema slaSchema;
private static final String OOZIE_WORKFLOW_XSD[] = { "oozie-workflow-0.1.xsd", "oozie-workflow-0.2.xsd",
"oozie-workflow-0.2.5.xsd" };
private static final String OOZIE_COORDINATOR_XSD[] = { "oozie-coordinator-0.1.xsd", "oozie-coordinator-0.2.xsd" };
private static final String OOZIE_BUNDLE_XSD[] = { "oozie-bundle-0.1.xsd" };
private static final String OOZIE_SLA_SEMANTIC_XSD[] = { "gms-oozie-sla-0.1.xsd" };
private Schema loadSchema(Configuration conf, String[] baseSchemas, String extSchema) throws SAXException,
IOException {
List<StreamSource> sources = new ArrayList<StreamSource>();
for (String baseSchema : baseSchemas) {
sources.add(new StreamSource(IOUtils.getResourceAsStream(baseSchema, -1)));
}
String[] schemas = conf.getStrings(extSchema);
if (schemas != null) {
for (String schema : schemas) {
sources.add(new StreamSource(IOUtils.getResourceAsStream(schema, -1)));
}
}
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
return factory.newSchema(sources.toArray(new StreamSource[sources.size()]));
}
/**
* Initialize the service.
*
* @param services services instance.
* @throws ServiceException thrown if the service could not be initialized.
*/
public void init(Services services) throws ServiceException {
try {
wfSchema = loadSchema(services.getConf(), OOZIE_WORKFLOW_XSD, WF_CONF_EXT_SCHEMAS);
coordSchema = loadSchema(services.getConf(), OOZIE_COORDINATOR_XSD, COORD_CONF_EXT_SCHEMAS);
bundleSchema = loadSchema(services.getConf(), OOZIE_BUNDLE_XSD, BUNDLE_CONF_EXT_SCHEMAS);
slaSchema = loadSchema(services.getConf(), OOZIE_SLA_SEMANTIC_XSD, SLA_CONF_EXT_SCHEMAS);
bundleSchema = loadSchema(services.getConf(), OOZIE_BUNDLE_XSD, BUNDLE_CONF_EXT_SCHEMAS);
}
catch (SAXException ex) {
throw new ServiceException(ErrorCode.E0130, ex.getMessage(), ex);
}
catch (IOException ex) {
throw new ServiceException(ErrorCode.E0131, ex.getMessage(), ex);
}
}
/**
* Return the public interface of the service.
*
* @return {@link SchemaService}.
*/
public Class<? extends Service> getInterface() {
return SchemaService.class;
}
/**
* Destroy the service.
*/
public void destroy() {
wfSchema = null;
bundleSchema = null;
slaSchema = null;
coordSchema = null;
}
/**
* Return the schema for XML validation of application definitions.
*
* @param schemaName: Name of schema definition (i.e.
* WORKFLOW/COORDINATOR/BUNDLE)
* @return the schema for XML validation of application definitions.
*/
public Schema getSchema(SchemaName schemaName) {
Schema returnSchema = null;
if (schemaName == SchemaName.WORKFLOW) {
returnSchema = wfSchema;
}
else if (schemaName == SchemaName.COORDINATOR) {
returnSchema = coordSchema;
}
else if (schemaName == SchemaName.BUNDLE) {
returnSchema = bundleSchema;
}
else if (schemaName == SchemaName.SLA_ORIGINAL) {
returnSchema = slaSchema;
}
else {
throw new RuntimeException("No schema found with name " + schemaName);
}
return returnSchema;
}
public enum SchemaName {
WORKFLOW(1), COORDINATOR(2), SLA_ORIGINAL(3), BUNDLE(4);
private final int id;
private SchemaName(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
}