Skip to Content
0
Former Member
Sep 20, 2005 at 02:17 PM

JCA

23 Views

Hi ALL,

i calling RFC using JCA .When i trigger RFC it is displaying null pointe exception.i am posting my code please suggest me where i am doing mistake

import com.sapportals.portal.prt.component.AbstractPortalComponent;

import com.sapportals.portal.prt.component.IPortalComponentRequest;

import com.sapportals.portal.prt.component.IPortalComponentResponse;

import com.sapportals.portal.prt.component.IPortalComponentProfile;

import com.sapportals.portal.prt.event.IPortalRequestEvent;

import com.sapportals.portal.prt.resource.IResource;

import com.sapportals.portal.prt.runtime.PortalRuntime;

//import com.sap.security.api.IPrincipal;

import java.util.Hashtable;

//import java.util.Enumeration;

import javax.naming.Context;

//import javax.naming.NamingException;

import javax.naming.NamingEnumeration;

import javax.naming.NameClassPair;

import com.sapportals.portal.pcd.gl.IPcdContext;

import com.sapportals.portal.pcd.gl.IPcdAttributes;

//import com.sapportals.portal.pcd.gl.IPcdAttribute;

import com.sapportals.portal.prt.jndisupport.InitialContext;

/*

  • System Landscape service API

*/

import com.sapportals.iviewserver.systemlandscape.service.ISystemLandscapeService;

import com.sapportals.iviewserver.systemlandscape.service.IDesigntimeSystemLandscapeService;

/*

  • Connector service API

*/

import com.sapportals.portal.ivs.cg.IConnectorService;

import com.sapportals.portal.ivs.cg.IConnectorGatewayService;

import com.sapportals.portal.ivs.cg.ConnectionProperties;

/*

  • javax.resource.cci

*/

import javax.resource.cci.MappedRecord;

import javax.resource.cci.RecordFactory;

import com.sapportals.connector.ConnectorException;

// connection

import com.sapportals.connector.connection.IConnection;

import com.sapportals.connector.connection.IConnectionFactory;

// execution.functions

import com.sapportals.connector.execution.functions.IInteraction;

import com.sapportals.connector.execution.functions.IInteractionSpec;

// metadata.functions

import com.sapportals.connector.metadata.functions.IFunction;

import com.sapportals.connector.metadata.functions.IFunctionsMetaData;

// execution.structures

import com.sapportals.connector.execution.structures.IRecord;

//import com.sapportals.connector.execution.structures.IStructureFactory;

import com.sapportals.connector.execution.structures.IRecordSet;

/

*

  • To change the template for this generated type comment go to

  • Window>Preferences>Java>Code Generation>Code and Comments

*/

