001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.license.util;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
024    import com.liferay.portal.license.LicenseInfo;
025    
026    import java.util.Arrays;
027    import java.util.HashMap;
028    import java.util.List;
029    import java.util.Map;
030    import java.util.Set;
031    
032    /**
033     * @author Amos Fong
034     */
035    public class DefaultLicenseManagerImpl
036            implements com.liferay.portal.license.util.LicenseManager {
037    
038            public void checkLicense(String productId) {
039            }
040    
041            public List<Map<String, String>> getClusterLicenseProperties(
042                    String clusterNodeId) {
043    
044                    return null;
045            }
046    
047            public String getHostName() {
048                    return LicenseUtil.getHostName();
049            }
050    
051            public Set<String> getIpAddresses() {
052                    return LicenseUtil.getIpAddresses();
053            }
054    
055            public LicenseInfo getLicenseInfo(String productId) {
056                    return null;
057            }
058    
059            public List<Map<String, String>> getLicenseProperties() {
060                    return null;
061            }
062    
063            public Map<String, String> getLicenseProperties(String productId) {
064                    return null;
065            }
066    
067            public int getLicenseState(Map<String, String> licenseProperties) {
068                    String productId = licenseProperties.get("productId");
069    
070                    if (Validator.isNull(productId)) {
071                            return 0;
072                    }
073    
074                    try {
075                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
076    
077                            byte[] serverIdBytes = LicenseUtil.getServerIdBytes();
078    
079                            jsonObject.put("cmd", "GET_LICENSE_STATE");
080                            jsonObject.put("hostName", getHostName());
081                            jsonObject.put("ipAddresses", StringUtil.merge(getIpAddresses()));
082                            jsonObject.put("macAddresses", StringUtil.merge(getMacAddresses()));
083                            jsonObject.put("productId", productId);
084    
085                            String productVersion = licenseProperties.get("productVersion");
086    
087                            jsonObject.put("productVersion", productVersion);
088    
089                            String randomUuid = PortalUUIDUtil.generate();
090    
091                            jsonObject.put("randomUuid", randomUuid);
092    
093                            jsonObject.put("serverId", Arrays.toString(serverIdBytes));
094    
095                            String userCount = licenseProperties.get("userCount");
096    
097                            jsonObject.put("userCount", userCount);
098    
099                            jsonObject.put("version", 2);
100    
101                            String response = LicenseUtil.sendRequest(jsonObject.toString());
102    
103                            JSONObject responseJSONObject = JSONFactoryUtil.createJSONObject(
104                                    response);
105    
106                            String errorMessage = responseJSONObject.getString("errorMessage");
107    
108                            if (Validator.isNotNull(errorMessage)) {
109                                    throw new Exception(errorMessage);
110                            }
111    
112                            String responseRandomUuid = responseJSONObject.getString(
113                                    "randomUuid");
114    
115                            if (responseRandomUuid.equals(randomUuid)) {
116                                    int licenseState = responseJSONObject.getInt("licenseState");
117    
118                                    return licenseState;
119                            }
120                    }
121                    catch (Exception e) {
122                            _log.error(e.getMessage());
123                    }
124    
125                    return 0;
126            }
127    
128            public int getLicenseState(String productId) {
129                    Map<String, String> licenseProperties = new HashMap<String, String>();
130    
131                    licenseProperties.put("productId", productId);
132    
133                    return getLicenseState(licenseProperties);
134            }
135    
136            public Set<String> getMacAddresses() {
137                    return LicenseUtil.getMacAddresses();
138            }
139    
140            public void registerLicense(JSONObject jsonObject) throws Exception {
141                    String serverId = jsonObject.getString("serverId");
142    
143                    if (serverId.length() <= 2) {
144                            return;
145                    }
146    
147                    serverId = serverId.substring(1, serverId.length() - 1);
148    
149                    String[] serverIdArray = StringUtil.split(serverId);
150    
151                    byte[] bytes = new byte[serverIdArray.length];
152    
153                    for (int i = 0; i < bytes.length; i++) {
154                            bytes[i] = Byte.valueOf(serverIdArray[i].trim());
155                    }
156    
157                    LicenseUtil.writeServerProperties(bytes);
158            }
159    
160            private static Log _log = LogFactoryUtil.getLog(
161                    DefaultLicenseManagerImpl.class);
162    
163    }