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.local; 020 021import org.apache.oozie.CoordinatorEngine; 022import org.apache.oozie.DagEngine; 023import org.apache.oozie.LocalOozieClient; 024import org.apache.oozie.LocalOozieClientCoord; 025import org.apache.oozie.client.OozieClient; 026import org.apache.oozie.service.CallbackService; 027import org.apache.oozie.service.CoordinatorEngineService; 028import org.apache.oozie.service.DagEngineService; 029import org.apache.oozie.service.Services; 030import org.apache.oozie.service.XLogService; 031import org.apache.oozie.servlet.CallbackServlet; 032import org.apache.oozie.test.EmbeddedServletContainer; 033import org.apache.oozie.util.ParamChecker; 034import org.apache.oozie.util.XLog; 035 036/** 037 * LocalOozie runs workflows in an embedded Oozie instance . <p/> LocalOozie is meant for development/debugging purposes 038 * only. 039 */ 040public class LocalOozie { 041 private static EmbeddedServletContainer container; 042 private static boolean localOozieActive = false; 043 044 /** 045 * Start LocalOozie. 046 * 047 * @throws Exception if LocalOozie could not be started. 048 */ 049 public synchronized static void start() throws Exception { 050 if (localOozieActive) { 051 throw new IllegalStateException("LocalOozie is already initialized"); 052 } 053 054 String log4jFile = System.getProperty(XLogService.LOG4J_FILE, null); 055 String oozieLocalLog = System.getProperty("oozielocal.log", null); 056 if (log4jFile == null) { 057 System.setProperty(XLogService.LOG4J_FILE, "localoozie-log4j.properties"); 058 } 059 if (oozieLocalLog == null) { 060 System.setProperty("oozielocal.log", "./oozielocal.log"); 061 } 062 063 localOozieActive = true; 064 new Services().init(); 065 066 if (log4jFile != null) { 067 System.setProperty(XLogService.LOG4J_FILE, log4jFile); 068 } 069 else { 070 System.getProperties().remove(XLogService.LOG4J_FILE); 071 } 072 if (oozieLocalLog != null) { 073 System.setProperty("oozielocal.log", oozieLocalLog); 074 } 075 else { 076 System.getProperties().remove("oozielocal.log"); 077 } 078 079 container = new EmbeddedServletContainer("oozie"); 080 container.addServletEndpoint("/callback", CallbackServlet.class); 081 container.start(); 082 String callbackUrl = container.getServletURL("/callback"); 083 Services.get().getConf().set(CallbackService.CONF_BASE_URL, callbackUrl); 084 XLog.getLog(LocalOozie.class).info("LocalOozie started callback set to [{0}]", callbackUrl); 085 } 086 087 /** 088 * Stop LocalOozie. 089 */ 090 public synchronized static void stop() { 091 RuntimeException thrown = null; 092 try { 093 if (container != null) { 094 container.stop(); 095 } 096 } 097 catch (RuntimeException ex) { 098 thrown = ex; 099 } 100 container = null; 101 XLog.getLog(LocalOozie.class).info("LocalOozie stopped"); 102 try { 103 Services.get().destroy(); 104 } 105 catch (RuntimeException ex) { 106 if (thrown != null) { 107 thrown = ex; 108 } 109 } 110 localOozieActive = false; 111 if (thrown != null) { 112 throw thrown; 113 } 114 } 115 116 /** 117 * Return a {@link org.apache.oozie.client.OozieClient} for LocalOozie. <p/> The returned instance is configured 118 * with the user name of the JVM (the value of the system property 'user.name'). <p/> The following methods of the 119 * client are NOP in the returned instance: {@link org.apache.oozie.client.OozieClient#validateWSVersion}, {@link 120 * org.apache.oozie.client.OozieClient#setHeader}, {@link org.apache.oozie.client.OozieClient#getHeader}, {@link 121 * org.apache.oozie.client.OozieClient#removeHeader}, {@link org.apache.oozie.client.OozieClient#getHeaderNames} and 122 * {@link org.apache.oozie.client.OozieClient#setSafeMode}. 123 * 124 * @return a {@link org.apache.oozie.client.OozieClient} for LocalOozie. 125 */ 126 public static OozieClient getClient() { 127 return getClient(System.getProperty("user.name")); 128 } 129 130 /** 131 * Return a {@link org.apache.oozie.client.OozieClient} for LocalOozie. 132 * <p/> 133 * The returned instance is configured with the user name of the JVM (the 134 * value of the system property 'user.name'). 135 * <p/> 136 * The following methods of the client are NOP in the returned instance: 137 * {@link org.apache.oozie.client.OozieClient#validateWSVersion}, 138 * {@link org.apache.oozie.client.OozieClient#setHeader}, 139 * {@link org.apache.oozie.client.OozieClient#getHeader}, 140 * {@link org.apache.oozie.client.OozieClient#removeHeader}, 141 * {@link org.apache.oozie.client.OozieClient#getHeaderNames} and 142 * {@link org.apache.oozie.client.OozieClient#setSafeMode}. 143 * 144 * @return a {@link org.apache.oozie.client.OozieClient} for LocalOozie. 145 */ 146 public static OozieClient getCoordClient() { 147 return getClientCoord(System.getProperty("user.name")); 148 } 149 150 /** 151 * Return a {@link org.apache.oozie.client.OozieClient} for LocalOozie configured for a given user. 152 * <p/> 153 * The following methods of the client are NOP in the returned instance: {@link org.apache.oozie.client.OozieClient#validateWSVersion}, 154 * {@link org.apache.oozie.client.OozieClient#setHeader}, {@link org.apache.oozie.client.OozieClient#getHeader}, {@link org.apache.oozie.client.OozieClient#removeHeader}, 155 * {@link org.apache.oozie.client.OozieClient#getHeaderNames} and {@link org.apache.oozie.client.OozieClient#setSafeMode}. 156 * 157 * @param user user name to use in LocalOozie for running workflows. 158 * @return a {@link org.apache.oozie.client.OozieClient} for LocalOozie configured for the given user. 159 */ 160 public static OozieClient getClient(String user) { 161 if (!localOozieActive) { 162 throw new IllegalStateException("LocalOozie is not initialized"); 163 } 164 ParamChecker.notEmpty(user, "user"); 165 DagEngine dagEngine = Services.get().get(DagEngineService.class).getDagEngine(user); 166 return new LocalOozieClient(dagEngine); 167 } 168 169 /** 170 * Return a {@link org.apache.oozie.client.OozieClient} for LocalOozie 171 * configured for a given user. 172 * <p/> 173 * The following methods of the client are NOP in the returned instance: 174 * {@link org.apache.oozie.client.OozieClient#validateWSVersion}, 175 * {@link org.apache.oozie.client.OozieClient#setHeader}, 176 * {@link org.apache.oozie.client.OozieClient#getHeader}, 177 * {@link org.apache.oozie.client.OozieClient#removeHeader}, 178 * {@link org.apache.oozie.client.OozieClient#getHeaderNames} and 179 * {@link org.apache.oozie.client.OozieClient#setSafeMode}. 180 * 181 * @param user user name to use in LocalOozie for running coordinator. 182 * @return a {@link org.apache.oozie.client.OozieClient} for LocalOozie 183 * configured for the given user. 184 */ 185 public static OozieClient getClientCoord(String user) { 186 if (!localOozieActive) { 187 throw new IllegalStateException("LocalOozie is not initialized"); 188 } 189 ParamChecker.notEmpty(user, "user"); 190 CoordinatorEngine coordEngine = Services.get().get(CoordinatorEngineService.class).getCoordinatorEngine(user); 191 return new LocalOozieClientCoord(coordEngine); 192 } 193 194}