001
014
015 package com.liferay.portal.kernel.license.messaging;
016
017 import com.liferay.portal.kernel.json.JSONFactoryUtil;
018 import com.liferay.portal.kernel.json.JSONObject;
019 import com.liferay.portal.kernel.messaging.Message;
020
021
024 public enum LicenseManagerMessageType {
025
026 LCS_AVAILABLE, SUBSCRIPTION_VALID, VALIDATE_LCS, VALIDATE_SUBSCRIPTION;
027
028 public static String MESSAGE_BUS_DESTINATION_REQUEST =
029 "liferay/lcs_request";
030
031 public static String MESSAGE_BUS_DESTINATION_STATUS = "liferay/lcs_status";
032
033 public static JSONObject getMessagePayload(Message message) {
034 if (!(message.getPayload() instanceof String)) {
035 return null;
036 }
037
038 try {
039 JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
040 (String)message.getPayload());
041
042 valueOf(jsonObject);
043
044 return jsonObject;
045 }
046 catch (Exception e) {
047 return null;
048 }
049 }
050
051 public static JSONObject getMessagePayload(Object object) {
052 if (!(object instanceof Message)) {
053 return null;
054 }
055
056 return getMessagePayload((Message)object);
057 }
058
059 public static LicenseManagerMessageType valueOf(JSONObject jsonObject) {
060 String type = jsonObject.getString("type");
061
062 return valueOf(type);
063 }
064
065 public Message createMessage() {
066 Message message = new Message();
067
068 message.setDestinationName(getDestinationName());
069
070 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
071
072 jsonObject.put("type", name());
073
074 message.setPayload(jsonObject.toString());
075
076 return message;
077 }
078
079 public Message createMessage(LCSPortletState state) {
080 Message message = new Message();
081
082 message.setDestinationName(getDestinationName());
083
084 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
085
086 jsonObject.put("type", name());
087 jsonObject.put("state", state.intValue());
088
089 message.setPayload(jsonObject.toString());
090
091 return message;
092 }
093
094 public String getDestinationName() {
095 if ((this == LCS_AVAILABLE) || (this == SUBSCRIPTION_VALID)) {
096 return MESSAGE_BUS_DESTINATION_STATUS;
097 }
098 else if ((this == VALIDATE_LCS) || (this == VALIDATE_SUBSCRIPTION)) {
099 return MESSAGE_BUS_DESTINATION_REQUEST;
100 }
101
102 return null;
103 }
104
105 }