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