/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jxa;
import esell.ProductKeyValidator;
import esell.Validate;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.Action;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import net.sf.jiga.xtended.kernel.DebugMap;
import net.sf.jiga.xtended.kernel.FileHelper;
import net.sf.jiga.xtended.kernel.JXAenvUtils;
import net.sf.jiga.xtended.kernel.ThreadWorks;
import net.sf.jiga.xtended.ui.AntApplet;
import net.sf.jiga.xtended.ui.AntFrame;
import net.sf.jiga.xtended.ui.DisplayInterface;
import net.sf.jiga.xtended.ui.UIMessage;
/**
*
* @author www.b23prodtm.info
*/
public class ESell implements AntApplet, AntFrame {
/**
* my validation protocol
*
* @param args syntax : -name "full name" -license <serialnumber>
*/
private boolean validate(String... args) {
Validate jni_validateEsell = new Validate();
/**
* first the passed arguments issued from the user are parsed...
*/
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("-name".equals(arg)) {
serialName = args[++i] != null ? args[i].replaceAll("\"", "") : "";
} else if ("-license".equals(arg)) {
serial = args[++i] != null ? args[i].replaceAll("\"", "") : "";;
}
}
/**
* ... so that the validation runtime is called with correct parameters.
* It will return a date in the format that we can read. It matches the
* purchased day or the day when the trial use will terminate.
*/
Calendar ret = jni_validateEsell.runValidateSerial(serial, serialName, "", jni_validateEsell.getPublisherKey());
if (ret != null) {
String trial = (ProductKeyValidator.isDemoMode() ? "trial until " : "licensed use. Purchased on ") + ret;
JXAenvUtils.log("provided serial " + jni_validateEsell.standardizeSerial(serial) + " for " + serialName + " is validated for " + trial, JXAenvUtils.LVL.SYS_NOT);
expiringDate.setTime(ret.getTime());
return true;
}
return false;
}
/**
* Performs a validated boot to prevent unauthorized access to the
* application.
*
* @param args args from java command-line
*/
public ESell(String[] args) throws ClassNotFoundException {
applicationClassName = this.getClass().getName();
applicationStore = new File(System.getProperty("user.home") + File.separator + "." + System.getProperty("user.name") + "-" + applicationClassName + ".bkp");
if (JXAenvUtils.env.OS_WINDOWS.isEnv() && !JXAenvUtils.env.JAVA_VERSION_5.isEnv() && !JXAenvUtils.env.JAVA_VERSION_6.isEnv()) {
try {
Files.setAttribute(applicationStore.toPath(), "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
} catch (IOException ex) {
if (DebugMap._getInstance().isJXADebugSysEnabled()) {
ex.printStackTrace();
}
}
}
/**
* loading from application file if it stores any license
*/
demoStoreLoad(applicationStore, Class.forName(applicationClassName));
if (!Validate.isDemoMode()) {
JXAenvUtils.log("demo mode is OFF", JXAenvUtils.LVL.SYS_NOT);
}
/**
* show input dialog if validation failed
*/
if (validate(args) || validate("-license " + serial, "-name " + serialName)) {
JXAenvUtils.log("Serial/Name keypair was validated. Booting licensed mode.", JXAenvUtils.LVL.SYS_NOT);
/**
* save new expiring date and continue
*/
writeFileStore(applicationStore, Class.forName(applicationClassName));
return;
} else {
String[] options = new String[]{"Purchase", "Enter serial number", "Continue Trial"};
String res;
/**
* res = null will exit
*/
while ((res = (String) JOptionPane.showInputDialog(null, new JLabel("<html>Product key validation</html>"), "Authenticate your product", JOptionPane.QUESTION_MESSAGE, null, options, options[1])) != null) {
try {
if (options[0].equals(res)) {
/**
* user wants to purchase the software
*/
try {
JXAenvUtils._goWeb(new URL("http://www.descom-online.info"));
Thread.sleep(3000);
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
} else if (options[1].equals(res)) {
/**
* user has got the serial number
*/
String serial = JOptionPane.showInputDialog(null, new JLabel("Please, enter a valid serial number :"));
String serialName = JOptionPane.showInputDialog(null, new JLabel("Please, enter your full name :"));
String msg;
boolean b = validate("-name", serialName, "-license", serial);
if (b) {
msg = "<html>Product key validation<br><center>Product key was validated.</center>"
+ "<br>" + (Validate.isDemoMode() ? getDaysExpiring() + " day(s) left." : "Purchased on " + expiringDate.getTime().toString()) + ". </html>";
} else {
msg = "<html>Product key not validated<br><center>Invalid serial and/or name for this product.</center>"
+ "<br>Expired on " + expiringDate.getTime().toString() + "</html>";
}
UIMessage.showLightPopupMessage(new JLabel(msg), null, null, UIMessage.UI_BOTTOM_RIGHT);
if (b) {
/**
* save new expiring date and owner, and continue
*/
writeFileStore(applicationStore, Class.forName(applicationClassName));
return;
}
} else if (options[2].equals(res)) {
/**
* user wants to go on with the trial use
*/
final String msg;
if (getDaysExpiring() > 0) {
msg = "<html>Trial Use<br><center>Remaining :</center>"
+ "<br>" + getDaysExpiring() + " day(s) left. </html>";
} else {
msg = "<html>Product key validation<br><center>Invalid serial and/or name for this product.</center>"
+ "<br>Expired on " + expiringDate.getTime().toString() + "</html>";
}
ThreadWorks.Swing.invokeLater(new Runnable() {
public void run() {
UIMessage.showLightPopupMessage(new JLabel(msg), null, null, UIMessage.UI_BOTTOM_RIGHT);
}
});
/**
* save new expiring date and continue
*/
writeFileStore(applicationStore, Class.forName(applicationClassName));
if (getDaysExpiring() > 0) {
return;
}
}
} catch (InterruptedException ex) {
System.exit(1);
}
}
}
System.exit(1);
}
private Calendar expiringDate = Calendar.getInstance();
public final int getDaysExpiring() {
return (int) Math.ceil((expiringDate.getTimeInMillis() - System.currentTimeMillis()) / 3600L / 1000L / 24L);
}
/**
* if {@link ProductKeyValidator#demoMode} is on, the date on which the
* trial finishes is returned if it's not in trial, the date of purchase is
* returned.
*/
public Calendar getExpiringDate() {
return expiringDate;
}
public static String getSerial() {
return serial;
}
public static String getSerialName() {
return serialName;
}
private String applicationClassName;
private File applicationStore;
private final static long demoModeTimeLimit = 30L * 24L * 3600L * 1000L;
private static String serial = "", serialName = "";
/**
* will load a file that stored the time at which the licensed use started
* with serial and serial owner.
*/
private void demoStoreLoad(File fileStore, Class clazz) {
try {
RandomAccessFile raf = new RandomAccessFile(fileStore, "r");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(raf.getFD()));
String versionImpl = in.readUTF();
expiringDate.setTimeInMillis(in.readLong());
if (!versionImpl.equals(clazz.getPackage().getImplementationVersion())) {
expiringDate.setTimeInMillis(System.currentTimeMillis() + demoModeTimeLimit);
}
serial = in.readUTF();
serialName = in.readUTF();
raf.close();
} catch (IOException ex) {
/**
* TODO : here it might be not continued, and a trial serial number
* should (have been) issued.
*/
expiringDate.setTimeInMillis(System.currentTimeMillis() + demoModeTimeLimit);
writeFileStore(fileStore, clazz);
}
}
private void writeFileStore(File fileStore, Class clazz) {
try {
File temp = FileHelper._createTempFile("Esell_", FileHelper._TMPDIRECTORY, true);
RandomAccessFile raf = new RandomAccessFile(temp, "rw");
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(raf.getFD())));
out.writeUTF(clazz.getPackage().getImplementationVersion());
out.writeLong(expiringDate.getTimeInMillis());
out.writeUTF(serial);
out.writeUTF(serialName);
out.flush();
raf.close();
FileHelper._fileCopy(temp, fileStore, false, true);
temp.delete();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public boolean open(Object o) {
/**
* ! implement with your code !
*/
return false;
}
public boolean save(String string) {
/**
* ! implement with your code !
*/
return false;
}
/**
* ### APPLET and FRAME SUPPORT ###
*/
public void start() {
}
/**
* ### APPLET and FRAME SUPPORT ###
*/
public void stop() {
/**
* ! implement with your code !
*/
}
boolean initialized = false;
public boolean isInitialized() {
return initialized;
}
public Runnable shutdownHook() {
/**
* ! implement with your code !
*/
return null;
}
public DataFlavor[] getTransferDataFlavors() {
/**
* ! implement with your code !
*/
return null;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
/**
* ! implement with your code !
*/
return false;
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
/**
* ! implement with your code !
*/
return null;
}
JApplet applet = null;
/**
* ### APPLET SUPPORT ###
*/
public void setApplet(JApplet ja) {
applet = ja;
}
/**
* ### APPLET SUPPORT ###
*/
public JApplet getApplet() {
return applet;
}
/**
* ### APPLET SUPPORT ###
*/
public void destroy() {
/**
* ! implement with your code !
*/
/**
*
*/
initialized = false;
}
/**
* ### APPLET SUPPORT ###
*/
public void init() {
/**
* ! implement with your code !
*/
/**
*
*/
initialized = true;
}
JFrame frame = null;
/**
* ### JFRAME APP SUPPORT ###
*/
public void setFrame(JFrame jframe) {
frame = jframe;
}
/**
* ### JFRAME APP SUPPORT ###
*/
public JFrame getFrame() {
return frame;
}
/**
* ### JFRAME APP SUPPORT ###
*/
public void destroyComponents() {
/**
* ! implement with your code !
*/
/**
*
*/
initialized = false;
}
/**
* ### JFRAME APP SUPPORT ###
*/
public void initComponents() {
/**
* ! implement with your code !
*/
/**
*
*/
initialized = true;
}
/**
* ### APPLET and FRAME SUPPORT ###
*/
public DisplayInterface getSplash() {
/**
* ! implement with your code !
*/
return null;
}
/**
* ### APPLET and FRAME SUPPORT ###
*/
public Vector<Action> getLoadLayers() {
/**
* ! implement with your code !
*/
return null;
}
}