List of usage examples for javax.resource.spi ResourceAdapter start
void start(BootstrapContext ctx) throws ResourceAdapterInternalException;
From source file:com.app.server.EJBDeployer.java
public void deployEjbJar(URL url, StandardFileSystemManager manager, ClassLoader cL) { try {/*w ww. j a v a 2s . c o m*/ Vector<EJBContext> ejbContexts = null; EJBContext ejbContext; log.info(url.toURI()); ConcurrentHashMap<String, RARArchiveData> rardata = (ConcurrentHashMap<String, RARArchiveData>) mbeanServer .getAttribute(rarDeployerName, "RARArchiveDataAllAdapters"); Collection<RARArchiveData> rarcls = rardata.values(); Iterator<RARArchiveData> rarcl = rarcls.iterator(); RARArchiveData rararcdata; FileObject filetoScan = manager.resolveFile("jar:" + url.toString() + "!/"); HashSet<Class<?>>[] classes = new HashSet[] { new HashSet(), new HashSet(), new HashSet() }; VFSClassLoader jarCL; if (cL != null) { jarCL = new VFSClassLoader(new FileObject[] { filetoScan }, manager, cL); } else { jarCL = new VFSClassLoader(new FileObject[] { filetoScan }, manager, Thread.currentThread().getContextClassLoader()); } Class[] annot = new Class[] { Stateless.class, Stateful.class, MessageDriven.class }; scanJar(filetoScan, classes, annot, jarCL); Set<Class<?>> clsStateless = classes[0]; Set<Class<?>> clsStateful = classes[1]; Set<Class<?>> clsMessageDriven = classes[2]; //System.gc(); staticObjs = null; EJBContainer container = EJBContainer.getInstance(classes); container.inject(); if (clsStateless.size() > 0) { staticObjs = new Vector<Object>(); ejbContexts = new Vector<EJBContext>(); ejbContext = new EJBContext(); ejbContext.setJarPath(url.toString()); ejbContext.setJarDeployed(url.toString()); for (Class<?> ejbInterface : clsStateless) { BeanPool.getInstance().create(ejbInterface); obj = BeanPool.getInstance().get(ejbInterface); System.out.println(obj); ProxyFactory factory = new ProxyFactory(); proxyobj = factory.createWithBean(obj); staticObjs.add(proxyobj); Object unicastobj = UnicastRemoteObject.exportObject((Remote) proxyobj, 0); String remoteBinding = container.getRemoteBinding(ejbInterface); System.out.println(remoteBinding + " for EJB" + obj); if (remoteBinding != null) { // registry.unbind(remoteBinding); ic.bind("java:/" + remoteBinding, (Remote) unicastobj); ejbContext.put(remoteBinding, obj.getClass()); //registry.rebind(remoteBinding, (Remote)unicastobj); } // registry.rebind("name", (Remote) obj); } ejbContexts.add(ejbContext); jarEJBMap.put(url.toString(), ejbContexts); } if (clsStateful.size() > 0) { if (staticObjs == null) { staticObjs = new Vector<Object>(); } if (ejbContexts == null) { ejbContexts = new Vector<EJBContext>(); } ejbContext = new EJBContext(); ejbContext.setJarPath(url.toString()); ejbContext.setJarDeployed(url.toString()); StatefulBeanObject statefulBeanObject = null; for (Class<?> ejbInterface : clsStateful) { BeanPool.getInstance().create(ejbInterface); obj1 = ejbInterface.newInstance(); if (statefulBeanObject == null) { statefulBeanObject = new StatefulBeanObject(obj1, url.toString()); } else { statefulBeanObject.addStatefulSessionBeanObject(obj1); } //System.out.println(obj1); staticObjs.add(statefulBeanObject); /*Object unicastobj1 = UnicastRemoteObject.exportObject( (Remote) obj1, 0);*/ String remoteBinding = container.getRemoteBinding(ejbInterface); System.out.println(remoteBinding + " for EJB" + statefulBeanObject); if (remoteBinding != null) { // registry.unbind(remoteBinding); ic.bind("java:/" + remoteBinding, statefulBeanObject); ejbContext.put(remoteBinding, statefulBeanObject.getClass()); //registry.rebind(remoteBinding, (Remote)unicastobj1); } // registry.rebind("name", (Remote) obj); } ejbContexts.add(ejbContext); jarEJBMap.put(url.toString(), ejbContexts); } if (clsMessageDriven.size() > 0) { MDBContext mdbContext = null; ConcurrentHashMap<String, MDBContext> mdbContexts; if (jarMDBMap.get(url.toString()) != null) { mdbContexts = jarMDBMap.get(url.toString()); } else { mdbContexts = new ConcurrentHashMap<String, MDBContext>(); } jarMDBMap.put(url.toString(), mdbContexts); MDBContext mdbContextOld; for (Class<?> mdbBean : clsMessageDriven) { String classwithpackage = mdbBean.getName(); //System.out.println("class package" + classwithpackage); classwithpackage = classwithpackage.replace("/", "."); //System.out.println("classList:" // + classwithpackage.replace("/", ".")); final Class mdbBeanCls = mdbBean; try { if (!classwithpackage.contains("$")) { // System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass); // System.out.println(); if (!mdbBean.isInterface()) { Annotation[] classServicesAnnot = mdbBean.getDeclaredAnnotations(); if (classServicesAnnot != null) { Adapter adapter = null; ActivationConfigProperty[] activationConfigProp = null; for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) { if (classServicesAnnot[annotcount] instanceof Adapter) { adapter = (Adapter) classServicesAnnot[annotcount]; } else if (classServicesAnnot[annotcount] instanceof MessageDriven) { MessageDriven messageDrivenAnnot = (MessageDriven) classServicesAnnot[annotcount]; activationConfigProp = messageDrivenAnnot.activationConfig(); mdbContext = new MDBContext(); mdbContext.setMdbName(messageDrivenAnnot.name()); } } if (adapter != null) { /*if(rarcl.hasNext()){ rararcdata=rardata.get(rarDeployerName); rararcdata=rarcl.next(); //classLoader = new VFSClassLoader(rararcdata.getVfsClassLoader().getFileObjects(),rararcdata.getFsManager(),classLoader); FileObject[] fileObjectArray=rararcdata.getVfsClassLoader().getFileObjects(); //urlset.addAll(ClasspathHelper.forClassLoader(rararcdata.getVfsClassLoader())); for(FileObject fileObject:fileObjectArray){ fileObjects.add(fileObject.getURL()); //urlset.add(fileObject.getURL()); //System.out.println(Vfs.fromURL(fileObject.getURL()).getFiles().iterator().next().getRelativePath()); } classLoader = new URLClassLoader(fileObjects.toArray(new URL[fileObjects.size()]),Thread.currentThread().getContextClassLoader()); }*/ RARArchiveData rarArchiveData = (RARArchiveData) mbeanServer.invoke( rarDeployerName, "getRARArchiveData", new Object[] { adapter.adapterName() }, new String[] { String.class.getName() }); if (rarArchiveData == null) throw new Exception("RAR Adapter " + adapter.adapterName() + " Not found in deploy folder"); Class resourceAdapterClass = rarArchiveData.getResourceAdapterClass(); final ResourceAdapter resourceAdapter = (ResourceAdapter) resourceAdapterClass .newInstance(); Class activationSpecClass = rarArchiveData.getActivationspecclass(); final ActivationSpec activationSpec = (ActivationSpec) activationSpecClass .newInstance(); Vector<ConfigProperty> configProperties = rarArchiveData.getConfigPropery(); Integer configPropertyInteger; Long configPropertyLong; Boolean configPropertyBoolean; Method method; if (configProperties != null) { for (ConfigProperty configProperty : configProperties) { String property = configProperty.getConfigpropertyname(); property = (property.charAt(0) + "").toUpperCase() + property.substring(1); Class propertytype = Class .forName(configProperty.getConfigpropertytype()); try { method = activationSpecClass.getMethod("set" + property, propertytype); if (propertytype == String.class) { method.invoke(activationSpec, configProperty.getConfigpropertyvalue()); ConfigResourceAdapter(resourceAdapterClass, propertytype, resourceAdapter, property, configProperty); } else if (propertytype == Integer.class) { if (configProperty.getConfigpropertyvalue() != null && !configProperty.getConfigpropertyvalue() .equalsIgnoreCase("")) { configPropertyInteger = new Integer( configProperty.getConfigpropertyvalue()); try { method.invoke(activationSpec, configPropertyInteger); } catch (Exception ex) { method.invoke(activationSpec, configPropertyInteger.intValue()); } ConfigResourceAdapter(resourceAdapterClass, propertytype, resourceAdapter, property, configProperty); } } else if (propertytype == Long.class) { if (configProperty.getConfigpropertyvalue() != null && !configProperty.getConfigpropertyvalue() .equalsIgnoreCase("")) { configPropertyLong = new Long( configProperty.getConfigpropertyvalue()); method.invoke(activationSpec, configPropertyLong); ConfigResourceAdapter(resourceAdapterClass, propertytype, resourceAdapter, property, configProperty); } } else if (propertytype == Boolean.class) { if (configProperty.getConfigpropertyvalue() != null && !configProperty.getConfigpropertyvalue() .equalsIgnoreCase("")) { configPropertyBoolean = new Boolean( configProperty.getConfigpropertyvalue()); method.invoke(activationSpec, configPropertyBoolean); ConfigResourceAdapter(resourceAdapterClass, propertytype, resourceAdapter, property, configProperty); } } } catch (Exception ex) { try { if (propertytype == Integer.class) { method = activationSpecClass.getMethod("set" + property, int.class); if (configProperty.getConfigpropertyvalue() != null && !configProperty.getConfigpropertyvalue() .equalsIgnoreCase("")) { method = activationSpecClass .getMethod("set" + property, int.class); configPropertyInteger = new Integer( configProperty.getConfigpropertyvalue()); method.invoke(activationSpec, configPropertyInteger.intValue()); //ConfigResourceAdapter(resourceAdapterClass,propertytype,resourceAdapter,property,configProperty); } } else if (propertytype == Long.class) { if (configProperty.getConfigpropertyvalue() != null && !configProperty.getConfigpropertyvalue() .equalsIgnoreCase("")) { method = activationSpecClass .getMethod("set" + property, long.class); configPropertyLong = new Long( configProperty.getConfigpropertyvalue()); method.invoke(activationSpec, configPropertyLong.longValue()); //ConfigResourceAdapter(resourceAdapterClass,propertytype,resourceAdapter,property,configProperty); } } else if (propertytype == Boolean.class) { if (configProperty.getConfigpropertyvalue() != null && !configProperty.getConfigpropertyvalue() .equalsIgnoreCase("")) { method = activationSpecClass .getMethod("set" + property, boolean.class); configPropertyBoolean = new Boolean( configProperty.getConfigpropertyvalue()); method.invoke(activationSpec, configPropertyBoolean.booleanValue()); //ConfigResourceAdapter(resourceAdapterClass,propertytype,resourceAdapter,property,configProperty); } } ConfigResourceAdapter(resourceAdapterClass, propertytype, resourceAdapter, property, configProperty); } catch (Exception ex1) { ConfigResourceAdapter(resourceAdapterClass, propertytype, resourceAdapter, property, configProperty); } //log.error("Could not set Configuration for rar activation spec", ex); } } } for (ActivationConfigProperty activationConfig : activationConfigProp) { String property = activationConfig.propertyName(); property = (property.charAt(0) + "").toUpperCase() + property.substring(1); try { method = activationSpecClass.getMethod("set" + property, String.class); method.invoke(activationSpec, activationConfig.propertyValue()); } catch (Exception ex) { try { method = activationSpecClass.getMethod("set" + property, boolean.class); method.invoke(activationSpec, new Boolean(activationConfig.propertyValue())); } catch (Exception ex1) { method = activationSpecClass.getMethod("set" + property, Boolean.class); method.invoke(activationSpec, new Boolean(activationConfig.propertyValue())); } } } final Class listenerClass = rarArchiveData.getMessagelistenertype(); ClassLoader cCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread() .setContextClassLoader(resourceAdapter.getClass().getClassLoader()); resourceAdapter .start(new com.app.server.connector.impl.BootstrapContextImpl()); MessageEndPointFactoryImpl messageEndPointFactoryImpl = new MessageEndPointFactoryImpl( listenerClass, mdbBeanCls.newInstance(), jarCL); resourceAdapter.endpointActivation(messageEndPointFactoryImpl, activationSpec); Thread.currentThread().setContextClassLoader(cCL); if (mdbContext != null) { mdbContext.setResourceAdapter(resourceAdapter); mdbContext.setMessageEndPointFactory(messageEndPointFactoryImpl); mdbContext.setActivationSpec(activationSpec); mdbContexts.put(mdbContext.getMdbName(), mdbContext); } /*new Thread(){ public void run(){ try { resourceAdapter.endpointActivation(new com.app.server.connector.impl.MessageEndPointFactoryImpl(listenerClass, mdbBeanCls.newInstance(),raCl), activationSpec); } catch ( Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start();*/ } else { for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) { if (classServicesAnnot[annotcount] instanceof MessageDriven) { MessageDriven messageDrivenAnnot = (MessageDriven) classServicesAnnot[annotcount]; ActivationConfigProperty[] activationConfigProperties = messageDrivenAnnot .activationConfig(); mdbContext = new MDBContext(); mdbContext.setMdbName(messageDrivenAnnot.name()); for (ActivationConfigProperty activationConfigProperty : activationConfigProperties) { if (activationConfigProperty.propertyName() .equals(MDBContext.DESTINATIONTYPE)) { mdbContext.setDestinationType( activationConfigProperty.propertyValue()); } else if (activationConfigProperty.propertyName() .equals(MDBContext.DESTINATION)) { mdbContext.setDestination( activationConfigProperty.propertyValue()); } else if (activationConfigProperty.propertyName() .equals(MDBContext.ACKNOWLEDGEMODE)) { mdbContext.setAcknowledgeMode( activationConfigProperty.propertyValue()); } } if (mdbContext.getDestinationType().equals(Queue.class.getName())) { mdbContextOld = null; if (mdbContexts.get(mdbContext.getMdbName()) != null) { mdbContextOld = mdbContexts.get(mdbContext.getMdbName()); if (mdbContextOld != null && mdbContext.getDestination() .equals(mdbContextOld.getDestination())) { throw new Exception( "Only one MDB can listen to destination:" + mdbContextOld.getDestination()); } } mdbContexts.put(mdbContext.getMdbName(), mdbContext); Queue queue = (Queue) jms.lookup(mdbContext.getDestination()); Connection connection = connectionFactory .createConnection("guest", "guest"); connection.start(); Session session; if (mdbContext.getAcknowledgeMode() != null && mdbContext .getAcknowledgeMode().equals("Auto-Acknowledge")) { session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); } else { session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); } MessageConsumer consumer = session.createConsumer(queue); consumer.setMessageListener( (MessageListener) mdbBean.newInstance()); mdbContext.setConnection(connection); mdbContext.setSession(session); mdbContext.setConsumer(consumer); System.out.println("Queue=" + queue); } else if (mdbContext.getDestinationType() .equals(Topic.class.getName())) { if (mdbContexts.get(mdbContext.getMdbName()) != null) { mdbContextOld = mdbContexts.get(mdbContext.getMdbName()); if (mdbContextOld.getConsumer() != null) mdbContextOld.getConsumer().setMessageListener(null); if (mdbContextOld.getSession() != null) mdbContextOld.getSession().close(); if (mdbContextOld.getConnection() != null) mdbContextOld.getConnection().close(); } mdbContexts.put(mdbContext.getMdbName(), mdbContext); Topic topic = (Topic) jms.lookup(mdbContext.getDestination()); Connection connection = connectionFactory .createConnection("guest", "guest"); connection.start(); Session session; if (mdbContext.getAcknowledgeMode() != null && mdbContext .getAcknowledgeMode().equals("Auto-Acknowledge")) { session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); } else { session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); } MessageConsumer consumer = session.createConsumer(topic); consumer.setMessageListener( (MessageListener) mdbBean.newInstance()); mdbContext.setConnection(connection); mdbContext.setSession(session); mdbContext.setConsumer(consumer); System.out.println("Topic=" + topic); } } } } } } } } catch (Exception e) { log.error("Error : ", e); // TODO Auto-generated catch block //e.printStackTrace(); } } } if (staticObjs != null) { staticObjsEjbMap.put(url.toString(), staticObjs); } this.jarsDeployed.add(url.toURI().toString()); log.info(url.toString() + " Deployed"); } catch (Exception ex) { log.error("Error in deploying the jar file: " + url, ex); //ex.printStackTrace(); } finally { /*if(classLoader!=null){ try { classLoader.close(); } catch (Exception e) { log.error("error in closing the classloader", e); // TODO Auto-generated catch block //e.printStackTrace(); } ClassLoaderUtil.closeClassLoader(classLoader); }*/ } }
From source file:org.apache.openejb.assembler.classic.Assembler.java
public void createResource(ResourceInfo serviceInfo) throws OpenEJBException { ObjectRecipe serviceRecipe = createRecipe(serviceInfo); serviceRecipe.setProperty("transactionManager", transactionManager); serviceRecipe.setProperty("properties", new UnsetPropertiesRecipe()); replaceResourceAdapterProperty(serviceRecipe); Object service = serviceRecipe.create(); // Java Connector spec ResourceAdapters and ManagedConnectionFactories need special activation if (service instanceof ResourceAdapter) { ResourceAdapter resourceAdapter = (ResourceAdapter) service; // Create a thead pool for work manager int threadPoolSize = getIntProperty(serviceInfo.properties, "threadPoolSize", 30); Executor threadPool;/*from w w w. ja v a 2 s.c o m*/ if (threadPoolSize <= 0) { threadPool = Executors.newCachedThreadPool(new ResourceAdapterThreadFactory(serviceInfo.id)); } else { threadPool = Executors.newFixedThreadPool(threadPoolSize, new ResourceAdapterThreadFactory(serviceInfo.id)); } // WorkManager: the resource adapter can use this to dispatch messages or perform tasks WorkManager workManager; if (transactionManager instanceof GeronimoTransactionManager) { GeronimoTransactionManager geronimoTransactionManager = (GeronimoTransactionManager) transactionManager; TransactionContextHandler txWorkContextHandler = new TransactionContextHandler( geronimoTransactionManager); // use id as default realm name if realm is not specified in service properties String securityRealmName = getStringProperty(serviceInfo.properties, "realm", serviceInfo.id); SecurityContextHandler securityContextHandler = new SecurityContextHandler(securityRealmName); HintsContextHandler hintsContextHandler = new HintsContextHandler(); Collection<WorkContextHandler> workContextHandlers = new ArrayList<WorkContextHandler>(); workContextHandlers.add(txWorkContextHandler); workContextHandlers.add(securityContextHandler); workContextHandlers.add(hintsContextHandler); workManager = new GeronimoWorkManager(threadPool, threadPool, threadPool, workContextHandlers); } else { workManager = new SimpleWorkManager(threadPool); } // BootstrapContext: wraps the WorkMananger and XATerminator BootstrapContext bootstrapContext; if (transactionManager instanceof GeronimoTransactionManager) { bootstrapContext = new GeronimoBootstrapContext((GeronimoWorkManager) workManager, (GeronimoTransactionManager) transactionManager, (GeronimoTransactionManager) transactionManager); } else if (transactionManager instanceof XATerminator) { bootstrapContext = new SimpleBootstrapContext(workManager, (XATerminator) transactionManager); } else { bootstrapContext = new SimpleBootstrapContext(workManager); } // start the resource adapter try { logger.debug("createResource.startingResourceAdapter", serviceInfo.id, service.getClass().getName()); resourceAdapter.start(bootstrapContext); } catch (ResourceAdapterInternalException e) { throw new OpenEJBException(e); } Map<String, Object> unset = serviceRecipe.getUnsetProperties(); unset.remove("threadPoolSize"); logUnusedProperties(unset, serviceInfo); } else if (service instanceof ManagedConnectionFactory) { ManagedConnectionFactory managedConnectionFactory = (ManagedConnectionFactory) service; // connection manager is constructed via a recipe so we automatically expose all cmf properties ObjectRecipe connectionManagerRecipe = new ObjectRecipe(GeronimoConnectionManagerFactory.class, "create"); connectionManagerRecipe.allow(Option.CASE_INSENSITIVE_PROPERTIES); connectionManagerRecipe.allow(Option.IGNORE_MISSING_PROPERTIES); connectionManagerRecipe.setAllProperties(serviceInfo.properties); connectionManagerRecipe.setProperty("name", serviceInfo.id); connectionManagerRecipe.setProperty("mcf", managedConnectionFactory); // standard properties connectionManagerRecipe.setProperty("transactionManager", transactionManager); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) classLoader = getClass().getClassLoader(); if (classLoader == null) classLoader = ClassLoader.getSystemClassLoader(); connectionManagerRecipe.setProperty("classLoader", classLoader); logger.getChildLogger("service").info("createResource.createConnectionManager", serviceInfo.id, service.getClass().getName()); // create the connection manager ConnectionManager connectionManager = (ConnectionManager) connectionManagerRecipe.create(); if (connectionManager == null) { throw new RuntimeException(messages.format("assembler.invalidConnectionManager", serviceInfo.id)); } Map<String, Object> unsetA = serviceRecipe.getUnsetProperties(); Map<String, Object> unsetB = connectionManagerRecipe.getUnsetProperties(); Map<String, Object> unset = new HashMap<String, Object>(); for (Map.Entry<String, Object> entry : unsetA.entrySet()) { if (unsetB.containsKey(entry.getKey())) unset.put(entry.getKey(), entry.getValue()); } logUnusedProperties(unset, serviceInfo); // service becomes a ConnectorReference which merges connection manager and mcf service = new ConnectorReference(connectionManager, managedConnectionFactory); } else { logUnusedProperties(serviceRecipe, serviceInfo); } try { final String name = OPENEJB_RESOURCE_JNDI_PREFIX + serviceInfo.id; containerSystem.getJNDIContext().bind(name, service); } catch (NamingException e) { throw new OpenEJBException("Cannot bind resource adapter with id " + serviceInfo.id, e); } // Update the config tree config.facilities.resources.add(serviceInfo); logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className); }