public class SampleComponent extends AbstractPortalComponent {

/*

  • Generic Layer service

  • */

IPcdContext pcdContext;

/*

  • Data Container

  • */

IPortalComponentProfile app;

/*

  • Refresh "systems" variable which contains a list of R3 system object.

  • This object is shown in the pulldown list on JSP.

*/

public void doContent(IPortalComponentRequest request, IPortalComponentResponse response) {

System.out.println("doContent()");

app = request.getComponentContext().getProfile();

try {

// Get the initial context from JNDI@PRT

pcdContext = getPcdContext(request);

// Get a list of R/3 system object aliases

Hashtable systems = getR3Systems(request);

// System.out.println("systems = " + systems);

//app.putValue("systems","SAP_R3");

// app.putValue("systems", "systems");

app.setProperty("systems","SAP_R3");

} catch (Exception e) {

System.out.println("Caught an exception: \n" + e);

}

IResource jsp = request.getResource(IResource.JSP, "pagelet/index.jsp");

response.include(request, jsp);

}

/**

  • Trigger a Remote Function Module via CCI...

*/

public void doJca(IPortalComponentRequest request, IPortalRequestEvent event) {

System.out.println("doJca()");

Context ctx = null;

IConnectionFactory connectionFactory = null;

IConnection client = null;

ConnectionProperties prop = null;

String rfm_name = "Y_SS_ABSENCE";

// String system_alias = request.getParameter("system");

String system_alias = request.getParameter("SAP_R3");

if (system_alias == null) {

app.putValue(

"error",

"Couldn't establish a connection with a target system " + system_alias + ".");

return;

}

System.out.println("system_alias = " + system_alias);

app.putValue("error", "");

app.putValue("exportParams", "");

app.putValue("system_alias", system_alias);

try {

IConnectorGatewayService cgService =(IConnectorGatewayService) PortalRuntime.getRuntimeResources().getService(IConnectorService.KEY);

try {

prop = new ConnectionProperties(request.getLocale(), request.getUser());

} catch (Exception e) {

app.putValue(

"error",

"Couldn't establish a connection with the user " + request.getUser() + ".");

return;

}

try {

client = cgService.getConnection(system_alias, prop);

} catch (Exception e) {

app.putValue(

"error",

"Couldn't establish a connection with a target system " + system_alias + ".");

return;

}

/*

  • Start Interaction

  • */

IInteraction interaction = client.createInteractionEx();

System.out.println("Starting Interaction...");

IInteractionSpec interactionSpec = interaction.getInteractionSpec();

interactionSpec.setPropertyValue("Name","Remote_function-module");

IFunctionsMetaData functionsMetaData = client.getFunctionsMetaData();

IFunction function = functionsMetaData.getFunction("Remote_function-module");

if (function == null) {

app.putValue(

"error",

"Couldn't find " + rfm_name + " in a target system " + system_alias + ".");

return;

}

/*

  • CCI api only has one datatype: Record

  • */

RecordFactory recordFactory = interaction.getRecordFactory();

MappedRecord importParams = recordFactory.createMappedRecord("CONTAINER_OF_IMPORT_PARAMS");

importParams.put("EMPname","Mike" );

importParams.put("EMPNO","09");

*/

// IFunctionsMetaData functionsMetaData = client.getFunctionsMetaData();

// IFunction function = functionsMetaData.getFunction("Remote_function-module");

//

// if (function == null) {

// app.putValue(

// "error",

// "Couldn't find " + rfm_name + " in a target system " + system_alias + ".");

// return;

// }

/*

  • How to invoke Function modules

  • */

//System.out.println("Invoking... " + function.getName());

MappedRecord exportParams = (MappedRecord) interaction.execute(interactionSpec, importParams);

app.putValue("exportParams", exportParams);

}

IRecord numOfDays = (IRecord) exportParams.get("NUMDAYS");

String numOfDaysStr=numOfDays.getString(0);

/*

  • Closing the connection

  • */

client.close();

} catch (ConnectorException e) {

//app.putValue("error", e);

System.out.println("Caught an exception: \n" + e);

} catch (Exception e) {

System.out.println("Caught an exception: \n" + e);

}

}

private IPcdContext getPcdContext(IPortalComponentRequest request) {

// System.out.println("getPcdContext()");

IPcdContext pcdContext = null;

try {

Hashtable env = new Hashtable();

env.put(

IPcdContext.INITIAL_CONTEXT_FACTORY,

"com.sapportals.portal.pcd.gl.PcdInitialContextFactory");

env.put(IPcdContext.SECURITY_PRINCIPAL, request.getUser());

pcdContext = (IPcdContext) new InitialContext(env).lookup("");

} catch (Exception e) {

System.out.println("Caught an exception: \n" + e);

}

return pcdContext;

}

private Hashtable getR3Systems(IPortalComponentRequest request) {

// System.out.println("getR3Systems()");

Hashtable systems = new Hashtable();

try {

// get a list of R/3 system aliases

// ISystemLandscapeService landscapeService =

// (ISystemLandscapeService) request.getService(ISystemLandscapeService.KEY);

ISystemLandscapeService landscapeService =(ISystemLandscapeService) PortalRuntime.getRuntimeResources().getService(ISystemLandscapeService.KEY);

IDesigntimeSystemLandscapeService landscape =

landscapeService.getIDesigntimeSystemLandscapeService();

NamingEnumeration enum = landscape.getAliasesNames(request.getUser());

while (enum.hasMore()) {

NameClassPair ncp = (NameClassPair) enum.next();

String path = landscape.getAliasTarget(ncp.getName(), request.getUser());

path = path.substring(4);

IPcdAttributes attrs = (IPcdAttributes) pcdContext.getAttributes(path);

if (attrs.get("SystemType") == null) {

continue;

}

String system_type = (String) attrs.get("SystemType").get();

if (system_type.equals("SAP_R3")) {

systems.put(ncp.getName(), ncp.getName());

}

}

} catch (Exception e) {

System.out.println("Caught an exception: \n" + e);

}

return systems;

}

}

Thanks,

Mike