| /*************************************************************************************************** |
| * Copyright (c) 2005 Eteration A.S. and others. All rights reserved. This program and the |
| * accompanying materials are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: Eteration A.S. - initial API and implementation |
| **************************************************************************************************/ |
| |
| |
| package org.eclipse.jst.j2ee.ejb.annotation.internal.operations; |
| |
| import java.lang.reflect.InvocationTargetException; |
| |
| import org.eclipse.core.resources.ICommand; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IProjectDescription; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IConfigurationElement; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.jst.j2ee.ejb.DestinationType; |
| import org.eclipse.jst.j2ee.ejb.EjbFactory; |
| import org.eclipse.jst.j2ee.ejb.MessageDriven; |
| import org.eclipse.jst.j2ee.ejb.MessageDrivenDestination; |
| import org.eclipse.jst.j2ee.ejb.TransactionType; |
| import org.eclipse.jst.j2ee.ejb.annotation.internal.model.BeanFactory; |
| import org.eclipse.jst.j2ee.ejb.annotation.internal.model.EjbCommonDataModel; |
| import org.eclipse.jst.j2ee.ejb.annotation.internal.model.MessageDrivenBeanDataModel; |
| import org.eclipse.jst.j2ee.ejb.annotations.internal.classgen.EjbBuilder; |
| import org.eclipse.jst.j2ee.ejb.annotations.internal.emitter.EjbEmitter; |
| import org.eclipse.jst.j2ee.ejb.annotations.internal.emitter.MessageDrivenEjbEmitter; |
| import org.eclipse.jst.j2ee.ejb.annotations.internal.emitter.model.IMessageDrivenBeanDelegate; |
| import org.eclipse.jst.j2ee.internal.common.operations.NewJavaClassDataModel; |
| import org.eclipse.wst.common.frameworks.internal.operations.WTPOperation; |
| |
| |
| |
| public class AddMessageDrivenBeanOperation extends WTPOperation { |
| /** |
| * @param dataModel |
| */ |
| public AddMessageDrivenBeanOperation(EjbCommonDataModel dataModel) { |
| super(dataModel); |
| } |
| |
| protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
| createDefaultSessionBean(monitor); |
| |
| } |
| |
| private void createDefaultSessionBean(IProgressMonitor monitor) { |
| |
| MessageDrivenBeanDataModel ejbModel = (MessageDrivenBeanDataModel)this.getOperationDataModel(); |
| NewJavaClassDataModel ejbClassModel = (NewJavaClassDataModel)ejbModel.getNestedModel("NewEJBJavaClassDataModel"); |
| |
| MessageDriven mdBean = EjbFactory.eINSTANCE.createMessageDriven(); |
| mdBean.setName(ejbModel.getStringProperty(EjbCommonDataModel.EJB_NAME)); |
| mdBean.setDescription(ejbModel.getStringProperty(EjbCommonDataModel.DESCRIPTION)); |
| mdBean.setDisplayName(ejbModel.getStringProperty(EjbCommonDataModel.DISPLAY_NAME)); |
| mdBean.setEjbClassName(ejbClassModel.getQualifiedClassName()); |
| |
| String destType = ejbModel.getStringProperty(MessageDrivenBeanDataModel.DESTINATIONTYPE); |
| DestinationType dType = DestinationType.QUEUE_LITERAL; |
| if( destType.equals(DestinationType.TOPIC_LITERAL.getName())) |
| dType = DestinationType.TOPIC_LITERAL; |
| |
| MessageDrivenDestination destination = EjbFactory.eINSTANCE.createMessageDrivenDestination(); |
| destination.setType(dType); |
| destination.setBean(mdBean); |
| |
| mdBean.setDestination(destination); |
| mdBean.setMessageSelector(ejbModel.getStringProperty(MessageDrivenBeanDataModel.DESTINATIONNAME)); |
| |
| String tType = ejbModel.getStringProperty(MessageDrivenBeanDataModel.TRANSACTIONTYPE); |
| TransactionType transactionType =TransactionType.CONTAINER_LITERAL; |
| if(tType.equals(TransactionType.BEAN_LITERAL.getName())) |
| transactionType = TransactionType.BEAN_LITERAL; |
| mdBean.setTransactionType(transactionType); |
| |
| IMessageDrivenBeanDelegate delegate =BeanFactory.getDelegate(mdBean, ejbModel); |
| |
| |
| try { |
| IConfigurationElement[] configurationElements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.jst.j2ee.ejb.annotations.internal.emitter.model.emitter.template"); |
| String builderId = configurationElements[0].getAttribute("builderId"); |
| addToEndOfBuildSpec( ejbClassModel.getTargetProject(), configurationElements[0].getNamespace() + "."+builderId); |
| EjbEmitter ejbEmitter = new MessageDrivenEjbEmitter(configurationElements[0]); |
| String fields = ejbEmitter.emitFields(delegate); |
| String comment = ejbEmitter.emitTypeComment(delegate); |
| String stub = ejbEmitter.emitTypeStub(delegate); |
| String method = ejbEmitter.emitInterfaceMethods(delegate); |
| String className = mdBean.getEjbClassName(); |
| |
| |
| |
| EjbBuilder ejbBuilder = new EjbBuilder(); |
| ejbBuilder.setConfigurationElement(configurationElements[0]); |
| ejbBuilder.setMonitor(monitor); |
| ejbBuilder.setPackageFragmentRoot(ejbClassModel.getJavaPackageFragmentRoot()); |
| ejbBuilder.setEnterpriseBeanDelegate(delegate); |
| ejbBuilder.setTypeName(ejbClassModel.getStringProperty(NewJavaClassDataModel.CLASS_NAME)); |
| ejbBuilder.setPackageName(ejbClassModel.getStringProperty(NewJavaClassDataModel.JAVA_PACKAGE)); |
| |
| ejbBuilder.setTypeComment(comment); |
| ejbBuilder.setTypeStub(stub); |
| ejbBuilder.setMethodStub(method); |
| ejbBuilder.setFields(fields); |
| |
| ejbBuilder.createType(); |
| |
| } catch (ClassNotFoundException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } catch (InstantiationException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } catch (IllegalAccessException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } catch (CoreException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } catch (InterruptedException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| |
| } |
| |
| /** |
| * Adds a builder to the build spec for the given project. |
| */ |
| protected void addToEndOfBuildSpec(IProject project, String builderID) throws CoreException { |
| IProjectDescription description = project.getDescription(); |
| ICommand[] commands = description.getBuildSpec(); |
| boolean found = false; |
| for (int i = 0; i < commands.length; ++i) { |
| if (commands[i].getBuilderName().equals(builderID)) { |
| found = true; |
| break; |
| } |
| } |
| if (!found) { |
| ICommand command = description.newCommand(); |
| command.setBuilderName(builderID); |
| ICommand[] newCommands = new ICommand[commands.length + 1]; |
| System.arraycopy(commands, 0, newCommands, 0, commands.length); |
| newCommands[commands.length] = command; |
| IProjectDescription desc = project.getDescription(); |
| desc.setBuildSpec(newCommands); |
| project.setDescription(desc, null); |
| } |
| } |
| |
| } |