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 org.apache.hadoop.util.ReflectionUtils; 021 import org.apache.oozie.action.ActionExecutor; 022 import org.apache.oozie.action.control.EndActionExecutor; 023 import org.apache.oozie.action.control.ForkActionExecutor; 024 import org.apache.oozie.action.control.JoinActionExecutor; 025 import org.apache.oozie.action.control.KillActionExecutor; 026 import org.apache.oozie.action.control.StartActionExecutor; 027 import org.apache.oozie.service.Service; 028 import org.apache.oozie.service.ServiceException; 029 import org.apache.oozie.service.Services; 030 import org.apache.oozie.util.ParamChecker; 031 import org.apache.oozie.util.XLog; 032 import org.apache.oozie.ErrorCode; 033 034 import java.util.HashMap; 035 import java.util.Map; 036 037 public 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 046 @SuppressWarnings("unchecked") 047 public void init(Services services) throws ServiceException { 048 this.services = services; 049 ActionExecutor.enableInit(); 050 ActionExecutor.resetInitInfo(); 051 ActionExecutor.disableInit(); 052 executors = new HashMap<String, Class<? extends ActionExecutor>>(); 053 054 Class<? extends ActionExecutor>[] classes = new Class[] { StartActionExecutor.class, 055 EndActionExecutor.class, KillActionExecutor.class, ForkActionExecutor.class, JoinActionExecutor.class }; 056 registerExecutors(classes); 057 058 classes = (Class<? extends ActionExecutor>[]) services.getConf().getClasses(CONF_ACTION_EXECUTOR_CLASSES); 059 registerExecutors(classes); 060 061 classes = (Class<? extends ActionExecutor>[]) services.getConf().getClasses(CONF_ACTION_EXECUTOR_EXT_CLASSES); 062 registerExecutors(classes); 063 } 064 065 private void registerExecutors(Class<? extends ActionExecutor>[] classes) throws ServiceException { 066 if (classes != null) { 067 for (Class<? extends ActionExecutor> executorClass : classes) { 068 register(executorClass); 069 } 070 } 071 } 072 073 public void destroy() { 074 ActionExecutor.enableInit(); 075 ActionExecutor.resetInitInfo(); 076 ActionExecutor.disableInit(); 077 executors = null; 078 } 079 080 public Class<? extends Service> getInterface() { 081 return ActionService.class; 082 } 083 084 public void register(Class<? extends ActionExecutor> klass) throws ServiceException { 085 XLog log = XLog.getLog(getClass()); 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 }