001
014
015 package com.liferay.portal.kernel.servlet.liferaypackage;
016
017 import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
018 import com.liferay.portal.kernel.cluster.ClusterNode;
019 import com.liferay.portal.kernel.cluster.ClusterNodeResponse;
020 import com.liferay.portal.kernel.cluster.ClusterNodeResponses;
021 import com.liferay.portal.kernel.cluster.ClusterRequest;
022 import com.liferay.portal.kernel.cluster.FutureClusterResponses;
023 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
024 import com.liferay.portal.kernel.deploy.hot.LiferayPackageHotDeployException;
025 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
026 import com.liferay.portal.kernel.servlet.ServletContextPool;
027 import com.liferay.portal.kernel.servlet.filters.LiferayPackageFilter;
028 import com.liferay.portal.kernel.servlet.filters.invoker.FilterMapping;
029 import com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterConfig;
030 import com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterHelper;
031 import com.liferay.portal.kernel.util.GetterUtil;
032 import com.liferay.portal.kernel.util.MethodHandler;
033 import com.liferay.portal.kernel.util.MethodKey;
034 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
035 import com.liferay.portal.kernel.util.StreamUtil;
036 import com.liferay.portal.kernel.util.StringPool;
037 import com.liferay.portal.kernel.util.Validator;
038 import com.liferay.portal.kernel.workflow.WorkflowConstants;
039 import com.liferay.portal.license.util.LicenseManagerUtil;
040
041 import java.io.InputStream;
042
043 import java.lang.reflect.Constructor;
044 import java.lang.reflect.Method;
045
046 import java.sql.Connection;
047 import java.sql.PreparedStatement;
048 import java.sql.ResultSet;
049
050 import java.util.ArrayList;
051 import java.util.HashMap;
052 import java.util.List;
053 import java.util.Map;
054 import java.util.concurrent.TimeUnit;
055
056 import javax.servlet.Filter;
057 import javax.servlet.FilterConfig;
058 import javax.servlet.ServletContext;
059
060
063 public class LiferayPackageUtil {
064
065 public static void checkPackage() throws Exception {
066 if (_getProductType() == _productTypeEE) {
067 int licenseState = LicenseManagerUtil.getLicenseState(
068 _productIdPortal);
069
070 if (licenseState != _stateGood) {
071 LicenseManagerUtil.checkLicense(_productIdPortal);
072
073 licenseState = LicenseManagerUtil.getLicenseState(
074 _productIdPortal);
075 }
076
077 if (licenseState != _stateGood) {
078 throw new LiferayPackageHotDeployException(
079 "This application requires a valid Liferay Portal EE " +
080 "license.");
081 }
082 }
083
084 if (Validator.isNull(_getProductId())) {
085 return;
086 }
087
088 int licenseState = _getLicenseState(
089 _getProductId(), _getProductVersion());
090
091 if (licenseState != _stateGood) {
092 LicenseManagerUtil.checkLicense(_getProductId());
093
094 licenseState = _getLicenseState(
095 _getProductId(), _getProductVersion());
096 }
097
098 if (licenseState != _stateGood) {
099 throw new LiferayPackageHotDeployException(
100 "This application does not have a valid license");
101 }
102
103 Map<String, String> licenseProperties =
104 LicenseManagerUtil.getLicenseProperties(_getProductId());
105
106 if (licenseProperties == null) {
107 throw new LiferayPackageHotDeployException(
108 "This Liferay version does not support this application.");
109 }
110
111 if (licenseProperties != null) {
112 int maxValidProductVersion = GetterUtil.getInteger(
113 licenseProperties.get("productVersion"));
114
115 if ((_getProductVersion() > 0) &&
116 (_getProductVersion() > maxValidProductVersion)) {
117
118 throw new LiferayPackageHotDeployException(
119 "The version of your application is not compatible with " +
120 "the registered license");
121 }
122 }
123
124 List<ClusterNode> clusterNodes = ClusterExecutorUtil.getClusterNodes();
125
126 if (clusterNodes.size() <= 1) {
127 return;
128 }
129
130 clusterNodes.remove(ClusterExecutorUtil.getLocalClusterNode());
131
132 for (ClusterNode clusterNode : clusterNodes) {
133 MethodHandler methodHandler = new MethodHandler(
134 _getLicenseStateMethodKey, _getProductId());
135
136 ClusterRequest clusterRequest = ClusterRequest.createUnicastRequest(
137 methodHandler, clusterNode.getClusterNodeId());
138
139 FutureClusterResponses futureClusterResponses =
140 ClusterExecutorUtil.execute(clusterRequest);
141
142 ClusterNodeResponses clusterNodeResponses =
143 futureClusterResponses.get(10000, TimeUnit.MILLISECONDS);
144
145 ClusterNodeResponse clusterNodeResponse =
146 clusterNodeResponses.getClusterResponse(clusterNode);
147
148 Object result = clusterNodeResponse.getResult();
149
150 if (result == null) {
151 return;
152 }
153
154 Integer remoteLicenseState = (Integer)result;
155
156 if (remoteLicenseState != _stateGood) {
157 throw new LiferayPackageHotDeployException(
158 "A clustered server has an invalid license.");
159 }
160 }
161 }
162
163 public static void registerFilter(
164 ServletContext servletContext, String pathContext)
165 throws LiferayPackageHotDeployException {
166
167 _pathContext = pathContext;
168
169 Map<String, String> licenseProperties =
170 LicenseManagerUtil.getLicenseProperties(_getProductId());
171
172 String description = GetterUtil.getString(
173 licenseProperties.get("description"));
174
175 if (!description.startsWith("Developer License")) {
176 return;
177 }
178
179 try {
180 InvokerFilterHelper invokerFilterHelper = _getInvokerFilterHelper(
181 pathContext);
182
183 Filter liferayPackageFilter = _getFilter(
184 servletContext.getServletContextName(),
185 pathContext + "/c/portal/license");
186
187 FilterConfig filterConfig = new InvokerFilterConfig(
188 servletContext, _getFilterName(),
189 new HashMap<String, String>());
190
191 liferayPackageFilter.init(filterConfig);
192
193 invokerFilterHelper.registerFilter(
194 _getFilterName(), liferayPackageFilter);
195
196 List<String> urlPatterns = new ArrayList<String>();
197
198 urlPatterns.add("