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