Skip to content

Commit

Permalink
FORGE-2028: Introduced 'save password' feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 22, 2014
1 parent 5533d1b commit 05f70d1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public abstract class AbstractConnectionProfileDetailsPage implements UICommand
@WithAttributes(label = "User Password", description = "The password for the database connection", required = false, defaultValue = "", type = InputType.SECRET)
protected UIInput<String> userPassword;

@Inject
@WithAttributes(label = "Save User Password?", description = "Should the connection password be saved?")
protected UIInput<Boolean> saveUserPassword;

@Inject
@WithAttributes(label = "Hibernate Dialect", description = "The Hibernate dialect to use", required = true)
protected UISelectOne<HibernateDialect> hibernateDialect;
Expand Down Expand Up @@ -190,6 +194,7 @@ public void validate(UIValidationContext context)
builder.add(jdbcUrl)
.add(userName)
.add(userPassword)
.add(saveUserPassword)
.add(hibernateDialect)
.add(driverLocation)
.add(driverClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ConnectionProfileManagerImpl implements ConnectionProfileManager
private final static String CONFIG_KEY_URL = "url";
private final static String CONFIG_KEY_USER = "user";
private final static String CONFIG_KEY_PASSWORD = "pass";
private final static String CONFIG_KEY_SAVE_PASSWORD = "save-password";

@Inject
private Configuration config;
Expand All @@ -47,6 +48,7 @@ public Map<String, ConnectionProfile> loadConnectionProfiles()
descriptor.setUrl(child.getAttribute(CONFIG_KEY_URL));
descriptor.setUser(child.getAttribute(CONFIG_KEY_USER));
descriptor.setPassword(child.getAttribute(CONFIG_KEY_PASSWORD));
descriptor.setSavePassword(Boolean.parseBoolean(child.getAttribute(CONFIG_KEY_SAVE_PASSWORD)));
result.put(descriptor.getName(), descriptor);
}
}
Expand All @@ -66,7 +68,8 @@ public void saveConnectionProfiles(Collection<ConnectionProfile> connectionProfi
child.attribute(CONFIG_KEY_PATH_TO_DRIVER, descriptor.getPath());
child.attribute(CONFIG_KEY_URL, descriptor.getUrl());
child.attribute(CONFIG_KEY_USER, descriptor.getUser());
if (!Strings.isNullOrEmpty(descriptor.getPassword()))
child.attribute(CONFIG_KEY_SAVE_PASSWORD, descriptor.isSavePassword());
if (descriptor.isSavePassword() && !Strings.isNullOrEmpty(descriptor.getPassword()))
{
child.attribute(CONFIG_KEY_PASSWORD, descriptor.getPassword());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public Result execute(UIExecutionContext context) throws Exception
connectionProfile.setPath(driverLocation.getValue().getFullyQualifiedName());
connectionProfile.setUrl(jdbcUrl.getValue());
connectionProfile.setUser(userName.getValue());
connectionProfile.setSavePassword(saveUserPassword.getValue());
connectionProfile.setPassword(userPassword.getValue());
connectionProfiles.put(name.getValue(), connectionProfile);
provider.getConnectionProfileManager().saveConnectionProfiles(connectionProfiles.values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Callable;

import javax.inject.Inject;
import javax.persistence.Entity;
Expand Down Expand Up @@ -45,7 +46,6 @@
public class GenerateEntitiesCommand extends AbstractProjectCommand implements
UIWizard
{

private static String[] COMMAND_CATEGORY = { "Java EE", "JPA" };
private static String COMMAND_NAME = "JPA: Generate Entities From Tables";
private static String COMMAND_DESCRIPTION = "Command to generate Java EE entities from database tables.";
Expand All @@ -56,6 +56,9 @@ public class GenerateEntitiesCommand extends AbstractProjectCommand implements
@Inject
private ResourceFactory resourceFactory;

@Inject
private ConnectionProfileManagerProvider managerProvider;

@Inject
@WithAttributes(
label = "Target package",
Expand All @@ -70,16 +73,13 @@ public class GenerateEntitiesCommand extends AbstractProjectCommand implements
description = "Select the database connection profile you want to use")
private UISelectOne<String> connectionProfile;

@Override
public Metadata getMetadata(UIContext context)
{
return Metadata.from(super.getMetadata(context), getClass())
.name(COMMAND_NAME).description(COMMAND_DESCRIPTION)
.category(Categories.create(COMMAND_CATEGORY));
}

@Inject
private ConnectionProfileManagerProvider managerProvider;
@WithAttributes(
label = "Connection Profile Password",
type = InputType.SECRET,
description = "Enter the database connection profile password")
private UIInput<String> connectionProfilePassword;

private Map<String, ConnectionProfile> profiles;

@Override
Expand All @@ -94,7 +94,21 @@ public void initializeUI(UIBuilder builder) throws Exception
profileNames.addAll(profiles.keySet());
connectionProfile.setValueChoices(profileNames);
connectionProfile.setValue("");
builder.add(targetPackage).add(connectionProfile);
// Enable password input only if profile does not store saved passwords
Callable<Boolean> enablePasswordInput = new Callable<Boolean>()
{
@Override
public Boolean call() throws Exception
{
String connectionProfileName = connectionProfile.getValue();
if (Strings.isNullOrEmpty(connectionProfileName))
return false;
ConnectionProfile profile = profiles.get(connectionProfileName);
return !profile.isSavePassword();
}
};
connectionProfilePassword.setEnabled(enablePasswordInput).setRequired(enablePasswordInput);
builder.add(targetPackage).add(connectionProfile).add(connectionProfilePassword);
}

@Inject
Expand Down Expand Up @@ -142,7 +156,6 @@ public NavigationResult next(UINavigationContext context) throws Exception
}
descriptor.setDriverClass(profile.getDriver());
descriptor.setConnectionProperties(createConnectionProperties(profile));

return Results.navigateTo(DatabaseTableSelectionStep.class);
}
}
Expand All @@ -156,8 +169,17 @@ private Properties createConnectionProperties(ConnectionProfile profile)
profile.getUser() == null ? "" : profile.getUser());
result.setProperty("hibernate.dialect",
profile.getDialect() == null ? "" : profile.getDialect());
result.setProperty("hibernate.connection.password",
profile.getPassword() == null ? "" : profile.getPassword());
String profilePassword;
// If password is not saved, user must supply it
if (profile.isSavePassword())
{
profilePassword = profile.getPassword() == null ? "" : profile.getPassword();
}
else
{
profilePassword = connectionProfilePassword.getValue();
}
result.setProperty("hibernate.connection.password", profilePassword);
result.setProperty("hibernate.connection.url",
profile.getUrl() == null ? "" : profile.getUrl());
return result;
Expand Down Expand Up @@ -206,4 +228,13 @@ private FileResource<?> createResource(String fullPath)
{
return resourceFactory.create(FileResource.class, new File(fullPath));
}

@Override
public Metadata getMetadata(UIContext context)
{
return Metadata.from(super.getMetadata(context), getClass())
.name(COMMAND_NAME).description(COMMAND_DESCRIPTION)
.category(Categories.create(COMMAND_CATEGORY));
}

}

0 comments on commit 05f70d1

Please sign in to comment.