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 */ 018package org.apache.oozie.service; 019 020import org.apache.hadoop.util.ReflectionUtils; 021import org.apache.oozie.action.ActionExecutor; 022import org.apache.oozie.action.control.EndActionExecutor; 023import org.apache.oozie.action.control.ForkActionExecutor; 024import org.apache.oozie.action.control.JoinActionExecutor; 025import org.apache.oozie.action.control.KillActionExecutor; 026import org.apache.oozie.action.control.StartActionExecutor; 027import org.apache.oozie.service.Service; 028import org.apache.oozie.service.ServiceException; 029import org.apache.oozie.service.Services; 030import org.apache.oozie.util.ParamChecker; 031import org.apache.oozie.util.XLog; 032import org.apache.oozie.ErrorCode; 033import java.util.HashMap; 034import java.util.Map; 035import java.util.Set; 036 037public class ActionService implements Service { 038 039 public static final String CONF_ACTION_EXECUTOR_CLASSES = CONF_PREFIX + "ActionService.executor.classes"; 040 041 public static final String CONF_ACTION_EXECUTOR_EXT_CLASSES = CONF_PREFIX + "ActionService.executor.ext.classes"; 042 043 private Services services; 044 private Map<String, Class<? extends ActionExecutor>> executors; 045 private static XLog LOG = XLog.getLog(ActionService.class); 046 047 @SuppressWarnings("unchecked") 048 public void init(Services services) throws ServiceException { 049 this.services = services; 050 ActionExecutor.enableInit(); 051 ActionExecutor.resetInitInfo(); 052 ActionExecutor.disableInit(); 053 executors = new HashMap<String, Class<? extends ActionExecutor>>(); 054 055 Class<? extends ActionExecutor>[] classes = new Class[] { StartActionExecutor.class, 056 EndActionExecutor.class, KillActionExecutor.class, ForkActionExecutor.class, JoinActionExecutor.class }; 057 registerExecutors(classes); 058 059 classes = (Class<? extends ActionExecutor>[]) services.getConf().getClasses(CONF_ACTION_EXECUTOR_CLASSES); 060 registerExecutors(classes); 061 062 classes = (Class<? extends ActionExecutor>[]) services.getConf().getClasses(CONF_ACTION_EXECUTOR_EXT_CLASSES); 063 registerExecutors(classes); 064 } 065 066 private void registerExecutors(Class<? extends ActionExecutor>[] classes) throws ServiceException { 067 if (classes != null) { 068 for (Class<? extends ActionExecutor> executorClass : classes) { 069 register(executorClass); 070 } 071 } 072 } 073 074 public void destroy() { 075 ActionExecutor.enableInit(); 076 ActionExecutor.resetInitInfo(); 077 ActionExecutor.disableInit(); 078 executors = null; 079 } 080 081 public Class<? extends Service> getInterface() { 082 return ActionService.class; 083 } 084 085 public void register(Class<? extends ActionExecutor> klass) throws ServiceException { 086 ActionExecutor executor = (ActionExecutor) ReflectionUtils.newInstance(klass, services.getConf()); 087 LOG.trace("Registering action type [{0}] class [{1}]", executor.getType(), klass); 088 if (executors.containsKey(executor.getType())) { 089 throw new ServiceException(ErrorCode.E0150, executor.getType()); 090 } 091 ActionExecutor.enableInit(); 092 executor.initActionType(); 093 ActionExecutor.disableInit(); 094 executors.put(executor.getType(), klass); 095 LOG.trace("Registered Action executor for action type [{0}] class [{1}]", executor.getType(), klass); 096 } 097 098 public ActionExecutor getExecutor(String actionType) { 099 ParamChecker.notEmpty(actionType, "actionType"); 100 Class<? extends ActionExecutor> executorClass = executors.get(actionType); 101 return (executorClass != null) ? (ActionExecutor) ReflectionUtils.newInstance(executorClass, null) : null; 102 } 103 104 Set<String> getActionTypes() { 105 return executors.keySet(); 106 } 107}