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