Detect whether Web application is running for the first time - java

I am developing a web application to migrate images from CVS to Adobe CQ. There is a requirement to maintain versioning for the images in a database table. The flow is as follows:
Check Out files from CVS -- returns a list of all files checked out, but does not tell whether a file was updated or newly added to CVS! (Due to the files being binary file, it seems it is not able to detect updates and all files are treated as additions)
Check if this is application's first run, if yes, then treat all files as additions. If not, for every file, check database for presence of record corresponding to this file. If record present, treat as update, else insert
... Carry on with other operations
For every insert, add an entry in the database
I have to detect if the application is running for the first time, or has been run previously. This also needs to support future tasks such as resetting everything and starting the application from scratch.
What would be a good way to do this? The application is hosted in WebSphere in Linux. I have thought of two ways:
a. creating an entry in a file with a flag set to true, which I will have to reset to false after the first run - difficult for a user to reset later
b. creating a .firstrun or similar file in the app folder somewhere and check presence of this file to determine first run, easier to reset for any user
Which of this is a better way? Or is there any other way to do it better?

You mentioned database. Why not to store in db, in even more extended way: when what version, # of runs, etc.

Related

How can I watch any particular file / directory based upon any file(located under specific directory if directory base watching) attempt for OPEN?

I want to execute few task based open targeted file's OPEN event.
For example, I am watching Sample.docx & whenever user will go for OPEN it, few subsequent task will be performed based upon it's OPEN action.
I have searched on internet & find out few solution but that are based upon file's MODIFICATION & DELETION operation. none of them shows based upon OPEN action which is actually I am looking for.
Any hint/suggestion would help me.
Thanks.
That is an operating-system specific functionality and is not something Java comes with out of the box. If you are on Windows you would use a FileSystemWatcher which exists in .NET, but if you need it in Java you would have to create native bindings if there isn't a library that already exists. Chances are this does not exist as not many people would have a valid use-case to do this and I don't think security people would be happy to see this either.
You could I suppose, in a specific thread, regularly poll currently running processes to see if the file name is contained within a process title.
As for .docx files for example, WORD would have this as its process title:
Sample.docx - Microsoft Word
You would need to utilize a JNA method named getAllWindowNames() to acquire a list of Window Names. This method works quite well. When Sample.docx is detected within the acquired list then start whatever file or files you like.
Keep in mind however, your Java application would need to always be running in the background and because of file association (as mentioned to you in a previous post) this technique would run the files you have associated with Sample.docx regardless of how the file was run (from a double-click in Windows Explorer, a shortcut on Desktop, opened from MS WORD itself, etc).
I have actually created a small demo application that does exactly what you are trying to accomplish however it is too large to post here. There is no tutorial that I know of for this sort of thing, it's just a matter of doing it....that is if the concept appeals to you.
Yes!...most people would not want this sort of thing dancing around on their System(s).

How to check javafx based program run first time or not?

I want to show configuration page if its run for the first time else its show other pages. How can I implement in my netbeans project?
There are several ways to do this, but this might be the easiest:
Save your configuration at the applications startup to a persistent storage. You could use a database (e.g. h2), a file (e.g. txt/ini/config, XML, YAML, ...) or even the users registry with the Java Persistence API.
After that, on every startup the application loads that configuration and displays the appropriate configuration menu when either all or some information cannot be found.
That way you can also ensure that there is always a valid configuration in case it gets lost for whatever reason.

Run a jar automatically

I am running automatic tests and after each test, all the sessions of these tests are automatically inserted into a temporary table.
My tool (in JAR), checks this temporary table and when it finds a new session inserted, my tool scans it and then delete this entry from the temporary table.
Actually I can do that manually without any problem (telling my tool to check the temporary table, if there is something new, scan it…), but I want to do that automatically, I mean my tool always running, and checks automatically (for example every hour) if there is something new in the temporary table.
Could you help me how I can do that ? I guess I need a server where I execute my tool 24/24, which type of server? Thank you very much
If you are on a unix based system, you could run the jar as a cronjob. The following would run a jar every 30 seconds.
*/30 * * * * java -jar /path/to/jar/myjar.jar
Read the following to learn how to setup a cronjob correctly https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job
For windows, use task scheduler. https://stackoverflow.com/a/26932169/802061 as suggested by #kevin-esche in the comments
Which database you are using.
If SQL, you can create the jobs in the database server itself.

