[转] Applet And JMS using Glassfish and JDK 6

 本文转自:http://it.toolbox.com/blogs/enterprise-web-solutions/applet-and-jms-using-glassfish-and-jdk-6-34816

 感谢原文作者!

Applet And JMS

Implementing Asynchronous Messaging model to the browser is becoming more and more needed. As for today the standard way to implement this is using polling with Javascript. Another interesting way of implementing this can be done using the JMS inside the client and have a hidden applet consume messages from a queue and then invoking the appropriate code in Javascript.

The Applet Class
you could create a separate project for this class and include all required libraries along.
The libraries you need to include (from your glassfish/lib) are:
appserv-rt.jar, javaee.jar, imqjmsra.jar,appserv-admin.jar,org.json-0.0.1.jar,plugin.jar.
When you create an InitialContext, set property APPLET so that the method getParameter will be invoked to initialize the properties for the jndi. JMS ORB resources will require extra permissions so we will have to sign this applet, though to avoid signing all other jars along with the applet jar we can set the policy to grant us full permissions to use other unsigned jar files:
 

Policy.setPolicy(new MyPolicy());


(the code is following the EventsApplet class)
The JSObject is responsible for interacting with Javascript code inside our web page, note that we will have to mark the applet tag with the mayscript attribute.
In your Javascript code create a method named: appletCallback , that method will be called when a new event arrives from the JMS destination.

public class EventsApplet extends Applet implements MessageListener{
private static final long serialVersionUID = 1l;
private TopicConnectionFactory topicConnectionFactory;
private Context context;
private Topic nmsEventTopic;
private TopicConnection connection;
private TopicSession session;
private MessageConsumer consumer;
private static final int ORB_PORT = 3700;

public EventsApplet(){
super.setVisible(false);
}

@Override
public String getParameter(String name) {
System.out.println("getParameter!!! " + name);
if(name.equals("java.naming.factory.initial")){
return "com.sun.enterprise.naming.SerialInitContextFactory";}
else if(name.equals("java.naming.factory.state")){
return "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl";
}else if(name.equals("java.naming.factory.url.pkgs")){
return "com.sun.enterprise.naming";
}
else if(name.equals("java.naming.provider.url")){
return "iiop://" + this.getCodeBase().getHost() + ":3700";
}
else if(name.equalsIgnoreCase("MAYSCRIPT")){
return name;
}
return super.getParameter(name);
}

@Override
public void init() {
System.out.println("init!!!!!");
try {
Policy.setPolicy(new MyPolicy());
initJmsResources();
}
catch (Exception exp) {
exp.printStackTrace();
}
}

private void initJmsResources() throws Exception{
cleanup();
String ip = this.getCodeBase().getHost();
Hashtable<String, Object> hash = new Hashtable<String, Object>();
hash.put(Context.APPLET, this);
hash.put("org.omg.CORBA.ORBInitialHost", ip);
hash.put("org.omg.CORBA.ORBInitialPort", String.valueOf(ORB_PORT));
context = new InitialContext(hash);
topicConnectionFactory = (TopicConnectionFactory)context.lookup("jms/TEMSTopicConnectionFactory");
nmsEventTopic = (Topic)this.context.lookup("jms/NmsEventTopic");
connection = this.topicConnectionFactory.createTopicConnection();
session = connection.createTopicSession(false, Session.DUPS_OK_ACKNOWLEDGE);
consumer = session.createConsumer(this.nmsEventTopic);
consumer.setMessageListener(this);
connection.start();
}

private void cleanup() throws JMSException, NamingException{
if(consumer != null){
consumer.close();
consumer = null;
}
if(session != null){
session.close();
session = null;
}
if(connection != null){
connection.close();
connection = null;
}
if(context != null){
context.close();
context = null;
}
nmsEventTopic = null;
topicConnectionFactory = null;
}

@Override
public void destroy() {
System.out.println("destroy!!!!!");
try {
cleanup();
}
catch (Exception exp) {
exp.printStackTrace();
}
}

@Override
public void start() {
System.out.println("start!!!");
JSObject window = JSObject.getWindow(this);
window.call("alert", new Object[]{"Applet started: " + connection + ", " + session + ", " + consumer});
System.out.println("Applet started: " + connection + ", " + session + ", " + consumer);}

@Override
public void stop() {
System.out.println("stop!!!");
try {
//cleanup();
}
catch (Exception exp) {
exp.printStackTrace();
}
}

@Override
public void onMessage(Message m) {
System.out.println("EventsApplet.onMessage " + m);
ObjectMessage message = (ObjectMessage)m;
try {
NmsAlarm alarm = (NmsAlarm)message.getObject();
String commandName = message.getStringProperty("commandName");
JSONObject jsonAlarm = (new JSONNmsAlarm()).toJsonObject(alarm);
jsonAlarm.put("queueOperation", commandName);
JSObject window = JSObject.getWindow(this);
window.call("appletCallback", new Object[]{window.eval("(" + jsonAlarm + ")")});
System.out.println("window.call: " + alarm);
}
catch (Exception e) {
e.printStackTrace();
}
}
}



