1 17 18 21 package org.quartz.jobs.ee.ejb; 22 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.rmi.RemoteException ; 26 import java.util.Hashtable ; 27 28 import javax.ejb.EJBHome ; 29 import javax.ejb.EJBMetaData ; 30 import javax.ejb.EJBObject ; 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.rmi.PortableRemoteObject ; 35 36 import org.quartz.Job; 37 import org.quartz.JobDataMap; 38 import org.quartz.JobDetail; 39 import org.quartz.JobExecutionContext; 40 import org.quartz.JobExecutionException; 41 42 83 public class EJBInvokerJob implements Job { 84 85 92 93 public static final String EJB_JNDI_NAME_KEY = "ejb"; 94 95 public static final String EJB_METHOD_KEY = "method"; 96 97 public static final String EJB_ARG_TYPES_KEY = "argTypes"; 98 99 public static final String EJB_ARGS_KEY = "args"; 100 101 public static final String INITIAL_CONTEXT_FACTORY = "java.naming.factory.initial"; 102 103 public static final String PROVIDER_URL = "java.naming.provider.url"; 104 105 public static final String PRINCIPAL = "java.naming.security.principal"; 106 107 public static final String CREDENTIALS = "java.naming.security.credentials"; 108 109 110 117 118 public EJBInvokerJob() { 119 } 121 122 129 130 public void execute(JobExecutionContext context) 131 throws JobExecutionException { 132 JobDetail detail = context.getJobDetail(); 133 134 JobDataMap dataMap = context.getMergedJobDataMap(); 135 136 String ejb = dataMap.getString(EJB_JNDI_NAME_KEY); 137 String method = dataMap.getString(EJB_METHOD_KEY); 138 Object [] arguments = (Object []) dataMap.get(EJB_ARGS_KEY); 139 if (arguments == null) { 140 arguments = new Object [0]; 141 } 142 143 if (ejb == null) { 144 throw new JobExecutionException(); 146 } 147 148 InitialContext jndiContext = null; 149 150 try { 152 jndiContext = getInitialContext(dataMap); 153 } catch (NamingException ne) { 154 throw new JobExecutionException(ne); 155 } 156 157 try { 158 Object value = null; 159 160 try { 162 value = jndiContext.lookup(ejb); 163 } catch (NamingException ne) { 164 throw new JobExecutionException(ne); 165 } 166 167 EJBHome ejbHome = (EJBHome ) PortableRemoteObject.narrow(value, 169 EJBHome .class); 170 171 EJBMetaData metaData = null; 173 174 try { 175 metaData = ejbHome.getEJBMetaData(); 176 } catch (RemoteException re) { 177 throw new JobExecutionException(re); 178 } 179 180 Class homeClass = metaData.getHomeInterfaceClass(); 182 183 Class remoteClass = metaData.getRemoteInterfaceClass(); 185 186 ejbHome = (EJBHome ) PortableRemoteObject.narrow(ejbHome, homeClass); 188 189 Method methodCreate = null; 190 191 try { 192 methodCreate = homeClass.getMethod("create", ((Class [])null)); 194 } catch (NoSuchMethodException nsme) { 195 throw new JobExecutionException(nsme); 196 } 197 198 EJBObject remoteObj = null; 200 201 try { 202 remoteObj = (EJBObject ) methodCreate.invoke(ejbHome, ((Object [])null)); 204 } catch (IllegalAccessException iae) { 205 throw new JobExecutionException(iae); 206 } catch (InvocationTargetException ite) { 207 throw new JobExecutionException(ite); 208 } 209 210 Method methodExecute = null; 212 213 try { 214 216 Class [] argTypes = (Class []) dataMap.get(EJB_ARG_TYPES_KEY); 217 if (argTypes == null) { 218 argTypes = new Class [arguments.length]; 219 for (int i = 0; i < arguments.length; i++) { 220 argTypes[i] = arguments[i].getClass(); 221 } 222 } 223 224 methodExecute = remoteClass.getMethod(method, argTypes); 226 } catch (NoSuchMethodException nsme) { 227 throw new JobExecutionException(nsme); 228 } 229 230 try { 231 Object returnObj = methodExecute.invoke(remoteObj, arguments); 233 234 context.setResult(returnObj); 237 } catch (IllegalAccessException iae) { 238 throw new JobExecutionException(iae); 239 } catch (InvocationTargetException ite) { 240 throw new JobExecutionException(ite); 241 } 242 } finally { 243 if (jndiContext != null) { 247 try { 248 jndiContext.close(); 249 } catch (NamingException e) { 250 } 252 } 253 } 254 } 255 256 private InitialContext getInitialContext(JobDataMap jobDataMap) 257 throws NamingException { 258 Hashtable params = new Hashtable (2); 259 260 String initialContextFactory = 261 jobDataMap.getString(INITIAL_CONTEXT_FACTORY); 262 if (initialContextFactory != null) { 263 params.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 264 } 265 266 String providerUrl = jobDataMap.getString(PROVIDER_URL); 267 if (providerUrl != null) { 268 params.put(Context.PROVIDER_URL, providerUrl); 269 } 270 271 String principal = jobDataMap.getString(PRINCIPAL); 272 if ( principal != null ) { 273 params.put( Context.SECURITY_PRINCIPAL, principal ); 274 } 275 276 String credentials = jobDataMap.getString(CREDENTIALS); 277 if ( credentials != null ) { 278 params.put( Context.SECURITY_CREDENTIALS, credentials ); 279 } 280 281 return (params.size() == 0) ? new InitialContext () : new InitialContext (params); 282 } 283 } | Popular Tags |