Reputation: 209
I've been working on a project lately and everything is fine until I try to send to project to someone.
So I have a hibernate.properties
file that looks like this:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.connection.driver_class = com.mysql.cj.jdbc.Driver
hibernate.show-sql = true
hibernate.hbm2ddl.auto = update
hibernate.connection.url = jdbc:mysql://localhost:3306/mypersonaldatabase?serverTimezone=UTC
hibernate.connection.username = root
hibernate.connection.password = example
As mentioned above, there are two problems when I send the project to someone:
he has to create a database with the name I settled in hibernate.properties
(which is mypersonaldatabase
in the code above).
He has to set his database password which is with high probability different from mine (which is example
in the above code).
I know that this problem is easily solved by changing what I've mentioned above manually in the project file but this would be more problematic when I send the project as a JAR file.
I know that jAR still can be accessed, opened, and then change manually but it's still not nice to do so.
So is there any possible solution for this?
Upvotes: 0
Views: 68
Reputation: 209319
Generally, for configuration that is client-specific, you should not include the configuration as a resource but should let the user specify it. If the configuration is unlikely to change from run to run of an application for a specific user (which is usual), you should allow the user to save it to a file to avoid having to input the information on application startup every time.
Note that there are many different ways to integrate Spring and JavaFX, and to integrate Hibernate and Spring, so I will not include that in this answer; you will need to figure out how to integrate this approach with whichever approaches you are using for those.
The idea looks something like this:
public class App extends Application {
@Override
public void start(Stage stage) throws Exception {
Properties hibernateProps = getHibernateProps();
if (hibernateProps != null) {
// start spring and pass it the hibernate properties...
// start the UI in the usual way...
stage.show();
} else {
// can't run without these properties, just bail
Platform.exit();
}
}
private Properties getHibernateProperties() {
Properties props = new Properties();
Path propertiesFile = Paths.get(
System.getProperty("user.home"),
".myapp",
"hibernate.properties"
);
if (Files.exists(propertiesFile)) {
props.load(Files.newBufferedReader(propertiesFile));
} else {
getHibernatePropsFromUser(props, propertiesFile);
}
if (props.isEmpty()) {
return null ;
}
if (props.getProperty("hibernate.connection.password")==null) {
props.setProperty("hibernate.connection.password", promptUserForDbPassword(props));
}
return props ;
}
private void getHibernatePropsFromUser(props, propertiesFile) {
GridPane grid = new GridPane();
ColumnConstraints leftCol = new ColumnConstraints();
leftCol.setHalignment(HPos.RIGHT);
leftCol.setHgrow(Priority.NEVER);
ColumnConstraints rightCol = new ColumnContraints();
rightCol.setHgrow(Priority.ALWAYS);
rightCol.setFillWidth(true);
grid.getColumnConstraints().addAll(leftCol, rightCol);
grid.addColumn(0,
new Label("Database name:"),
new Label("Database user:"),
new Label("Database password:"),
new Label("Save password?")
);
TextField name = new TextField();
TextField user = new TextField();
PasswordField password = new PasswordField();
CheckBox savePW = new CheckBox();
grid.addColumn(1, name, user, password, savePW);
Button ok = new Button("OK");
Button cancel = new Button("Cancel");
HBox buttons = new HBox(5, ok, cancel);
buttons.setAlignment(Pos.CENTER);
grid.add(buttons, 0, 4, 2, 1);
Scene scene = new Scene(grid);
Stage stage = new Stage();
stage.setScene(scene);
ok.setOnAction(e -> {
props.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/"+name.getText()+"mypersonaldatabase?serverTimezone=UTC");
props.setProperty("hibernate.connection.username", user.getText());
if (savePW.isSelected()) {
props.setProperty("hibernate.connection.password", password.getText());
}
props.setProperty(...) ; // hard-coded properties
props.write(Files.newBufferedWriter(propertiesFile));
props.setProperty("hibernate.connection.password", password.getText());
stage.close();
});
cancel.setOnAction(e -> stage.close());
stage.showAndWait();
}
private void promptUserForDBPassword(Properties props) {
// show a "enter password" dialog and save password to props...
}
}
Upvotes: 1