Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #5 from pjanouse/JENKINS-38243
[JENKINS-38243] Fixed unhandled NPE, added @checkfornull annotation
  • Loading branch information
scoheb committed Sep 19, 2016
2 parents e1b4524 + b7faa5d commit e2e8aa2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/com/redhat/foreman/ForemanAPI.java
Expand Up @@ -8,12 +8,14 @@
import java.util.List;
import java.util.Map;

import javax.annotation.CheckForNull;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
Expand Down Expand Up @@ -73,6 +75,7 @@ public ForemanAPI(String url, String user, Secret password) {
* @param hostname resource in Foreman.
* @return host in json form.
*/
@CheckForNull
public JsonNode reserveHost(String hostname) {
LOGGER.info("Reserving host " + hostname);
WebTarget target = base.path(FOREMAN_RESERVE_PATH)
Expand Down Expand Up @@ -155,6 +158,7 @@ private Response getForemanResponse(WebTarget target) {
* @return version.
* @throws Exception if occurs.
*/
@CheckForNull
public String getVersion() throws Exception {
WebTarget target = base.path(FOREMAN_STATUS_PATH);
Response response = getForemanResponse(target);
Expand All @@ -178,6 +182,7 @@ public String getVersion() throws Exception {
* @param parameterName name of param.
* @return value.
*/
@CheckForNull
public String getHostParameterValue(String hostname, String parameterName) {
String hostParamPath = FOREMAN_HOSTS_PATH + "/" + hostname + "/parameters/" + parameterName;
WebTarget target = base.path(hostParamPath);
Expand Down Expand Up @@ -211,6 +216,7 @@ public String getHostParameterValue(String hostname, String parameterName) {
* @param hostname name of host.
* @return value of slave remote FS root.
*/
@CheckForNull
public String getRemoteFSForSlave(String hostname) {
return getHostParameterValue(hostname, JENKINS_SLAVE_REMOTEFS_ROOT);
}
Expand All @@ -221,6 +227,7 @@ public String getRemoteFSForSlave(String hostname) {
* @param attribute attrib to look for.
* @return value of attrib.
*/
@CheckForNull
public String getHostAttributeValue(String hostname, String attribute) {
String hostParamPath = FOREMAN_HOSTS_PATH + "/" + hostname;
WebTarget target = base.path(hostParamPath);
Expand Down Expand Up @@ -251,6 +258,7 @@ public String getHostAttributeValue(String hostname, String attribute) {
* @param hostname name of host.
* @return IP.
*/
@CheckForNull
public String getIPForHost(String hostname) {
return getHostAttributeValue(hostname, "ip");
}
Expand Down Expand Up @@ -322,6 +330,6 @@ public String getLabelsForHost(String hostName) {
*/
public boolean isHostFree(String host) {
String free = getHostParameterValue(host, FOREMAN_SEARCH_RESERVEDPARAMNAME);
return free.equalsIgnoreCase("false");
return !StringUtils.isEmpty(free) && free.equalsIgnoreCase("false");
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/redhat/foreman/ForemanSharedNodeCloud.java
Expand Up @@ -30,6 +30,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import javax.annotation.CheckForNull;
import javax.security.auth.login.LoginException;
import javax.servlet.ServletException;

Expand Down Expand Up @@ -161,6 +162,7 @@ public boolean canProvision(Label label) {
}

@Override
@CheckForNull
public Collection<PlannedNode> provision(final Label label, int excessWorkload) {
Collection<NodeProvisioner.PlannedNode> result = new ArrayList<NodeProvisioner.PlannedNode>();
if (canProvision(label)) {
Expand Down Expand Up @@ -195,6 +197,7 @@ public Node call() throws Exception {
* @return a Foreman Slave.
* @throws Exception if occurs.
*/
@CheckForNull
private ForemanSharedNode provision(Label label) throws Exception {
LOGGER.debug("Trying to provision Foreman Shared Node for '" + label.toString() + "'");

Expand Down Expand Up @@ -275,6 +278,7 @@ private ForemanSharedNode provision(Label label) throws Exception {
* @param label Label to reserve for.
* @return name of host that was reserved.
*/
@CheckForNull
private String getHostToReserve(Label label) {
Map<String, String> hostsMap = getForemanAPI().getCompatibleHosts();
Set<String> hosts = hostsMap.keySet();
Expand All @@ -295,6 +299,7 @@ private String getHostToReserve(Label label) {
* @return a Foreman Cloud.
* @throws IllegalArgumentException if occurs.
*/
@CheckForNull
public static ForemanSharedNodeCloud getByName(String name) throws IllegalArgumentException {
if (name == null) {
return null;
Expand Down Expand Up @@ -493,6 +498,7 @@ private Set<String> checkForCompatibleHosts(String url, String user, Secret pass
* @return Foreman version.
* @throws Exception if occurs.
*/
@CheckForNull
private String testConnection(String url, String user, Secret password) throws Exception {
url = StringUtils.strip(StringUtils.stripToNull(url), "/");
if (url != null && isValidURL(url)) {
Expand Down

0 comments on commit e2e8aa2

Please sign in to comment.