How do I export a jar with an sqlite database inside?

I have a program that I want to distribute, without giving the source code or database used. It's an sqlite database, and doesn't need to be updated. I tried using eclipse fatjar and changing where to look for the jar, but when I run the program, it just creates a blank database file in the same directory as the jar. I just want one file that I can distribute.
From comments deemed relevant:
..include an XML after tested with a TXT file.
I would, but this database is 80,000 lines long, and has 4 columns. It's only going to get bigger too, with updates to the program, not during runtime.
Put it on a server and have a web interface.
I want to avoid using any internet connection really. If someone wants to decompile the jar, whatever I don't care. I just want it to work from double click, and no extra files laying around.
(deployment) ..usability and neatness is important for me
That makes me think that what is really needed for this is a cross-platform installer. The user gets one file and double clicks it, 'follows the prompts' (if any prompts are required) & it extracts the app. ready for use.
It might create multiple files, but this will be largely invisible to the end user. 'Out of site is out of mind'.
I want to avoid using any internet connection..
I recommend you rethink that. What size does the app. come to when Jar'd? A couple of megabytes? That is nothing in this day and age of internet traffic. That's a 2 minute YouTube.
The ratio of devices having internet connections to machines having (for example) CD/DVD drives to load software is also changing. It is coming to a time when more machines capable of running J2SE have internet connections than have drives. I have a desktop PC and a Netbook that can both run J2SE. Both have an internet connection, but only the desktop PC has CD/DVD drives.
If that is the case (getting to my point) look to Java Web Start to deploy the app. and DB. Very user friendly, with good desktop integration.
it's about 50mb, but the problem is not all end users will have access to internet at all times. Distributing the application can be done through the internet, but I don't want to rely on it for accessing the database or loading the application all together.
That is not necessary. JWS caches the application resources locally. It will check the server for updated Jars, but can be configured to allow launch from the cached copy even if there is no internet connection at that moment. The launch file element to configure that would look something like:
<update check="always" policy="prompt-run">
Don't know whether you can do this with a SQLite database, but Derby supports jar: paths: http://db.apache.org/derby/docs/dev/devguide/cdevdvlp17453.html
Alternatively, extract the database from the jar to the filesystem upon launch and point there.
I think you can do the following:
Package your database on the classpath.
When the app loads, copy the database to some temporary directory (like /tmp)
Instruct sqlite to read it from there (by setting the jdbc url)
Add a jvm hook to delete the file when the app gets closed.
That should work like a charm.
Hint:
Use getClass().getResourceAsStream(); to get the reference of the file on the classpath.

which is the best way to update an application from a server?

I'm looking for a non-webstart/jnlp solution.
I'd like to add to my app an update feature that checks in an ftp or http server and downloads the last version (if there is a newer one) replacing the libs that has been changed.
How can i do that? I want to implement something like JDownloaders updates.
Thanks
It looks like you just described exactly how to do it. Add an update feature that checks an FTP or HTTP server and downloads the latest version.
Remember that you cannot download and overwrite a file which is in use. So you have two options for a design from where I sit:
When you start up the application, copy all of the jar and library files to a /temp folder of some sort before running them. Then, when you download the update, overwrite the files in the original place. The next time the application starts up, it will use the new files.
When you start up the application, first startup an updater. Have it connect to the server and compare all of the file versions. It will be able to overwrite any of the application files because it doesn't use them. It only uses the updater jar. Once this is done and everything has been updated, then start a new process from the updater with the actual application. You will also need to put some code in to be able to update the updater jar. Either make the main application be able to update the updater, or use the first technique and run the updater from a copy of the updater jar.
Remember when you download the files that you should be downloading them to a temporary location and then moving them to the right place when they're done. This will make sure that you never leave your application in a "half-downloaded" state.
Beyond that, getting this to work is going to be about a lot of testing. Good luck!
Have a look at http://code.google.com/p/getdown/
According to this question on stackoverflow it seem to be a viable alternative for web start (at least worth having a look at).

Categories