Default data in Webapp? - java

how can I setup default data in a webapp? Eg default users: admin, test, etc.
I first thought of defining a static section in of of my session-beans, but that would still cause a new creation of users for every session. Which is not suitable.
How can I do else?
ty!

If you are using hibernate (I'll assume you are from the question tags), then all you need is to have an file called import.sql in your classpath, and hibernate will automatically execute it if you have the hibernate.hbm2ddl.auto property set to create or create-drop. Have a look at this link:
http://relation.to/Bloggers/RotterdamJBugAndHibernatesImportsql#H-ImportsqlEasilyImportDataInYourUnitTests

the best way is to put in your webapp's configuration section, there it will be loaded and read only once through the following steps :
an xml file of your data, just put it on your class path and read it by using java propery which will get an input stream of your class loader path

Related

MyBatis Spring DYNAMIC number of multiple databases Java configuration

This is fairly straight forward with a simple Spring DAO approach. However, using MyBatis, is there a way to setup multiple potential datasources?
The best approach I can think of is to use an ArraList of a Bean each containing datasource.driverclass,datasource.url, datasource.username, datasource.password etc.
The values for the datasources are stored in individual properties files. There could be 1 or 10 of these property files (or more).
So for example, one application startup all the property files would be loaded one at a time into an ArrayList. Then, based on the NAME=value line from the property file, we would know which datasource to hit.
So http:localhost:8080/name=db1
... would access all the data from the datasource configured with the name "09". Each property file would contain:
name=db1
datasource.driverclass=jdbc:sqlserver
datasource.url=jdbc:sqlserver://localhost:1433;databaseName=someDBname
datasource.username=user1
datasource.password=pass1
So the identifier here is "name=db1".
Would the best approach from a MyBatis implementation utilise an ArrayList of Beans?
Here are some leads if you want to keep up with multiple DB:
Anyway, I would say datasources shall be managed in the server confiquration instead of in the App.
Then Mybatis main configuration file must be placed in a location added to the classpath, but outside of the app package, because every new datasource must be referenced there inside an environment element.
And for every user request or session (in case of a web app), the configuration will be parsed because SqlSessionFactoryBuilder.build(reader, environment=NAME); must be called to choose the environment (=> the DB).
I ended up using a hierarchical application.yml file detailing the multitenant connection values, based on a selected tenant code.

cannot generate hibernate.reveng.xml since the hibernate.connection.url is dynamic

I want to generate a hibernate.reveng.xml, but the "hibernate.connection.URL" in the hibernate.cfg.xml is a variable.
jdbc:jtds:sqlserver://${database.server.name}:1433/XXX_DB
so my question is how to let the hibernate-tool know where defined the variables?
You can specify, within an ant task, the path to a .properties file (which will have this content):
hibernate.connection.url=jdbc:mysql://127.0.0.1:1433/XXX_DB
hibernate.connection.username=xxx
hibernate.connection.password=yyy
then, in the ant task:
<hibernatetool ...>
<jdbcconfiguration configurationfile="pathTo/your.cfg.cml"
propertyfile="pathTo/your.file.properties"/>
Hope it helps,
Diego.
It seems you are expecting, dynamic change of IP address in Data base connection URl and want a database connection at runtime.
If so you need to create a new hibernate Configuration instance in
the project.
or
Change the machine name in the properties file and restart your server.
Usually,DB machine name should be decided before building your project.
And these Machine details should be passed as an input to your build process and it will construct you DB URL accordingly(you can use Spring expression language to do this).

Where can I put i18n messages when using Struts2 annotation validations?

I'd want to use annotation validators provided by Struts2 in form
#RequiredStringValidator(key="required")
Where can I put properties file that will be looked up?
I18N messages/resources can go in a variety of locations depending on where they'll be used.
Resources are looked up hierarchically, starting with action-specific property files, package files, eventually in the global properties defined with the resource property in the other answer.
XML configuration is the preferred mechanism, however, if you decide to explicitly name resources.
See the S2 Localization docs for additional details.
In your Struts.properties insert the following;
struts.customs.i18n.resources=MyResources
And create a file called MyResources.properties to be resided in the same path as the Struts.properties and have your key/value based messages in that file

How to load app-wide settings at startup (for Spring3 webapp)?

I am in the basic stages of writing a Spring3 MVC webapp with Hibernate. I want all of data model classes to be able to access basic configuration values for example, database table prefix name, etc. I want this option, so I (or other developers) can change things on the fly by modifying them in the .properties file.
Is my best bet to create a Config class in a util package with a static block that loads a bunch of properties from a .properties file? I suppose the class itself could be static with a variety of getters to access the values within.
If I choose the method above, how could I insure the application didn't load (Failed gently) if for some reason the .properties file I have specified was not able to be loaded? With exceptions?
If my way stinks, what might be a better scenario?
Thanks!
That's a fine approach IMHO. If you would explicitly declare a bean for this class, like
<bean id="myConfig" class="com.yourcompany.yourproject.Config"/>
spring will fail at startup if it cannot instantiate the bean. So if the properties file is unreadable/not available just throw an unchecked Exception from Configs constructor.
if -for some reason- you enabled lazy loading globally you have to explicitly disable it for this bean, otherwise you won't get a failfast solution
<bean id="myConfig" class="com.yourcompany.yourproject.Config" lazy-init="false"/>
EDIT:
another nice feature of this scenario is that you can tell maven to 'filter' the resource (the .properties file), and you can get all the maven variables. This is how my prop file looks (I use this info for the About dialog. Does anybody ever opens an about-dialog btw?)
project.version=${project.version}
project.name=${project.name}
project.organization.name=${project.organization.name}
project.url=${project.url}
project.description=${project.description}

Changing Spring PropertyPlaceholderConfigurer to read from another source

I'd like to extend/replace the Spring PropertyPlaceholderConfigurer to read from a web server as opposed to properties files.
A bit of background:
I work on a project, and we're finding the number of properties files located on the users systems is getting a little unwieldy. We'd like to replace these files with a 'config server' which will store basic key/value pairs and serve them when the user starts up the app.
To avoid making too many changes, I'd like to change the way the PropertyPlaceholderConfigurer finds properties - rather than implementing an entirely new way to manage properties. So on startup - Spring will read all properties from a url, and feed these into my spring config xml in the same way as it would have with actual files.
Bonus!
If anyone has any ideas how to do this where properties are reloaded from the server only when they change, will get bonus points (I have no idea if I have the ability to assign bonus points, but I'll try!). That would be a 'nice to have, if there's not too much effort involved' solution.
Spring's PropertyPlaceholderConfigurer (PPC) already uses the Resource interface to specfiy the location from where to read properties (via the setLocation(Resource) method inherited from PropertiesLoaderSupport.
There is an implementing class of this interface called URLResource which probably does what you want. You could simply create a PPC and set the location property with a bean of this type to load the properties from a URL instead of a file. This class also supports file:// type URLs, so you could switch between on- and offline properties loading depending on the URL you use.

Categories