View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web;
24  
25  import java.io.Serializable;
26  import java.lang.reflect.InvocationTargetException;
27  import java.rmi.RemoteException;
28  import java.util.Collection;
29  import java.util.HashMap;
30  import java.util.Set;
31  
32  import javax.ejb.EJBObject;
33  import javax.ejb.SessionBean;
34  
35  /**
36   * @TODO documentation
37   * @author rlogiacco@smartlab.net
38   */
39  public abstract class EnterpriseDomain implements SessionBean {
40  
41  	private static final long serialVersionUID = -2893852982428251155L;
42  
43  
44  	public static class RequestContext implements Serializable {
45  
46  		private static final long serialVersionUID = 2724059707578588014L;
47  
48  		/**
49  		 * Attributes container.
50  		 */
51  		private HashMap attributes = new HashMap();
52  
53  
54  		public boolean contains(String key) {
55  			return attributes.containsKey(key);
56  		}
57  
58  		public Set getAttributeNames() {
59  			return attributes.keySet();
60  		}
61  
62  		public Serializable setAttribute(String key, Serializable value) {
63  			return (Serializable)attributes.put(key, value);
64  		}
65  
66  		public Serializable removeAttribute(String key) {
67  			return (Serializable)attributes.remove(key);
68  		}
69  
70  		public int size() {
71  			return attributes.size();
72  		}
73  
74  		public Collection getAttributesMap() {
75  			return attributes.values();
76  		}
77  	}
78  
79  
80  	public Object execute(String method, Object[] arguments, String[] types, EnterpriseDomain.RequestContext context)
81  			throws RemoteException, InvocationTargetException {
82  		try {
83  			Domain.context.set(context);
84  			return this.getClass().getMethod(method, EnterpriseDomain.forNames(types)).invoke(this, arguments);
85  		} catch (IllegalArgumentException iae) {
86  			throw new InvocationTargetException(iae);
87  		} catch (IllegalAccessException iae) {
88  			throw new InvocationTargetException(iae);
89  		} catch (NoSuchMethodException nsme) {
90  			throw new InvocationTargetException(nsme);
91  		} catch (ClassNotFoundException cnfe) {
92  			throw new InvocationTargetException(cnfe);
93  		} finally {
94  			try {
95  				BusinessObjectFactory.close();
96  			} catch (DAOException daoe) {
97  				throw new InvocationTargetException(daoe);
98  			}
99  		}
100 	}
101 
102 	protected static RequestContext getContext() {
103 		return (RequestContext)Domain.context.get();
104 	}
105 
106 	/**
107 	 * Get the classes from names. <p/> argTypes is not null.
108 	 * 
109 	 * @param argTypes The method argument classes' names
110 	 * @return ClassNotFoundException if any class can not be located
111 	 */
112 	private static Class[] forNames(String[] argTypes) throws ClassNotFoundException {
113 		if (argTypes == null) {
114 			return null;
115 		}
116 		Class[] result = new Class[argTypes.length];
117 		for (int i = 0; i < argTypes.length; i++) {
118 			if (EnterpriseDomainBuilder.primitives.containsKey(argTypes[i])) {
119 				result[i] = (Class)EnterpriseDomainBuilder.primitives.get(argTypes[i]);
120 			} else {
121 				result[i] = Class.forName(argTypes[i]);
122 			}
123 		}
124 		return result;
125 	}
126 
127 
128 	/**
129 	 * @TODO documentation
130 	 * @author rlogiacco@smartlab.net
131 	 */
132 	public static interface Remote extends EJBObject {
133 
134 		/**
135 		 * @TODO documentation
136 		 * @param method
137 		 * @param arguments
138 		 * @param types
139 		 * @param context
140 		 * @return
141 		 * @throws RemoteException
142 		 */
143 		public Object execute(String method, Object[] arguments, String[] types,
144 				EnterpriseDomain.RequestContext context) throws RemoteException;
145 	}
146 }