001    /**
002     * Copyright (c) 2000-2013 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.security.pacl.DoPrivileged;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
025    import com.liferay.portal.license.LicenseInfo;
026    
027    import java.util.Arrays;
028    import java.util.HashMap;
029    import java.util.List;
030    import java.util.Map;
031    import java.util.Set;
032    
033    /**
034     * @author Amos Fong
035     */
036    @DoPrivileged
037    public class DefaultLicenseManagerImpl
038            implements com.liferay.portal.license.util.LicenseManager {
039    
040            public void checkLicense(String productId) {
041            }
042    
043            public List<Map<String, String>> getClusterLicenseProperties(
044                    String clusterNodeId) {
045    
046                    return null;
047            }
048    
049            public String getHostName() {
050                    return LicenseUtil.getHostName();
051            }
052    
053            public Set<String> getIpAddresses() {
054                    return LicenseUtil.getIpAddresses();
055            }
056    
057            public LicenseInfo getLicenseInfo(String productId) {
058                    return null;
059            }
060    
061            public List<Map<String, String>> getLicenseProperties() {
062                    return null;
063            }
064    
065            public Map<String, String> getLicenseProperties(String productId) {
066                    return null;
067            }
068    
069            public int getLicenseState(Map<String, String> licenseProperties) {
070                    String productId = licenseProperties.get("productId");
071    
072                    if (Validator.isNull(productId)) {
073                            return 0;
074                    }
075    
076                    try {
077                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
078    
079                            byte[] serverIdBytes = LicenseUtil.getServerIdBytes();
080    
081                            jsonObject.put("cmd", "GET_LICENSE_STATE");
082                            jsonObject.put("hostName", getHostName());
083                            jsonObject.put("ipAddresses", StringUtil.merge(getIpAddresses()));
084                            jsonObject.put("macAddresses", StringUtil.merge(getMacAddresses()));
085                            jsonObject.put("productId", productId);
086    
087                            String productVersion = licenseProperties.get("productVersion");
088    
089                            jsonObject.put("productVersion", productVersion);
090    
091                            String randomUuid = PortalUUIDUtil.generate();
092    
093                            jsonObject.put("randomUuid", randomUuid);
094    
095                            jsonObject.put("serverId", Arrays.toString(serverIdBytes));
096    
097                            String userCount = licenseProperties.get("userCount");
098    
099                            jsonObject.put("userCount", userCount);
100    
101                            jsonObject.put("version", 2);
102    
103                            String response = LicenseUtil.sendRequest(jsonObject.toString());
104    
105                            JSONObject responseJSONObject = JSONFactoryUtil.createJSONObject(
106                                    response);
107    
108                            String errorMessage = responseJSONObject.getString("errorMessage");
109    
110                            if (Validator.isNotNull(errorMessage)) {
111                                    throw new Exception(errorMessage);
112                            }
113    
114                            String responseRandomUuid = responseJSONObject.getString(
115                                    "randomUuid");
116    
117                            if (responseRandomUuid.equals(randomUuid)) {
118                                    int licenseState = responseJSONObject.getInt("licenseState");
119    
120                                    return licenseState;
121                            }
122                    }
123                    catch (Exception e) {
124                            _log.error(e.getMessage());
125                    }
126    
127                    return 0;
128            }
129    
130            public int getLicenseState(String productId) {
131                    Map<String, String> licenseProperties = new HashMap<String, String>();
132    
133                    licenseProperties.put("productId", productId);
134    
135                    return getLicenseState(licenseProperties);
136            }
137    
138            public Set<String> getMacAddresses() {
139                    return LicenseUtil.getMacAddresses();
140            }
141    
142            public void registerLicense(JSONObject jsonObject) throws Exception {
143                    String serverId = jsonObject.getString("serverId");
144    
145                    if (serverId.length() <= 2) {
146                            return;
147                    }
148    
149                    serverId = serverId.substring(1, serverId.length() - 1);
150    
151                    String[] serverIdArray = StringUtil.split(serverId);
152    
153                    byte[] bytes = new byte[serverIdArray.length];
154    
155                    for (int i = 0; i < bytes.length; i++) {
156                            bytes[i] = Byte.valueOf(serverIdArray[i].trim());
157                    }
158    
159                    LicenseUtil.writeServerProperties(bytes);
160            }
161    
162            private static Log _log = LogFactoryUtil.getLog(
163                    DefaultLicenseManagerImpl.class);
164    
165    }