Custom plug-in sample modifier class

When an object is created in the registry, the sample modification plug-in creates a new concept object, and creates a relationship from the concept object to the object that was originally created.

The name of the concept object is the original object name appended with "_BusinessObject".

The modifier creates a WSRR client, and then creates the new concept by creating an object of type GenericObject. An if statement ensures that the modifier code is run only for WSDL documents. Note that if the modifier was allowed to run when a new concept object is created, the modifier would repeatedly create new concept objects and would never terminate.

The modifier creates a logger object with a component name of "pluginsample.modifier". This means that if you enable WebSphere® Application Server tracing for that component, the server logs the calls of the modifier methods.

The modifier code shows you how you can log in as a different user to the user that performed the original operation that resulted in the modifier being called. This is useful if the original user does not have sufficient privileges to perform the modifier actions. For the purposes of this sample, the relevant code is commented out, and marked with OPTIONAL CODE comments. To log in as a different user, remove the comment symbols and set the username and password String variables to the required values.

See the Related link for more details on creating modification plug-in class.

The source code for the custom plug-in sample modifier class is as follows:
package pluginsample.modifier;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.security.auth.login.LoginException;

import com.ibm.serviceregistry.ServiceRegistryModifier;
import com.ibm.serviceregistry.ServiceRegistrySession;
import com.ibm.serviceregistry.ServiceRegistrySessionHome;
import com.ibm.serviceregistry.ServiceRegistryStatus;
import com.ibm.serviceregistry.sdo.BaseObject;
import com.ibm.serviceregistry.sdo.GenericObject;
import com.ibm.serviceregistry.sdo.OriginalObject;
import com.ibm.serviceregistry.sdo.WSDLDocument;
import com.ibm.serviceregistry.sdo.helper.BSRSDOHelper;
import com.ibm.serviceregistry.sdo.helper.DataFactory;
import com.ibm.serviceregistry.sdo.helper.TypeConstants;
import com.ibm.websphere.security.WSSecurityException;

public class CreateBusinessObject implements ServiceRegistryModifier {

  private static Logger logger = Logger
      .getLogger("com.ibm.serviceregistry.pluginSample.modifier");
  private static final String CLASS_NAME = CreateBusinessObject.class.getName();

  /**
   * Following a create, create a concept/generic object with a relationship to
   * the created document. This modifier can be set to only run for
   * WSDLDocuments by specifying
   * modifier.WSDLDocument=
   * com.ibm.serviceregistry.pluginSample.modifier.CreateBusinessObject
   */
  public ServiceRegistryStatus create(final OriginalObject newObject) {
    final String METHOD_NAME = "create";
    if (logger.isLoggable(Level.FINEST)) {
      logger.entering(CLASS_NAME, METHOD_NAME);
    }

    ServiceRegistryStatus status = new ServiceRegistryStatus();

    if (newObject instanceof WSDLDocument) {
      try {

// OPTIONAL CODE
// START**************************************************************************
// Optionally, you can, at this point, login as a different user to the user
// making the originating call. This might be useful if the original user
// does not have sufficient privileges to perform the actions made by the modifier.
        
        /*

        LoginContext loginContext = null;

        try {
          String username = "admin";
          String password = "password";

          WSCallbackHandlerImpl callBackHandler = new WSCallbackHandlerImpl(
              username, password);
          loginContext = new LoginContext("WSLogin", callBackHandler);
          loginContext.login();
          logger.log(Level.INFO, "JNDI: logged in as "
              + loginContext.getSubject().getPrincipals());
          Subject subject = loginContext.getSubject();

          Object objRC = WSSubject.doAs(subject, new PrivilegedAction() {
            public Object run() {
              Object objRC = null;
              try {
         */

// OPTIONAL CODE
// END************************************************************************
        
                // Create a client
                RepositoryDelegate rd = DelegateFactory.createRepositoryDelegate();
                // Create the "in-memory" generic object and relationship
                GenericObject go = (GenericObject) DataFactory.INSTANCE.create(
                    TypeConstants.SR_URI, TypeConstants.TYPE_GENERICOBJECT);
                go.setName(newObject.getName() + "_BusinessObject");
                BSRSDOHelper.INSTANCE.addRelationship(go, "referencedWSDL",
                    newObject);

                // Persist the new object and relationship to the service
                // registry triggering a nested plug-in execution sequence of
                // Validation -> <action> -> Modification -> Notification.
                rd.create(go);
                
// OPTIONAL CODE
// START*************************************************************
                
        /*  
              } catch (Exception e) {
                objRC = e;
              }
              return objRC;
            }
          });
          if (objRC != null) {
            throw (Exception) objRC;
          }
        } finally {
          if (loginContext != null) {
            loginContext.logout();
          }
        }
        */
// OPTIONAL CODE
// END***************************************************************

      } catch (Exception e) {
        status.addException(e);
      }
    }

    if (logger.isLoggable(Level.FINEST)) {
      logger.exiting(CLASS_NAME, METHOD_NAME);
    }
    return status;
  }

  public ServiceRegistryStatus delete(OriginalObject oldObject) {
    return new ServiceRegistryStatus();
  }

  public ServiceRegistryStatus update(BaseObject oldObject, BaseObject newObject) {
    return new ServiceRegistryStatus();
  }
 
}