| |
SimpleModularProduct.java

//SimpleModularProduct.java
//A sample source code for JChain SA
//
//Edit and execute the run_SimplestModularProduct.sh/.bat
//to run this program.
//
//(c) 2002-2008 ChainKey, Inc.
package myCompany;
//for the graphical wizard of JChain
import javax.swing.JDialog;
//for using JChain
import com.chainkey.jchain.sa.licensee.Licensee;
public class SimpleModularProduct
{
private static boolean DEBUG = true;
//license status for modules in your program
//licenseStatus[0]: license status for the 1st module
//licenseStatus[1]: license status for the 2nd module
//...
//the 1st module has a special role to represent the entire program
private static int[] licenseStatus;
public static void main(String[] args)
{
SimpleModularProduct myProgram = new SimpleModularProduct();
//First, try to authorize the current end-user's computer
myProgram.tryToAuthorize();
//Then, execute corresponding functions
myProgram.startProgram( licenseStatus );
//It is recommended to stop the Key Updater before calling exit()
Licensee.getInstance().stopKeyUpdateDaemon();
//finish program after using JDialog. This is necessary because of the specifications of Java.
System.exit(0);
}
//try to authorize the computer and return the license status
private int[] tryToAuthorize(){
if(DEBUG) System.out.println("Trying to authorize...");
Licensee ck = Licensee.getInstance();
//the default JChain wizard for end-user
JDialog wizard = null;
boolean wizardUsed = false;
//get the number of modules
int totalModules = ck.getTotalModules();
licenseStatus = new int[totalModules];
//get the current license status
refreshLicenseStatus("at startup");
for(int i=0; i< totalModules; i++){
switch (licenseStatus[i]) {
case -1://i.e. Error
break;
case 0://i.e. unauthorized
if( !wizardUsed ){
if(DEBUG) System.out.println("Launching wizard...");
wizard = ck.prompt();
//wait until the wizard is closed
if( wizard != null ){
wizardUsed = true;
while( wizard.isDisplayable() ){
try{ Thread.sleep(500);}catch(Exception e){}
}
}
//refresh the license status after prompt()
refreshLicenseStatus("after prompt()");
}
break;
case 1://i.e. authorized (permanent license)
break;
case 2://i.e. authorized (trial license)
if( !wizardUsed ){
if(DEBUG) System.out.println("Launching wizard...");
wizard = ck.prompt();// prompt end-user for ordering a key //wait until the wizard is closed
if( wizard != null ){
wizardUsed = true;
while( wizard.isDisplayable() ){
try{ Thread.sleep(500);}catch(Exception e){}
}
}
//refresh the license status after prompt()
refreshLicenseStatus("after prompt()");
}
break;
case 3://i.e. authorized (lease license)
break;
default:
break;
}
}
return licenseStatus;
}
//refresh the license status
private void refreshLicenseStatus(String whenStr){
Licensee ck = Licensee.getInstance();
for(int j=0; j< licenseStatus.length ; j++){
int fid=j+1; //in this sample, fid of n-th module is n
licenseStatus[j] = ck.getLicenseStatus(fid);
if(DEBUG) System.out.println("License status " + whenStr + " (fid=" + fid + ") : " + licenseStatus[j] );
}
}
//start the program depending on user's license status
private void startProgram (int[] licenseStatus){
//the 1st module is special
switch (licenseStatus[0]) {
case -1://i.e. Error
System.err.println("An error occured in JChain module." );
myShowErrorMessage();
break;
case 0://i.e. unauthorized
myShowErrorMessage();
break;
case 1://i.e. authorized (permanent license)
myStartFullFunction();
break;
case 2://i.e. authorized (trial license)
myStartTrialFunction();
break;
case 3://i.e. authorized (lease license)
myStartFullFunction();
break;
default:
System.err.println("Value of the license status is invalid." );
myShowErrorMessage();
break;
}//switch
}
//----------- NONESSENTIALS -----------
//Shows an error message
private static void myShowErrorMessage(){
System.err.println("\n--> Authorization failed. Can not start the program.\n" );
}
//Starts the program
private void myStartFullFunction(){
System.out.println("\n--> Authorized. Starting the program...");
//assuming that the program contains at least 3 modules
myModularFunction(1);
myModularFunction(2);
myModularFunction(1);
myModularFunction(3);
myModularFunction(1);
System.out.println("\n--> Program finished\n");
}
//Starts the evaluation version
private void myStartTrialFunction(){
System.out.println("\n--> Authorized. Starting the evaluation version...Done\n");
//assuming that the program contains at least 3 modules
myModularFunction(1);
myModularFunction(2);
myModularFunction(1);
myModularFunction(3);
myModularFunction(1);
System.out.println("\n--> Program finished\n");
}
//start the program
private void myStartFullModularFunction(int fid){
System.out.print("Using a module (fid=" + fid + ")...");
System.out.println("Done");
}
//start the evaluation version
private void myStartTrialModularFunction(int fid){
System.out.print("Using a module (fid=" + fid + ") in evaluation mode...");
System.out.println("Done");
}
//show error message when a module can not be used
private static void myShowModularErrorMessage(int fid){
System.err.println("Can not use a module (fid=" + fid +")");
}
//use a module (or a feature) of your program
//specified by its FID (feature id. 1,2,3...)
private void myModularFunction(int fid){
switch (licenseStatus[fid-1]) {
case -1://i.e. Error
System.err.print("An error occured in JChain module." );
myShowModularErrorMessage(fid);
break;
case 0://i.e. unauthorized
System.err.print("You have no license for this module. " );
myShowModularErrorMessage(fid);
break;
case 1://i.e. authorized (permanent license)
myStartFullModularFunction(fid);
break;
case 2://i.e. authorized (trial license)
myStartTrialModularFunction(fid);
break;
case 3://i.e. authorized (lease license)
myStartFullModularFunction(fid);
break;
default:
System.err.print("Value of the license status is invalid. " );
myShowModularErrorMessage(fid);
break;
}//switch
}
}

|