Skip to content

Commit 6e9cae8

Browse files
Phase2 refactor (#4)
* Simplified logic of controllers. Avoided TSQ object memory leak. Removed unnecessary imports. Removed un-thrown exceptions. Added RootController as a 'usage' convenience landing page. Aliased REST endpoints for simplicity. Improved error messages and comments. Some renames for consistency and simplicity. * Minor messsage format * Simplify getMapping URL * Comments for RootController Co-authored-by: ivanh <[email protected]>
1 parent 94d2410 commit 6e9cae8

File tree

9 files changed

+169
-262
lines changed

9 files changed

+169
-262
lines changed

src/main/java/com/ibm/cicsdev/springboot/jcics/Application.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
import org.springframework.boot.autoconfigure.SpringBootApplication;
1515

1616
@SpringBootApplication
17-
public class Application {
18-
19-
public static void main(String[] args) {
17+
public class Application
18+
{
19+
public static void main(String[] args)
20+
{
2021
SpringApplication.run(Application.class, args);
2122
}
22-
2323
}

src/main/java/com/ibm/cicsdev/springboot/jcics/BrowseTSQController.java

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
package com.ibm.cicsdev.springboot.jcics;
1212

13-
import java.io.IOException;
14-
import java.io.UnsupportedEncodingException;
1513
import java.util.ArrayList;
1614

1715
import org.springframework.web.bind.annotation.GetMapping;
@@ -23,77 +21,72 @@
2321
import com.ibm.cics.server.TSQ;
2422

2523
@RestController
26-
public class BrowseTSQController {
27-
24+
public class BrowseTSQController
25+
{
2826
/**
2927
* The @GetMapping annotation ensures that HTTP GET requests to the /browseTSQs URL path are
30-
* mapped to the annotated method.
31-
* @throws IOException
32-
**/
33-
34-
@GetMapping("/browseTSQs")
35-
public String browseTSQs(@RequestParam(value = "tsqName", defaultValue = "ANNE") String tsqName) throws IOException {
36-
37-
// retrieve and store the list of items on the TSQ
38-
ArrayList<String> records = new ArrayList<String>();
39-
40-
// browse the records
41-
try {
42-
43-
records = browseTSQ(tsqName);
44-
45-
} catch ( CicsConditionException e) {
46-
47-
// Print the stack trace
28+
* mapped to the annotated method.
29+
**/
30+
@GetMapping("/browse")
31+
public String browseTSQ(@RequestParam(value = "tsq", defaultValue = "ANNE") String tsqName)
32+
{
33+
// ArrayList of items in the TSQ
34+
ArrayList<String> tsqItems;
35+
36+
// Populate the records
37+
try
38+
{
39+
tsqItems = readTSQ(tsqName);
40+
}
41+
catch ( CicsConditionException e)
42+
{
43+
// Return error details
4844
e.printStackTrace();
49-
50-
// Return useful information to the users when meeting errors
51-
return "Oops! Unexpected CICS conditionn exception: " + e.getMessage() + ". Please check stderr for details.";
45+
return "Unexpected CICS condition exception: " + e.getMessage() + ". Check dfhjvmerr for further details.";
5246
}
5347

54-
return records.toString();
48+
return tsqItems.toString();
5549
}
5650

51+
5752
/**
5853
* A method to browse a CICS TSQ
5954
*
60-
* @param tsqName, the name of the TSQ to browse @return, an ArrayList of records from the TSQ
55+
* @param tsqName, the name of the TSQ to browse
56+
* @return, an ArrayList of records from the TSQ
6157
* @throws CicsConditionException
62-
* @throws UnsupportedEncodingException
6358
*/
64-
private ArrayList<String> browseTSQ(String tsqName) throws CicsConditionException, UnsupportedEncodingException {
65-
// An ArrayList to hold all of the items read from TSQ to be returned to the servlet.
66-
ArrayList<String> records = new ArrayList<String>();
59+
private ArrayList<String> readTSQ(String tsqName) throws CicsConditionException
60+
{
61+
// An ArrayList to hold all of the items read from the TSQ
62+
ArrayList<String> tsqItems = new ArrayList<String>();
6763

68-
// construct a JCICS representation of the TSQ object
69-
TSQ tsqQ = new TSQ();
64+
// Create a JCICS representation of the TSQ object
65+
TSQ tsqQ = new TSQ();
7066
tsqQ.setName(tsqName);
7167

72-
// the holder object will hold the byte array that is read from the TSQ
68+
// Create a holder object to store the byte array representing a tsq item
7369
ItemHolder holder = new ItemHolder();
7470

75-
// initialize loop variables
71+
// initialise item position
7672
int itemPos = 1;
77-
int totalItems = 0;
78-
String recordStr = null;
79-
80-
// read an item and store the total number of items on the queue
81-
totalItems = tsqQ.readItem(itemPos, holder);
82-
83-
// get string from the holder byte[] - defaults to using EBCDIC encoding of CICS region
84-
// then add the record to the ArrayList to be returned
85-
recordStr = holder.getStringValue();
86-
records.add(recordStr);
8773

88-
// iterate over the remaining items and add to the ArrayList
89-
while (itemPos < totalItems) {
90-
tsqQ.readNextItem(holder);
91-
recordStr = holder.getStringValue();
92-
records.add(recordStr);
74+
// read the first item into the holder (and get the total number of items on the queue)
75+
int totalItems = tsqQ.readItem(itemPos, holder);
76+
77+
// add the string representation of the tsq item to the ArrayList
78+
// getStringValue() - uses the default EBCDIC encoding of CICS region
79+
tsqItems.add(holder.getStringValue());
80+
81+
// iterate over, and add, the remaining items to the ArrayList
82+
while (itemPos < totalItems)
83+
{
84+
tsqQ.readNextItem(holder);
85+
tsqItems.add(holder.getStringValue());
9386
itemPos++;
9487
}
9588

96-
// return the arraylist containing the records from the TSQ
97-
return records;
89+
// return the Arraylist containing the records from the TSQ
90+
return tsqItems;
9891
}
9992
}

src/main/java/com/ibm/cicsdev/springboot/jcics/DeleteTSQController.java

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,38 @@
88
/* restricted by GSA ADP Schedule Contract with IBM Corp */
99
/* */
1010

11-
1211
package com.ibm.cicsdev.springboot.jcics;
1312

14-
import java.io.IOException;
1513
import org.springframework.web.bind.annotation.GetMapping;
1614
import org.springframework.web.bind.annotation.RequestParam;
1715
import org.springframework.web.bind.annotation.RestController;
1816
import com.ibm.cics.server.CicsConditionException;
1917
import com.ibm.cics.server.TSQ;
2018

2119
@RestController
22-
public class DeleteTSQController {
23-
20+
public class DeleteTSQController
21+
{
2422
/**
2523
* The @GetMapping annotation ensures that HTTP GET requests are mapped to the annotated method.
2624
* @throws IOException
2725
**/
28-
@GetMapping("/deleteTSQs")
29-
public String deleteTSQs(@RequestParam(value = "tsqName", defaultValue = "ANNE") String tsqName) {
30-
31-
String response = "";
32-
26+
@GetMapping("/delete")
27+
public String deleteTSQ(@RequestParam(value = "tsq", defaultValue = "ANNE") String tsqName)
28+
{
3329
// Delete the TSQ
34-
try {
35-
response = deleteTSQ(tsqName);
36-
} catch ( CicsConditionException e) {
37-
38-
// Print the stack trace
30+
try
31+
{
32+
// perform the delete of the TSQ and return success result
33+
TSQ tsqQ = new TSQ();
34+
tsqQ.setName(tsqName);
35+
tsqQ.delete();
36+
return "TSQ " + tsqName + " successfully deleted.";
37+
}
38+
catch ( CicsConditionException e)
39+
{
40+
// Return error details
3941
e.printStackTrace();
40-
41-
// Return useful information to the user when meeting errors
42-
return "Oops! Unexpected CICS condition exception: " + e.getMessage() + ". Please check stderr for details.";
42+
return "Unexpected CICS condition exception: " + e.getMessage() + ". Check dfhjvmerr for further details.";
4343
}
44-
45-
return response;
46-
}
47-
48-
/**
49-
* A method to delete a TSQ.
50-
*
51-
* @param tsqName, the name of the TSQ to be deleted
52-
* @return, a message describing the outcome of the action
53-
* @throws CicsConditionException
54-
*/
55-
private String deleteTSQ(String tsqName) throws CicsConditionException {
56-
57-
// construct the TSQ object
58-
TSQ tsqQ = new TSQ();
59-
tsqQ.setName(tsqName);
60-
61-
// perform the delete
62-
tsqQ.delete();
63-
64-
// return the result of the delete action to the calling servlet
65-
return "TSQ " + tsqName + " successfully deleted.";
6644
}
6745
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Licensed Materials - Property of IBM */
2+
/* */
3+
/* SAMPLE */
4+
/* */
5+
/* (c) Copyright IBM Corp. 2020 All Rights Reserved */
6+
/* */
7+
/* US Government Users Restricted Rights - Use, duplication or disclosure */
8+
/* restricted by GSA ADP Schedule Contract with IBM Corp */
9+
/* */
10+
11+
package com.ibm.cicsdev.springboot.jcics;
12+
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.RequestParam;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
import com.ibm.cics.server.ItemHolder;
18+
import com.ibm.cics.server.TSQ;
19+
20+
@RestController
21+
public class InfoTSQController
22+
{
23+
/**
24+
* The @GetMapping annotation ensures that HTTP GET requests are mapped to the annotated method.
25+
* @throws IOException
26+
**/
27+
@GetMapping("/info")
28+
public String infoTSQ(@RequestParam(value = "tsq", defaultValue = "ANNE") String tsqName)
29+
{
30+
// Create a JCICS representation of the TSQ object
31+
TSQ tsq = new TSQ();
32+
tsq.setName(tsqName);
33+
34+
// obtain some information about the TSQ
35+
String name = "<name>" + tsq.getName() + "</name>";
36+
String type = "<type>" + tsq.getType() + "</type>";
37+
String lenStr = "<length>" + getTSQLength(tsq) + "</length>";
38+
39+
return "The current TSQ information is: " + "Name: " + name + ", Type: " + type + ", Length: " + lenStr;
40+
}
41+
42+
43+
/**
44+
* A method to get the number of items (length) of the TSQ.
45+
* @return, the number of items on the TSQ or associated error code.
46+
*/
47+
public int getTSQLength(TSQ tsq)
48+
{
49+
// Read the first item to get the length of the queue
50+
try
51+
{
52+
return tsq.readItem(1, new ItemHolder());
53+
}
54+
catch (Exception e)
55+
{
56+
e.printStackTrace();
57+
return -1;
58+
}
59+
}
60+
61+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ibm.cicsdev.springboot.jcics;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
7+
@RestController
8+
public class RootController
9+
{
10+
11+
/**
12+
* Provide a root URL for usage: information
13+
**/
14+
@GetMapping("/")
15+
public String root()
16+
{
17+
return "Spring Boot JCICS REST sample usage: /browse{tsq=name}, /write{tsq=name&item=content}, /info{tsq=name}, /delete{tsq=name}";
18+
}
19+
}

src/main/java/com/ibm/cicsdev/springboot/jcics/ServletInitializer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
import org.springframework.boot.builder.SpringApplicationBuilder;
1414
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
1515

16-
public class ServletInitializer extends SpringBootServletInitializer {
17-
16+
public class ServletInitializer extends SpringBootServletInitializer
17+
{
1818
@Override
19-
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
19+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
20+
{
2021
return application.sources(Application.class);
2122
}
22-
2323
}

0 commit comments

Comments
 (0)