Hi all,
I'm new to java, finished Java Syntax course and in process of Java Core basic course. Next step should be Java Collections course.
What I do for living is sql. That's why I'd like to know jdbc and start combining my skills. Though I will need to learn much before start working with jdbc..
So, thanks for attention and have a nice day!
--- Update ---
Taking the opportunity, below is my first real question.
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!
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"; }