程式設計必要條件
這裡說明開發 Virtual Member Manager 應用程式的程式設計師所需的一般方法、必要步驟及其他資訊。
匯入 Virtual Member Manager 套件
在將 Virtual Member Manager 功能整合至應用程式之前,您必須先匯入 Virtual Member Manager 套件及其他相關套件。 下列範例顯示您必須匯入的套件,以及如何定義類別。
import java.util.Hashtable;
import java.util.List;
import com.ibm.websphere.wim.SchemaConstants;
import com.ibm.websphere.wim.Service;
import com.ibm.websphere.wim.client.LocalServiceProvider;
import com.ibm.websphere.wim.ras.WIMTraceHelper;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import com.ibm.websphere.security.auth.WSSubject;
import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl;
import commonj.sdo.DataObject;
取得 Virtual Member Manager 服務及其他一般方法
如果您的應用程式是在 WebSphere Application Server內執行,您可以從遠端 EJB 或本端 JVM 取得 Virtual Member Manager 服務。
下列範例基本應用程式包含 locateService () 方法,這些方法顯示如何取得 Virtual Member Manager 服務,以及程式碼範例中針對各種 Virtual Member Manager 作業使用的其他一般方法。 將下列程式碼中以斜體顯示的 變數 取代為您需要的實際值。
/**
* This is a base application which defines common methods that are
* used by other code samples.
**/
public class BaseApp implements SchemaConstants
{
/**
* Common variable declaration: update based on the environment
**/
static final String HOST = "localhost"; // host name of the WebSphere Application Server
static final String BOOTSTRAP_PORT = "2809"; // Bootstrap/RMI port number
// Virtual member manager service that is used to make API calls
static Service service = null;
/**
* Locates virtual member manager service using a remote EJB
* @param ejbJndiName JNDI name of the EJB.
* Default EJB name is "ejb/com/ibm/websphere/wim/ejb/WIMServiceHome"
**/
public static Service locateService(String ejbJndiName)
{
try {
// Remote access virtual member manager Service EJB
Hashtable environment = new Hashtable();
String providerURL = "corbaloc:iiop:" + HOST + ":" + BOOTSTRAP_PORT;
environment.put(LocalServiceProvider.PROVIDER_URL, providerURL);
if (ejbJndiName == null) {
ejbJndiName = "ejb/com/ibm/websphere/wim/ejb/WIMServiceHome";
}
environment.put(LocalServiceProvider.EJB_JNDI_NAME, ejbJndiName);
service = new LocalServiceProvider(environment);
}
catch (Exception e) {
e.printStackTrace();
}
return service;
}
/**
* Locates virtual member manager service in local JVM
**/
public static Service locateService()
{
try {
// Local access virtual member manager Service
return new LocalServiceProvider(null);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* Runs action as specified user
* @param user user name
* @param password password of the user
* @param action Action to invoke after successful login of the user
* @return Object returned by the action
**/
public static Object runAsUser(String user, String password, PrivilegedExceptionAction action) throws Exception
{
LoginContext loginContext;
Subject subject;
// Login using the userid and password that was passed, which has the required role
loginContext = new LoginContext("WSLogin", new WSCallbackHandlerImpl(user, "", password));
loginContext.login();
subject = loginContext.getSubject();
try {
return WSSubject.doAs(subject, action);
}
catch (PrivilegedActionException excp) {
throw (Exception) excp.getCause();
}
}
public static String printDO(DataObject obj)
{
return WIMTraceHelper.printDataObject(obj);
}
/**
* Loop through the entities in the DataObject and print its uniqueName
* @param root input DataObject
*/
@SuppressWarnings("unchecked")
public static void printIdentifiers(DataObject root) throws Exception
{
// Get all entities in the DataObject
List entities = root.getList(SchemaConstants.DO_ENTITIES);
for (int i = 0; i < entities.size(); i++) {
DataObject ent = (DataObject) entities.get(i);
// Get the entity Identifier
DataObject id = ent.getDataObject(SchemaConstants.DO_IDENTIFIER);
if (id != null) {
String uniqueName = id.getString(SchemaConstants.PROP_UNIQUE_NAME);
System.out.println("UniqueName is -> " +uniqueName);
}
else {
System.out.println("Missing Identifier");
}
}
}
}
org.eclipse.emf.ecore.EPackage.Registry.INSTANCE=com.ibm.ws.wim.util.VMMEMFGlobalDelegatorRegistry如果您未設定此系統內容,則預設 EMF 實作會生效,這不支援多個安全網域環境,且可能會毀損 EMF 綱目,且可能會發生綱目違規錯誤。呼叫 Virtual Member Manager API
各種 Virtual Member Manager 作業的程式碼範例使用 BaseApp 類別中定義的方法。 如需如何進行 API 呼叫的相關指示,請參閱程式碼範例。
若要在應用程式碼中呼叫 Virtual Member Manager API ,您必須獲指派下列其中一個角色:
WebSphere Application Server 管理者角色。
使用聯合儲存庫管理權限來指派 Virtual Member Manager 角色。
如需預先定義虛擬成員管理程式角色的相關資訊,請參閱 提供安全中的「將使用者和群組對映至角色以指派聯合儲存庫管理權限」一節。
如需如何將使用者或群組指派給預先定義虛擬成員管理程式角色的相關資訊,請閱讀主題中的 mapIdMgrUserToRole、mapIdMgrGroupToRole、removeIdMgrUsersFromRole、removeIdMgrGroupsFromRole、listIdMgrUsersForRoles 及 listIdMgrGroupsForRoles 指令,以及 WebSphere Application Server 說明文件中的 AdminTask 物件的 IdMgrConfig 指令群組 。
如需端對端範例實務,請參閱 使用聯合儲存庫管理權限的範例程式碼主題。
編譯程式碼
請檢查類別路徑設定,確定它包含正確的 Java 保存檔 (JAR) ,以編譯程式碼。
<WAS_HOME>\plugins\com.ibm.ws.runtime.jar<WAS_HOME>\plugins\com.ibm.ws.runtime.wim.base.jar<WAS_HOME>\plugins\org.eclipse.emf.commonj.sdo.jar<WAS_HOME>\lib\j2ee.jar
執行程式碼
如果應用程式碼在 WebSphere Application Server 內作為應用程式或 Servlet 執行,則會隱含地使用用於存取 Virtual Member Manager API 的 Subject 和其他參數,且與部署應用程式的伺服器或程序相同。
如果應用程式在 WebSphere Application Server外部執行 (例如,從 WebSphere Application Server 用戶端執行) ,則在執行已編譯的程式碼時,請使用下列 JVM 引數。 將下列引數中以斜體顯示的 變數 取代為您需要的實際值。
-Djava.security.auth.login.config=<WAS_HOME>/properties/wsjaas_client.conf
-Dcom.ibm.CORBA.ConfigURL=<WAS_HOME_URL>/properties/sas.client.props
-Dcom.ibm.SSL.ConfigURL=<WAS_HOME_URL>/properties/ssl.client.props
僅當您必須置換 CORBA 內容檔中指定的認證時,才使用下列引數:
-Dcom.ibm.CORBA.loginSource=properties
-Dcom.ibm.CORBA.loginUserid=AdminUserId
-Dcom.ibm.CORBA.loginPassword=Admin Password
-Djava.security.auth.login.config=C:/Progra~1/IBM/WebSphere/AppClient/properties/wsjaas_client.conf
-Dcom.ibm.CORBA.ConfigURL=file:/Progra~1/IBM/WebSphere/AppClient/properties/sas.client.props
-Dcom.ibm.SSL.ConfigURL=file:/Progra~1/IBM/WebSphere/AppClient/properties/ssl.client.props
-Dcom.ibm.CORBA.loginSource=properties
-Dcom.ibm.CORBA.loginUserid=admin
-Dcom.ibm.CORBA.loginPassword=admin<WAS_HOME>\lib\j2ee.jar<WAS_HOME>\lib\bootstrap.jar<WAS_HOME>\plugins下的所有 JAR 檔
延伸內容綱目
- propertySchema 和 extensionPropertySchema 資料物件
- propertySchema 資料物件用來建立內容類型,並在執行時期將它新增至現有的 Virtual Member Manager 實體類型。 新內容會新增至 wimxmlextension.xml 檔案。 不過,如果您也想要延伸內容延伸儲存庫的資料庫綱目,則必須使用 extensionPropertySchema 資料物件。 如果您使用 extensionPropertySchema 資料物件,新內容會新增至 wimxmlextension.xml 檔案中的現有實體類型,以及儲存在內容延伸資料庫中。
- 內容資料類型
- 這裡列出 Virtual Member Manager 內容所支援的資料類型語法。 如需相關資訊,請參閱 WebSphere Application Server 說明文件中 Virtual Member Manager Javadoc 資訊的 SchemaConstants 小節。
DATA_TYPE_ANY_SIMPLE_TYPEDATA_TYPE_ANY_URIDATA_TYPE_BASE_64_BINARYDATA_TYPE_BOOLEANDATA_TYPE_BYTEDATA_TYPE_DATEDATA_TYPE_DATE_TIMEDATA_TYPE_DOUBLEDATA_TYPE_IDENTIFIER_TYPEDATA_TYPE_INTDATA_TYPE_LONGDATA_TYPE_SHORTDATA_TYPE_STRINGDATA_TYPE_TOKEN