The Policy classes:

 1 public class MyPolicy extends Policy{
2 private MyPermissionCollection perms;
3 public TycoPolicy(){
4 perms = new MyPermissionCollection();
5 }
6
7 @Override
8 public PermissionCollection getPermissions(CodeSource codesource) {
9 return perms;
10 }
11
12 @Override
13 public PermissionCollection getPermissions(ProtectionDomain domain) {
14 return perms;
15 }
16 }
17 public class MyPermissionCollection extends PermissionCollection{
18 private static final long serialVersionUID = 1L;
19
20 @Override
21 public void add(Permission permission) {}
22
23 @Override
24 public Enumeration<Permission> elements() {
25 return null;
26 }
27 @Override
28 public boolean implies(Permission permission) {
29 return true;
30 }
31 }
32 public class MyPermision extends BasicPermission{
33 private static final long serialVersionUID = 1L;
34 public TycoPermision(){
35 super("X.509");
36 }
37 }



Signing the jar

  • create a jar file from you applet project (with any relevant tool) and place the jar in a published directory inside your web application along with the jars: appserv-rt.jar, javaee.jar, imqjmsra.jar,appserv-admin.jar,org.json-0.0.1.jar,plugin.jar, appserv-deployment-client.jar, appserv-launch.jar.
  • using command line change directory to the directory in which the applet resides.
  • [jdk installation directory]/bin/keytool -genkey -alias signFiles -keystore compstore -keypass 111111 -dname "cn=yourname" -storepass 123456
  • [jdk installation directory]/bin/jarsigner -keystore compstore -storepass 123456 -keypass 111111 your-applet.jar signFiles
  • [jdk installation directory]/bin/keytool -export -keystore compstore -storepass 123456 -alias signFiles -file your-applet.cer

Note: if you would like to test it without signing the jar you could go to [JRE installation directory]/lib/security/java.policy and add the following lines:

grant codeBase "http://ipaddress:port/your-web-context/-" {
    permission java.security.AllPermission;
};

The Simple Tag class
create a simple tag file as follows, not that the parameter separate_jvm is a jdk.1.6.10 parameter and up.

 

 1 public class MyPolicy extends Policy{
2 private MyPermissionCollection perms;
3 public TycoPolicy(){
4 perms = new MyPermissionCollection();
5 }
6
7 @Override
8 public PermissionCollection getPermissions(CodeSource codesource) {
9 return perms;
10 }
11
12 @Override
13 public PermissionCollection getPermissions(ProtectionDomain domain) {
14 return perms;
15 }
16 }
17 public class MyPermissionCollection extends PermissionCollection{
18 private static final long serialVersionUID = 1L;
19
20 @Override
21 public void add(Permission permission) {}
22
23 @Override
24 public Enumeration<Permission> elements() {
25 return null;
26 }
27 @Override
28 public boolean implies(Permission permission) {
29 return true;
30 }
31 }
32 public class MyPermision extends BasicPermission{
33 private static final long serialVersionUID = 1L;
34 public TycoPermision(){
35 super("X.509");
36 }
37 }

 

inside the simple-tags.tld file (under WEB-INF in the web project) create the simple tag: (do not be confused with EventsApplet, that code refers to the tag EventsApplet and not the applet class.

 1 <tag>
2 <name>events-applet</name>
3 <tag-class>your-tag-package.EventsApplet</tag-class>
4 <body-content>empty</body-content>
5 </tag>
6
7 The HTML - here I created a hidden applet just to leverage the JMS
8
9 <div style="position: absolute; visibility: hidden;">
10 <st:events-applet />
11 </div>


posted on 2012-02-29 12:53  sportscar  阅读(279)  评论(0编辑  收藏  举报

导航