Author: bfitzpat
Date: 2010-07-23 13:53:12 -0400 (Fri, 23 Jul 2010)
New Revision: 23710
Added:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/AddRestEasyJarsCommand.java
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSMergeWebXMLCommand.java
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSServiceSampleCreationCommand.java
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizard.java
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardPage.java
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardValidator.java
Modified:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/data/ServiceModel.java
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/messages/JBossWSCreationCore.properties
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/messages/JBossWSCreationCoreMessages.java
trunk/ws/plugins/org.jboss.tools.ws.ui/META-INF/MANIFEST.MF
trunk/ws/plugins/org.jboss.tools.ws.ui/plugin.xml
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/messages/JBossWSUI.properties
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/messages/JBossWSUIMessages.java
Log:
[JBIDE-5723] New wizard for creating RESTful Service sample in Dynamic Web Project
Added:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/AddRestEasyJarsCommand.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/AddRestEasyJarsCommand.java
(rev 0)
+++
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/AddRestEasyJarsCommand.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -0,0 +1,219 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+
+package org.jboss.tools.ws.creation.core.commands;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
+import org.jboss.tools.ws.core.utils.StatusUtils;
+import org.jboss.tools.ws.creation.core.JBossWSCreationCorePlugin;
+import org.jboss.tools.ws.creation.core.data.ServiceModel;
+import org.jboss.tools.ws.creation.core.messages.JBossWSCreationCoreMessages;
+import org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils;
+
+/**
+ * @author Brian Fitzpatrick
+ *
+ * add jars necessary for RestEasy web services
+ */
+public class AddRestEasyJarsCommand extends AbstractDataModelOperation{
+
+ private static final String SCANNOTATION_JAR = "scannotation.jar";
//$NON-NLS-1$
+ private static final String REST_EASY = "RestEasy"; //$NON-NLS-1$
+ private static final String JAXRS_API_JAR = "jaxrs-api.jar"; //$NON-NLS-1$
+ private static final String LIB = "lib"; //$NON-NLS-1$
+ private static final String RESTEASY_JAXRS_JAR = "resteasy-jaxrs.jar";
//$NON-NLS-1$
+ private ServiceModel model;
+ private static String WEB_XML = "web.xml"; //$NON-NLS-1$
+
+ public AddRestEasyJarsCommand(ServiceModel model) {
+ this.model = model;
+ }
+
+ @Override
+ public IStatus execute(IProgressMonitor monitor, IAdaptable info)
+ throws ExecutionException {
+ return executeOverride(monitor);
+ }
+
+ public IStatus executeOverride(IProgressMonitor monitor) {
+ IStatus status = Status.OK_STATUS;
+ IJavaProject project = null;
+ try {
+ project = JBossWSCreationUtils.getJavaProjectByName(model
+ .getWebProjectName());
+ } catch (JavaModelException e) {
+ JBossWSCreationCorePlugin.getDefault().logError(e);
+ return
StatusUtils.errorStatus(JBossWSCreationCoreMessages.Error_Create_Client_Sample);
+ }
+ status = addJars(project);
+ return status;
+ }
+
+ private File findLibDir ( File in ) {
+ File[] children =
+ in.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ if (dir.isDirectory() && name.equals(LIB)) {
+ return true;
+ }
+ return false;
+ }
+ });
+ if (children != null ) {
+ for (int i = 0; i < children.length; i++) {
+ File libDir = (File) children[i];
+ if (libDir.exists() && libDir.isDirectory()) {
+ File[] jars = libDir.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ if (name.equalsIgnoreCase(JAXRS_API_JAR)) {
+ return true;
+ }
+ return false;
+ }
+ });
+ if (jars != null && jars.length > 0) {
+ return libDir;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ private IStatus addJars (IJavaProject project ) {
+ IStatus status = Status.OK_STATUS;
+
+ try {
+ String path =
+ JBossWSCreationUtils.getJBossWSRuntimeLocation(project.getProject());
+ File runtime = new File(path);
+ if (runtime.exists()) {
+ File findJar = findLibDir(runtime);
+ if (findJar == null) {
+ File parent = runtime.getParentFile();
+ if (parent.exists() && parent.isDirectory()) {
+ File[] restEasyDir = parent.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ if (name.equalsIgnoreCase(REST_EASY)) {
+ return true;
+ }
+ return false;
+ }
+ });
+ if (restEasyDir != null && restEasyDir.length > 0) {
+ findJar = findLibDir(restEasyDir[0]);
+ }
+ }
+ }
+ if (findJar == null) {
+ // if it's still null, resteasy's not installed??
+ status =
StatusUtils.errorStatus(JBossWSCreationCoreMessages.AddRestEasyJarsCommand_RestEasy_JARS_Not_Found);
+ } else {
+ File[] jaxrsJar = findJar.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ if (name.equalsIgnoreCase(JAXRS_API_JAR)) {
+ return true;
+ }
+ return false;
+ }
+ });
+ File[] resteasyJar = findJar.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ if (name.equalsIgnoreCase(RESTEASY_JAXRS_JAR)) {
+ return true;
+ }
+ return false;
+ }
+ });
+ File[] scannotationsJar = findJar.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ if (name.equalsIgnoreCase(SCANNOTATION_JAR)) {
+ return true;
+ }
+ return false;
+ }
+ });
+ addJarToClassPath(project, jaxrsJar[0]);
+ addJarToClassPath(project, resteasyJar[0]);
+ addJarToClassPath(project, scannotationsJar[0]);
+ }
+ }
+ } catch (CoreException e) {
+ e.printStackTrace();
+ }
+
+ return status;
+ }
+
+ private IStatus addJarToClassPath ( IJavaProject project, File jar) {
+
+ IStatus status = Status.OK_STATUS;
+ try {
+ copyFileToLibsDir(project.getProject(), jar);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return status;
+ }
+
+ private void copyFileToLibsDir ( IProject pro, File jar ) throws IOException {
+ File file = JBossWSCreationUtils.findFileByPath(WEB_XML,
pro.getLocation().toOSString());
+ if(file != null){
+ String libdir = file.getParentFile().getPath() + File.separatorChar + LIB;
+ File libdirFile = new File(libdir);
+ if (!libdirFile.exists()) {
+ try {
+ libdirFile.createNewFile();
+ } catch (IOException e) {
+ throw e;
+ }
+ }
+ try {
+
+ // Create channel on the source
+ FileChannel srcChannel =
+ new FileInputStream(jar).getChannel();
+
+ // Create channel on the destination
+ FileChannel dstChannel =
+ new FileOutputStream(libdirFile.getAbsolutePath() + File.separatorChar
+ jar.getName()).getChannel();
+
+ // Copy file contents from source to destination
+ dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
+
+ // Close the channels
+ srcChannel.close();
+ dstChannel.close();
+
+ } catch (IOException e) {
+ throw e;
+ }
+ }
+ }
+
+}
Property changes on:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/AddRestEasyJarsCommand.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSMergeWebXMLCommand.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSMergeWebXMLCommand.java
(rev 0)
+++
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSMergeWebXMLCommand.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -0,0 +1,314 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+
+package org.jboss.tools.ws.creation.core.commands;
+
+import java.io.File;
+import java.util.List;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jst.j2ee.model.IModelProvider;
+import org.eclipse.jst.j2ee.model.ModelProviderManager;
+import org.eclipse.jst.javaee.core.JavaeeFactory;
+import org.eclipse.jst.javaee.core.Listener;
+import org.eclipse.jst.javaee.core.ParamValue;
+import org.eclipse.jst.javaee.core.UrlPatternType;
+import org.eclipse.jst.javaee.web.Servlet;
+import org.eclipse.jst.javaee.web.ServletMapping;
+import org.eclipse.jst.javaee.web.WebApp;
+import org.eclipse.jst.javaee.web.WebFactory;
+import org.eclipse.jst.jee.project.facet.ICreateDeploymentFilesDataModelProperties;
+import org.eclipse.jst.jee.project.facet.WebCreateDeploymentFilesDataModelProvider;
+import org.eclipse.wst.common.componentcore.ComponentCore;
+import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
+import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
+import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory;
+import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
+import org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation;
+import org.jboss.tools.ws.core.utils.StatusUtils;
+import org.jboss.tools.ws.creation.core.data.ServiceModel;
+import org.jboss.tools.ws.creation.core.data.ServletDescriptor;
+import org.jboss.tools.ws.creation.core.messages.JBossWSCreationCoreMessages;
+import org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils;
+
+/**
+ * @author Brian Fitzpatrick
+ */
+public class RSMergeWebXMLCommand extends AbstractDataModelOperation {
+
+ private ServiceModel model;
+ IStatus status;
+ private static String WEB_XML = "web.xml"; //$NON-NLS-1$
+ private static String RE_LISTENER =
"org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap"; //$NON-NLS-1$
+ public static String RS_APPLICATION_PARM_NAME =
"javax.ws.rs.Application";//$NON-NLS-1$
+ private static String RE_SERVLET_NAME = "Resteasy";//$NON-NLS-1$
+ private static String RE_SERVLET_CLASS =
"org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher";//$NON-NLS-1$
+ private static String RE_SERVLET_MAPPING = "/*";//$NON-NLS-1$
+
+ public RSMergeWebXMLCommand(ServiceModel model) {
+ this.model = model;
+ }
+
+ @Override
+ public IStatus execute(IProgressMonitor monitor, IAdaptable info)
+ throws ExecutionException {
+ status = Status.OK_STATUS;
+ if (!model.isUpdateWebxml()) {
+ return status;
+ }
+
+ ServletDescriptor[] servletDescriptors = new ServletDescriptor[model
+ .getServiceClasses().size()];
+ List<String> serviceClasses = model.getServiceClasses();
+ for (int i = 0; i < serviceClasses.size(); i++) {
+ servletDescriptors[i] = getServletDescriptor(serviceClasses.get(i));
+ }
+ IProject pro = JBossWSCreationUtils.getProjectByName(model.getWebProjectName());
+ if (!hasWebXML(pro)) {
+ IVirtualComponent vc = ComponentCore.createComponent(pro);
+ IDataModel model = DataModelFactory.createDataModel(new
WebCreateDeploymentFilesDataModelProvider());
+ model.setProperty(ICreateDeploymentFilesDataModelProperties.GENERATE_DD, vc);
+ model.setProperty(ICreateDeploymentFilesDataModelProperties.TARGET_PROJECT, pro);
+ IDataModelOperation op = model.getDefaultOperation();
+ try {
+ op.execute(new NullProgressMonitor(), null);
+ } catch (ExecutionException e1) {
+ // Ignore
+ }
+ }
+ mergeWebXML(servletDescriptors);
+ return status;
+ }
+
+ private void mergeWebXML(final ServletDescriptor[] servletDescriptors) {
+ final IModelProvider provider = ModelProviderManager
+ .getModelProvider(JBossWSCreationUtils.getProjectByName(model
+ .getWebProjectName()));
+ provider.modify(new Runnable() {
+ public void run() {
+ Object object = provider.getModelObject();
+ if (object instanceof WebApp) {
+ WebApp webApp = (WebApp) object;
+ for (int i = 0; i < servletDescriptors.length; i++) {
+ addjeeServlet(JBossWSCreationUtils
+ .getProjectByName(model.getWebProjectName()),
+ servletDescriptors[i], webApp);
+ addContextParams (model.getCustomPackage() + '.' +
+ model.getApplicationClassName(), webApp);
+ }
+ addRESTListener(RE_LISTENER, webApp);
+ }
+ if (object instanceof org.eclipse.jst.j2ee.webapplication.WebApp) {
+ org.eclipse.jst.j2ee.webapplication.WebApp webApp =
(org.eclipse.jst.j2ee.webapplication.WebApp) object;
+ for (int i = 0; i < servletDescriptors.length; i++) {
+ addServlet(JBossWSCreationUtils.getProjectByName(model
+ .getWebProjectName()), servletDescriptors[i],
+ webApp);
+ addContextParams (model.getCustomPackage() + '.' +
+ model.getApplicationClassName(), webApp);
+ }
+ addRESTListener(RE_LISTENER, webApp);
+ }
+ }
+
+ }, null);
+ }
+
+ protected void addRESTListener ( String listenerClass, WebApp webapp ) {
+ List<Listener> theListeners = webapp.getListeners();
+ for (int i = 0; i < theListeners.size(); i++) {
+ Listener listener = (Listener) theListeners.get(i);
+ if (listener.getListenerClass().equals(listenerClass)) {
+ status = Status.OK_STATUS;
+ return;
+ }
+ }
+ Listener listener = JavaeeFactory.eINSTANCE.createListener();
+ listener.setListenerClass(listenerClass);
+ webapp.getListeners().add(listener);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void addRESTListener ( String listenerClass,
org.eclipse.jst.j2ee.webapplication.WebApp webapp ) {
+ List<Listener> theListeners = webapp.getListeners();
+ for (int i = 0; i < theListeners.size(); i++) {
+ Listener listener = (Listener) theListeners.get(i);
+ if (listener.getListenerClass().equals(listenerClass)) {
+ status = Status.OK_STATUS;
+ return;
+ }
+ }
+ Listener listener = JavaeeFactory.eINSTANCE.createListener();
+ listener.setListenerClass(listenerClass);
+ webapp.getListeners().add(listener);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void addContextParams ( String className,
org.eclipse.jst.j2ee.webapplication.WebApp webapp) {
+ List<ParamValue> theContextParams = webapp.getContextParams();
+ for (int i = 0; i < theContextParams.size(); i++) {
+ ParamValue pvalue = (ParamValue) theContextParams.get(i);
+ if (pvalue.getParamName().equals(RS_APPLICATION_PARM_NAME)) {
+ if (pvalue.getParamValue().equals(className)) {
+ status =
StatusUtils.errorStatus(JBossWSCreationCoreMessages.RSMergeWebXMLCommand_REST_App_Exists);
+ return;
+ }
+ }
+ }
+ ParamValue pvalue = JavaeeFactory.eINSTANCE.createParamValue();
+ pvalue.setParamName(RS_APPLICATION_PARM_NAME);
+ pvalue.setParamValue(className);
+ webapp.getContextParams().add(pvalue);
+ }
+
+ protected void addContextParams ( String className, WebApp webapp ) {
+ List<ParamValue> theContextParams = webapp.getContextParams();
+ for (int i = 0; i < theContextParams.size(); i++) {
+ ParamValue pvalue = (ParamValue) theContextParams.get(i);
+ if (pvalue.getParamName().equals(RS_APPLICATION_PARM_NAME)) {
+ if (pvalue.getParamValue().equals(className)) {
+ status =
StatusUtils.errorStatus(JBossWSCreationCoreMessages.RSMergeWebXMLCommand_REST_App_Exists);
+ return;
+ }
+ }
+ }
+ ParamValue pvalue = JavaeeFactory.eINSTANCE.createParamValue();
+ pvalue.setParamName(RS_APPLICATION_PARM_NAME);
+ pvalue.setParamValue(className);
+ webapp.getContextParams().add(pvalue);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void addServlet(IProject projectByName,
+ ServletDescriptor servletDescriptor,
+ org.eclipse.jst.j2ee.webapplication.WebApp webapp) {
+ List<org.eclipse.jst.j2ee.webapplication.Servlet> theServlets =
webapp.getServlets();
+ for (int i = 0; i < theServlets.size(); i++) {
+ org.eclipse.jst.j2ee.webapplication.Servlet aServlet =
(org.eclipse.jst.j2ee.webapplication.Servlet) theServlets
+ .get(i);
+ if (aServlet.getServletName().equals(RE_SERVLET_NAME)) {
+ status =
StatusUtils.errorStatus(JBossWSCreationCoreMessages.RSMergeWebXMLCommand_REST_Servlet_Exists);
+ return;
+ }
+ }
+ List<org.eclipse.jst.j2ee.webapplication.ServletMapping> theServletMapplings =
webapp.getServletMappings();
+ for (int i = 0; i < theServletMapplings.size(); i++) {
+ org.eclipse.jst.j2ee.webapplication.ServletMapping aServletMapping =
(org.eclipse.jst.j2ee.webapplication.ServletMapping) theServletMapplings
+ .get(i);
+ if (aServletMapping.getName().equals(RE_SERVLET_NAME)
+ || aServletMapping.getUrlPattern().equals(
+ RE_SERVLET_MAPPING)) {
+ status =
StatusUtils.errorStatus(JBossWSCreationCoreMessages.RSMergeWebXMLCommand_REST_Servlet_Mapping_Exists);
+ return;
+ }
+ }
+ org.eclipse.jst.j2ee.webapplication.WebapplicationFactory factory =
org.eclipse.jst.j2ee.webapplication.WebapplicationFactory.eINSTANCE;
+ org.eclipse.jst.j2ee.webapplication.Servlet servlet = factory
+ .createServlet();
+ org.eclipse.jst.j2ee.webapplication.ServletType servletType = factory
+ .createServletType();
+ servlet.setWebType(servletType);
+ servlet.setServletName(RE_SERVLET_NAME);
+ servletType.setClassName(RE_SERVLET_CLASS);
+ webapp.getServlets().add(servlet);
+
+ if (servletDescriptor.getMappings() != null) {
+ org.eclipse.jst.j2ee.webapplication.ServletMapping servletMapping = factory
+ .createServletMapping();
+ servletMapping.setServlet(servlet);
+ servletMapping.setUrlPattern(RE_SERVLET_MAPPING);
+ webapp.getServletMappings().add(servletMapping);
+ }
+ }
+
+ private ServletDescriptor getServletDescriptor(String clsName) {
+ String servletName = model.getServiceName();
+ if (servletName == null) {
+ servletName = JBossWSCreationUtils
+ .classNameFromQualifiedName(clsName);
+ }
+ if (servletName.endsWith("Impl") && servletName.length() > 4) {
//$NON-NLS-1$
+ servletName = servletName.substring(0, servletName.length() - 4);
+ }
+ ServletDescriptor sd = new ServletDescriptor();
+ sd.setName(servletName);
+ sd.setDisplayName(sd.getName());
+ sd.setClassName(clsName);
+ sd.setMappings(JBossWSCreationCoreMessages.Separator_Java + sd.getName());
+ return sd;
+ }
+
+ public void addjeeServlet(IProject webProject,
+ ServletDescriptor servletDescriptor, WebApp webapp) {
+ List<Servlet> theServlets = webapp.getServlets();
+
+ for (int i = 0; i < theServlets.size(); i++) {
+ Servlet aServlet = (Servlet) theServlets.get(i);
+ if (aServlet.getServletName().equals(RE_SERVLET_NAME)) {
+ status =
StatusUtils.errorStatus(JBossWSCreationCoreMessages.RSMergeWebXMLCommand_REST_Servlet_Exists);
+ return;
+ }
+ }
+
+ List<ServletMapping> theServletMapplings = webapp.getServletMappings();
+ for (int i = 0; i < theServletMapplings.size(); i++) {
+ ServletMapping aServletMapping = (ServletMapping) theServletMapplings
+ .get(i);
+ if (aServletMapping.getServletName()
+ .equals(RE_SERVLET_NAME)) {
+ status =
StatusUtils.errorStatus(JBossWSCreationCoreMessages.RSMergeWebXMLCommand_REST_Servlet_Mapping_Exists);
+ return;
+ }
+ List<UrlPatternType> list = aServletMapping.getUrlPatterns();
+ if (list != null) {
+ for (int j = 0; j < list.size(); j++) {
+ UrlPatternType url = (UrlPatternType) list.get(j);
+ if (url.getValue().equals(RE_SERVLET_MAPPING)) {
+ status =
StatusUtils.errorStatus(JBossWSCreationCoreMessages.RSMergeWebXMLCommand_REST_Servlet_Mapping_Exists);
+ return;
+ }
+ }
+ }
+ }
+
+ WebFactory factory = WebFactory.eINSTANCE;
+ Servlet servlet = factory.createServlet();
+ servlet.setServletName(RE_SERVLET_NAME);
+ servlet.setServletClass(RE_SERVLET_CLASS);
+ webapp.getServlets().add(servlet);
+
+ if (servletDescriptor.getMappings() != null) {
+ ServletMapping servletMapping = factory.createServletMapping();
+ servletMapping.setServletName(RE_SERVLET_NAME);
+ UrlPatternType url = JavaeeFactory.eINSTANCE.createUrlPatternType();
+ url.setValue(RE_SERVLET_MAPPING);
+ servletMapping.getUrlPatterns().add(url);
+ webapp.getServletMappings().add(servletMapping);
+ }
+ }
+
+ private boolean hasWebXML(IProject pro) {
+ // we are looking for this recursively because though application.xml
+ // is always in META-INF, it's not always in "earcontent" since the
+ // earcontent folder name can be custom
+ File file = JBossWSCreationUtils.findFileByPath(WEB_XML,
pro.getLocation().toOSString());
+ if(file == null){
+ return false;
+ }
+ return true;
+ }
+}
Property changes on:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSMergeWebXMLCommand.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSServiceSampleCreationCommand.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSServiceSampleCreationCommand.java
(rev 0)
+++
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSServiceSampleCreationCommand.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -0,0 +1,190 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.ws.creation.core.commands;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
+import org.jboss.tools.ws.core.utils.StatusUtils;
+import org.jboss.tools.ws.creation.core.JBossWSCreationCorePlugin;
+import org.jboss.tools.ws.creation.core.data.ServiceModel;
+import org.jboss.tools.ws.creation.core.messages.JBossWSCreationCoreMessages;
+import org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils;
+
+/**
+ * @author Brian Fitzpatrick
+ *
+ */
+public class RSServiceSampleCreationCommand extends AbstractDataModelOperation {
+
+ private ServiceModel model;
+ public static final String LINE_SEPARATOR = System
+ .getProperty("line.separator"); //$NON-NLS-1$
+
+ public RSServiceSampleCreationCommand(ServiceModel model) {
+ this.model = model;
+ }
+
+ @Override
+ public IStatus execute(IProgressMonitor monitor, IAdaptable info)
+ throws ExecutionException {
+ IJavaProject project = null;
+ try {
+ project = JBossWSCreationUtils.getJavaProjectByName(model
+ .getWebProjectName());
+ } catch (JavaModelException e) {
+ JBossWSCreationCorePlugin.getDefault().logError(e);
+ return StatusUtils
+ .errorStatus(JBossWSCreationCoreMessages.Error_Create_Client_Sample);
+ }
+
+ createRESTAnnotatedJavaClass (model.getCustomPackage(), JBossWSCreationUtils
+ .classNameFromQualifiedName(model.getServiceClasses().get(0)),
+ project);
+ createRESTApplicationClass (model.getCustomPackage(), model.getApplicationClassName(),
+ project);
+
+ return null;
+ }
+
+ private ICompilationUnit createRESTApplicationClass(String packageName,
+ String className, IJavaProject project) {
+ try {
+ IPath srcPath = new Path(JBossWSCreationUtils
+ .getJavaProjectSrcLocation(project.getProject()));
+ srcPath = project.getPath().append(
+ srcPath.makeRelativeTo(project.getProject()
+ .getLocation()));
+ IPackageFragmentRoot root = project
+ .findPackageFragmentRoot(srcPath);
+ if (packageName == null) {
+ packageName = ""; //$NON-NLS-1$
+ }
+ IPackageFragment pkg = root.createPackageFragment(packageName,
+ false, null);
+ ICompilationUnit wrapperCls = pkg.createCompilationUnit(className
+ + ".java", "", true, null); //$NON-NLS-1$//$NON-NLS-2$
+ if (!packageName.equals("")) { //$NON-NLS-1$
+ wrapperCls.createPackageDeclaration(packageName, null);
+ }
+
+ StringBuffer clsContent = new StringBuffer();
+ clsContent.append("public class ").append(className).append(" extends
Application").append(" {" + LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
//$NON-NLS-3$
+ clsContent.append("}").append(LINE_SEPARATOR); //$NON-NLS-1$
+ wrapperCls.createType(clsContent.toString(), null, true, null);
+
+ wrapperCls.createImport("java.util.Set", null,null); //$NON-NLS-1$
+ wrapperCls.createImport("java.util.HashSet", null,null); //$NON-NLS-1$
+ wrapperCls.createImport("javax.ws.rs.core.Application", null,null);
//$NON-NLS-1$
+
+ IType serviceClsType = wrapperCls.findPrimaryType();
+ clsContent = new StringBuffer();
+ clsContent.append("private Set<Object> singletons = new
HashSet<Object>();" + LINE_SEPARATOR); //$NON-NLS-1$
+ serviceClsType.createField(clsContent.toString(), null, false, null);
+
+ clsContent = new StringBuffer();
+ clsContent.append("private Set<Class<?>> empty = new
HashSet<Class<?>>();" + LINE_SEPARATOR); //$NON-NLS-1$
+ serviceClsType.createField(clsContent.toString(), null, false, null);
+
+ clsContent = new StringBuffer();
+ clsContent.append(LINE_SEPARATOR);
+ clsContent.append("public " + className + "(){" + LINE_SEPARATOR);
//$NON-NLS-1$ //$NON-NLS-2$
+ clsContent.append(" singletons.add(new " + JBossWSCreationUtils
//$NON-NLS-1$
+ .classNameFromQualifiedName(model.getServiceClasses().get(0)) + "());" +
LINE_SEPARATOR); //$NON-NLS-1$
+ clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
+ serviceClsType.createMethod(clsContent.toString(), null, true, null);
+
+ clsContent = new StringBuffer();
+ clsContent.append(LINE_SEPARATOR);
+ clsContent.append("@Override" + LINE_SEPARATOR); //$NON-NLS-1$
+ clsContent.append("public Set<Class<?>> getClasses() {" +
LINE_SEPARATOR); //$NON-NLS-1$
+ clsContent.append(" return empty;" + LINE_SEPARATOR); //$NON-NLS-1$
+ clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
+ serviceClsType.createMethod(clsContent.toString(), null, true, null);
+
+ clsContent = new StringBuffer();
+ clsContent.append(LINE_SEPARATOR);
+ clsContent.append("@Override" + LINE_SEPARATOR); //$NON-NLS-1$
+ clsContent.append("public Set<Object> getSingletons() {" +
LINE_SEPARATOR); //$NON-NLS-1$
+ clsContent.append(" return singletons;" + LINE_SEPARATOR);
//$NON-NLS-1$
+ clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
+ serviceClsType.createMethod(clsContent.toString(), null, true, null);
+
+ wrapperCls.save(null, true);
+ return wrapperCls;
+ } catch (Exception e) {
+ JBossWSCreationCorePlugin.getDefault().logError(e);
+ return null;
+ }
+ }
+
+ private ICompilationUnit createRESTAnnotatedJavaClass(String packageName,
+ String className, IJavaProject project) {
+ try {
+ IPath srcPath = new Path(JBossWSCreationUtils
+ .getJavaProjectSrcLocation(project.getProject()));
+ srcPath = project.getPath().append(
+ srcPath.makeRelativeTo(project.getProject()
+ .getLocation()));
+ IPackageFragmentRoot root = project
+ .findPackageFragmentRoot(srcPath);
+ if (packageName == null) {
+ packageName = ""; //$NON-NLS-1$
+ }
+ IPackageFragment pkg = root.createPackageFragment(packageName,
+ false, null);
+ ICompilationUnit wrapperCls = pkg.createCompilationUnit(className
+ + ".java", "", true, null); //$NON-NLS-1$//$NON-NLS-2$
+ if (!packageName.equals("")) { //$NON-NLS-1$
+ wrapperCls.createPackageDeclaration(packageName, null);
+ }
+
+ StringBuffer clsContent = new StringBuffer();
+ clsContent.append("@Path(\"/" + model.getServiceName() +
"\")").append(LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
+ clsContent.append("public class ").append(className).append(" {" +
LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
+ clsContent.append("}").append(LINE_SEPARATOR); //$NON-NLS-1$
+ wrapperCls.createType(clsContent.toString(), null, true, null);
+
+ wrapperCls.createImport("javax.ws.rs.Produces", null,null); //$NON-NLS-1$
+ wrapperCls.createImport("javax.ws.rs.GET", null,null); //$NON-NLS-1$
+ wrapperCls.createImport("javax.ws.rs.Path", null,null); //$NON-NLS-1$
+
+ IType serviceClsType = wrapperCls.findPrimaryType();
+ clsContent = new StringBuffer();
+ clsContent.append("@GET()"); //$NON-NLS-1$
+ clsContent.append(LINE_SEPARATOR);
+ clsContent.append("@Produces(\"text/plain\")"); //$NON-NLS-1$
+ clsContent.append(LINE_SEPARATOR);
+ clsContent.append("public String sayHello() {"); //$NON-NLS-1$
+ clsContent.append(LINE_SEPARATOR);
+ clsContent.append(" return \"Hello World!\";"); //$NON-NLS-1$
+ clsContent.append(LINE_SEPARATOR);
+ clsContent.append("}"); //$NON-NLS-1$
+ serviceClsType.createMethod(clsContent.toString(), null, true, null);
+ wrapperCls.save(null, true);
+ return wrapperCls;
+ } catch (Exception e) {
+ JBossWSCreationCorePlugin.getDefault().logError(e);
+ return null;
+ }
+ }
+
+}
Property changes on:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/commands/RSServiceSampleCreationCommand.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/data/ServiceModel.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/data/ServiceModel.java 2010-07-23
17:39:18 UTC (rev 23709)
+++
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/data/ServiceModel.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -21,6 +21,8 @@
private boolean extension = true;
private String serviceName;
private String customClassName;
+ private String applicationClassName;
+ private String applicationName;
public int getWsScenario() {
return wsScenario;
@@ -183,4 +185,20 @@
public String getCustomClassName() {
return customClassName;
}
+
+ public String getApplicationClassName() {
+ return applicationClassName;
+ }
+
+ public void setApplicationClassName(String applicationClassName) {
+ this.applicationClassName = applicationClassName;
+ }
+
+ public String getApplicationName() {
+ return applicationName;
+ }
+
+ public void setApplicationName(String applicationName) {
+ this.applicationName = applicationName;
+ }
}
Modified:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/messages/JBossWSCreationCore.properties
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/messages/JBossWSCreationCore.properties 2010-07-23
17:39:18 UTC (rev 23709)
+++
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/messages/JBossWSCreationCore.properties 2010-07-23
17:53:12 UTC (rev 23710)
@@ -1,4 +1,5 @@
# START NON-TRANSLATABLE
+AddRestEasyJarsCommand_RestEasy_JARS_Not_Found=Cannot find RestEasy JARs.
Value_Target_0=2.0
Value_Target_1=2.1
Separator_Java=/
@@ -37,3 +38,6 @@
Error_Create_Client_Sample=When create a client sample, a error comes up. Please check
the generated files.
Error_No_Package=The service implementation selected does not follow Java naming
conventions. This may result in not being able to generate your WebService.
Error_JBossWS_GenerateWizard_WSName_Same=The Web Service Name has been used in the
web.xml of the web project.
+RSMergeWebXMLCommand_REST_App_Exists=Rest Application exists with same name
+RSMergeWebXMLCommand_REST_Servlet_Exists=RestEasy servlet already exists.
+RSMergeWebXMLCommand_REST_Servlet_Mapping_Exists=RestEasy servlet mapping already
exists.
Modified:
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/messages/JBossWSCreationCoreMessages.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/messages/JBossWSCreationCoreMessages.java 2010-07-23
17:39:18 UTC (rev 23709)
+++
trunk/ws/plugins/org.jboss.tools.ws.creation.core/src/org/jboss/tools/ws/creation/core/messages/JBossWSCreationCoreMessages.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -5,6 +5,8 @@
public class JBossWSCreationCoreMessages extends NLS {
private static final String BUNDLE_NAME =
"org.jboss.tools.ws.creation.core.messages.JBossWSCreationCore"; //$NON-NLS-1$
+ public static String AddRestEasyJarsCommand_RestEasy_JARS_Not_Found;
+
public static String Value_Target_0;
public static String Value_Target_1;
public static String Separator_Java;
@@ -42,6 +44,12 @@
public static String Error_Create_Client_Sample;
public static String Error_Message_No_Runtime_Specified;
public static String Error_JBossWS_GenerateWizard_WSName_Same;
+
+ public static String RSMergeWebXMLCommand_REST_App_Exists;
+
+ public static String RSMergeWebXMLCommand_REST_Servlet_Exists;
+
+ public static String RSMergeWebXMLCommand_REST_Servlet_Mapping_Exists;
private JBossWSCreationCoreMessages() {
Modified: trunk/ws/plugins/org.jboss.tools.ws.ui/META-INF/MANIFEST.MF
===================================================================
--- trunk/ws/plugins/org.jboss.tools.ws.ui/META-INF/MANIFEST.MF 2010-07-23 17:39:18 UTC
(rev 23709)
+++ trunk/ws/plugins/org.jboss.tools.ws.ui/META-INF/MANIFEST.MF 2010-07-23 17:53:12 UTC
(rev 23710)
@@ -32,7 +32,8 @@
org.jdom,
javax.xml.ws,
org.eclipse.ui.forms,
- org.eclipse.emf.common
+ org.eclipse.emf.common,
+ org.eclipse.jdt.ui
Bundle-ActivationPolicy: lazy
Export-Package: org.jboss.tools.ws.ui,
org.jboss.tools.ws.ui.messages,
Modified: trunk/ws/plugins/org.jboss.tools.ws.ui/plugin.xml
===================================================================
--- trunk/ws/plugins/org.jboss.tools.ws.ui/plugin.xml 2010-07-23 17:39:18 UTC (rev 23709)
+++ trunk/ws/plugins/org.jboss.tools.ws.ui/plugin.xml 2010-07-23 17:53:12 UTC (rev 23710)
@@ -37,6 +37,34 @@
%JBOSSWS_WIZARD_DESC
</description>
</wizard>
+ <wizard
+ category="org.jboss.ide.eclipse.ui.wizards"
+ class="org.jboss.tools.ws.ui.wizards.JBossRSGenerateWizard"
+ hasPages="true"
+ icon="icons/obj16/new_webserv_wiz.gif"
+ id="org.jboss.tools.ws.ui.wizard.rsgenerate"
+ name="Create a Sample RESTful Web Service">
+ <selection
+ class="org.eclipse.core.resources.IProject">
+ </selection>
+ <description>
+ Create a Sample RESTful Web Service
+ </description>
+ </wizard>
+ <wizard
+ category="org.eclipse.jst.ws.ui.new"
+ class="org.jboss.tools.ws.ui.wizards.JBossRSGenerateWizard"
+ hasPages="true"
+ icon="icons/obj16/new_webserv_wiz.gif"
+ id="org.jboss.tools.ws.ui.wizard.rsgenerate2"
+ name="Create a Sample RESTful Web Service">
+ <selection
+ class="org.eclipse.core.resources.IProject">
+ </selection>
+ <description>
+ Create a Sample RESTful Web Service
+ </description>
+ </wizard>
</extension>
<extension point="org.eclipse.wst.common.project.facet.ui.images">
<image facet="jbossws.core"
path="icons/obj16/new_webserv_wiz.gif"/>
Modified:
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/messages/JBossWSUI.properties
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/messages/JBossWSUI.properties 2010-07-23
17:39:18 UTC (rev 23709)
+++
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/messages/JBossWSUI.properties 2010-07-23
17:53:12 UTC (rev 23710)
@@ -6,6 +6,10 @@
Endorsed=endorsed
# END NON-TRANSLATABLE
+JBossRSGenerateWizard_RS_Wizard_Window_Title=Generate a Sample RESTful Web Service
+JBossRSGenerateWizardPage_Label_Application_Class_Name=Application Class Name:
+JBossRSGenerateWizardPage_Page_title=Specify the Dynamic Web Project, service, package
and class name for the sample web service and web service classes.
+JBossRSGenerateWizardValidator_ERROR_Can_Only_Add_Sample_Once=Project already has the
sample RESTful service installed. Cannot be added a second time.
JBossRuntimeListFieldEditor_ActionAdd=&Add
JBossRuntimeListFieldEditor_ActionEdit=&Edit
JBossRuntimeListFieldEditor_ActionRemove=&Remove
Modified:
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/messages/JBossWSUIMessages.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/messages/JBossWSUIMessages.java 2010-07-23
17:39:18 UTC (rev 23709)
+++
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/messages/JBossWSUIMessages.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -38,6 +38,10 @@
public static String Lib;
public static String Endorsed;
+ public static String JBossRSGenerateWizard_RS_Wizard_Window_Title;
+ public static String JBossRSGenerateWizardPage_Label_Application_Class_Name;
+ public static String JBossRSGenerateWizardPage_Page_title;
+ public static String JBossRSGenerateWizardValidator_ERROR_Can_Only_Add_Sample_Once;
public static String JBossRuntimeListFieldEditor_ActionAdd;
public static String JBossRuntimeListFieldEditor_ActionEdit;
public static String JBossRuntimeListFieldEditor_ActionRemove;
Added:
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizard.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizard.java
(rev 0)
+++
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizard.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -0,0 +1,245 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.ws.ui.wizards;
+
+import java.io.File;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities;
+import org.eclipse.ui.INewWizard;
+import org.eclipse.ui.IWorkbench;
+import org.jboss.tools.ws.creation.core.commands.AddRestEasyJarsCommand;
+import org.jboss.tools.ws.creation.core.commands.RSMergeWebXMLCommand;
+import org.jboss.tools.ws.creation.core.commands.RSServiceSampleCreationCommand;
+import org.jboss.tools.ws.creation.core.data.ServiceModel;
+import org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils;
+import org.jboss.tools.ws.ui.JBossWSUIPlugin;
+import org.jboss.tools.ws.ui.messages.JBossWSUIMessages;
+
+/**
+ * @author Brian Fitzpatrick
+ *
+ */
+public class JBossRSGenerateWizard extends Wizard implements INewWizard {
+
+ String NAMEDEFAULT = "MyRESTApplication"; //$NON-NLS-1$
+ String PACKAGEDEFAULT = "org.jboss.samples.rs.webservices"; //$NON-NLS-1$
+ String CLASSDEFAULT = "HelloWorldResource"; //$NON-NLS-1$
+ String APPCLASSDEFAULT = "MyRESTApplication"; //$NON-NLS-1$
+
+ private String serviceName = NAMEDEFAULT;
+ private String packageName = PACKAGEDEFAULT;
+ private String className = CLASSDEFAULT;
+ private String appClassName = APPCLASSDEFAULT;
+ private boolean useDefaultServiceName = true;
+ private boolean useDefaultClassName = true;
+
+ private IStructuredSelection selection;
+ private IProject project;
+
+ private static String WEB = "web.xml"; //$NON-NLS-1$
+ private static String JAVA = ".java"; //$NON-NLS-1$
+ private static String WEBINF = "WEB-INF"; //$NON-NLS-1$
+ private IFile webFile;
+
+ public JBossRSGenerateWizard() {
+ super();
+ super.setWindowTitle(JBossWSUIMessages.JBossRSGenerateWizard_RS_Wizard_Window_Title);
+ super.setHelpAvailable(false);
+ }
+
+ public void addPages() {
+ super.addPages();
+ JBossRSGenerateWizardPage onePage =
+ new JBossRSGenerateWizardPage("onePage"); //$NON-NLS-1$
+ addPage(onePage);
+ }
+
+ @Override
+ public boolean performFinish() {
+ if (canFinish()) {
+ ServiceModel model = new ServiceModel();
+ model.setWebProjectName(project.getName());
+ model.addServiceClasses(new StringBuffer().append(getPackageName())
+ .append(".").append(getClassName()).toString()); //$NON-NLS-1$
+// model.addServiceClasses(new StringBuffer().append(getPackageName())
+// .append(".").append(getAppClassName()).toString()); //$NON-NLS-1$
+ model.setServiceName(getServiceName());
+ model.setUpdateWebxml(true);
+ model.setCustomPackage(getPackageName());
+ model.setApplicationClassName( getAppClassName());
+
+ File file = JBossWSCreationUtils.findFileByPath(getClassName() + JAVA, project
+ .getLocation().toOSString());
+ if (file != null) {
+ MessageDialog
+ .openError(
+ this.getShell(),
+ JBossWSUIMessages.JBossWS_GenerateWizard_MessageDialog_Title,
+ JBossWSUIMessages.Error_JBossWS_GenerateWizard_ClassName_Same);
+ return false;
+ }
+
+ IStatus status = null;
+ try {
+ RSMergeWebXMLCommand mergeCommand = new RSMergeWebXMLCommand(model);
+ status = mergeCommand.execute(null, null);
+ } catch (ExecutionException e) {
+ JBossWSUIPlugin.log(e);
+ }
+ if (status != null && status.getSeverity() == Status.ERROR) {
+ MessageDialog
+ .openError(
+ this.getShell(),
+ JBossWSUIMessages.JBossWS_GenerateWizard_MessageDialog_Title,
+ status.getMessage());
+ return false;
+ }
+ try {
+ new AddRestEasyJarsCommand(model).execute(null, null);
+ new RSServiceSampleCreationCommand(model).execute(null, null);
+ getProject().refreshLocal(IProject.DEPTH_INFINITE, new NullProgressMonitor());
+ } catch (ExecutionException e) {
+ JBossWSUIPlugin.log(e);
+ MessageDialog
+ .openError(
+ this.getShell(),
+ JBossWSUIMessages.JBossWS_GenerateWizard_MessageDialog_Title,
+ e.getMessage());
+ } catch (CoreException e) {
+ JBossWSUIPlugin.log(e);
+ MessageDialog
+ .openError(
+ this.getShell(),
+ JBossWSUIMessages.JBossWS_GenerateWizard_MessageDialog_Title,
+ e.getMessage());
+ }
+ }
+ return true;
+ }
+
+ public void init(IWorkbench workbench, IStructuredSelection selection) {
+ this.selection = selection;
+ if (this.selection.getFirstElement() instanceof IProject) {
+ project = (IProject) this.selection.getFirstElement();
+ }
+ if (project != null
+ && JavaEEProjectUtilities.isDynamicWebProject(project)) {
+ webFile = project.getParent().getFolder(
+ JBossWSCreationUtils.getWebContentRootPath(project).append(WEBINF))
+ .getFile(WEB);
+ }
+ }
+
+ @Override
+ public boolean canFinish() {
+ return super.canFinish();
+ }
+
+ public String getServiceName() {
+ return serviceName;
+ }
+
+ public void setServiceName(String serviceName) {
+ this.serviceName = serviceName;
+ }
+
+ public String getPackageName() {
+ return packageName;
+ }
+
+ public void setPackageName(String packageName) {
+ this.packageName = packageName;
+ }
+
+ public String getClassName() {
+ return className;
+ }
+
+ public void setClassName(String className) {
+ this.className = className;
+ }
+
+ public String getAppClassName() {
+ return appClassName;
+ }
+
+ public void setAppClassName(String className) {
+ this.appClassName = className;
+ }
+
+ public boolean isUseDefaultServiceName() {
+ return useDefaultServiceName;
+ }
+
+ public void setUseDefaultServiceName(boolean useDefaultServiceName) {
+ this.useDefaultServiceName = useDefaultServiceName;
+ }
+
+ public boolean isUseDefaultClassName() {
+ return useDefaultClassName;
+ }
+
+ public void setUseDefaultClassName(boolean useDefaultClassName) {
+ this.useDefaultClassName = useDefaultClassName;
+ }
+
+ public IProject getProject() {
+ return project;
+ }
+
+ public ServiceModel getServiceModel() {
+ ServiceModel model = new ServiceModel();
+ if (project != null) {
+ model.setWebProjectName(project.getName());
+ }
+ if (getPackageName() != null) {
+ model.addServiceClasses(new StringBuffer().append(getPackageName())
+ .append(".").append(getClassName()).toString()); //$NON-NLS-1$
+ }
+ model.setServiceName(getServiceName());
+ model.setUpdateWebxml(true);
+ model.setCustomPackage(getPackageName());
+ model.setCustomClassName(getClassName());
+ return model;
+ }
+
+ public void setProject (String projectName) {
+ if (projectName != null && projectName.trim().length() > 0) {
+ IProject test =
+ ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+ if (test != null) {
+ this.project = test;
+ if (project != null
+ && JavaEEProjectUtilities.isDynamicWebProject(project)) {
+ webFile = project.getParent().getFolder(
+ JBossWSCreationUtils.getWebContentRootPath(project).append(WEBINF))
+ .getFile(WEB);
+ }
+ }
+ }
+ }
+
+
+ public IFile getWebFile() {
+ return webFile;
+ }
+}
Property changes on:
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizard.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardPage.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardPage.java
(rev 0)
+++
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardPage.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -0,0 +1,398 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.ws.ui.wizards;
+
+import java.util.ArrayList;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jface.dialogs.DialogPage;
+import org.eclipse.jface.wizard.IWizardPage;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.jboss.tools.ws.creation.core.data.ServiceModel;
+import org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils;
+import org.jboss.tools.ws.ui.messages.JBossWSUIMessages;
+
+public class JBossRSGenerateWizardPage extends WizardPage {
+
+ private JBossRSGenerateWizard wizard;
+ private Text name;
+ private Combo projects;
+ private boolean bHasChanged = false;
+ private Text packageName;
+ private Text className;
+ private Text appClassName;
+
+ protected JBossRSGenerateWizardPage(String pageName) {
+ super(pageName);
+ this
+ .setTitle(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_Title);
+ this
+ .setDescription(JBossWSUIMessages.JBossRSGenerateWizardPage_Page_title);
+ }
+
+ public void createControl(Composite parent) {
+ Composite composite = createDialogArea(parent);
+ this.wizard = (JBossRSGenerateWizard) this.getWizard();
+
+ Group group = new Group(composite, SWT.NONE);
+ group
+ .setText(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_Project_Group);
+ GridData gd = new GridData(GridData.FILL_HORIZONTAL);
+ gd.horizontalSpan = 2;
+ group.setLayout(new GridLayout(2, false));
+ group.setLayoutData(gd);
+
+ projects = new Combo(group, SWT.BORDER | SWT.DROP_DOWN);
+ projects
+ .setToolTipText(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_Project_Group_Tooltip);
+ gd = new GridData(GridData.FILL_HORIZONTAL);
+ gd.horizontalSpan = 2;
+ projects.setLayoutData(gd);
+ refreshProjectList(wizard.getServiceModel().getWebProjectName());
+
+ projects.addSelectionListener(new SelectionListener() {
+ public void widgetSelected(SelectionEvent e) {
+ wizard.setProject(projects.getText());
+ name.setText(updateDefaultName());
+ className.setText(updateDefaultClassName());
+ bHasChanged = true;
+ setPageComplete(isPageComplete());
+ }
+ public void widgetDefaultSelected(SelectionEvent e) {
+ widgetSelected(e);
+ }
+ });
+
+ Group group2 = new Group(composite, SWT.NONE);
+ group2
+ .setText(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_Web_Service_Group);
+ gd = new GridData(GridData.FILL_HORIZONTAL);
+ gd.horizontalSpan = 2;
+ group2.setLayout(new GridLayout(2, false));
+ group2.setLayoutData(gd);
+
+ new Label(group2, SWT.NONE)
+ .setText(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_ServiceName_Label);
+ name = new Text(group2, SWT.BORDER);
+ gd = new GridData(GridData.FILL_HORIZONTAL);
+ name.setLayoutData(gd);
+ name.setText(updateDefaultName());
+ name.addModifyListener(new ModifyListener() {
+
+ public void modifyText(ModifyEvent e) {
+ wizard.setServiceName(name.getText());
+ bHasChanged = true;
+ setPageComplete(isPageComplete());
+ }
+
+ });
+
+ Group group3 = new Group(composite, SWT.NONE);
+ group3.setText(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_Class_Group);
+ gd = new GridData(GridData.FILL_HORIZONTAL);
+ gd.horizontalSpan = 2;
+ group3.setLayout(new GridLayout(2, false));
+ group3.setLayoutData(gd);
+
+ new Label(group3, SWT.NONE)
+ .setText(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_Package_Label);
+ packageName = new Text(group3, SWT.BORDER);
+ gd = new GridData(GridData.FILL_HORIZONTAL);
+ packageName.setLayoutData(gd);
+ packageName.setText(wizard.getPackageName());
+ packageName.addModifyListener(new ModifyListener() {
+
+ public void modifyText(ModifyEvent e) {
+ wizard.setPackageName(packageName.getText());
+ setPageComplete(isPageComplete());
+ }
+
+ });
+
+ new Label(group3, SWT.NONE)
+ .setText(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_ClassName_Label);
+ className = new Text(group3, SWT.BORDER);
+ gd = new GridData(GridData.FILL_HORIZONTAL);
+ className.setLayoutData(gd);
+ className.setText(updateDefaultClassName());
+ className.addModifyListener(new ModifyListener() {
+
+ public void modifyText(ModifyEvent e) {
+ wizard.setClassName(className.getText());
+ setPageComplete(isPageComplete());
+ }
+
+ });
+
+ new Label(group3, SWT.NONE)
+ .setText(JBossWSUIMessages.JBossRSGenerateWizardPage_Label_Application_Class_Name);
+ appClassName = new Text(group3, SWT.BORDER);
+ gd = new GridData(GridData.FILL_HORIZONTAL);
+ appClassName.setLayoutData(gd);
+ appClassName.setText(updateDefaultAppClassName());
+ appClassName.addModifyListener(new ModifyListener() {
+
+ public void modifyText(ModifyEvent e) {
+ wizard.setAppClassName(appClassName.getText());
+ setPageComplete(isPageComplete());
+ }
+
+ });
+
+ setControl(composite);
+ }
+
+ private void refreshProjectList(String projectName) {
+ String[] projectNames = getProjects();
+ boolean foundInitialProject = false;
+ projects.removeAll();
+ for (int i = 0; i < projectNames.length; i++) {
+ projects.add(projectNames[i]);
+ if (projectNames[i].equals(projectName)) {
+ foundInitialProject = true;
+ }
+ }
+ if (foundInitialProject)
+ projects.setText(projectName);
+ }
+
+ public IWizardPage getNextPage() {
+ return super.getNextPage();
+ }
+
+ private Composite createDialogArea(Composite parent) {
+ // create a composite with standard margins and spacing
+ Composite composite = new Composite(parent, SWT.NONE);
+ GridLayout layout = new GridLayout();
+ layout.marginHeight = 7;
+ layout.marginWidth = 7;
+ layout.verticalSpacing = 4;
+ layout.horizontalSpacing = 4;
+ layout.numColumns = 2;
+ composite.setLayout(layout);
+ composite.setLayoutData(new GridData(GridData.FILL_BOTH));
+ return composite;
+ }
+
+ @Override
+ public boolean isPageComplete() {
+ return validate();
+ }
+
+ private String updateDefaultName() {
+ ServiceModel model = wizard.getServiceModel();
+ JBossWSGenerateWizardValidator.setServiceModel(model);
+ String currentName = wizard.getServiceName();
+ IStatus status = JBossWSGenerateWizardValidator.isWSNameValid();
+ try {
+ if (status.getSeverity() == IStatus.ERROR
+ && !JavaEEProjectUtilities.isDynamicWebProject(wizard
+ .getProject())) {
+ return currentName;
+ }
+ } catch (NullPointerException npe) {
+ return currentName;
+ }
+ String testName = currentName;
+ int i = 1;
+ while (status != null) {
+ testName = currentName + i;
+ wizard.setServiceName(testName);
+ model = wizard.getServiceModel();
+ JBossWSGenerateWizardValidator.setServiceModel(model);
+ status = JBossWSGenerateWizardValidator.isWSNameValid();
+ i++;
+ }
+ return testName;
+ }
+
+ private String updateDefaultAppClassName() {
+ ServiceModel model = wizard.getServiceModel();
+ JBossRSGenerateWizardValidator.setServiceModel(model);
+ String currentName = wizard.getAppClassName();
+ if (wizard.getProject() == null) {
+ return currentName;
+ } else {
+ boolean isDynamicWebProject = false;
+ try {
+ if (wizard.getProject().getNature(
+ "org.eclipse.wst.common.project.facet.core.nature") != null) {
//$NON-NLS-1$
+ isDynamicWebProject = true;
+ }
+ } catch (CoreException e) {
+ // ignore
+ }
+ if (!isDynamicWebProject) {
+ return currentName;
+ }
+ }
+ String testName = currentName;
+ IStatus status = JBossRSGenerateWizardValidator.isAppClassNameValid(
+ model.getCustomPackage() + '.' + model.getApplicationClassName());
+ int i = 1;
+ while (status != null && status.getSeverity() == IStatus.ERROR) {
+ testName = currentName + i;
+ wizard.setClassName(testName);
+ model = wizard.getServiceModel();
+ JBossWSGenerateWizardValidator.setServiceModel(model);
+ status = JBossWSGenerateWizardValidator.isWSClassValid(testName,
+ wizard.getProject());
+ i++;
+ }
+ return testName;
+ }
+
+ private String updateDefaultClassName() {
+ ServiceModel model = wizard.getServiceModel();
+ JBossWSGenerateWizardValidator.setServiceModel(model);
+ String currentName = wizard.getClassName();
+ if (wizard.getProject() == null) {
+ return currentName;
+ } else {
+ boolean isDynamicWebProject = false;
+ try {
+ if (wizard.getProject().getNature(
+ "org.eclipse.wst.common.project.facet.core.nature") != null) {
//$NON-NLS-1$
+ isDynamicWebProject = true;
+ }
+ } catch (CoreException e) {
+ // ignore
+ }
+ if (!isDynamicWebProject) {
+ return currentName;
+ }
+ }
+ String testName = currentName;
+ IStatus status = JBossWSGenerateWizardValidator.isWSClassValid(
+ testName, wizard.getProject());
+ int i = 1;
+ while (status != null && status.getSeverity() == IStatus.ERROR) {
+ testName = currentName + i;
+ wizard.setClassName(testName);
+ model = wizard.getServiceModel();
+ JBossWSGenerateWizardValidator.setServiceModel(model);
+ status = JBossWSGenerateWizardValidator.isWSClassValid(testName,
+ wizard.getProject());
+ i++;
+ }
+ return testName;
+ }
+
+
+ private boolean validate() {
+ ServiceModel model = wizard.getServiceModel();
+ JBossRSGenerateWizardValidator.setServiceModel(model);
+ if (!projects.isDisposed() && projects.getText().length() > 0) {
+ model.setWebProjectName(projects.getText());
+ }
+
+ // no project selected
+ if (((JBossRSGenerateWizard) this.getWizard()).getProject() == null) {
+ setErrorMessage(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NoProjectSelected);
+ return false;
+ }
+
+ // project not a dynamic web project
+ IFile web = ((JBossRSGenerateWizard) this.getWizard()).getWebFile();
+ if (web == null || !web.exists()) {
+ setErrorMessage(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NotDynamicWebProject);
+ return false;
+ }
+
+ // no source folder in web project
+ try {
+ if
("" .equals(JBossWSCreationUtils.getJavaProjectSrcLocation(((JBossRSGenerateWizard)
this.getWizard()).getProject()))) { //$NON-NLS-1$
+ setErrorMessage(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NoSrcInProject);
+ return false;
+ }
+ } catch (JavaModelException e) {
+ e.printStackTrace();
+ }
+
+ // already has a REST sample installed - can't use wizard again
+ IStatus alreadyHasREST = JBossRSGenerateWizardValidator.RESTAppExists();
+ if (alreadyHasREST != null) {
+ if (alreadyHasREST.getSeverity() == IStatus.ERROR) {
+ setMessage(alreadyHasREST.getMessage(), DialogPage.ERROR);
+ return false;
+ } else if (alreadyHasREST.getSeverity() == IStatus.WARNING) {
+ setMessage(alreadyHasREST.getMessage(), DialogPage.WARNING);
+ return true;
+ }
+ }
+
+ // Check the service class name
+ IStatus classNameStatus = JBossRSGenerateWizardValidator.isWSClassValid(model
+ .getCustomClassName(), wizard.getProject());
+ if (classNameStatus != null) {
+ if (classNameStatus.getSeverity() == IStatus.ERROR) {
+ setMessage(classNameStatus.getMessage(), DialogPage.ERROR);
+ return false;
+ } else if (classNameStatus.getSeverity() == IStatus.WARNING) {
+ setMessage(classNameStatus.getMessage(), DialogPage.WARNING);
+ return true;
+ }
+ }
+
+ // check the application class name
+ IStatus appClassNameStatus = JBossRSGenerateWizardValidator.isAppClassNameValid(
+ model.getCustomPackage() + '.' + model.getApplicationClassName());
+ if (appClassNameStatus != null) {
+ if (appClassNameStatus.getSeverity() == IStatus.ERROR) {
+ setMessage(appClassNameStatus.getMessage(), DialogPage.ERROR);
+ return false;
+ } else if (appClassNameStatus.getSeverity() == IStatus.WARNING) {
+ setMessage(appClassNameStatus.getMessage(), DialogPage.WARNING);
+ return true;
+ }
+ }
+
+ setMessage(JBossWSUIMessages.JBossWS_GenerateWizard_GenerateWizardPage_Description);
+ setErrorMessage(null);
+ return true;
+ }
+
+ private String[] getProjects() {
+ IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
+ .getProjects();
+ ArrayList<String> dynamicProjects = new ArrayList<String>();
+ for (int i = 0; i < projects.length; i++) {
+ boolean isDynamicWebProject = JavaEEProjectUtilities
+ .isDynamicWebProject(projects[i]);
+ if (isDynamicWebProject) {
+ dynamicProjects.add(projects[i].getName());
+ }
+ }
+ return dynamicProjects.toArray(new String[dynamicProjects.size()]);
+ }
+
+ protected boolean hasChanged() {
+ return bHasChanged;
+ }
+}
Property changes on:
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardPage.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardValidator.java
===================================================================
---
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardValidator.java
(rev 0)
+++
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardValidator.java 2010-07-23
17:53:12 UTC (rev 23710)
@@ -0,0 +1,303 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.ws.ui.wizards;
+
+import java.io.File;
+import java.util.List;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jst.j2ee.model.IModelProvider;
+import org.eclipse.jst.j2ee.model.ModelProviderManager;
+import org.eclipse.jst.j2ee.project.JavaEEProjectUtilities;
+import org.eclipse.jst.javaee.core.ParamValue;
+import org.eclipse.jst.javaee.core.UrlPatternType;
+import org.eclipse.jst.javaee.web.Servlet;
+import org.eclipse.jst.javaee.web.ServletMapping;
+import org.eclipse.jst.javaee.web.WebApp;
+import org.jboss.tools.ws.core.utils.StatusUtils;
+import org.jboss.tools.ws.creation.core.commands.RSMergeWebXMLCommand;
+import org.jboss.tools.ws.creation.core.data.ServiceModel;
+import org.jboss.tools.ws.creation.core.data.ServletDescriptor;
+import org.jboss.tools.ws.creation.core.messages.JBossWSCreationCoreMessages;
+import org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils;
+import org.jboss.tools.ws.ui.messages.JBossWSUIMessages;
+import org.jboss.tools.ws.ui.utils.JBossWSUIUtils;
+
+/**
+ * @author Grid Qian and Brian Fitzpatrick
+ *
+ */
+public class JBossRSGenerateWizardValidator {
+
+ private static ServiceModel model;
+ private static ServletDescriptor[] descriptors;
+
+ private static String JAVA = ".java"; //$NON-NLS-1$
+
+ public static void setServiceModel(ServiceModel inModel) {
+ model = inModel;
+
+ descriptors = new ServletDescriptor[model.getServiceClasses().size()];
+ List<String> serviceClasses = model.getServiceClasses();
+ for (int i = 0; i < serviceClasses.size(); i++) {
+ descriptors[i] = getServletDescriptor(serviceClasses.get(i));
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static IStatus RESTAppExists() {
+ IModelProvider provider = null;
+ if (model.getWebProjectName() == null) {
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NoProjectSelected);
+ } else {
+ try {
+ IProject project = JBossWSCreationUtils.getProjectByName(model
+ .getWebProjectName());
+ if (!JavaEEProjectUtilities.isDynamicWebProject(project)) {
+ throw new Exception();
+ }
+ provider = ModelProviderManager.getModelProvider(project);
+ } catch (Exception exc) {
+ model.setWebProjectName(null);
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NoProjectSelected);
+ }
+ }
+ Object object = provider.getModelObject();
+ if (object instanceof WebApp) {
+ WebApp webApp = (WebApp) object;
+ if (model != null) {
+ List<ParamValue> theContextParams = webApp.getContextParams();
+ for (int i = 0; i < theContextParams.size(); i++) {
+ ParamValue pvalue = (ParamValue) theContextParams.get(i);
+ if (pvalue.getParamName().equals(RSMergeWebXMLCommand.RS_APPLICATION_PARM_NAME)) {
+ return
StatusUtils.errorStatus(JBossWSUIMessages.JBossRSGenerateWizardValidator_ERROR_Can_Only_Add_Sample_Once);
+ }
+ }
+ }
+ return null;
+ } else if (object instanceof org.eclipse.jst.j2ee.webapplication.WebApp) {
+ org.eclipse.jst.j2ee.webapplication.WebApp webapp =
(org.eclipse.jst.j2ee.webapplication.WebApp) object;
+ if (model != null) {
+ List<ParamValue> theContextParams = webapp.getContextParams();
+ for (int i = 0; i < theContextParams.size(); i++) {
+ ParamValue pvalue = (ParamValue) theContextParams.get(i);
+ if (pvalue.getParamName().equals(RSMergeWebXMLCommand.RS_APPLICATION_PARM_NAME)) {
+ return
StatusUtils.errorStatus(JBossWSUIMessages.JBossRSGenerateWizardValidator_ERROR_Can_Only_Add_Sample_Once);
+ }
+ }
+ }
+ return null;
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static IStatus isAppClassNameValid(String appClassName) {
+
+ IModelProvider provider = null;
+ if (model.getWebProjectName() == null) {
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NoProjectSelected);
+ } else {
+ try {
+ IProject project = JBossWSCreationUtils.getProjectByName(model
+ .getWebProjectName());
+ if (!JavaEEProjectUtilities.isDynamicWebProject(project)) {
+ throw new Exception();
+ }
+ provider = ModelProviderManager.getModelProvider(project);
+ } catch (Exception exc) {
+ model.setWebProjectName(null);
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NoProjectSelected);
+ }
+ }
+ Object object = provider.getModelObject();
+ if (object instanceof WebApp) {
+ WebApp webApp = (WebApp) object;
+ if (model != null) {
+ List<ParamValue> theContextParams = webApp.getContextParams();
+ for (int j = 0; j < theContextParams.size(); j++) {
+ ParamValue pvalue = (ParamValue) theContextParams.get(j);
+ if (pvalue.getParamName().equals(RSMergeWebXMLCommand.RS_APPLICATION_PARM_NAME)) {
+ if (pvalue.getParamValue().equals(appClassName)) {
+ return StatusUtils
+ .errorStatus(JBossWSCreationCoreMessages.Error_JBossWS_GenerateWizard_WSName_Same);
+ }
+ }
+ }
+ return null;
+ }
+ } else if (object instanceof org.eclipse.jst.j2ee.webapplication.WebApp) {
+ org.eclipse.jst.j2ee.webapplication.WebApp webapp =
(org.eclipse.jst.j2ee.webapplication.WebApp) object;
+ if (model != null) {
+ List<ParamValue> theContextParams = webapp.getContextParams();
+ for (int j = 0; j < theContextParams.size(); j++) {
+ ParamValue pvalue = (ParamValue) theContextParams.get(j);
+ if (pvalue.getParamName().equals(RSMergeWebXMLCommand.RS_APPLICATION_PARM_NAME)) {
+ if (pvalue.getParamValue().equals(appClassName)) {
+ return StatusUtils
+ .errorStatus(JBossWSCreationCoreMessages.Error_JBossWS_GenerateWizard_WSName_Same);
+ }
+ }
+ }
+ return null;
+ }
+ }
+ return null;
+ }
+
+ public static IStatus isWSNameValid() {
+ IModelProvider provider = null;
+ if (model.getWebProjectName() == null) {
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NoProjectSelected);
+ } else {
+ try {
+ IProject project = JBossWSCreationUtils.getProjectByName(model
+ .getWebProjectName());
+ if (!JavaEEProjectUtilities.isDynamicWebProject(project)) {
+ throw new Exception();
+ }
+ provider = ModelProviderManager.getModelProvider(project);
+ } catch (Exception exc) {
+ model.setWebProjectName(null);
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_NoProjectSelected);
+ }
+ }
+ Object object = provider.getModelObject();
+ if (object instanceof WebApp) {
+ WebApp webApp = (WebApp) object;
+ if (model != null) {
+ for (int i = 0; i < descriptors.length; i++) {
+ if (descriptors[i].getName().trim().length() == 0) {
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_ServiceName_Empty);
+ }
+ List<?> theServlets = webApp.getServlets();
+ for (int j = 0; j < theServlets.size(); j++) {
+ Servlet aServlet = (Servlet) theServlets.get(j);
+ if (aServlet.getServletName().equals(
+ descriptors[i].getName())) {
+ return StatusUtils
+ .errorStatus(JBossWSCreationCoreMessages.Error_JBossWS_GenerateWizard_WSName_Same);
+ }
+ }
+ List<?> theServletMappings = webApp.getServletMappings();
+ for (int j = 0; j < theServletMappings.size(); j++) {
+ ServletMapping aServletMapping = (ServletMapping) theServletMappings
+ .get(j);
+ List<?> urlPatterns = aServletMapping.getUrlPatterns();
+ for (int k = 0; k < urlPatterns.size(); k++) {
+ UrlPatternType upt = (UrlPatternType) urlPatterns
+ .get(k);
+ if (aServletMapping.getServletName().equals(
+ descriptors[i].getName())
+ || upt.getValue().equals(
+ descriptors[i].getMappings())) {
+ return StatusUtils
+ .errorStatus(JBossWSCreationCoreMessages.Error_JBossWS_GenerateWizard_WSName_Same);
+ }
+ }
+ }
+ }
+ }
+ return null;
+ } else if (object instanceof org.eclipse.jst.j2ee.webapplication.WebApp) {
+ org.eclipse.jst.j2ee.webapplication.WebApp webApp =
(org.eclipse.jst.j2ee.webapplication.WebApp) object;
+ if (model != null) {
+ for (int i = 0; i < descriptors.length; i++) {
+ if (descriptors[i].getName().trim().length() == 0) {
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_ServiceName_Empty);
+ }
+ List<?> theServlets = webApp.getServlets();
+ for (int j = 0; j < theServlets.size(); j++) {
+ org.eclipse.jst.j2ee.webapplication.Servlet aServlet =
(org.eclipse.jst.j2ee.webapplication.Servlet) theServlets
+ .get(j);
+ if (aServlet.getServletName().equals(
+ descriptors[i].getName())) {
+ return StatusUtils
+ .errorStatus(JBossWSCreationCoreMessages.Error_JBossWS_GenerateWizard_WSName_Same);
+ }
+ }
+ List<?> theServletMappings = webApp.getServletMappings();
+ for (int j = 0; j < theServletMappings.size(); j++) {
+ org.eclipse.jst.j2ee.webapplication.ServletMapping aServletMapping =
(org.eclipse.jst.j2ee.webapplication.ServletMapping) theServletMappings
+ .get(j);
+ String url = aServletMapping.getUrlPattern();
+ if (aServletMapping.getServlet().getServletName().equals(
+ descriptors[i].getName())
+ || url.equals(descriptors[i].getMappings())) {
+ return StatusUtils
+ .errorStatus(JBossWSCreationCoreMessages.Error_JBossWS_GenerateWizard_WSName_Same);
+ }
+ }
+ }
+ }
+ return null;
+ }
+ return null;
+ }
+
+ public static IStatus isWSClassValid(String className, IProject project) {
+ if (model.getCustomPackage().trim().length() == 0) {
+ // empty package name
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageName_Cannot_Be_Empty);
+ } else if (model.getCustomClassName().trim().length() == 0) {
+ // empty class name
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_ClassName_Cannot_Be_Empty);
+ } else if (project == null
+ || !JavaEEProjectUtilities.isDynamicWebProject(project)) {
+ return null;
+ } else {
+ IStatus status = JBossWSUIUtils.validatePackageName(
+ model.getCustomPackage(), JavaCore.create(project));
+ if (status != null && status.getSeverity() == IStatus.ERROR) {
+ return status;
+ }
+ File file = JBossWSCreationUtils.findFileByPath(className + JAVA,
+ project.getLocation().toOSString());
+ if (file != null) {
+ // class already exists
+ return StatusUtils
+ .errorStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_ClassName_Same);
+ }
+ return status;
+ }
+ }
+
+ private static ServletDescriptor getServletDescriptor(String clsName) {
+ String servletName = model.getServiceName();
+ if (servletName == null) {
+ servletName = JBossWSCreationUtils
+ .classNameFromQualifiedName(clsName);
+ }
+ if (servletName.endsWith("Impl") && servletName.length() > 4) {
//$NON-NLS-1$
+ servletName = servletName.substring(0, servletName.length() - 4);
+ }
+ ServletDescriptor sd = new ServletDescriptor();
+ sd.setName(servletName);
+ sd.setDisplayName(sd.getName());
+ sd.setClassName(clsName);
+ sd.setMappings(JBossWSCreationCoreMessages.Separator_Java
+ + sd.getDisplayName());
+ return sd;
+ }
+
+}
Property changes on:
trunk/ws/plugins/org.jboss.tools.ws.ui/src/org/jboss/tools/ws/ui/wizards/JBossRSGenerateWizardValidator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain