| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 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 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@author |
| 49 |
|
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 10 |
Complexity Density: 0,5 |
|
| 50 |
|
public class EnterpriseDomainBuilder extends Domain { |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
private final static Log logger = LogFactory.getLog(EnterpriseDomainBuilder.class); |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
private final static Map homes = Collections.synchronizedMap(new HashMap()); |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@param |
| 66 |
|
@param |
| 67 |
|
@return |
| 68 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 69 |
0
|
public static Object getInstance(String name, Class type) {... |
| 70 |
0
|
return EnterpriseDomainBuilder.getInstance(name, type, null, null); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@param |
| 76 |
|
@param |
| 77 |
|
@param |
| 78 |
|
@return |
| 79 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 80 |
0
|
public static Object getInstance(String name, Class type, Context context) {... |
| 81 |
0
|
return EnterpriseDomainBuilder.getInstance(name, type, context, null); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
@param |
| 87 |
|
@param |
| 88 |
|
@param |
| 89 |
|
@param |
| 90 |
|
@return |
| 91 |
|
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 8 |
Complexity Density: 0,44 |
|
| 92 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 5 |
Complexity Density: 0,56 |
|
| 127 |
|
private static class EnterpriseDomain implements InvocationHandler { |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
private static final Log logger = LogFactory.getLog(EnterpriseDomain.class); |
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
private Object ejb; |
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
@param |
| 142 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 143 |
0
|
private EnterpriseDomain(Object ejb) {... |
| 144 |
0
|
this.ejb = ejb; |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
@see |
| 149 |
|
|
| 150 |
|
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0,5 |
|
| 151 |
0
|
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 |
|
} |