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.client.rest; 019 020 import org.apache.oozie.client.BundleJob; 021 import org.apache.oozie.client.CoordinatorAction; 022 import org.apache.oozie.client.CoordinatorJob; 023 import org.apache.oozie.client.WorkflowAction; 024 import org.apache.oozie.client.WorkflowJob; 025 import org.json.simple.JSONArray; 026 import org.json.simple.JSONObject; 027 028 import java.lang.reflect.InvocationHandler; 029 import java.lang.reflect.Method; 030 import java.lang.reflect.Proxy; 031 import java.util.ArrayList; 032 import java.util.Date; 033 import java.util.HashMap; 034 import java.util.List; 035 import java.util.Map; 036 037 /** 038 * JSON to bean converter for {@link WorkflowAction}, {@link WorkflowJob}, {@link CoordinatorAction} 039 * and {@link CoordinatorJob}. 040 * <p/> 041 * It uses JDK dynamic proxy to create bean instances. 042 */ 043 public class JsonToBean { 044 045 private static class Property { 046 String label; 047 Class type; 048 boolean isList; 049 050 public Property(String label, Class type) { 051 this(label, type, false); 052 } 053 054 public Property(String label, Class type, boolean isList) { 055 this.label = label; 056 this.type = type; 057 this.isList = isList; 058 } 059 } 060 061 private static final Map<String, Property> WF_JOB = new HashMap<String, Property>(); 062 private static final Map<String, Property> WF_ACTION = new HashMap<String, Property>(); 063 private static final Map<String, Property> COORD_JOB = new HashMap<String, Property>(); 064 private static final Map<String, Property> COORD_ACTION = new HashMap<String, Property>(); 065 private static final Map<String, Property> BUNDLE_JOB = new HashMap<String, Property>(); 066 067 static { 068 WF_ACTION.put("getId", new Property(JsonTags.WORKFLOW_ACTION_ID, String.class)); 069 WF_ACTION.put("getName", new Property(JsonTags.WORKFLOW_ACTION_NAME, String.class)); 070 WF_ACTION.put("getType", new Property(JsonTags.WORKFLOW_ACTION_TYPE, String.class)); 071 WF_ACTION.put("getConf", new Property(JsonTags.WORKFLOW_ACTION_CONF, String.class)); 072 WF_ACTION.put("getStatus", new Property(JsonTags.WORKFLOW_ACTION_STATUS, WorkflowAction.Status.class)); 073 WF_ACTION.put("getRetries", new Property(JsonTags.WORKFLOW_ACTION_RETRIES, Integer.TYPE)); 074 WF_ACTION.put("getStartTime", new Property(JsonTags.WORKFLOW_ACTION_START_TIME, Date.class)); 075 WF_ACTION.put("getEndTime", new Property(JsonTags.WORKFLOW_ACTION_END_TIME, Date.class)); 076 WF_ACTION.put("getTransition", new Property(JsonTags.WORKFLOW_ACTION_TRANSITION, String.class)); 077 WF_ACTION.put("getData", new Property(JsonTags.WORKFLOW_ACTION_DATA, String.class)); 078 WF_ACTION.put("getExternalId", new Property(JsonTags.WORKFLOW_ACTION_EXTERNAL_ID, String.class)); 079 WF_ACTION.put("getExternalStatus", new Property(JsonTags.WORKFLOW_ACTION_EXTERNAL_STATUS, String.class)); 080 WF_ACTION.put("getTrackerUri", new Property(JsonTags.WORKFLOW_ACTION_TRACKER_URI, String.class)); 081 WF_ACTION.put("getConsoleUrl", new Property(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, String.class)); 082 WF_ACTION.put("getErrorCode", new Property(JsonTags.WORKFLOW_ACTION_ERROR_CODE, String.class)); 083 WF_ACTION.put("getErrorMessage", new Property(JsonTags.WORKFLOW_ACTION_ERROR_MESSAGE, String.class)); 084 WF_ACTION.put("toString", new Property(JsonTags.TO_STRING, String.class)); 085 086 WF_JOB.put("getAppPath", new Property(JsonTags.WORKFLOW_APP_PATH, String.class)); 087 WF_JOB.put("getAppName", new Property(JsonTags.WORKFLOW_APP_NAME, String.class)); 088 WF_JOB.put("getId", new Property(JsonTags.WORKFLOW_ID, String.class)); 089 WF_JOB.put("getConf", new Property(JsonTags.WORKFLOW_CONF, String.class)); 090 WF_JOB.put("getStatus", new Property(JsonTags.WORKFLOW_STATUS, WorkflowJob.Status.class)); 091 WF_JOB.put("getLastModifiedTime", new Property(JsonTags.WORKFLOW_LAST_MOD_TIME, Date.class)); 092 WF_JOB.put("getCreatedTime", new Property(JsonTags.WORKFLOW_CREATED_TIME, Date.class)); 093 WF_JOB.put("getStartTime", new Property(JsonTags.WORKFLOW_CREATED_TIME, Date.class)); 094 WF_JOB.put("getEndTime", new Property(JsonTags.WORKFLOW_END_TIME, Date.class)); 095 WF_JOB.put("getUser", new Property(JsonTags.WORKFLOW_USER, String.class)); 096 WF_JOB.put("getGroup", new Property(JsonTags.WORKFLOW_GROUP, String.class)); 097 WF_JOB.put("getRun", new Property(JsonTags.WORKFLOW_RUN, Integer.TYPE)); 098 WF_JOB.put("getConsoleUrl", new Property(JsonTags.WORKFLOW_CONSOLE_URL, String.class)); 099 WF_JOB.put("getActions", new Property(JsonTags.WORKFLOW_ACTIONS, WorkflowAction.class, true)); 100 WF_JOB.put("getParentId", new Property(JsonTags.WORKFLOW_PARENT_ID, String.class)); 101 WF_JOB.put("toString", new Property(JsonTags.TO_STRING, String.class)); 102 103 COORD_ACTION.put("getId", new Property(JsonTags.COORDINATOR_ACTION_ID, String.class)); 104 COORD_ACTION.put("getJobId", new Property(JsonTags.COORDINATOR_JOB_ID, String.class)); 105 COORD_ACTION.put("getActionNumber", new Property(JsonTags.COORDINATOR_ACTION_NUMBER, Integer.TYPE)); 106 COORD_ACTION.put("getCreatedConf", new Property(JsonTags.COORDINATOR_ACTION_CREATED_CONF, String.class)); 107 COORD_ACTION.put("getCreatedTime", new Property(JsonTags.COORDINATOR_ACTION_CREATED_TIME, Date.class)); 108 COORD_ACTION.put("getNominalTime", new Property(JsonTags.COORDINATOR_ACTION_NOMINAL_TIME, Date.class)); 109 COORD_ACTION.put("getExternalId", new Property(JsonTags.COORDINATOR_ACTION_EXTERNALID, String.class)); 110 COORD_ACTION.put("getStatus", new Property(JsonTags.COORDINATOR_ACTION_STATUS, CoordinatorAction.Status.class)); 111 COORD_ACTION.put("getRunConf", new Property(JsonTags.COORDINATOR_ACTION_RUNTIME_CONF, String.class)); 112 COORD_ACTION 113 .put("getLastModifiedTime", new Property(JsonTags.COORDINATOR_ACTION_LAST_MODIFIED_TIME, Date.class)); 114 COORD_ACTION 115 .put("getMissingDependencies", new Property(JsonTags.COORDINATOR_ACTION_MISSING_DEPS, String.class)); 116 COORD_ACTION.put("getExternalStatus", new Property(JsonTags.COORDINATOR_ACTION_EXTERNAL_STATUS, String.class)); 117 COORD_ACTION.put("getTrackerUri", new Property(JsonTags.COORDINATOR_ACTION_TRACKER_URI, String.class)); 118 COORD_ACTION.put("getConsoleUrl", new Property(JsonTags.COORDINATOR_ACTION_CONSOLE_URL, String.class)); 119 COORD_ACTION.put("getErrorCode", new Property(JsonTags.COORDINATOR_ACTION_ERROR_CODE, String.class)); 120 COORD_ACTION.put("getErrorMessage", new Property(JsonTags.COORDINATOR_ACTION_ERROR_MESSAGE, String.class)); 121 COORD_ACTION.put("toString", new Property(JsonTags.TO_STRING, String.class)); 122 123 COORD_JOB.put("getAppPath", new Property(JsonTags.COORDINATOR_JOB_PATH, String.class)); 124 COORD_JOB.put("getAppName", new Property(JsonTags.COORDINATOR_JOB_NAME, String.class)); 125 COORD_JOB.put("getId", new Property(JsonTags.COORDINATOR_JOB_ID, String.class)); 126 COORD_JOB.put("getConf", new Property(JsonTags.COORDINATOR_JOB_CONF, String.class)); 127 COORD_JOB.put("getStatus", new Property(JsonTags.COORDINATOR_JOB_STATUS, CoordinatorJob.Status.class)); 128 COORD_JOB.put("getExecutionOrder", 129 new Property(JsonTags.COORDINATOR_JOB_EXECUTIONPOLICY, CoordinatorJob.Execution.class)); 130 COORD_JOB.put("getFrequency", new Property(JsonTags.COORDINATOR_JOB_FREQUENCY, Integer.TYPE)); 131 COORD_JOB.put("getTimeUnit", new Property(JsonTags.COORDINATOR_JOB_TIMEUNIT, CoordinatorJob.Timeunit.class)); 132 COORD_JOB.put("getTimeZone", new Property(JsonTags.COORDINATOR_JOB_TIMEZONE, String.class)); 133 COORD_JOB.put("getConcurrency", new Property(JsonTags.COORDINATOR_JOB_CONCURRENCY, Integer.TYPE)); 134 COORD_JOB.put("getTimeout", new Property(JsonTags.COORDINATOR_JOB_TIMEOUT, Integer.TYPE)); 135 COORD_JOB.put("getLastActionTime", new Property(JsonTags.COORDINATOR_JOB_LAST_ACTION_TIME, Date.class)); 136 COORD_JOB.put("getNextMaterializedTime", 137 new Property(JsonTags.COORDINATOR_JOB_NEXT_MATERIALIZED_TIME, Date.class)); 138 COORD_JOB.put("getStartTime", new Property(JsonTags.COORDINATOR_JOB_START_TIME, Date.class)); 139 COORD_JOB.put("getEndTime", new Property(JsonTags.COORDINATOR_JOB_END_TIME, Date.class)); 140 COORD_JOB.put("getUser", new Property(JsonTags.COORDINATOR_JOB_USER, String.class)); 141 COORD_JOB.put("getGroup", new Property(JsonTags.COORDINATOR_JOB_GROUP, String.class)); 142 COORD_JOB.put("getConsoleUrl", new Property(JsonTags.COORDINATOR_JOB_CONSOLE_URL, String.class)); 143 COORD_JOB.put("getActions", new Property(JsonTags.COORDINATOR_ACTIONS, CoordinatorAction.class, true)); 144 COORD_JOB.put("toString", new Property(JsonTags.TO_STRING, String.class)); 145 146 BUNDLE_JOB.put("getActions", new Property(JsonTags.COORDINATOR_ACTIONS, CoordinatorAction.class, true)); 147 148 BUNDLE_JOB.put("getAppPath",new Property(JsonTags.BUNDLE_JOB_PATH, String.class)); 149 BUNDLE_JOB.put("getAppName",new Property(JsonTags.BUNDLE_JOB_NAME, String.class)); 150 BUNDLE_JOB.put("getId",new Property(JsonTags.BUNDLE_JOB_ID, String.class)); 151 BUNDLE_JOB.put("getExternalId",new Property(JsonTags.BUNDLE_JOB_EXTERNAL_ID, String.class)); 152 BUNDLE_JOB.put("getConf",new Property(JsonTags.BUNDLE_JOB_CONF, String.class)); 153 BUNDLE_JOB.put("getStatus",new Property(JsonTags.BUNDLE_JOB_STATUS, BundleJob.Status.class)); 154 BUNDLE_JOB.put("getTimeUnit",new Property(JsonTags.BUNDLE_JOB_TIMEUNIT, BundleJob.Timeunit.class)); 155 BUNDLE_JOB.put("getTimeout",new Property(JsonTags.BUNDLE_JOB_TIMEOUT, Integer.TYPE)); 156 BUNDLE_JOB.put("getKickoffTime",new Property(JsonTags.BUNDLE_JOB_KICKOFF_TIME, Date.class)); 157 BUNDLE_JOB.put("getStartTime",new Property(JsonTags.BUNDLE_JOB_START_TIME, Date.class)); 158 BUNDLE_JOB.put("getEndTime",new Property(JsonTags.BUNDLE_JOB_END_TIME, Date.class)); 159 BUNDLE_JOB.put("getPauseTime",new Property(JsonTags.BUNDLE_JOB_PAUSE_TIME, Date.class)); 160 BUNDLE_JOB.put("getCreatedTime",new Property(JsonTags.BUNDLE_JOB_CREATED_TIME, Date.class)); 161 BUNDLE_JOB.put("getUser",new Property(JsonTags.BUNDLE_JOB_USER, String.class)); 162 BUNDLE_JOB.put("getGroup",new Property(JsonTags.BUNDLE_JOB_GROUP, String.class)); 163 BUNDLE_JOB.put("getConsoleUrl",new Property(JsonTags.BUNDLE_JOB_CONSOLE_URL, String.class)); 164 BUNDLE_JOB.put("getCoordinators",new Property(JsonTags.BUNDLE_COORDINATOR_JOBS, CoordinatorJob.class, true)); 165 BUNDLE_JOB.put("toString", new Property(JsonTags.TO_STRING, String.class)); 166 } 167 168 /** 169 * The dynamic proxy invocation handler used to convert JSON values to bean properties using a mapping. 170 */ 171 private static class JsonInvocationHandler implements InvocationHandler { 172 private final Map<String, Property> mapping; 173 private final JSONObject json; 174 175 /** 176 * Invocation handler constructor. 177 * 178 * @param mapping property to JSON/type-info mapping. 179 * @param json the json object to back the property values. 180 */ 181 public JsonInvocationHandler(Map<String, Property> mapping, JSONObject json) { 182 this.mapping = mapping; 183 this.json = json; 184 } 185 186 @Override 187 public Object invoke(Object o, Method method, Object[] objects) throws Throwable { 188 Property prop = mapping.get(method.getName()); 189 if (prop == null) { 190 throw new RuntimeException("Undefined method mapping: " + method.getName()); 191 } 192 if (prop.isList) { 193 if (prop.type == WorkflowAction.class) { 194 return createWorkflowActionList((JSONArray) json.get(prop.label)); 195 } 196 else if (prop.type == CoordinatorAction.class) { 197 return createCoordinatorActionList((JSONArray) json.get(prop.label)); 198 } 199 else if (prop.type == CoordinatorJob.class) { 200 return createCoordinatorJobList((JSONArray) json.get(prop.label)); 201 } 202 else { 203 throw new RuntimeException("Unsupported list type : " + prop.type.getSimpleName()); 204 } 205 } 206 else { 207 return parseType(prop.type, json.get(prop.label)); 208 } 209 } 210 211 @SuppressWarnings("unchecked") 212 private Object parseType(Class type, Object obj) { 213 if (type == String.class) { 214 return obj; 215 } 216 else if (type == Integer.TYPE) { 217 return (obj != null) ? new Integer(((Long) obj).intValue()) : new Integer(0); 218 } 219 else if (type == Long.TYPE) { 220 return (obj != null) ? obj : new Long(0); 221 } 222 else if (type == Date.class) { 223 return JsonUtils.parseDateRfc822((String) obj); 224 } 225 else if (type.isEnum()) { 226 return Enum.valueOf(type, (String) obj); 227 } 228 else if (type == WorkflowAction.class) { 229 return createWorkflowAction((JSONObject) obj); 230 } 231 else { 232 throw new RuntimeException("Unsupported type : " + type.getSimpleName()); 233 } 234 } 235 } 236 237 /** 238 * Creates a workflow action bean from a JSON object. 239 * 240 * @param json json object. 241 * @return a workflow action bean populated with the JSON object values. 242 */ 243 public static WorkflowAction createWorkflowAction(JSONObject json) { 244 return (WorkflowAction) Proxy.newProxyInstance(JsonToBean.class.getClassLoader(), 245 new Class[]{WorkflowAction.class}, 246 new JsonInvocationHandler(WF_ACTION, json)); 247 } 248 249 /** 250 * Creates a list of workflow action beans from a JSON array. 251 * 252 * @param json json array. 253 * @return a list of workflow action beans from a JSON array. 254 */ 255 public static List<WorkflowAction> createWorkflowActionList(JSONArray json) { 256 List<WorkflowAction> list = new ArrayList<WorkflowAction>(); 257 for (Object obj : json) { 258 list.add(createWorkflowAction((JSONObject) obj)); 259 } 260 return list; 261 } 262 263 /** 264 * Creates a workflow job bean from a JSON object. 265 * 266 * @param json json object. 267 * @return a workflow job bean populated with the JSON object values. 268 */ 269 public static WorkflowJob createWorkflowJob(JSONObject json) { 270 return (WorkflowJob) Proxy.newProxyInstance(JsonToBean.class.getClassLoader(), 271 new Class[]{WorkflowJob.class}, 272 new JsonInvocationHandler(WF_JOB, json)); 273 } 274 275 /** 276 * Creates a list of workflow job beans from a JSON array. 277 * 278 * @param json json array. 279 * @return a list of workflow job beans from a JSON array. 280 */ 281 public static List<WorkflowJob> createWorkflowJobList(JSONArray json) { 282 List<WorkflowJob> list = new ArrayList<WorkflowJob>(); 283 for (Object obj : json) { 284 list.add(createWorkflowJob((JSONObject) obj)); 285 } 286 return list; 287 } 288 289 /** 290 * Creates a coordinator action bean from a JSON object. 291 * 292 * @param json json object. 293 * @return a coordinator action bean populated with the JSON object values. 294 */ 295 public static CoordinatorAction createCoordinatorAction(JSONObject json) { 296 return (CoordinatorAction) Proxy.newProxyInstance(JsonToBean.class.getClassLoader(), 297 new Class[]{CoordinatorAction.class}, 298 new JsonInvocationHandler(COORD_ACTION, json)); 299 } 300 301 /** 302 * Creates a list of coordinator action beans from a JSON array. 303 * 304 * @param json json array. 305 * @return a list of coordinator action beans from a JSON array. 306 */ 307 public static List<CoordinatorAction> createCoordinatorActionList(JSONArray json) { 308 List<CoordinatorAction> list = new ArrayList<CoordinatorAction>(); 309 for (Object obj : json) { 310 list.add(createCoordinatorAction((JSONObject) obj)); 311 } 312 return list; 313 } 314 315 /** 316 * Creates a coordinator job bean from a JSON object. 317 * 318 * @param json json object. 319 * @return a coordinator job bean populated with the JSON object values. 320 */ 321 public static CoordinatorJob createCoordinatorJob(JSONObject json) { 322 return (CoordinatorJob) Proxy.newProxyInstance(JsonToBean.class.getClassLoader(), 323 new Class[]{CoordinatorJob.class}, 324 new JsonInvocationHandler(COORD_JOB, json)); 325 } 326 327 /** 328 * Creates a list of coordinator job beans from a JSON array. 329 * 330 * @param json json array. 331 * @return a list of coordinator job beans from a JSON array. 332 */ 333 public static List<CoordinatorJob> createCoordinatorJobList(JSONArray json) { 334 List<CoordinatorJob> list = new ArrayList<CoordinatorJob>(); 335 for (Object obj : json) { 336 list.add(createCoordinatorJob((JSONObject) obj)); 337 } 338 return list; 339 } 340 341 /** 342 * Creates a bundle job bean from a JSON object. 343 * 344 * @param json json object. 345 * @return a bundle job bean populated with the JSON object values. 346 */ 347 public static BundleJob createBundleJob(JSONObject json) { 348 return (BundleJob) Proxy.newProxyInstance(JsonToBean.class.getClassLoader(), 349 new Class[]{BundleJob.class}, 350 new JsonInvocationHandler(BUNDLE_JOB, json)); 351 } 352 353 /** 354 * Creates a list of bundle job beans from a JSON array. 355 * 356 * @param json json array. 357 * @return a list of bundle job beans from a JSON array. 358 */ 359 public static List<BundleJob> createBundleJobList(JSONArray json) { 360 List<BundleJob> list = new ArrayList<BundleJob>(); 361 for (Object obj : json) { 362 list.add(createBundleJob((JSONObject) obj)); 363 } 364 return list; 365 } 366 }