KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > ejb > RunAsWithRolesMDB


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.ejb;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.MessageDrivenBean JavaDoc;
28 import javax.ejb.MessageDrivenContext JavaDoc;
29 import javax.jms.Message JavaDoc;
30 import javax.jms.MessageListener JavaDoc;
31 import javax.jms.Queue JavaDoc;
32 import javax.jms.QueueConnection JavaDoc;
33 import javax.jms.QueueConnectionFactory JavaDoc;
34 import javax.jms.QueueSender JavaDoc;
35 import javax.jms.QueueSession JavaDoc;
36 import javax.jms.Session JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.Name JavaDoc;
39 import javax.naming.NameParser JavaDoc;
40 import javax.naming.NamingException JavaDoc;
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 /** An MDB that invokes several methods on the ProjRepository session bean,
47  each of which require a seperate role to test the assignement of multiple
48  roles to the run-as identity.
49  
50  @author Scott.Stark@jboss.org
51  @version $Revision: 37406 $
52  */

53 public class RunAsWithRolesMDB implements MessageDrivenBean JavaDoc, MessageListener JavaDoc
54 {
55    static Logger log = Logger.getLogger(RunAsWithRolesMDB.class);
56    
57    private MessageDrivenContext JavaDoc ctx = null;
58    private InitialContext JavaDoc iniCtx;
59    
60    public RunAsWithRolesMDB()
61    {
62    }
63
64    public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
65       throws EJBException JavaDoc
66    {
67       this.ctx = ctx;
68       try
69       {
70          iniCtx = new InitialContext JavaDoc();
71       }
72       catch(NamingException JavaDoc e)
73       {
74          throw new EJBException JavaDoc(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 JavaDoc message)
88    {
89       Queue JavaDoc replyTo = null;
90       try
91       {
92          replyTo = (Queue JavaDoc) message.getJMSReplyTo();
93          String JavaDoc name = message.getStringProperty("name");
94          ProjRepositoryHome home = (ProjRepositoryHome) iniCtx.lookup("java:comp/env/ejb/ProjRepository");
95          NameParser JavaDoc parser = iniCtx.getNameParser("");
96          Name JavaDoc projName = parser.parse(name);
97          // This requires the ProjectAdmin role
98
ProjRepository bean = home.create(projName);
99          // This requires CreateFolder role
100
Name JavaDoc programs = parser.parse("/Programs Files");
101          bean.createFolder(programs);
102          // This requires DeleteFolder role
103
bean.deleteItem(programs);
104          sendReply(replyTo, "Role tests ok");
105          bean.remove();
106       }
107       catch(Throwable JavaDoc e)
108       {
109          log.debug("failed", e);
110          if( replyTo != null )
111          {
112             StringWriter JavaDoc sw = new StringWriter JavaDoc();
113             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
114             e.printStackTrace(pw);
115             sendReply(replyTo, "Failed, ex=\n"+sw.toString());
116          }
117       }
118    }
119    private void sendReply(Queue JavaDoc replyTo, String JavaDoc info)
120    {
121       try
122       {
123          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
124          QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) ctx.lookup("java:comp/env/jms/QueFactory");
125          QueueConnection JavaDoc queueConn = queueFactory.createQueueConnection();
126          QueueSession JavaDoc session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
127          Message JavaDoc msg = session.createMessage();
128          msg.setStringProperty("reply", info);
129          QueueSender JavaDoc 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 JavaDoc e)
137       {
138          log.error("Failed to send reply", e);
139       }
140    }
141 }
142
Popular Tags