Introduction
In the weblog i am going to present JAVA code to broadcast messages to users who are logged on to portal. This is very useful for broadcasting critical system messages to user.
JAR files required:
prtcoreservice.jar
Steps to create the component.
Step 1
Build an empty project ( i am calling it com.ust.broadcast). Create a portal service called "alertreceiver" which
implements
ITopicListener
This is what your alertreceiver.java and Ialertreceiver.java should look like. Copy the following code to your project
alertreceiver.java
/* Built By: Prakash Singh
Technical Consultant
Universal System Technologies, Inc
2500 W. Lake Mary Blvd., Ste 212-A
Lake Mary, FL 32746 USA
M 407-474-2216
*/
package com.ust.broadcast;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;
import com.sapportals.portal.prt.runtime.PortalRuntime;
import com.sapportals.portal.prt.service.IServiceContext;
import com.sapportals.portal.prt.service.notification.INotificationService;
import com.sapportals.portal.prt.service.notification.ITopicListener;
import com.sapportals.portal.prt.service.notification.TopicData;
import com.sapportals.portal.prt.service.notification.TopicDataContainer;
public class alertreceiver implements Ialertreceiver, ITopicListener {
private IServiceContext mm_serviceContext;
private INotificationService notificationService;
private final static String TOPIC_NAME = "USTBroadCastAlerts";
public String alertmessage ="";
public String userid ="";
private Timer timer;
/**
Generic init method of the service. Will be called by the portal runtime.
@param serviceContext
*/
public void init(IServiceContext serviceContext) {
mm_serviceContext = serviceContext;
this.subscribe();
}
/**
This method is called after all services in the portal runtime
have already been initialized.
*/
public void afterInit() {
}
/**
configure the service
@param configuration
@deprecated
*/
public void configure(
com
.sapportals
.portal
.prt
.service
.IServiceConfiguration configuration) {
}
/**
This method is called by the portal runtime
when the service is destroyed.
*/
public void destroy() {
timer = null;
}
/**
This method is called by the portal runtime
when the service is released.
@deprecated
*/
public void release() {
}
/**
@return the context of the service, which was previously set
by the portal runtime
*/
public IServiceContext getContext() {
return mm_serviceContext;
}
public String GetMessage() {
return alertmessage;
}
public String GetUserid() {
return userid;
}
/**
This method should return a string that is unique to this service amongst all
other services deployed in the portal runtime.
@return a unique key of the service
*/
public String getKey() {
return KEY;
}
public void subscribe() {
INotificationService notService = getNotificationInstance();
notService.subscribe(TOPIC_NAME, this);
}
private INotificationService getNotificationInstance() {
if (notificationService == null)
notificationService =
(INotificationService) PortalRuntime
.getRuntimeResources()
.getService(
"com.sap.portal.runtime.system.notification.notification");
return notificationService;
}
public void handleTopic(TopicDataContainer topicDataContainer) {
if ( timer != null)
{
timer.cancel();
timer = null;
}
Enumeration enum = topicDataContainer.getTopicDataKeys();
if (enum.hasMoreElements()) {
timer = new Timer();
TopicData topicData = topicDataContainer.getTopicDataValue("duration");
Long duration = new Long(topicData.getTopicValue().trim());
topicData = topicDataContainer.getTopicDataValue("message");
alertmessage = topicData.getTopicValue();
System.out.println(alertmessage);
topicData = topicDataContainer.getTopicDataValue("user");
userid = topicData.getTopicValue();
System.out.println(userid);
timer.schedule(new MessageClear(),duration.longValue()*1000);
}
}
public class MessageClear extends TimerTask {
/* (non-Javadoc)
@see java.util.TimerTask#run()
*/
public void run() {
alertmessage = "";
userid ="";
}
}
}
Ialertreceiver.java
/* Built By: Prakash Singh
Technical Consultant
Universal System Technologies, Inc
2500 W. Lake Mary Blvd., Ste 212-A
Lake Mary, FL 32746 USA
M 407-474-2216
*/
package com.ust.broadcast;
import com.sapportals.portal.prt.service.IService;
public interface Ialertreceiver extends IService
{
public static final String KEY = "com.ust.broadcast.alertreceiver";
public String GetMessage();
public String GetUserid();
}
Step 2
In the same project (com.ust.broadcast) build a DynPage component to publish messages to the user. Use the DynPage wizard to create component
publishmessage
. The code should look like following(cut and paste)./* Built By: Prakash Singh
Technical Consultant
Universal System Technologies, Inc
2500 W. Lake Mary Blvd., Ste 212-A
Lake Mary, FL 32746 USA
M 407-474-2216
*/
package com.ust.broadcast;
import java.io.ByteArrayInputStream;
import com.sap.security.api.IUser;
import com.sapportals.htmlb.Button;
import com.sapportals.htmlb.Form;
import com.sapportals.htmlb.GridLayout;
import com.sapportals.htmlb.Group;
import com.sapportals.htmlb.InputField;
import com.sapportals.htmlb.Label;
import com.sapportals.htmlb.TextEdit;
import com.sapportals.htmlb.TextView;
import com.sapportals.htmlb.enum.GroupDesign;
import com.sapportals.htmlb.event.Event;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.runtime.PortalRuntime;
import com.sapportals.portal.prt.service.notification.INotificationService;
import com.sapportals.portal.prt.service.notification.StreamData;
import com.sapportals.portal.prt.service.notification.TopicData;
import com.sapportals.portal.prt.service.notification.TopicDataContainer;
public class publishmessage extends PageProcessorComponent {
public DynPage getPage() {
return new publishPage();
}
public static class publishPage extends DynPage {
private final static int INITIAL_STATE = 0;
private final static int PUBLISH_STATE = 1;
private int state = INITIAL_STATE;
private final static String TOPIC_NAME = "USTBroadCastAlerts";
private static String message = "";
private static String duration = "";
public void doInitialization() {
// setup the bean which contains all state information
}
public void doProcessAfterInput() throws PageException {
}
public void doProcessBeforeOutput() throws PageException {
IPortalComponentRequest request =
(IPortalComponentRequest) this.getRequest();
IUser user = request.getUser();
Form myForm = getForm();
Group myGroup = new Group();
myGroup.setWidth("350");
myGroup.setDesign(GroupDesign.SAPCOLOR);
GridLayout gl = new GridLayout();
myGroup.addComponent(gl);
switch (state) {
case PUBLISH_STATE :
myGroup.setTitle("Broadcast");
//get the notification service
INotificationService notService =
(INotificationService) PortalRuntime
.getRuntimeResources()
.getService(
"com.sap.portal.runtime.system.notification.notification");
//Define a data container with information that will be send to the receiver
TopicDataContainer container =
new TopicDataContainer(TOPIC_NAME);
// add a message
StreamData messageStream = new StreamData(new ByteArrayInputStream(message.getBytes()));
TopicData durationdata =
new TopicData(TopicDataContainer.STRING, duration);
TopicData userdata =
new TopicData(TopicDataContainer.STRING, user.getUniqueName());
container.addTopicData("message", messageStream);
container.addTopicData("duration", duration);
container.addTopicData("user", userdata);
notService.publish(TOPIC_NAME, container);
TextView success =
new TextView("Message was succesfully broadcasted.");
gl.addComponent(1, 1, success);
Button buttonback = new Button("Back");
buttonback.setText("Back");
buttonback.setOnClick("back");
gl.addComponent(2, 1, buttonback);
break;
case INITIAL_STATE :
myGroup.setTitle("Broadcast Message");
Label lblmessage = new Label("Message to be broadcasted");
lblmessage.setRequired(true);
lblmessage.setLabeledComponentId("Message");
gl.addComponent(1, 1, lblmessage);
TextEdit message = new TextEdit("Message");
message.setId("Message");
message.setCols(50);
message.setRows(5);
gl.addComponent(2, 1, message);
Label lblduration = new Label("Duration(seconds)");
lblduration.setRequired(true);
lblduration.setLabeledComponentId("Duration");
gl.addComponent(3, 1, lblduration);
InputField duration = new InputField("Duration");
duration.setId("Duration");
duration.setSize(5);
duration.setMaxlength(4);
gl.addComponent(4, 1, duration);
Button button = new Button("Publish");
button.setText("Publish");
button.setOnClick("publish");
gl.addComponent(5, 1, button);
default :
break;
}
myForm.addComponent(myGroup);
}
public void onPublish(Event event) throws PageException {
state = PUBLISH_STATE;
TextEdit messagefld = (TextEdit) getComponentByName("Message");
InputField durationfld =
(InputField) getComponentByName("Duration");
message = messagefld.getText();
duration = durationfld.getValueAsDataType().toString();
}
public void onBack(Event event) throws PageException {
state = INITIAL_STATE;
}
}
}
Step 3
In the same project (com.ust.broadcast) build a JSPDynPage component that gets alert and displays it as a popup by calling a dynpage component(defined in step4). Use the JSPDynPage wizard to create component called
display
. The code should look like following(cut and paste).Display.Java
/* Built By: Prakash Singh
Technical Consultant
Universal System Technologies, Inc
2500 W. Lake Mary Blvd., Ste 212-A
Lake Mary, FL 32746 USA
M 407-474-2216
*/
package com.ust.broadcast;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.portal.htmlb.page.JSPDynPage;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.prt.component.IPortalComponentContext;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.component.IPortalComponentURI;
import com.sapportals.portal.prt.runtime.PortalRuntime;
import com.ust.broadcast.bean.alertinfo;
//import bean.ContextBean;
public class display extends PageProcessorComponent {
public DynPage getPage() {
return new displayDynPage();
}
public static class displayDynPage extends JSPDynPage {
private alertinfo bean;
private final static String BEAN_KEY = "myBean";
public void doInitialization() {
bean = null;
IPortalComponentRequest request =
(IPortalComponentRequest) this.getRequest();
IPortalComponentContext context = request.getComponentContext();
IPortalComponentURI uri = request.createPortalComponentURI();
uri.setContextName("com.ust.broadcast.popup");
Ialertreceiver receiver =
(Ialertreceiver) PortalRuntime
.getRuntimeResources()
.getService(
Ialertreceiver.KEY);
if (!receiver.GetMessage().equals("")) {
receiver.GetMessage();
}
Object o = context.getValue(BEAN_KEY);
if (o instanceof alertinfo) {
bean = (alertinfo) o;
}
if (bean == null) {
// no bean or probably wrong class.
// We simply instantiate a new bean and put
// it into the session
bean = new alertinfo();
}
bean.url = uri.toString();
if (!receiver.GetMessage().equals(""))
bean.no_message = "false";
else
bean.no_message = "true";
context.putValue(BEAN_KEY, bean);
}
public void doProcessAfterInput() throws PageException {
}
public void doProcessBeforeOutput() throws PageException {
this.setJspName("displayalert.jsp");
}
}
}
/* Built By: Prakash Singh
Technical Consultant
Universal System Technologies, Inc
2500 W. Lake Mary Blvd., Ste 212-A
Lake Mary, FL 32746 USA
M 407-474-2216
*/
package com.ust.broadcast.bean;
public class alertinfo
{
public String url;
public String no_message;
public String geturl() {
return url;
}
public String no_message() {
return no_message;
}
}
Step 4
In the same project (com.ust.broadcast) build a DynPage component called
popupup
that displays the messageSource code for popup.java. (Cut and Paste)
/* Built By: Prakash Singh
Technical Consultant
Universal System Technologies, Inc
2500 W. Lake Mary Blvd., Ste 212-A
Lake Mary, FL 32746 USA
M 407-474-2216
*/
package com.ust.broadcast;
import com.sapportals.htmlb.Form;
import com.sapportals.htmlb.GridLayout;
import com.sapportals.htmlb.Group;
import com.sapportals.htmlb.Label;
import com.sapportals.htmlb.TextEdit;
import com.sapportals.htmlb.TextView;
import com.sapportals.htmlb.enum.GroupDesign;
import com.sapportals.htmlb.enum.TextViewDesign;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.prt.runtime.PortalRuntime;
public class popup extends PageProcessorComponent {
public DynPage getPage() {
return new popupDynPage();
}
public static class popupDynPage extends DynPage {
/**
Initialization code executed once per user.
*/
public void doInitialization() {
}
/**
Input handling code. In general called the first time with the second page request from the user.
*/
public void doProcessAfterInput() throws PageException {
}
/**
Create output. Called once per request.
*/
public void doProcessBeforeOutput() throws PageException {
Form myForm = this.getForm(); // get the form from DynPage
Ialertreceiver receiver =
(Ialertreceiver) PortalRuntime
.getRuntimeResources()
.getService(
Ialertreceiver.KEY);
Group myGroup = new Group();
myGroup.setTitle("System Message");
myGroup.setWidth("450");
myGroup.setDesign(GroupDesign.SAPCOLOR);
GridLayout gl = new GridLayout();
myGroup.addComponent(gl);
Label lblauthor = new Label("Author");
gl.addComponent(1,1,lblauthor);
TextView author = new TextView("Author");
author.setText(receiver.GetUserid());
author.setDesign(TextViewDesign.HEADER2);
gl.addComponent(2,1,author);
Label lblmessage = new Label("Message");
gl.addComponent(3,1,lblmessage);
TextEdit alert = new TextEdit("message");
alert.setCols(80);
alert.setRows(6);
alert.setText(receiver.GetMessage());
gl.addComponent(4,1,alert);
myForm.addComponent(myGroup);
// create your GUI here....
}
}
}
User | Count |
---|---|
1 | |
1 | |
1 | |
1 |