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