This project has retired. For details please refer to its
Attic page.
001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.oozie.service;
019
020 import java.io.IOException;
021 import java.util.ArrayList;
022 import java.util.List;
023
024 import javax.xml.XMLConstants;
025 import javax.xml.transform.stream.StreamSource;
026 import javax.xml.validation.Schema;
027 import javax.xml.validation.SchemaFactory;
028
029 import org.apache.hadoop.conf.Configuration;
030 import org.apache.oozie.ErrorCode;
031 import org.apache.oozie.util.IOUtils;
032 import org.xml.sax.SAXException;
033
034 /**
035 * Service that loads Oozie workflow definition schema and registered extension
036 * schemas.
037 */
038 public class SchemaService implements Service {
039
040 public static final String CONF_PREFIX = Service.CONF_PREFIX + "SchemaService.";
041
042 public static final String WF_CONF_EXT_SCHEMAS = CONF_PREFIX + "wf.ext.schemas";
043
044 public static final String COORD_CONF_EXT_SCHEMAS = CONF_PREFIX + "coord.ext.schemas";
045
046 public static final String BUNDLE_CONF_EXT_SCHEMAS = CONF_PREFIX + "bundle.ext.schemas";
047
048 public static final String SLA_CONF_EXT_SCHEMAS = CONF_PREFIX + "sla.ext.schemas";
049
050 public static final String SLA_NAME_SPACE_URI = "uri:oozie:sla:0.1";
051
052 public static final String COORDINATOR_NAMESPACE_URI_1 = "uri:oozie:coordinator:0.1";
053
054 private Schema wfSchema;
055
056 private Schema coordSchema;
057
058 private Schema bundleSchema;
059
060 private Schema slaSchema;
061
062 private static final String OOZIE_WORKFLOW_XSD[] = {
063 "oozie-workflow-0.1.xsd",
064 "oozie-workflow-0.2.xsd",
065 "oozie-workflow-0.2.5.xsd",
066 "oozie-workflow-0.3.xsd"};
067 private static final String OOZIE_COORDINATOR_XSD[] = { "oozie-coordinator-0.1.xsd", "oozie-coordinator-0.2.xsd", "oozie-coordinator-0.3.xsd"};
068 private static final String OOZIE_BUNDLE_XSD[] = { "oozie-bundle-0.1.xsd" };
069 private static final String OOZIE_SLA_SEMANTIC_XSD[] = { "gms-oozie-sla-0.1.xsd" };
070
071 private Schema loadSchema(Configuration conf, String[] baseSchemas, String extSchema) throws SAXException,
072 IOException {
073 List<StreamSource> sources = new ArrayList<StreamSource>();
074 for (String baseSchema : baseSchemas) {
075 sources.add(new StreamSource(IOUtils.getResourceAsStream(baseSchema, -1)));
076 }
077 String[] schemas = conf.getStrings(extSchema);
078 if (schemas != null) {
079 for (String schema : schemas) {
080 sources.add(new StreamSource(IOUtils.getResourceAsStream(schema, -1)));
081 }
082 }
083 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
084 return factory.newSchema(sources.toArray(new StreamSource[sources.size()]));
085 }
086
087 /**
088 * Initialize the service.
089 *
090 * @param services services instance.
091 * @throws ServiceException thrown if the service could not be initialized.
092 */
093 public void init(Services services) throws ServiceException {
094 try {
095 wfSchema = loadSchema(services.getConf(), OOZIE_WORKFLOW_XSD, WF_CONF_EXT_SCHEMAS);
096 coordSchema = loadSchema(services.getConf(), OOZIE_COORDINATOR_XSD, COORD_CONF_EXT_SCHEMAS);
097 bundleSchema = loadSchema(services.getConf(), OOZIE_BUNDLE_XSD, BUNDLE_CONF_EXT_SCHEMAS);
098 slaSchema = loadSchema(services.getConf(), OOZIE_SLA_SEMANTIC_XSD, SLA_CONF_EXT_SCHEMAS);
099 bundleSchema = loadSchema(services.getConf(), OOZIE_BUNDLE_XSD, BUNDLE_CONF_EXT_SCHEMAS);
100 }
101 catch (SAXException ex) {
102 throw new ServiceException(ErrorCode.E0130, ex.getMessage(), ex);
103 }
104 catch (IOException ex) {
105 throw new ServiceException(ErrorCode.E0131, ex.getMessage(), ex);
106 }
107 }
108
109 /**
110 * Return the public interface of the service.
111 *
112 * @return {@link SchemaService}.
113 */
114 public Class<? extends Service> getInterface() {
115 return SchemaService.class;
116 }
117
118 /**
119 * Destroy the service.
120 */
121 public void destroy() {
122 wfSchema = null;
123 bundleSchema = null;
124 slaSchema = null;
125 coordSchema = null;
126 }
127
128 /**
129 * Return the schema for XML validation of application definitions.
130 *
131 * @param schemaName: Name of schema definition (i.e.
132 * WORKFLOW/COORDINATOR/BUNDLE)
133 * @return the schema for XML validation of application definitions.
134 */
135 public Schema getSchema(SchemaName schemaName) {
136 Schema returnSchema = null;
137 if (schemaName == SchemaName.WORKFLOW) {
138 returnSchema = wfSchema;
139 }
140 else if (schemaName == SchemaName.COORDINATOR) {
141 returnSchema = coordSchema;
142 }
143 else if (schemaName == SchemaName.BUNDLE) {
144 returnSchema = bundleSchema;
145 }
146 else if (schemaName == SchemaName.SLA_ORIGINAL) {
147 returnSchema = slaSchema;
148 }
149 else {
150 throw new RuntimeException("No schema found with name " + schemaName);
151 }
152 return returnSchema;
153 }
154
155 public enum SchemaName {
156 WORKFLOW(1), COORDINATOR(2), SLA_ORIGINAL(3), BUNDLE(4);
157 private final int id;
158
159 private SchemaName(int id) {
160 this.id = id;
161 }
162
163 public int getId() {
164 return id;
165 }
166 }
167 }