Results 1 to 8 of 8
  1. #1
    marchello is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Question pls help to find clue in this code (not mine)

    Hi all,

    I'm very new to java, though trying to understand production code already.
    The code below should not start downloading tar.gz file again if it was downloaded already today. But it starts to download it anyway. This is the main question "why" so far. Any hint appreciated!

    The second thing is that the code should create lock file during downloading, so that other call of the code will not start the process again if current one is not yet finished. I can see "lock" is mentioned in the code, though I do not see any "lock" file in the directory during file download. It should also check if existing "lock" file is rather old, then we delete it and start downloading from scratch anyway. Thoughts pls?

    I really hope to learn more on this real example, so please advise if possible. Thx ahead!

    Java Code:
    					importPackage(java.io);
    					importPackage(java.util.zip);
    					importPackage(java.lang);
    					importPackage(org.apache.commons.io);
    					importPackage(java.nio.channels);	
    					importPackage(java.net);
    					importPackage(java.text);
    					importPackage(java.util);
    					var fseparator = System.getProperty("file.separator");
    					var licensefile = File( csv_path  + fseparator + "file_license.txt" );
    					var archivefile = csv_path + fseparator + "csv_export.tar.gz";
    					var archivetarfile = csv_path + fseparator + "csv_export.tar";
    					var status = "refreshed from server";
    					var lockname = "file_get_lock";
    					var timeout = i_timeout;
    					var sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.ENGLISH);
    					if( dv_context.getVdb().getPropertyValue(lockname) !== null && dv_context.getVdb().getPropertyValue(lockname) != ""){
    						if( dv_context.getVdb().getPropertyValue(lockname) !== null && dv_context.getVdb().getPropertyValue(lockname) != "") { 
    			     			var timediff = Date.now() - dv_context.getVdb().getPropertyValue(lockname) 
    					     	if(  timediff < timeout ) { 
    					     		var i = 0;
    					     		while(i<( timeout - timediff) ) {
    					     			java.lang.Thread.sleep(1000);
    					     			if( dv_context.getVdb().getPropertyValue(lockname) == "" ) {
    					     				break;
    					     			}
    					     			i=i+1000;
    					     		}
    					     	} 
    				     	 
    				    	}
    					}
    					if( !licensefile.exists() || ((System.currentTimeMillis()-licensefile.lastModified())/100000000) >=1 ) {
    						dv_context.getVdb().addProperty(lockname,Date.now());
    						var directory = File( csv_path );
    						FileUtils.cleanDirectory( directory );
    						var csv_url = URL( endpoint + "/csv_export/csv_export.tar.gz?user_key=" + user_key); 
    				    	var rbc = Channels.newChannel(csv_url.openStream()); 
    						var fos = new FileOutputStream( archivefile ); 
    						fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 
    						fos.close(); 
    						rbc.close();
    					    var tarIn = 
    							new org.apache.commons.compress.archivers.tar.TarArchiveInputStream(
    								new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(
    									new BufferedInputStream(
    										new FileInputStream(archivefile)
    									)
    								)
    							);
    					
    					    var tarEntry = tarIn.getNextTarEntry();
    					    while (tarEntry != null) {
    					        var destPath = new File(directory, tarEntry.getName()); 
    					        if (tarEntry.isDirectory()) {
    					            destPath.mkdirs();
    					        } else {
    					            destPath.createNewFile();
    					            var btoRead = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 65536);
    					            var bout = new BufferedOutputStream(new FileOutputStream(destPath));
    					            var len = 0;
    					            while((len = tarIn.read(btoRead)) != -1) {
    					                bout.write(btoRead,0,len);
    					            }
    					            bout.close();
    								var date = sdf.parse(tarEntry.getLastModifiedDate());
    						        var currentLastModified = date.getTime(); 
    					        	destPath.setLastModified(currentLastModified);
    					            btoRead = null;
    					        }
    					        tarEntry = tarIn.getNextTarEntry();
    					    }
    					    tarIn.close();
    					    dv_context.getVdb().addProperty(lockname,"");
    						} else {
    							status = "used cache";
    						}

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: pls help to find clue in this code (not mine)

    Someone may help on this but this is a Java forum and the code you pasted appears to be JavaScript. The two are unrelated.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: pls help to find clue in this code (not mine)

    This is script in Rhino/Nashorn. It is somewhat related.
    Last edited by SurfMan; 10-25-2017 at 02:52 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    marchello is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Default Re: pls help to find clue in this code (not mine)

    So.. it does not belong here. Sorry friends.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: pls help to find clue in this code (not mine)

    How does that make it related to Java? You can imbed css and xml in JavaFX but they are not related in any way to Java.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: pls help to find clue in this code (not mine)

    I would say that any questions regarding the proper evaluation of the code using the script engine features of Java would be appropriate. But as for the language itself, it probably shouldn't be here. But that would be up to the moderators and/or the owner of the forum. I'm just a grunt.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    beatzz is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default Re: pls help to find clue in this code (not mine)

    Just curious, why do the 'if' statements on line 17 & 18 both evaluate the same condition??

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: pls help to find clue in this code (not mine)

    Quote Originally Posted by beatzz View Post
    Just curious, why do the 'if' statements on line 17 & 18 both evaluate the same condition??
    It's probably a bad copy/paste job and line 18 should contain a different property value to test.

    having said that, the second part of the if-condition checks if the object is an empty String. Please note that this is the wrong way of testing that. Use dv_context.getVdb().getPropertyValue(lockname) .equals("").
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. I have no clue how this isn't working.
    By Exultant in forum New To Java
    Replies: 2
    Last Post: 04-05-2017, 11:23 AM
  2. Code wont work have no clue why
    By CreatingDrake in forum New To Java
    Replies: 12
    Last Post: 11-25-2013, 08:52 AM
  3. Replies: 5
    Last Post: 10-12-2012, 10:12 AM
  4. No clue how to do this.
    By jjth39347 in forum New To Java
    Replies: 17
    Last Post: 03-02-2011, 05:11 AM
  5. I have no clue what this is... Please Help
    By 4rch in forum New To Java
    Replies: 3
    Last Post: 12-29-2010, 08:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •