Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions src/main/java/com/openshift/client/IApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
/**
* @author André Dietisheim
* @author Syed Iqbal
* @author Martes G Wigglesworth
*/
public interface IApplication extends IOpenShiftResource {

Expand Down Expand Up @@ -423,38 +424,34 @@ public IEmbeddedCartridge getEmbeddedCartridge(IEmbeddableCartridge cartridge)
/**
* Retrieves the map of environment variables
*
* @return the list of environment variables
* @return the Map<String,IEnvironmentVariable> of environment variables
* @throws OpenShiftSSHOperationException
*/
public Map<String, IEnvironmentVariable> getEnvironmentVariables() throws OpenShiftSSHOperationException;

/**
* Checks if the environment variable is present in the application.
*
* @param name
* Name of the environment variable
* @return
* @param name Name of the environment variable
* @return <code>true</code> if the current instance has IEnvironmentVariables to return <br>
* <code>false</code> if the current instance has no IEnvironmentVariables to return
* @throws OpenShiftSSHOperationException
*/
public boolean hasEnvironmentVariable(String name) throws OpenShiftException;

/**
* Adds an environment variable to this application. If the environment
* variable exists already, then an OpenShift exception is thrown.
* Adds an environment variable to this application.
*
* @param name
* name of the variable to add
* @param value
* value of the new variable
* @throws OpenShiftSSHOperationException
* @param name key associated with the variable to add
* @param value value of the new variable
* @throws OpenShiftSSHOperationException - if the variable already exists
*/
public IEnvironmentVariable addEnvironmentVariable(String name, String value) throws OpenShiftException;

/**
* Adds a map of environment variables to the application
*
* @param environmentVariables
* map of environment variables
* @param environmentVariables Map<String,String> of environment variables
* @throws OpenShiftSSHOperationException
*/
public Map<String, IEnvironmentVariable> addEnvironmentVariables(Map<String, String> environmentVariables)
Expand All @@ -463,42 +460,47 @@ public Map<String, IEnvironmentVariable> addEnvironmentVariables(Map<String, Str
/**
* Return the environment variable for the specified name
*
* @param name
* Name of the environment variable
* @return environment variable
* @throws OpenShiftSSHOperationException
* @param name key to be used to locate the environment variable
* @return IEnvironmentVariable associated with the provided key
* @throws OpenShiftSSHOperationException - thrown if the key does not exist
*/
public IEnvironmentVariable getEnvironmentVariable(String name) throws OpenShiftException;

/**
* Removes the environment variables with the given name from this application.
*
* @param name
* @return
* @throws OpenShiftException
* @param name key associated with the IEnvironmentVariable to be removed
* @return
* @throws OpenShiftException - thrown if there is no IEnvironmentVariable associated with the explicit parameter.
*/
public void removeEnvironmentVariable(String name) throws OpenShiftException;

/**
* Removes the environment variables with the given name from this application.
*
* @param environmentVariable IEnvironmentVariable instance which should be removed
* @return
* @throws OpenShiftException - thrown if there is no instance of IEnvironmentVariable available to be removed
*/
public void removeEnvironmentVariable(IEnvironmentVariable environmentVariable);

/**
* Returns <code>true</code> if this application can list its environment
* variables. Returns <code>false</code> if it cant. Internally this
* translates to the presence of the link to list environment variables.
* Used to determine if environment variables exist and are available to be retrieved
*
* @return true if this application can list its envirnoment variables
* @return Returns <code>true</code> if this application can list its environment variables. <br>
* Returns <code>false</code> if it cannot. Internally this translates to the presence of the link to list environment variables.
*
* @see #getEnvironmentVariables()
* @see #getEnvironmentVariablesMap()
* @see #getEnvironmentVariable(String)
* @see ApplicationResource#LINK_LIST_ENVIRONMENT_VARIABLES
*/
public boolean canGetEnvironmentVariables();

/**
* Returns <code>true</code> if this application can update (set or unset)
* its environment variables. Returns <code>false</code> if it cannot.
* Internally this translates to the presence of the link to set and unset
* environment variables.
* Used to determine if the current instance is able to update environment variables.
*
* @return true if this application can set/unset its environment variables
* @return Returns <code>true</code> if this application can augment its environment variables.<br>
* Returns <code>false</code> if it cannot. <br>
*
* @see #addEnvironmentVariable(String, String)
* @see #addEnvironmentVariables(Map)
Expand Down
132 changes: 85 additions & 47 deletions src/main/java/com/openshift/internal/client/ApplicationResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@
import com.openshift.internal.client.utils.StringUtils;

/**
* The Class Application.
* The ApplicationResource object is an implementation of com.openshift.client.IApplication, and provides
* a runtime model for the real application that resides on the OpenShift platform being accessed.
*
* @author André Dietisheim
* @author Syed Iqbal
* @author Martes G Wigglesworth
*/
public class ApplicationResource extends AbstractOpenShiftResource implements IApplication {

Expand Down Expand Up @@ -149,7 +151,7 @@ public class ApplicationResource extends AbstractOpenShiftResource implements IA
/**
* The environment variables for this application
*/
private Map<String, IEnvironmentVariable> environmentVariableByName;
private Map<String, IEnvironmentVariable> environmentVariablesMap;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest keeping the "ByName" since its more explicit than the "Map", it tells you what the keys and what the values are.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I am going to have to rebase and then update the PR again.

I see that I got a message from OpenShift-bot saying that the PR can't be automerged.

Respectfully,

Martes G Wigglesworth
Consultant - Middleware Engineer
Red Hat Consulting
Red Hat, Inc.

----- Original Message -----

From: "André Dietisheim" notifications@github.com
To: "openshift/openshift-java-client" openshift-java-client@noreply.github.com
Cc: "Martes Wigglesworth" mwigglesworth@redhat.com
Sent: Friday, November 8, 2013 12:01:28 PM
Subject: Re: [openshift-java-client] [JBIDE-15766]:Remove refresh of all IEnvironmentVariable(s) on add/remove actions (#100)

In src/main/java/com/openshift/internal/client/ApplicationResource.java:

@@ -149,7 +151,7 @@
/**

  • The environment variables for this application
    */
    • private Map<String, IEnvironmentVariable> environmentVariableByName;
    • private Map<String, IEnvironmentVariable> environmentVariablesMap;

I would suggest keeping the "ByName" since its more explicit than the "Map", it tells you what the keys and what the values are.


Reply to this email directly or view it on GitHub .



protected ApplicationResource(ApplicationResourceDTO dto, DomainResource domain) {
Expand Down Expand Up @@ -203,6 +205,7 @@ protected ApplicationResource(final String name, final String uuid, final String
this.domain = domain;
this.aliases = aliases;
updateCartridges(cartridgesByName);
environmentVariablesMap = new HashMap<String, IEnvironmentVariable>();
}

public String getName() {
Expand Down Expand Up @@ -611,11 +614,11 @@ public Map<String, IEnvironmentVariable> getEnvironmentVariables() throws OpenSh
return Collections.unmodifiableMap(new LinkedHashMap<String, IEnvironmentVariable>(getOrLoadEnvironmentVariables()));
}

protected Map<String, IEnvironmentVariable> getOrLoadEnvironmentVariables() throws OpenShiftException {
if (environmentVariableByName == null) {
this.environmentVariableByName = loadEnvironmentVariables();
}
return environmentVariableByName;

protected Map<String, IEnvironmentVariable> getOrLoadEnvironmentVariables() throws OpenShiftException {
if(environmentVariablesMap.isEmpty())
environmentVariablesMap = loadEnvironmentVariables();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no env vars in the applciation in the backend, wouldnt the map be empty, too? so we'd end up loading them until we add some var? Should we have a null-reference to the map as long as it wasnt loaded?

return environmentVariablesMap;
}

private Map<String, IEnvironmentVariable> loadEnvironmentVariables() throws OpenShiftException {
Expand All @@ -624,13 +627,14 @@ private Map<String, IEnvironmentVariable> loadEnvironmentVariables() throws Open
return new LinkedHashMap<String, IEnvironmentVariable>();
}

Map<String, IEnvironmentVariable> environmentVariablesByName = new LinkedHashMap<String, IEnvironmentVariable>();
for (EnvironmentVariableResourceDTO environmentVariableResourceDTO : environmentVariableDTOs) {
final IEnvironmentVariable environmentVariable =
new EnvironmentVariableResource(environmentVariableResourceDTO, this);
environmentVariablesByName.put(environmentVariable.getName(), environmentVariable);

environmentVariablesMap.put(environmentVariable.getName(),environmentVariable);

}
return environmentVariablesByName;
return environmentVariablesMap;
}

@Override
Expand All @@ -643,42 +647,67 @@ public IEnvironmentVariable addEnvironmentVariable(String name, String value) th
}
if (hasEnvironmentVariable(name)) {
throw new OpenShiftException("Environment variable with name \"{0}\" already exists.", name);
}

}
EnvironmentVariableResourceDTO environmentVariableResourceDTO =
new AddEnvironmentVariableRequest().execute(name, value);
new AddEnvironmentVariableRequest().execute(name, value);
IEnvironmentVariable environmentVariable = new EnvironmentVariableResource(environmentVariableResourceDTO, this);
updateEnvironmentVariables();

environmentVariablesMap.put(environmentVariable.getName(), environmentVariable);

return environmentVariable;
}

@Override
public Map<String, IEnvironmentVariable> addEnvironmentVariables(Map<String, String> environmentVariablesMap)
public Map<String, IEnvironmentVariable> addEnvironmentVariables(Map<String, String> environmentVariables)
throws OpenShiftException {

List<EnvironmentVariableResourceDTO> environmentVariableResourceDTOs = new AddEnvironmentVariablesRequest()
.execute(environmentVariablesMap);
Map<String, IEnvironmentVariable> environmentVariables = new HashMap<String, IEnvironmentVariable>();

Map<String,String>variablesCandidateMap = new HashMap<String,String>();
for(String varCandidateName:environmentVariables.keySet()){
IEnvironmentVariable tempVar = environmentVariablesMap.get(varCandidateName);
if(tempVar != null)
{ if(tempVar.getValue() == environmentVariables.get(varCandidateName))
variablesCandidateMap.put(varCandidateName,environmentVariables.get(varCandidateName));
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're filing single add-requests for each addition. It turns out that we could mutate the existing map and send it as a whole (or even better: only the changed parts in a single request). But nevermind, we can do this in a later change.
If the rest is fine we'd gain less requests with your change.

variablesCandidateMap.put(varCandidateName, environmentVariables.get(varCandidateName));
}
List<EnvironmentVariableResourceDTO> environmentVariableResourceDTOs = new AddEnvironmentVariablesRequest()
.execute(variablesCandidateMap);

for (EnvironmentVariableResourceDTO dto : environmentVariableResourceDTOs) {
IEnvironmentVariable environmentVariable = new EnvironmentVariableResource(dto, this);
environmentVariables.put(environmentVariable.getName(), environmentVariable);
environmentVariablesMap.put(environmentVariable.getName(), environmentVariable);
}

updateEnvironmentVariables();
return environmentVariables;

return environmentVariablesMap;
}

/*
* (non-Javadoc)
* @see com.openshift.client.IApplication#removeEnvironmentVariable(java.lang.String)
*/
@Override
public void removeEnvironmentVariable(String name) {
IEnvironmentVariable environmentVariable = getEnvironmentVariable(name);
if (environmentVariable == null) {
return;
}

environmentVariable.destroy();
updateEnvironmentVariables();
public void removeEnvironmentVariable(String targetName) {
removeEnvironmentVariable(getEnvironmentVariable(targetName));
}

/* (non-Javadoc)
* @see com.openshift.client.IApplication#removeEnvironmentVariable(com.openshift.client.IEnvironmentVariable)
*/
@Override
public void removeEnvironmentVariable(IEnvironmentVariable environmentVariable){
if(getEnvironmentVariable(environmentVariable.getName()) == null)
throw new OpenShiftException("IEnvironmentVariable with supplied name does not exist.");
environmentVariable.destroy();
environmentVariablesMap.remove(environmentVariable.getName());

}


/*
* (non-Javadoc)
* @see com.openshift.client.IApplication#hasEnvironmentVariable(java.lang.String)
*/
@Override
public boolean hasEnvironmentVariable(String name) throws OpenShiftException {
if (StringUtils.isEmpty(name)) {
Expand All @@ -689,17 +718,20 @@ public boolean hasEnvironmentVariable(String name) throws OpenShiftException {
}

protected void updateEnvironmentVariables() throws OpenShiftException {
if (!canGetEnvironmentVariables()) {
if (!canGetEnvironmentVariables())
return;
else
{
environmentVariablesMap.clear();
environmentVariablesMap = loadEnvironmentVariables();
}
if (environmentVariableByName == null) {
environmentVariableByName = loadEnvironmentVariables();
} else {
environmentVariableByName.clear();
environmentVariableByName.putAll(loadEnvironmentVariables());
}

}

/*
* (non-Javadoc)
* @see com.openshift.client.IApplication#getEnvironmentVariable(java.lang.String)
*/
@Override
public IEnvironmentVariable getEnvironmentVariable(String name) {
return getEnvironmentVariables().get(name);
Expand All @@ -714,6 +746,10 @@ public boolean canGetEnvironmentVariables() {
}
}

/*
* (non-Javadoc)
* @see com.openshift.client.IApplication#canUpdateEnvironmentVariables()
*/
@Override
public boolean canUpdateEnvironmentVariables() {
try {
Expand Down Expand Up @@ -820,9 +856,11 @@ public List<IApplicationPortForwarding> startPortForwarding() throws OpenShiftSS
try {
port.start(session);
} catch (OpenShiftSSHOperationException oss) {
// ignore for now
// FIXME: should store this error on the forward to let user
// know why it could not start/stop
/*
* ignore for now
* FIXME: should store this error on the forward to let user
* know why it could not start/stop
*/
}
}
return ports;
Expand All @@ -833,9 +871,10 @@ public List<IApplicationPortForwarding> stopPortForwarding() throws OpenShiftSSH
try {
port.stop(session);
} catch (OpenShiftSSHOperationException oss) {
// ignore for now
// should store this error on the forward to let user know why
// it could not start/stop
/* ignore for now
* should store this error on the forward to let user know why
* it could not start/stop
*/
}
}
// make sure port forwarding is stopped by closing session...
Expand Down Expand Up @@ -1094,5 +1133,4 @@ protected List<EnvironmentVariableResourceDTO> execute(Map<String, String> envir
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,22 @@ public String getValue() {
}

@Override
public void update(String value) throws OpenShiftException {
if (value == null) {
public void update(String newValue) throws OpenShiftException {
if (newValue == null) {
throw new OpenShiftException("Value for environment variable \"{0}\" not given.", name);
}
EnvironmentVariableResourceDTO environmentVariableResourceDTO =
new UpdateEnvironmentVariableRequest().execute(value);
new UpdateEnvironmentVariableRequest().execute(newValue);
updateEnvironmentVariable(environmentVariableResourceDTO);
/*
* This should be done in the IApplication, to break up this dependency
* on the entity, i.e. IEnvironmentVariable, on something that is
* outside of itself, such as the implementation of IApplication.
* @author Martes G Wigglesworth
*/
application.updateEnvironmentVariables();


}

private void updateEnvironmentVariable(EnvironmentVariableResourceDTO dto) {
Expand All @@ -81,7 +89,7 @@ private void updateEnvironmentVariable(EnvironmentVariableResourceDTO dto) {
@Override
public void destroy() throws OpenShiftException {
new DeleteEnvironmentVariableRequest().execute();
application.updateEnvironmentVariables();

}

@Override
Expand Down Expand Up @@ -118,5 +126,10 @@ protected DeleteEnvironmentVariableRequest() {
public IApplication getApplication() {
return application;
}


public String toString(){
return new String(
"Name:"+this.name+",Value:"+value
);
}
}
Loading