Spring MVC Web App with Default user - java

I am developing a web app using Spring MVC and Hibernate that I hope to package up as a WAR file and distribute for users to deploy where needed.
The application will require the database connection details before deploying, can anyone advise on the best practice of how to do this? currently my database.properties file is packaged as part of the WAR file, is there an easier way than asking the user to manually edit this file inside the WAR file or in the source and then expect them to build the war file themselves?
I would also like an easy way for users to create a single admin user on first deploy easily - is it best to just provide a sql script to create that or is there a way to include the functionality in the web app so on first deploy it creates the user?
Thanks

Since web applications are not focused to "mass production", I can't tell if something like that is possible.
You can make an SQL script containing all your CREATE statements limited by "IF NOT EXISTS" to avoid deleting existing data. Hibernate can execute a sql script at the moment of creating the SessionFactory, if Hibernate finds a file named "import.sql" located in the classpath root, Hibernate will execute it. Also you can always edit the database.properties of an application already deployed without re-packaging the WAR.

Try Liquibase, http://www.liquibase.org/ . It does not allow your users to setup database connection details but it allows you to manage the database schema (create and update the schema) and default data.

Related

How to include Hsql database in WAR File?

i am developing a little application in JAVA, in which i use HSQL database to store some statistics and perform some calculations, now the requirement of application is like
The web application should be packaged as a self-contained portable war file. The war should contain any/all 3rd party jars that it depends on.
It should be possible to download a fresh tomcat, install the war, and use web app immediately without any configuration.
All config parameters should have useful default values.
i have developed the application but i am not able to include thw HSQL database in WAR file, i have tried different tutorials and search on stack overflow but i can't find any solution,
please tell me the way how i include hsql database in war file ?
Create a java class that create HSQL database on tomcat start if not exist
path of HSQL database for exemple tomcat/conf:
Server server = new Server();
server.setDatabaseName(0, "test");
server.setDatabasePath(0, "file:/path/to/db");
server.start();

Where should we store database config files in Java Eclipse Dynamic Web Project?

I'm new in Java development and learning the basics of Eclipse environment.
Referring to the link, I got to know the folder structure of any web application (Dynamic Web Project).
I need to know the location of JDBC database configuration files having connection string, username, passwords etc.
Question: Should we keep the database configuration files (DAL) in WEB-INF and gain access from outside (I don't know how to do that) or should we keep the configuration files in src/package ?
JDBC database configuration files must be done in the Dao class ... which is a simple java class we dont keep the database config files in web-inf... WEB_INF is not used for this.. u must read some docs first about that...
If you use hibernate for the database.. then the configuration file is hibernate.cfg.xml which is placed in src/ Main/resources
and if you are using jpa(java persistence api) then
The persistence.xml file which contains the configurations of database is typically present in the src/META-INF directory of the persistence (JPA) project.

How to let user configure a file inside the war file

There is an application, say myApp.war (developed using Spring MVC) that I give the users to deploy in their tomcat webapp folder. When the user starts tomcat, the war is exploded, and then I ask the user to go myApp/WEB-INF/classes/persistence.properties and ask him to edit just one property name (actually an HSQLDB path). Post that I ask the user to stop tomcat, delete the war file and start tomcat server again. And the application is up and running.
Although the users are not complaining, I believe there has to be a better way of doing this. For example when the users deploy wordpress or hudson and the first time they try to access the app. they are redirected to an install page where they do their basic configuration and they are up and running. How can I achieve it here.
I have used JNDI to solve this very problem in the past. Here is a nice example to show you how to do this with Spring:
http://www.journaldev.com/2597/spring-datasource-jndi-with-tomcat-example
Checkout JMX which can allow on the fly configuration, or there is one obix framework
Why not something like this:
Use a relative path with sysprops, like ${user.home}/path-to-hsqldb. If the user runs tomcat as user "jim", it will look in c:\users\jim\path-to-hsqldb (Windows) or /usr/jim/path-to-hsqldb (Linux)
You might need to use Spring's <context:property-placeholder/> to enable this.

Can tomcat store variables/properties that get called from a war

I want to be able to deploy my app using ANT to Tomcat.
I don't want the process to be any different for dev and prod. However the two use different databases i.e. myapp and myapp-dev
How can I make this happen? Can I store a variable in the different tomcat containers and make the application call the name of the database from Tomcat.
Or if what I am asking is ridiculous what is the generally accepted way to achieve deploying to dev and prod with the same process.
The generic way is to put the configuration string in a JNDI entry.
If JNDI is not a possible solution, then a property file in the right location (so it shows up in the classpath of the WAR files) is also useful, but needs careful documentation.
Have you considered letting the web container manage the database connection pool, so you only need a single one pr container, which then can be retrieved through JNDI?

Best way to access a sqlite database file in a web service

First question from me on stack overflow.
I have created a java web application containing a web service using netbeans (I hope a web application were the correct choice). I use the web application as is with no extra frameworks. This web service use a sqlite JDBC driver for accessing a sqlite database file.
My problem is that the file path end up incorrect when I try to form the JDBC connection string. Also, the working directory is different when deploying and when running JUnit tests. I read somewhere about including the file as a resource, but examples of this were nowhere to be seen.
In any case, what is the best way to open the sqlite database, both when the web service is deployed and when I test it "locally"?
I don't know much about web services, I just need it to work, so please, help me with the technicalities.
Update
To put this a litle bit in context, some "println" code gives this:
Printing the work directory from a simple JUnit test gives
C:\MinaFiler\Work\SOA\BusTimetableWS
Invoking a similar web servic method returns
C:\Program Files\sges-v3\glassfish\domains\domain1
The connection string is formed from prepending "jdbc:sqlite:" to the path which at the moment is absolute:
C:\MinaFiler\Work\SOA\BusTimetableWS\src\java\miun\bustimetable\database\sqlit\BusTimetableWS.db
However, this fails because my tests throws exceptions stating database tables doesn't exist although they really do, I can see them with sqlite3.exe .
One way would be to use a config file that you can read and fetch your connection string from there.
I'm sure the framework you are using has some kind of standard way of saving configurations.
Another option would be to place the db in a known relative path from your main execution files. Then when executed fetch your current directory, and look for the db from that path.
In any case, what is the best way to open the sqlite database, both when the web service is deployed and when I test it "locally"?
The web service should use a DataSource to retrieve a connection from a connection pool configured at the application server level. In your unit test, use whatever you want (a standalone connection pool, a direct JDBC connection).
But in both cases, why don't you use an absolute path to the database file in your jdbc url? From How to Specify Database Files:
jdbc:sqlite:C:/work/mydatabase.db
The working directory wouldn't matter if you do so.

Categories

Resources