Use try-with-resources
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / PolicyManagerServlet.java
index 91f8293..e62c878 100644 (file)
@@ -25,7 +25,6 @@ import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -150,34 +149,22 @@ public class PolicyManagerServlet extends HttpServlet {
        protected static void initializeJSONLoad() {
                closedLoopJsonLocation = Paths.get(XACMLProperties
                                .getProperty(XACMLRestProperties.PROP_ADMIN_CLOSEDLOOP));
-               FileInputStream inputStream = null;
-               JsonReader jsonReader = null;
                String location = closedLoopJsonLocation.toString();
-               try {
-                       inputStream = new FileInputStream(location);
-                       if (location.endsWith("json")) {
-                               jsonReader = Json.createReader(inputStream);
-                               policyNames = jsonReader.readArray();
-                               serviceTypeNamesList = new ArrayList<>();
-                               for (int i = 0; i < policyNames.size(); i++) {
-                                       javax.json.JsonObject policyName = policyNames.getJsonObject(i);
-                                       String name = policyName.getJsonString("serviceTypePolicyName").getString();
-                                       serviceTypeNamesList.add(name);
-                               }
+               if (! location.endsWith("json")) {
+                       LOGGER.warn("JSONConfig file does not end with extension .json");
+                       return;
+               }
+               try (FileInputStream inputStream = new FileInputStream(location);
+                       JsonReader jsonReader = Json.createReader(inputStream)) {
+                       policyNames = jsonReader.readArray();
+                       serviceTypeNamesList = new ArrayList<>();
+                       for (int i = 0; i < policyNames.size(); i++) {
+                               javax.json.JsonObject policyName = policyNames.getJsonObject(i);
+                               String name = policyName.getJsonString("serviceTypePolicyName").getString();
+                               serviceTypeNamesList.add(name);
                        }
-               } catch (FileNotFoundException e) {
+               } catch (IOException e) {
                        LOGGER.error("Exception Occured while initializing the JSONConfig file"+e);
-               }finally{
-                       try {
-                               if(inputStream != null){
-                                       inputStream.close();
-                               }
-                               if(jsonReader != null){
-                                       jsonReader.close();
-                               }
-                       } catch (IOException e) {
-                               LOGGER.error("Exception Occured while closing the File InputStream"+e);
-                       }
                }
        }
 
@@ -248,7 +235,7 @@ public class PolicyManagerServlet extends HttpServlet {
                                }
                        }
 
-                       JSONObject responseJsonObject = null;
+                       JSONObject responseJsonObject;
                        responseJsonObject = this.success();
                        response.setContentType(CONTENTTYPE);
                        PrintWriter out = response.getWriter();
@@ -913,7 +900,6 @@ public class PolicyManagerServlet extends HttpServlet {
                        policyParams.put("newPolicySplit_0", newPolicySplit[0]);
                        List<Object> queryData = controller.getDataByQuery(policyEntityquery, policyParams);
                        if(!queryData.isEmpty()){
-                               entity = (PolicyEntity) queryData.get(0);
                                return error("Policy rename failed. Since, the policy with same name already exists.");
                        }
 
@@ -925,20 +911,21 @@ public class PolicyManagerServlet extends HttpServlet {
                        params.put("oldPolicySplit_0", oldPolicySplit[0]);
                        List<Object> oldEntityData = controller.getDataByQuery(oldpolicyEntityquery, params);
                        if(!oldEntityData.isEmpty()){
-                               String groupQuery = "FROM PolicyGroupEntity where (";
+                               StringBuilder groupQuery = new StringBuilder();
+                               groupQuery.append("FROM PolicyGroupEntity where (");
                                SimpleBindings geParams = new SimpleBindings();
                                for(int i=0; i<oldEntityData.size(); i++){
                                        entity = (PolicyEntity) oldEntityData.get(i);
                                        if(i == 0){
-                                               groupQuery = groupQuery +  "policyid = :policyId";
+                                               groupQuery.append("policyid = :policyId");
                                                geParams.put("policyId", entity.getPolicyId());
                                        }else{
-                                               groupQuery = groupQuery +  " or policyid = :policyId" + i;
+                                               groupQuery.append(" or policyid = :policyId" + i);
                                                geParams.put("policyId" + i, entity.getPolicyId());
                                        }
                                }
-                               groupQuery = groupQuery + ")";
-                               List<Object> groupEntityData = controller.getDataByQuery(groupQuery, geParams);
+                               groupQuery.append(")");
+                               List<Object> groupEntityData = controller.getDataByQuery(groupQuery.toString(), geParams);
                                if(! groupEntityData.isEmpty()){
                                        return error("Policy rename failed. Since the policy or its version is active in PDP Groups.");
                                }
@@ -1478,7 +1465,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        String path = params.getString("path");
                        try{
                                if(params.has("subScopename")){
-                                       if(!params.getString("subScopename").equals("")){
+                                       if(! "".equals(params.getString("subScopename"))) {
                                                name = params.getString("path").replace("/", File.separator) + File.separator +params.getString("subScopename");
                                        }
                                }else{
@@ -1501,7 +1488,7 @@ public class PolicyManagerServlet extends HttpServlet {
                                }
                        }
                        LOGGER.debug("addFolder path: {} name: {}" + path +name);
-                       if(!name.equals("")){
+                       if(! "".equals(name)){
                                if(name.startsWith(File.separator)){
                                        name = name.substring(1);
                                }