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