Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Code to test basic auto update functionality of the 51Degrees Java API
- */
- package updatetest;
- import fiftyone.mobile.detection.*;
- import fiftyone.mobile.detection.factories.MemoryFactory;
- import fiftyone.mobile.detection.factories.StreamFactory;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author mike
- */
- public class UpdateTest {
- private static final String LICENCE_KEY = "YOUR_KEY";
- private static final String PATH_TO_FILE = "path\\to\\51Degrees.dat";
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws AutoUpdateException, IOException, InterruptedException {
- updateWithTimeInterval();
- System.exit(0);
- }
- /**
- * Function performs an update with a given time interval in between each
- * attempt.
- */
- private static void updateWithTimeInterval() {
- //Loop break condition.
- boolean update = true;
- //Counter of iterations.
- int counter = 0;
- do {
- counter++;
- System.out.println("Commencing iteration "+counter+".");
- //Perform the update.
- update = update();
- if(update) {
- if(verifyUsingMemoryFactory())
- System.out.println("OK: Data file verified.");
- else
- System.out.println("ERROR: Data file not verified.");
- //Try to put thread to sleep for 5 seconds.
- try {
- System.out.println("Pausing for 5 seconds.");
- Thread.sleep(5000);
- } catch (InterruptedException ex) {
- System.out.println("ERROR: Pause failed.");
- Logger.getLogger(UpdateTest.class.getName()).log(Level.SEVERE, null, ex);
- }
- } else {
- System.out.println("Update failed.");
- }
- System.out.println("Iteration "+counter+" complete.\n");
- } while(update);
- //Print report.
- System.out.println("Total iterations: "+counter);
- }
- /**
- * Function performs an update repeatedly until the first fail. Data file
- * gets deleted after every update.
- */
- private static void updateUntilDownloadLimit() {
- //Indicates if update was successful.
- boolean update = true;
- //Counts the successful updates.
- int icount = 0;
- do {
- //Perform update.
- update = update();
- //Update sucessful.
- if (update) {
- if (verifyUsingMemoryFactory())
- System.out.println("OK: Data file verified.");
- else
- System.out.println("ERROR: Data file not verified.");
- if (deleteFile())
- System.out.println("OK: Data file deleted.");
- else
- System.out.println("ERROR: Data file not deleted.");
- }
- } while (update);
- //Print number of successful updates.
- System.out.println("number of successfull attempts: " + icount);
- }
- /**
- * function reports the amount of memory allocated to JVM.
- */
- private static void reportMemory() {
- long heapSize = Runtime.getRuntime().totalMemory();
- System.out.println("current heap: " + heapSize);
- }
- /**
- * Function attempts to remove a data file at the path specified in
- * PATH_TO_FILE.
- * @return True is successful
- * False otherwise
- */
- private static boolean deleteFile() {
- //try to delete the file.
- Path p = Paths.get(PATH_TO_FILE);
- try {
- Files.delete(p);
- return true;
- } catch(Exception e) {
- System.out.println("Could not delete file");
- } finally {
- p = null;
- }
- return false;
- }
- /**
- * Function verifies the data file is valid by constructing a provider and
- * performing a match. As the name suggests StreamProvider is used.
- * @return True if Provider was initialised and Match performed.
- * False otherwise.
- */
- private static boolean verifyUsingStreamFactory() {
- try {
- //Initialise provider.
- Provider p = new Provider(StreamFactory.create(PATH_TO_FILE));
- //Perform match. We know the following is mobile.
- Match m = p.match("Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; "
- + "LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like "
- + "Gecko) Version/4.0 Mobile Safari/534.30");
- if(m.getValues("IsMobile").toBool()) {
- p.dataSet.dispose();
- return true;
- }
- } catch (IOException ex) {
- Logger.getLogger(UpdateTest.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- return false;
- }
- /**
- * Function verifies the data file is valid by constructing a provider and
- * performing a match. As the name suggests MemoryProvider is used.
- * @return True if Provider was initialised and Match performed.
- * False otherwise.
- */
- private static boolean verifyUsingMemoryFactory() {
- try {
- //Initialise provider.
- Provider p = new Provider(MemoryFactory.create(PATH_TO_FILE));
- //Perform match. We know the following is mobile.
- Match m = p.match("Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; "
- + "LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like "
- + "Gecko) Version/4.0 Mobile Safari/534.30");
- if(m.getValues("IsMobile").toBool()) {
- p.dataSet.dispose();
- return true;
- }
- } catch (IOException ex) {
- Logger.getLogger(UpdateTest.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- return false;
- }
- /**
- * Function invokes 51D update method. File saved to the path specified in
- * PATH_TO_FILE variable. LICENSE_KEY variable must not be null.
- * @return True is update successful
- * False if update failed.
- */
- private static boolean update() {
- //Try to update.
- boolean update = false;
- try {
- //AutoUpdate returns True if success, False if failed.
- update = fiftyone.mobile.detection.AutoUpdate.update(LICENCE_KEY, PATH_TO_FILE);
- } catch(Exception e) {
- System.out.println("Auto update failed" + e.getMessage() + e.getLocalizedMessage() + e.toString());
- }
- return update;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment