001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.cfg;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.ejb.gen.*;
033: import com.caucho.util.L10N;
034:
035: import java.util.ArrayList;
036: import java.lang.reflect.*;
037:
038: import javax.ejb.*;
039:
040: /**
041: * Configuration for a method.
042: */
043: public class EjbMethodPattern {
044: private static L10N L = new L10N(EjbMethodPattern.class);
045:
046: public final static int RESIN_DATABASE = 0;
047: public final static int RESIN_READ_ONLY = 1;
048: public final static int RESIN_ROW_LOCK = 2;
049:
050: private EjbBean _bean;
051:
052: private String _location;
053:
054: private MethodSignature _signature;
055:
056: private int _resinIsolation = -1;
057: private int _jdbcIsolation = -1;
058:
059: private boolean _queryLoadsBean = true;
060: private boolean _relationLoadsBean;
061:
062: private String _query;
063: private String _queryLocation;
064:
065: private TransactionAttributeType _transactionType = null;
066: private ArrayList<String> _roles;
067:
068: /**
069: * Creates a new method.
070: */
071: public EjbMethodPattern() {
072: }
073:
074: /**
075: * Creates a new method.
076: *
077: * @param entity the owning entity bean.
078: * @param signature the method signature.
079: */
080: public EjbMethodPattern(EjbBean bean, MethodSignature signature) {
081: _bean = bean;
082: _signature = signature;
083: }
084:
085: /**
086: * Sets the bean.
087: */
088: public void setBean(EjbBean bean) {
089: _bean = bean;
090: }
091:
092: /**
093: * Sets the config location.
094: */
095: public void setLocation(String location) {
096: _location = location;
097: }
098:
099: /**
100: * Returns the config location.
101: */
102: public String getLocation() {
103: return _location;
104: }
105:
106: /**
107: * Sets the method signature.
108: */
109: public void setSignature(MethodSignature sig) {
110: _signature = sig;
111: }
112:
113: /**
114: * Returns the method signature.
115: */
116: public MethodSignature getSignature() {
117: return _signature;
118: }
119:
120: /**
121: * Returns the method name.
122: */
123: public String getName() {
124: return _signature.getName();
125: }
126:
127: /**
128: * Returns true if the method does not set any values.
129: */
130: public boolean isReadOnly() {
131: return _resinIsolation == RESIN_READ_ONLY;
132: }
133:
134: /**
135: * Returns the Resin isolation.
136: */
137: public int getResinIsolation() {
138: return _resinIsolation;
139: }
140:
141: /**
142: * Sets the Resin isolation.
143: */
144: public void setResinIsolation(String isolation)
145: throws ConfigException {
146: if (isolation.equals("read-only"))
147: _resinIsolation = RESIN_READ_ONLY;
148: else if (isolation.equals("database"))
149: _resinIsolation = RESIN_DATABASE;
150: else if (isolation.equals("row-locking"))
151: _resinIsolation = RESIN_ROW_LOCK;
152: else
153: throw new ConfigException(
154: L
155: .l(
156: "'{0}' is an unknown value for resin-isolation. Only 'read-only', 'database', and 'row-locking' are allowed.",
157: isolation));
158: }
159:
160: /**
161: * Returns the JDBC isolation.
162: */
163: public int getJDBCIsolation() {
164: return _jdbcIsolation;
165: }
166:
167: /**
168: * Sets the JDBC isolation.
169: */
170: public void setJDBCIsolation(int isolation) {
171: _jdbcIsolation = isolation;
172: }
173:
174: /**
175: * Returns the method's query.
176: */
177: public String getQuery() {
178: return _query;
179: }
180:
181: /**
182: * Sets the method's query.
183: */
184: public void setQuery(String query) {
185: _query = query;
186: }
187:
188: /**
189: * Returns the query config location.
190: */
191: public String getQueryLocation() {
192: return _queryLocation;
193: }
194:
195: /**
196: * Sets the query node.
197: */
198: public void setQueryLocation(String location) {
199: _queryLocation = location;
200: }
201:
202: /**
203: * Returns the method's transaction type, e.g. Required.
204: */
205: public TransactionAttributeType getTransactionType() {
206: if (_transactionType != null)
207: return _transactionType;
208: else if (isReadOnly())
209: return TransactionAttributeType.SUPPORTS;
210: else
211: return TransactionAttributeType.REQUIRED;
212: }
213:
214: public void setTransaction(TransactionAttributeType type)
215: throws ConfigException {
216: _transactionType = type;
217: }
218:
219: /**
220: * Sets the method's transaction type, e.g. Required
221: */
222: public void setTransAttribute(String type) throws ConfigException {
223: if ("Required".equals(type))
224: _transactionType = TransactionAttributeType.REQUIRED;
225: else if ("RequiresNew".equals(type))
226: _transactionType = TransactionAttributeType.REQUIRES_NEW;
227: else if ("Mandatory".equals(type))
228: _transactionType = TransactionAttributeType.MANDATORY;
229: else if ("NotSupported".equals(type))
230: _transactionType = TransactionAttributeType.NOT_SUPPORTED;
231: else if ("Never".equals(type))
232: _transactionType = TransactionAttributeType.NEVER;
233: else if ("Supports".equals(type))
234: _transactionType = TransactionAttributeType.SUPPORTS;
235: else
236: throw new ConfigException(
237: L
238: .l(
239: "'{0}' is an unknown transaction type. The transaction types are:\n Required - creates a new transaction if none is active.\n RequiresNew - always creates a new transaction.\n Mandatory - requires an active transaction.\n NotSupported - suspends any active transaction.\n Never - forbids any active transaction.\n Supports - allows a transaction or no transaction.",
240: type));
241: }
242:
243: /**
244: * Returns true if the query method should load bean values.
245: */
246: public boolean getQueryLoadsBean() {
247: return _queryLoadsBean;
248: }
249:
250: /**
251: * Set true if the query method should load bean values.
252: */
253: public void setQueryLoadsBean(boolean loadBean) {
254: _queryLoadsBean = loadBean;
255: }
256:
257: /**
258: * Returns true if the relation method should load bean values.
259: */
260: public boolean getRelationLoadsBean() {
261: return _relationLoadsBean;
262: }
263:
264: /**
265: * Set true if the relation method should load bean values.
266: */
267: public void setRelationLoadsBean(boolean loadBean) {
268: _relationLoadsBean = loadBean;
269: }
270:
271: /**
272: * Returns the roles allowed for the method.
273: */
274: public ArrayList<String> getRoles() {
275: return _roles;
276: }
277:
278: /**
279: * Set the roles allowed for the method.
280: */
281: public void setRoles(ArrayList<String> roles) {
282: _roles = roles;
283: }
284:
285: /**
286: * Set the roles allowed for the method.
287: */
288: public void setRoles(String[] roles) {
289: if (roles != null) {
290: if (_roles == null)
291: _roles = new ArrayList();
292:
293: for (String role : roles) {
294: _roles.add(role);
295: }
296: }
297: }
298:
299: /**
300: * Configures the bean with the override values
301: */
302: public void configure(BeanGenerator bean) {
303: for (View view : bean.getViews()) {
304: // XXX: check for type
305:
306: for (BusinessMethodGenerator bizMethod : view.getMethods()) {
307: Method apiMethod = bizMethod.getApiMethod();
308: if (_signature.isMatch(apiMethod.getName(), apiMethod
309: .getParameterTypes())) {
310: configureSecurity(bizMethod);
311: configureXA(bizMethod);
312: }
313: }
314: }
315: }
316:
317: private void configureXA(BusinessMethodGenerator bizMethod) {
318: if (_transactionType == null)
319: return;
320:
321: XaCallChain xa = bizMethod.getXa();
322:
323: xa.setTransactionType(_transactionType);
324: }
325:
326: private void configureSecurity(BusinessMethodGenerator bizMethod) {
327: if (_roles == null)
328: return;
329:
330: SecurityCallChain security = bizMethod.getSecurity();
331:
332: security.setRoles(_roles);
333: }
334:
335: /**
336: * Returns true if these are equivalent.
337: */
338: public boolean equals(Object o) {
339: if (!(o instanceof EjbMethodPattern))
340: return false;
341:
342: EjbMethodPattern method = (EjbMethodPattern) o;
343:
344: return _signature.equals(method.getSignature());
345: }
346:
347: public String toString() {
348: return "EJBMethodPattern[" + _signature.getName() + "]";
349: }
350: }
|