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 019package org.apache.oozie.service; 020 021import org.apache.hadoop.conf.Configuration; 022import org.apache.oozie.util.IOUtils; 023import org.apache.oozie.ErrorCode; 024import org.xml.sax.SAXException; 025 026import javax.xml.XMLConstants; 027import javax.xml.transform.stream.StreamSource; 028import javax.xml.validation.Schema; 029import javax.xml.validation.SchemaFactory; 030import java.io.IOException; 031import java.util.ArrayList; 032import java.util.List; 033 034/** 035 * Service that loads Oozie workflow definition schema and registered extension schemas. 036 */ 037public class WorkflowSchemaService implements Service { 038 039 public static final String CONF_PREFIX = Service.CONF_PREFIX + "WorkflowSchemaService."; 040 041 public static final String CONF_EXT_SCHEMAS = CONF_PREFIX + "ext.schemas"; 042 043 private Schema dagSchema; 044 045 private static final String OOZIE_WORKFLOW_XSD = "oozie-workflow-0.1.xsd"; 046 047 private Schema loadSchema(Configuration conf) throws SAXException, IOException { 048 List<StreamSource> sources = new ArrayList<StreamSource>(); 049 sources.add(new StreamSource(IOUtils.getResourceAsStream(OOZIE_WORKFLOW_XSD, -1))); 050 String[] schemas = conf.getStrings(CONF_EXT_SCHEMAS); 051 if (schemas != null) { 052 for (String schema : schemas) { 053 sources.add(new StreamSource(IOUtils.getResourceAsStream(schema, -1))); 054 } 055 } 056 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 057 return factory.newSchema(sources.toArray(new StreamSource[sources.size()])); 058 } 059 060 /** 061 * Initialize the service. 062 * 063 * @param services services instance. 064 * @throws ServiceException thrown if the service could not be initialized. 065 */ 066 public void init(Services services) throws ServiceException { 067 try { 068 dagSchema = loadSchema(services.getConf()); 069 } 070 catch (SAXException ex) { 071 throw new ServiceException(ErrorCode.E0130, ex.getMessage(), ex); 072 } 073 catch (IOException ex) { 074 throw new ServiceException(ErrorCode.E0131, ex.getMessage(), ex); 075 } 076 } 077 078 /** 079 * Return the public interface of the service. 080 * 081 * @return {@link WorkflowSchemaService}. 082 */ 083 public Class<? extends Service> getInterface() { 084 return WorkflowSchemaService.class; 085 } 086 087 /** 088 * Destroy the service. 089 */ 090 public void destroy() { 091 dagSchema = null; 092 } 093 094 /** 095 * Return the schema for XML validation of application definitions. 096 * 097 * @return the schema for XML validation of application definitions. 098 */ 099 public Schema getSchema() { 100 return dagSchema; 101 } 102 103}