KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > AllowedOperationsAssociation


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.ejb;
23
24 // $Id: AllowedOperationsAssociation.java 40312 2006-01-18 12:13:46Z dimitris $
25

26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedHashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Stack JavaDoc;
33
34 import org.jboss.logging.Logger;
35
36 /**
37  * Associates the current thread with a stack of flags that
38  * indicate the callers current ejb method.
39  *
40  * According to the EJB2.1 spec not all context methods can be accessed at all times
41  * For example ctx.getPrimaryKey() should throw an IllegalStateException when called from within ejbCreate()
42  *
43  * @author Thomas.Diesler@jboss.org
44  * @author Dimitris.Andreadis@jboss.org
45  * @version $Revision: 40312 $
46  */

47 public final class AllowedOperationsAssociation implements AllowedOperationsFlags
48 {
49    // provide logging
50
private static final Logger log = Logger.getLogger(AllowedOperationsAssociation.class);
51
52    // Constants -----------------------------------------------------
53

54    public static final HashMap JavaDoc methodMap = new LinkedHashMap JavaDoc();
55    static
56    {
57       methodMap.put(new Integer JavaDoc(IN_INTERCEPTOR_METHOD), "IN_INTERCEPTOR_METHOD");
58       methodMap.put(new Integer JavaDoc(IN_EJB_ACTIVATE), "IN_EJB_ACTIVATE");
59       methodMap.put(new Integer JavaDoc(IN_EJB_PASSIVATE), "IN_EJB_PASSIVATE");
60       methodMap.put(new Integer JavaDoc(IN_EJB_REMOVE), "IN_EJB_REMOVE");
61       methodMap.put(new Integer JavaDoc(IN_EJB_CREATE), "IN_EJB_CREATE");
62       methodMap.put(new Integer JavaDoc(IN_EJB_POST_CREATE), "IN_EJB_POST_CREATE");
63       methodMap.put(new Integer JavaDoc(IN_EJB_FIND), "IN_EJB_FIND");
64       methodMap.put(new Integer JavaDoc(IN_EJB_HOME), "IN_EJB_HOME");
65       methodMap.put(new Integer JavaDoc(IN_EJB_TIMEOUT), "IN_EJB_TIMEOUT");
66       methodMap.put(new Integer JavaDoc(IN_EJB_LOAD), "IN_EJB_LOAD");
67       methodMap.put(new Integer JavaDoc(IN_EJB_STORE), "IN_EJB_STORE");
68       methodMap.put(new Integer JavaDoc(IN_SET_ENTITY_CONTEXT), "IN_SET_ENTITY_CONTEXT");
69       methodMap.put(new Integer JavaDoc(IN_UNSET_ENTITY_CONTEXT), "IN_UNSET_ENTITY_CONTEXT");
70       methodMap.put(new Integer JavaDoc(IN_SET_SESSION_CONTEXT), "IN_SET_SESSION_CONTEXT");
71       methodMap.put(new Integer JavaDoc(IN_SET_MESSAGE_DRIVEN_CONTEXT), "IN_SET_MESSAGE_DRIVEN_CONTEXT");
72       methodMap.put(new Integer JavaDoc(IN_AFTER_BEGIN), "IN_AFTER_BEGIN");
73       methodMap.put(new Integer JavaDoc(IN_BEFORE_COMPLETION), "IN_BEFORE_COMPLETION");
74       methodMap.put(new Integer JavaDoc(IN_AFTER_COMPLETION), "IN_AFTER_COMPLETION");
75       methodMap.put(new Integer JavaDoc(IN_BUSINESS_METHOD), "IN_BUSINESS_METHOD");
76       methodMap.put(new Integer JavaDoc(IN_SERVICE_ENDPOINT_METHOD), "IN_SERVICE_ENDPOINT_METHOD");
77    }
78
79    /**
80     * Holds a stack of the IN_METHOD constants, to indicate that we are in an ejb method
81     */

82    private static ThreadLocal JavaDoc threadLocal = new ThreadLocal JavaDoc() {
83       protected Object JavaDoc initialValue()
84       {
85          return new Stack JavaDoc();
86       }
87    };
88
89    // Static --------------------------------------------------------
90

91    /**
92     * Set when the instance enters an ejb method, reset on exit
93     *
94     * @param inMethodFlag one of the IN_METHOD contants or null
95     */

96    public static void pushInMethodFlag(int inMethodFlag)
97    {
98       Stack JavaDoc inMethodStack = (Stack JavaDoc)threadLocal.get();
99       inMethodStack.push(new Integer JavaDoc(inMethodFlag));
100    }
101
102    /**
103     * Reset when the instance exits an ejb method
104     */

105    public static void popInMethodFlag()
106    {
107       Stack JavaDoc inMethodStack = (Stack JavaDoc)threadLocal.get();
108       inMethodStack.pop();
109    }
110
111    /**
112     * Return the current inMethodFlag, or -1 if there is none
113     */

114    public static int peekInMethodFlag()
115    {
116       Stack JavaDoc inMethodStack = (Stack JavaDoc)threadLocal.get();
117       if (inMethodStack.isEmpty() == false)
118          return ((Integer JavaDoc)inMethodStack.peek()).intValue();
119       else
120          return -1;
121    }
122
123    /**
124     * Return the current inMethodFlag in String form,
125     * or null if there is none
126     */

127    public static String JavaDoc peekInMethodFlagAsString()
128    {
129       int currentMethodFlag = peekInMethodFlag();
130       
131       return (String JavaDoc)methodMap.get(new Integer JavaDoc(currentMethodFlag));
132    }
133    
134    /**
135     * Throw an IllegalStateException if the current inMethodFlag
136     * does not match the given flags
137     */

138    public static void assertAllowedIn(String JavaDoc ctxMethod, int flags)
139    {
140       Stack JavaDoc inMethodStack = (Stack JavaDoc)threadLocal.get();
141
142       // Strict validation, the caller MUST set the in method flag
143
if (inMethodStack.empty())
144       {
145          throw new IllegalStateException JavaDoc("Cannot obtain inMethodFlag for: " + ctxMethod);
146       }
147
148       // The container should push a method flag into the context just before
149
// a call to the instance method
150
if (inMethodStack.empty() == false)
151       {
152          // Check if the given ctxMethod can be called from the ejb instance
153
// this relies on the inMethodFlag being pushed prior to the call to the ejb method
154
Integer JavaDoc inMethodFlag = ((Integer JavaDoc) inMethodStack.peek());
155          if ((inMethodFlag.intValue() & flags) == 0 && inMethodFlag.intValue() != IN_INTERCEPTOR_METHOD)
156          {
157             String JavaDoc message = ctxMethod + " should not be access from this bean method: " + methodMap.get(inMethodFlag);
158             IllegalStateException JavaDoc ex = new IllegalStateException JavaDoc(message);
159             log.error(message + ", allowed is " + getAllowedMethodList(flags), ex);
160             throw ex;
161          }
162       }
163    }
164
165    /**
166     * Get a list of strings corresponding to the given method flags
167     */

168    private static List JavaDoc getAllowedMethodList(int flags)
169    {
170       ArrayList JavaDoc allowed = new ArrayList JavaDoc();
171       Iterator JavaDoc it = methodMap.entrySet().iterator();
172       while (it.hasNext())
173       {
174          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
175          Integer JavaDoc flag = (Integer JavaDoc) entry.getKey();
176          if ((flag.intValue() & flags) > 0)
177             allowed.add(entry.getValue());
178       }
179       return allowed;
180    }
181 }
182
Popular Tags