Java SE security tutorial - Step 1
In order to work with the rest of the tutorial, you need to create and package a simple Java program and two XML files. These set of files defines a simple ObjectGrid configuration with one ObjectGrid instance named accounting and a customer map. The SimpleDP.xml file features a deployment policy of one map set configured with one partition and zero minimum required replicas.
Procedure
- In a command line window, go to the wxs_home directory.
- Create a directory called applib.
- Ensure your development environment contains the ogclient.jar file in the classpath. For more information, see the Programming Guide.
- Create and compile the following SimpleApp.java class:
SimpleApp.java // This sample program is provided AS IS and may be used, executed, copied and modified // without royalty payment by customer // (a) for its own instruction and study, // (b) in order to develop applications designed to run with an IBM WebSphere product, // either for customer's own internal use or for redistribution by customer, as part of such an // application, in customer's own products. // Licensed Materials - Property of IBM // 5724-J34 (C) COPYRIGHT International Business Machines Corp. 2007-2009 package com.ibm.websphere.objectgrid.security.sample.guide; import com.ibm.websphere.objectgrid.ClientClusterContext; import com.ibm.websphere.objectgrid.ObjectGrid; import com.ibm.websphere.objectgrid.ObjectGridManager; import com.ibm.websphere.objectgrid.ObjectGridManagerFactory; import com.ibm.websphere.objectgrid.ObjectMap; import com.ibm.websphere.objectgrid.Session; public class SimpleApp { public static void main(String[] args) throws Exception { SimpleApp app = new SimpleApp(); app.run(args); } /** * read and write the map * @throws Exception */ protected void run(String[] args) throws Exception { ObjectGrid og = getObjectGrid(args); Session session = og.getSession(); ObjectMap customerMap = session.getMap("customer"); String customer = (String) customerMap.get("0001"); if (customer == null) { customerMap.insert("0001", "fName lName"); } else { customerMap.update("0001", "fName lName"); } customer = (String) customerMap.get("0001"); // Close the session (optional in Version 7.1.1 and later) for improved performance session.close(); System.out.println("The customer name for ID 0001 is " + customer); } /** * Get the ObjectGrid * @return an ObjectGrid instance * @throws Exception */ protected ObjectGrid getObjectGrid(String[] args) throws Exception { ObjectGridManager ogManager = ObjectGridManagerFactory.getObjectGridManager(); // Create an ObjectGrid ClientClusterContext ccContext = ogManager.connect("localhost:2809", null, null); ObjectGrid og = ogManager.getObjectGrid(ccContext, "accounting"); return og; } }
- Compile the package with this file and name the JAR sec_sample.jar. Put this JAR file in the /applib directory.
- Go to the wxs_home directory, and create a directory called xml
- In thewxs_home/xml
directory, create the following configuration files:
SimpleApp.xml <?xml version="1.0" encoding="UTF-8"?> <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd" xmlns="http://ibm.com/ws/objectgrid/config"> <objectGrids> <objectGrid name="accounting"> <backingMap name="customer" readOnly="false" copyKey="true"/> </objectGrid> </objectGrids> </objectGridConfig>
The following XML file configures the deployment environment.
SimpleDP.xml <?xml version="1.0" encoding="UTF-8"?> <deploymentPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ibm.com/ws/objectgrid/deploymentPolicy ../deploymentPolicy.xsd" xmlns="http://ibm.com/ws/objectgrid/deploymentPolicy"> <objectgridDeployment objectgridName="accounting"> <mapSet name="mapSet1" numberOfPartitions="1" minSyncReplicas="0" maxSyncReplicas="2" maxAsyncReplicas="1"> <map ref="customer"/> </mapSet> </objectgridDeployment> </deploymentPolicy>
Results
These files create a simple ObjectGrid configuration with one ObjectGrid an accounting instance and a customer map.