Clover Coverage Report - SmartWeb
Coverage timestamp: Sun Jun 8 2008 21:20:12 CEST
../../../img/srcFileCovDistChart0.png 29% of files have more coverage
29   171   15   5,8
14   85   0,52   2,5
5     3  
2    
17,2% of code in this file is excluded from these metrics.
 
  EnterpriseDomainBuilder       Line # 50 20 10 0% 0.0
  EnterpriseDomainBuilder.EnterpriseDomain       Line # 127 9 5 0% 0.0
 
No Tests
 
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   
24    package net.smartlab.web;
25   
26    import java.lang.reflect.InvocationHandler;
27    import java.lang.reflect.InvocationTargetException;
28    import java.lang.reflect.Method;
29    import java.lang.reflect.Proxy;
30    import java.rmi.RemoteException;
31    import java.util.Collections;
32    import java.util.HashMap;
33    import java.util.Map;
34   
35    import javax.ejb.EJBHome;
36    import javax.naming.Context;
37    import javax.naming.InitialContext;
38    import javax.naming.NamingException;
39    import javax.rmi.PortableRemoteObject;
40   
41    import org.apache.commons.logging.Log;
42    import org.apache.commons.logging.LogFactory;
43   
44    /**
45    * This class works as a <strong>Business Delegate</strong> for Session
46    * Enterprise Java Beans.
47    *
48    * @author rlogiacco
49    */
 
50    public class EnterpriseDomainBuilder extends Domain {
51   
52    /**
53    * Provides logging capabilities to the builder.
54    */
55    private final static Log logger = LogFactory.getLog(EnterpriseDomainBuilder.class);
56   
57    /**
58    * Associating map for EJB homes. Home objects are cached here for single
59    * JNDI lookup, but them should be checked for bindings update.
60    */
61    private final static Map homes = Collections.synchronizedMap(new HashMap());
62   
63    /**
64    * TODO documentation
65    * @param name
66    * @param type
67    * @return
68    */
 
69  0 toggle public static Object getInstance(String name, Class type) {
70  0 return EnterpriseDomainBuilder.getInstance(name, type, null, null);
71    }
72   
73    /**
74    * TODO documentation
75    * @param name
76    * @param type
77    * @param context
78    * @return
79    */
 
80  0 toggle public static Object getInstance(String name, Class type, Context context) {
81  0 return EnterpriseDomainBuilder.getInstance(name, type, context, null);
82    }
83   
84    /**
85    * TODO documentation
86    * @param name
87    * @param type
88    * @param context
89    * @param parameters
90    * @return
91    */
 
92  0 toggle public static Object getInstance(String name, Class type, Context context, Object[] parameters) {
93  0 Object home = homes.get(name);
94  0 if (home == null) {
95  0 try {
96  0 if (context == null) {
97  0 context = new InitialContext();
98  0 logger.debug("no context specified");
99    }
100  0 home = context.lookup(name);
101  0 if (home instanceof EJBHome) {
102  0 home = PortableRemoteObject.narrow(home, EJBHome.class);
103    }
104  0 homes.put(name, home);
105    } catch (NamingException ne) {
106    logger.error("lookup(" + name + ") - error", ne);
107    return null;
108    }
109    }
110  0 try {
111  0 Class[] parametersType = null;
112  0 if (parameters != null) {
113  0 parametersType = new Class[parameters.length];
114  0 for (int iIndex = 0; iIndex < parametersType.length; iIndex++) {
115  0 parametersType[iIndex] = parameters[iIndex].getClass();
116    }
117    }
118  0 Object ejb = home.getClass().getDeclaredMethod("create", parametersType).invoke(home, parameters);
119  0 return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] {type}, new EnterpriseDomain(ejb));
120    } catch (Exception e) {
121    homes.remove(name);
122    logger.error("instantiation failed", e);
123    return null;
124    }
125    }
126   
 
127    private static class EnterpriseDomain implements InvocationHandler {
128   
129    /**
130    * Logger for this class
131    */
132    private static final Log logger = LogFactory.getLog(EnterpriseDomain.class);
133   
134    /**
135    * TODO documentation Comment for <code>ejb</code>
136    */
137    private Object ejb;
138   
139    /**
140    * TODO documentation
141    * @param ejb
142    */
 
143  0 toggle private EnterpriseDomain(Object ejb) {
144  0 this.ejb = ejb;
145    }
146   
147    /**
148    * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
149    * java.lang.reflect.Method, java.lang.Object[])
150    */
 
151  0 toggle public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
152  0 if (logger.isDebugEnabled()) {
153  0 logger.debug("invoke(" + ejb + ", " + method + ", " + params + ")");
154    }
155  0 Object result = null;
156  0 try {
157  0 result = method.invoke(ejb, params);
158    } catch (InvocationTargetException ite) {
159    if (ite.getTargetException() instanceof RemoteException) {
160    throw new BusinessException(ite);
161    } else {
162    throw ite.getTargetException();
163    }
164    }
165  0 if (logger.isDebugEnabled()) {
166  0 logger.debug("invoke(" + ejb + ", " + method + ", " + params + ") - finish with result=" + result);
167    }
168  0 return result;
169    }
170    }
171    }