1 22 package org.jboss.test.security.ejb; 23 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import javax.ejb.EJBException ; 27 import javax.ejb.MessageDrivenBean ; 28 import javax.ejb.MessageDrivenContext ; 29 import javax.jms.Message ; 30 import javax.jms.MessageListener ; 31 import javax.jms.Queue ; 32 import javax.jms.QueueConnection ; 33 import javax.jms.QueueConnectionFactory ; 34 import javax.jms.QueueSender ; 35 import javax.jms.QueueSession ; 36 import javax.jms.Session ; 37 import javax.naming.InitialContext ; 38 import javax.naming.Name ; 39 import javax.naming.NameParser ; 40 import javax.naming.NamingException ; 41 42 import org.jboss.logging.Logger; 43 import org.jboss.test.security.interfaces.ProjRepository; 44 import org.jboss.test.security.interfaces.ProjRepositoryHome; 45 46 53 public class RunAsWithRolesMDB implements MessageDrivenBean , MessageListener 54 { 55 static Logger log = Logger.getLogger(RunAsWithRolesMDB.class); 56 57 private MessageDrivenContext ctx = null; 58 private InitialContext iniCtx; 59 60 public RunAsWithRolesMDB() 61 { 62 } 63 64 public void setMessageDrivenContext(MessageDrivenContext ctx) 65 throws EJBException 66 { 67 this.ctx = ctx; 68 try 69 { 70 iniCtx = new InitialContext (); 71 } 72 catch(NamingException e) 73 { 74 throw new EJBException (e); 75 } 76 } 77 78 public void ejbCreate() 79 { 80 } 81 82 public void ejbRemove() 83 { 84 ctx = null; 85 } 86 87 public void onMessage(Message message) 88 { 89 Queue replyTo = null; 90 try 91 { 92 replyTo = (Queue ) message.getJMSReplyTo(); 93 String name = message.getStringProperty("name"); 94 ProjRepositoryHome home = (ProjRepositoryHome) iniCtx.lookup("java:comp/env/ejb/ProjRepository"); 95 NameParser parser = iniCtx.getNameParser(""); 96 Name projName = parser.parse(name); 97 ProjRepository bean = home.create(projName); 99 Name programs = parser.parse("/Programs Files"); 101 bean.createFolder(programs); 102 bean.deleteItem(programs); 104 sendReply(replyTo, "Role tests ok"); 105 bean.remove(); 106 } 107 catch(Throwable e) 108 { 109 log.debug("failed", e); 110 if( replyTo != null ) 111 { 112 StringWriter sw = new StringWriter (); 113 PrintWriter pw = new PrintWriter (sw); 114 e.printStackTrace(pw); 115 sendReply(replyTo, "Failed, ex=\n"+sw.toString()); 116 } 117 } 118 } 119 private void sendReply(Queue replyTo, String info) 120 { 121 try 122 { 123 InitialContext ctx = new InitialContext (); 124 QueueConnectionFactory queueFactory = (QueueConnectionFactory ) ctx.lookup("java:comp/env/jms/QueFactory"); 125 QueueConnection queueConn = queueFactory.createQueueConnection(); 126 QueueSession session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 127 Message msg = session.createMessage(); 128 msg.setStringProperty("reply", info); 129 QueueSender sender = session.createSender(replyTo); 130 sender.send(msg); 131 sender.close(); 132 session.close(); 133 queueConn.close(); 134 log.info("Sent reply"); 135 } 136 catch(Exception e) 137 { 138 log.error("Failed to send reply", e); 139 } 140 } 141 } 142 | Popular Tags |