package com.onpositive.gae.tools.license;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import com.onpositive.gae.tools.Activator;
public class LicenseChecker {
private static final String TRIAL = "Trial:";
private static final String TRIAL_STARTED = "TrialStarted:";
private static final String TRIAL_EXPIRED = "TrialExpired";
static boolean webLicenseOk=false;
// Profiler
// Base
// Console
public static boolean checkLicense(boolean debug) {
//License!!!!
//if(true){
// return true;
//}
if (!debug) {
if (!isWebLicenceOk(getUserId())) {
showBadLicense();
return false;
}
}
return true;
}
private static boolean isWebLicenceOk(String userId) {
try {
checkUserId(userId, false);
} catch (BadUserIdException e1) {
return false;
}
if (webLicenseOk){
return true;
}
HttpClient cl = new HttpClient();
PostMethod ms = new PostMethod(
"http://java.latest.gae-tools.appspot.com/license");
cl.setConnectionTimeout(3000);
cl.setTimeout(3000);
ms.setParameter("id", userId);
int executeMethod;
try {
executeMethod = cl.executeMethod(ms);
if (executeMethod == 200) {
final String response = ms.getResponseBodyAsString().trim();
if (response.equals("OK")) {
webLicenseOk=true;
return true;
}
if (response.startsWith(TRIAL_EXPIRED)) {
return false;
}
if (response.startsWith(TRIAL_STARTED)) {
webLicenseOk=true;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
MessageDialog
.openInformation(
Display.getCurrent()
.getActiveShell(),
"Your trial of AppWrench started",
"You have "
+ response
.substring(TRIAL_STARTED.length())
+ " days to evaluate AppWrench");
}
});
return true;
}
if (response.startsWith(TRIAL)) {
webLicenseOk=true;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
MessageDialog
.openInformation(
Display.getCurrent()
.getActiveShell(),
"Welcome to AppWrench trial",
"You have "
+ response
.substring(TRIAL.length() )
+ " days to evaluate AppWrench");
}
});
return true;
}
}
} catch (HttpException e) {
Activator.log(e);
} catch (IOException e) {
Activator.log(e);
}
return true;
}
public static String getUserId() {
return Activator.getDefault().getPreferenceStore().getString("userId");
}
private static void showBadLicense() {
if (getUserId()==null||getUserId().trim().length()==0){
return;
}
Runnable runnable = new Runnable() {
public void run() {
Shell activeShell = Display.getCurrent().getActiveShell();
MessageDialog dialog = new MessageDialog(
activeShell,
"Sorry, your trial expired",
null,
"Your trial is expired, please visit <a>http://appwrench.onpositive.com/buy</a> and purchase license to AppWrench",
MessageDialog.ERROR,
new String[] { IDialogConstants.OK_LABEL }, 0) {
protected Control createMessageArea(Composite composite) {
Image image = getImage();
if (image != null) {
imageLabel = new Label(composite, SWT.NULL);
image.setBackground(imageLabel.getBackground());
imageLabel.setImage(image);
GridDataFactory.fillDefaults().align(SWT.CENTER,
SWT.BEGINNING).applyTo(imageLabel);
}
// create message
if (message != null) {
Link link = new Link(composite,
getMessageLabelStyle());
link.setText(message);
GridDataFactory
.fillDefaults()
.align(SWT.FILL, SWT.BEGINNING)
.grab(true, false)
.hint(
convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
SWT.DEFAULT).applyTo(link);
link.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
try {
PlatformUI
.getWorkbench()
.getBrowserSupport()
.getExternalBrowser()
.openURL(
new URL(
"http://appwrench.onpositive.com/buy/"));
} catch (PartInitException e1) {
Activator.log(e1);
} catch (MalformedURLException e1) {
Activator.log(e1);
}
};
});
}
return composite;
}
};
dialog.open();
}
};
if (Display.getCurrent() != null) {
runnable.run();
} else {
Display.getDefault().syncExec(runnable);
}
}
private static void showError() {
Runnable runnable = new Runnable() {
public void run() {
Shell activeShell = Display.getCurrent().getActiveShell();
MessageDialog dialog = new MessageDialog(
activeShell,
"Beta expired",
null,
"Beta is expired,please visit <a>http://appwrench.onpositive.com</a> and install production version of the AppWrench",
MessageDialog.ERROR,
new String[] { IDialogConstants.OK_LABEL }, 0) {
protected Control createMessageArea(Composite composite) {
Image image = getImage();
if (image != null) {
imageLabel = new Label(composite, SWT.NULL);
image.setBackground(imageLabel.getBackground());
imageLabel.setImage(image);
GridDataFactory.fillDefaults().align(SWT.CENTER,
SWT.BEGINNING).applyTo(imageLabel);
}
// create message
if (message != null) {
Link link = new Link(composite,
getMessageLabelStyle());
link.setText(message);
GridDataFactory
.fillDefaults()
.align(SWT.FILL, SWT.BEGINNING)
.grab(true, false)
.hint(
convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
SWT.DEFAULT).applyTo(link);
link.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
try {
PlatformUI
.getWorkbench()
.getBrowserSupport()
.getExternalBrowser()
.openURL(
new URL(
"http://appwrench.onpositive.com"));
} catch (PartInitException e1) {
Activator.log(e1);
} catch (MalformedURLException e1) {
Activator.log(e1);
}
};
});
}
return composite;
}
};
dialog.open();
}
};
if (Display.getCurrent() != null) {
runnable.run();
} else {
Display.getDefault().syncExec(runnable);
}
}
public static boolean checkUserId(String corePart, boolean debug)
throws BadUserIdException {
if (!debug) {
String userId = getUserId();
if (userId==null||userId.trim().length()==0) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog
.openError(
Display.getCurrent().getActiveShell(),
"Wrong user id",
"You should setup correct user id before using AppWrench with production server.");
PreferenceDialog createPreferenceDialogOn = org.eclipse.ui.dialogs.PreferencesUtil
.createPreferenceDialogOn(
Display.getCurrent().getActiveShell(),
"com.onpositive.gae.tools.core.license",
new String[] {}, null);
createPreferenceDialogOn.open();
}
});
userId = getUserId();
if (userId==null||userId.trim().length()!=0) {
return true;
}
MessageDialog
.openError(
Display.getCurrent().getActiveShell(),
"Wrong user id",
"You should setup correct user id before using AppWrench with production server.");
throw new BadUserIdException();
}
return true;
}
return true;
}
}