1. Trang chủ
  2. » Công Nghệ Thông Tin

Bắt đầu với IBM Websphere smash - p 12 docx

10 268 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 618,66 KB

Nội dung

ptg 92 Chapter 5 Global Context zdelete Method To remove a list from the global context, we use zdelete: boolean zdelete(String location [, boolean deleteChildren ]); The zdelete method deletes the specified location, removing any values stored there (see Listing 5.15). Listing 5.15 zdelete a List GlobalContext.zdelete("/app/stringList"); GlobalContext.zdelete("/app/integerList"); Here we delete both of the lists we’ve previously created and updated. The optional deleteChildren parameter works the same as the object usage of this method. As with zput, we can delete single elements from our list by specifying the #<key> in the location (see Listing 5.16). Listing 5.16 zdelete a List Element GlobalContext.zdelete("/app/stringList#0"); GlobalContext.zdelete("/app/integerList#1"); The first zdelete removes the first element of the string list, and the second zdelete removes the second element of the integer list. Sometimes code requires that a particular list be available before executing. We can test the availability of a list by using the zcontains method, the same way we do for objects. zcontains Method To test if a list is in the global context, we use the zcontains method: boolean zcontains(String location); The zcontains method returns a boolean value, indicating whether the particular list is available in the global context (see Listing 5.17). Listing 5.17 The zcontains Method for a List if(GlobalContext.zcontains("/app/stringList") { //do something } The zcontains method can be used as a guard code that must have particular lists avail- able. Like other list operations, the zcontains method can access particular elements of a list by using the #<key> notation (see Listing 5.18). Download from www.wowebook.com ptg Accessing the Global Context 93 Listing 5.18 The zcontains Method for a List Element if(GlobalContext.zcontains("/app/stringList#0") { //do something } This code tests to be sure that the first element in the string list exists before executing some code. The rest of the global context methods (zlist, zlistAll, and zdump) work the same as they do with objects. FirstElementLists FirstElementLists work very much the same as lists in many cases. However, the first ele- ment is operated on by default. The general idea is that these lists operate like stacks. You would normally be operating only on the head of the list, which with a first element list is found at "<zone or locationPrefix>/list" instead of at "<zone or locationPrefix>/list#0" with a normal list. zput Method To put a FirstElementList into the global context, we use the zput method: boolean zput(String location, List list); The zput method places the FirstElementList in the particular global context location (see Listing 5.19). Listing 5.19 Creating a FirstElementArrayList FirstElementArrayList list = new FirstElementArrayList<String>(); list.add("some string"); list.add("string object"); GlobalContext.zput("/tmp/list#*", list); The FirstElementArrayList is available in the zero.util package. With this code, we’ve created a list and then used zput to create or replace the list in the tmp zone so that we can use it later during the course of our application execution. Notice that with FirstElementLists, we must use the #* notation to indicate that we’d like to use a FirstElementList. We can replace single elements of our stored list by using #<key> to specify where to put the value: GlobalContext.zput("/tmp/list#1", "new string"); This replaces "string object" with "new string" in our stored list. To replace the first item in the list, we don’t need to use the #<key>. We simply need to use the location: GlobalContext.zput("/tmp/list", "new first item"); Download from www.wowebook.com ptg 94 Chapter 5 Global Context This replaces the first item in the list. If we need to add to a list, we must use the zpost method. zpost Method To append to a FirstElementList in the global context, we use the zpost method: boolean zpost(String location, Object object); The zpost method appends the FirstElementList to the list stored at the given loca- tion (see Listing 5.20). Listing 5.20 Using zpost to Append to a FirstElementList FirstElementArrayList list = new FirstElementArrayList (); GlobalContext.zpost("/tmp/list#*", list); GlobalContext.zpost("/tmp/list", "posted string object"); Here we’ve created an empty list and then posted a single element into the list. This method can also be used to append a list to another list (see Listing 5.21). Listing 5.21 Using zpost to Append a List to a FirstElementList FirstElementArrayList list = new FirstElementArrayList (); GlobalContext.zput("/tmp/list/#*", list); GlobalContext.zpost("/tmp/list", "posted string object"); list.add("string object"); GlobalContext.zpost("/tmp/list#*", list); In this case, we’ve created an empty list and posted an element to the list stored in the tmp zone. Next, we add some elements to the Java list and then append them to the stored list using a zpost. If we have several lists that we’d like to place in the global context, we can use the zputs method just like we did with objects. zputs Method To append to a FirstElementList in the global context, we use the zpost method: Map zputs(String location, Map lists); This is another convenient method to place several lists into the global context at once (see Listing 5.22). Listing 5.22 Using zputs to Put Multiple Lists into the Global Context FirstElementArrayList slist = new ArrayList(); slist.add("string one"); slist.add("string two"); Download from www.wowebook.com ptg Accessing the Global Context 95 FirstElementArrayList ilist = new ArrayList(); ilist.add(new Integer(1)); ilist.add(new Integer(2)); Map map = new HashMap(); map.put("stringList#*", slist); map.put("integerList#*", ilist); GlobalContext.zputs("/app", map); The zputs method places two variables containing the lists into the app zone: /app/stringList with the contents of slist and /app/integerList with the contents of ilist. zget Method To retrieve variables placed in the global context, we use the zget method: Object zget(String location [, Object defaultValue ]); The zget method retrieves the list in the particular global context location. Optionally, you can provide a default value for the list being retrieved. If a default is not provided and the location is not found, a null is returned (see Listing 5.23). Listing 5.23 zget of a FirstElementList List slist = GlobalContext.zget("/app/stringList#*", new ArrayList()); List ilist = GlobalContext.zget("/app/integerList#*"); Here we are retrieving our string list with a default empty ArrayList and our integer list with no default. We can also retrieve individual elements from our list by again using the #<key> notation (see Listing 5.24). Listing 5.24 zget of the First Element of a FirstElementList String s = GlobalContext.zget("/app/stringList"); Integer i = GlobalContext.zget("/app/integerList"); This code retrieves the first element of each list. It is possible to retrieve the Nth item in the list as well, using the #<key> notation (see Listing 5.25). Listing 5.25 zget of an Element from a FirstElementList String s = GlobalContext.zget("/app/stringList#1"); Integer i = GlobalContext.zget("/app/integerList#2"); This code retrieves the second item in the string list and the third item in the integer list. Download from www.wowebook.com ptg 96 Chapter 5 Global Context zdelete Method To remove a list from the global context, we use zdelete: boolean zdelete(String location [, boolean deleteChildren ]); The zdelete method deletes the specified location, removing any values stored there (see Listing 5.26). Listing 5.26 zdelete of a FirstElementList GlobalContext.zdelete("/app/stringList#*"); GlobalContext.zdelete("/app/integerList#*"); Here we delete both of the lists we’ve previously created and updated. The optional deleteChildren parameter works the same as the object usage of this method. As with zput, we can delete single elements from our list by specifying the #<key> in the location (see Listing 5.27). Listing 5.27 zdelete of an Element of a FirstElementList GlobalContext.zdelete("/app/stringList"); GlobalContext.zdelete("/app/integerList#1"); The first zdelete removes the first element of the string list, and the second zdelete removes the second element of the integer list. Sometimes code requires that a particular list be available before executing. We can test the availability of a list by using the zcontains method, the same way we do for objects. zcontains Method To test if a list is in the global context, we use the zcontains method: boolean zcontains(String location); The zcontains method returns a boolean value indicating whether the particular list is available in the global context (see Listing 5.28). Listing 5.28 zcontains of a FirstElementList if(GlobalContext.zcontains("/app/stringList#*") { //do something } The zcontains method can be used as a guard code that must have particular lists avail- able. With FirstElementLists, we can test the first existence of the first by directly accessing the variable (see Listing 5.29). Download from www.wowebook.com ptg Accessing the Global Context 97 Listing 5.29 zcontains of the First Element of a FirstElementList if(GlobalContext.zcontains("/app/stringList") { //do something } This code tests to verify that the first element in the list exists before executing code. We can test other elements in the list using the #<key> notation (see Listing 5.30). Listing 5.30 zcontains of an Element from a FirstElementList if(GlobalContext.zcontains("/app/stringList#1") { //do something } This code tests to see whether the second element in the list exists. The rest of the global context methods (zlist, zlistAll, and zdump) work the same as they do with lists and objects. Maps Maps are lists of key/value pairs. To put a single map into the global context, we use the zput method. zput Method To put a map into the global context, we use the zput method: boolean zput(String location, Map map); The zput method places the object in the particular global context location (see Listing 5.31). Listing 5.31 zput of a Map HashMap map = new HashMap(); map.put("integer",new Integer(42)); map.put("string", "string object"); GlobalContext.zput("/tmp/map", map); With this code, we’ve created a map and then used zput to create or replace the map in the tmp zone. This way, we can use it at a later time during the course of our application execution. We can replace single elements of our stored map by using #<key> to specify where to put the value: GlobalContext.zput("/tmp/map#string", "new string"); Download from www.wowebook.com ptg 98 Chapter 5 Global Context This replaces "string object" with "new string" in our stored map. We can also use zput to add to a map: GlobalContext.zput("/tmp/map#foo", "added foo"); zpost Method If we need to merge two maps, we must use the zpost method: boolean zpost(String location, Map map); The zpost method appends the object to the map stored at the given location (see Listing 5.32). Listing 5.32 zpost an Append to a Map HashMap map = new HashMap(); map.put("foo", "foo value"); GlobalContext.zpost("/tmp/map", map); map = new HashMap(); map.put("bar", "bar value"); GlobalContext.zpost("/tmp/map", map); Here we create a map and place a single key/value pair into it. Then we create another new map with another key/value pair. Finally, we use zpost to merge the maps in the tmp zone. If we have several maps that we’d like to place in the global context, we can use the zputs method just like we did in the “Lists” section. zputs Method To put several maps into the global context, we use the zputs method: Map zputs(String location, Map maps); This is another convenient method to place several maps into the global context at once (see Listing 5.33). Listing 5.33 zputs Several Maps into the Global Context HashMap smap = new HashMap(); smap.add("one", "string one"); smap.add("two", "string two"); HashMap imap = new HashMap(); imap.add("one", new Integer(1)); imap.add("two", new Integer(2)); HashMap map = new HashMap(); map.put("stringMap", smap); map.put("integerMap", imap); GlobalContext.zputs("/app", map); Download from www.wowebook.com ptg Accessing the Global Context 99 The zputs method places two variables containing the maps into the app zone: /app/ stringMap with the contents of smap and /app/integerMap with the contents of imap. zget Method To retrieve variables placed in the global context, we use the zget method: Object zget(String location [, Object defaultValue ]); The zget method retrieves the map in the particular global context location. Optionally, you can provide a default value for the map being retrieved. If a default is not provided and the location is not found, a null is returned (see Listing 5.34). Listing 5.34 zget a Map Map smap = GlobalContext.zget("/app/stringMap", new HashMap()); Map imap = GlobalContext.zget("/app/integerMap"); Here we are retrieving our string map with a default empty HashMap and our integer map with no default. We can also retrieve individual elements from our map by again using the #<key> notation (see Listing 5.35). Listing 5.35 zget a Map Value String s = GlobalContext.zget("/app/stringMap#foo"); Integer i = GlobalContext.zget("/app/integerMap#one"); This code retrieves the specified value element of each map that corresponds to the given key. To remove a map from the global context, we use zdelete. zdelete Method To remove a map the global context, we use the zdelete method: boolean zdelete(String location [, boolean deleteChildren ]); The zdelete method deletes the specified location, removing any values stored there (see Listing 5.36). Listing 5.36 zdelete a Map GlobalContext.zdelete("/app/stringMap"); GlobalContext.zdelete("/app/integerMap"); Here we delete both of the maps we’ve previously created and updated. The optional deleteChildren parameter works the same as the object usage of this method. As with zput, we can delete single elements from our map by specifying the #<key> in the location (see Listing 5.37). Download from www.wowebook.com ptg 100 Chapter 5 Global Context Listing 5.37 zdelete a Map Value GlobalContext.zdelete("/app/stringMap#foo"); GlobalContext.zdelete("/app/integerMap#one"); The first zdelete removes the element with the "foo" key from the string map, and the second zdelete removes the element with the "one" key from the integer map. Sometimes code requires that a particular map be available before executing. We can test the availability of a map by again using the zcontains method, the same way we do for objects. zcontains Method To test if a map is in the global context, we use the zcontains method: boolean zcontains(String location); The zcontains method returns a boolean value indicating if the particular map is avail- able in the global context (see Listing 5.38). Listing 5.38 zcontains a Map if(GlobalContext.zcontains("/app/stringMap") { //do something } The zcontains method can be used as a guard code that needs to have particular maps available. Like other map operations, the zcontains method can access particular elements of a map by using the #<key> notation (see Listing 5.39). Listing 5.39 zcontains a Map Value if(GlobalContext.zcontains("/app/stringMap#foo") { //do something } This code tests to be sure that the "foo" element in the map exists before executing code. The rest of the global context methods (zlist, zlistAll, and zdump) work as they do with objects. Groovy APIs To access the global context in Groovy, you can use the same APIs as presented in the Java sec- tion, but there are shortcuts in Groovy to aid access. In this section, we show you these shortcuts. Please refer to the previous Java API section for complete explanations of the Java APIs. Download from www.wowebook.com ptg Accessing the Global Context 101 Objects zput Method The Java zput method places objects into the global context: boolean zput(String location, Object object); The Groovy version of this API is simply an assignment to the location for the particular context. In other words, you can address the global context directly in Groovy as if it were a vari- able in your Groovy script (see Listing 5.40). Listing 5.40 Placing Groovy Variables into the Global Context app.string = "string object" app.integer = 42 In these two lines of code, we’ve created or replaced a string object and an integer object in the app zone so that we can use it at a later time. To retrieve variables placed in the global context, we use the zget method in Java. zget Method The zget method retrieves the object in the particular global context location. In Groovy, we use the following to directly retrieve objects in the global context (see Listing 5.41). Listing 5.41 Retrieving Variables from the Global Context with Groovy var s = app.string[] var i = app.integer[] Here we are retrieving a string object and an integer object. zdelete Method To remove variables from the global context, we use zdelete in Groovy just like we do in Java. Please refer to the previous section on Java. zcontains Method In Java, to test if a variable exists in the global context, we’d use the zcontains method. How- ever, because we access the global context directly in Groovy, we can test the availability of a variable in a natural way (see Listing 5.42). Download from www.wowebook.com . imap.add("two", new Integer(2)); HashMap map = new HashMap(); map.put("stringMap", smap); map.put("integerMap", imap); GlobalContext.zputs("/app", map); . zpost(String location, Map map); The zpost method appends the object to the map stored at the given location (see Listing 5.32). Listing 5.32 zpost an Append to a Map HashMap map = new HashMap();. zput(String location, Map map); The zput method places the object in the particular global context location (see Listing 5.31). Listing 5.31 zput of a Map HashMap map = new HashMap(); map.put("integer",new

Ngày đăng: 06/07/2014, 19:20