blob: 896c2f9747b98d6c4cbbd45949ffdf033a0d4633 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004 IBM Corporation 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:
* IBM Corporation - initial API and implementation
* yyyymmdd bug Email and other contact information
* -------- -------- -----------------------------------------------------------
* 20060131 121071 rsinha@ca.ibm.com - Rupam Kuehner
* 20060206 126408 rsinha@ca.ibm.com - Rupam Kuehner
*******************************************************************************/
package org.eclipse.jst.ws.internal.consumption.ui.widgets.runtime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
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.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.server.core.FacetUtil;
import org.eclipse.jst.ws.internal.common.J2EEUtils;
import org.eclipse.jst.ws.internal.common.ResourceUtils;
import org.eclipse.jst.ws.internal.common.ServerUtils;
import org.eclipse.jst.ws.internal.consumption.common.FacetMatcher;
import org.eclipse.jst.ws.internal.consumption.common.FacetUtils;
import org.eclipse.jst.ws.internal.consumption.common.RequiredFacetVersion;
import org.eclipse.jst.ws.internal.consumption.ui.ConsumptionUIMessages;
import org.eclipse.jst.ws.internal.consumption.ui.plugin.WebServiceConsumptionUIPlugin;
import org.eclipse.jst.ws.internal.consumption.ui.preferences.PersistentServerRuntimeContext;
import org.eclipse.jst.ws.internal.consumption.ui.preferences.ProjectTopologyContext;
import org.eclipse.jst.ws.internal.consumption.ui.wsrt.ClientRuntimeDescriptor;
import org.eclipse.jst.ws.internal.consumption.ui.wsrt.FacetMatchCache;
import org.eclipse.jst.ws.internal.consumption.ui.wsrt.ServiceRuntimeDescriptor;
import org.eclipse.jst.ws.internal.consumption.ui.wsrt.WebServiceRuntimeExtensionUtils;
import org.eclipse.jst.ws.internal.consumption.ui.wsrt.WebServiceRuntimeExtensionUtils2;
import org.eclipse.jst.ws.internal.data.TypeRuntimeServer;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.command.internal.env.core.common.StatusUtils;
import org.eclipse.wst.command.internal.env.core.context.ResourceContext;
import org.eclipse.wst.command.internal.env.core.selection.SelectionList;
import org.eclipse.wst.command.internal.env.core.selection.SelectionListChoices;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.environment.IEnvironment;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.eclipse.wst.server.core.IRuntime;
import org.eclipse.wst.server.core.IServer;
import org.eclipse.wst.server.core.IServerType;
import org.eclipse.wst.server.core.ServerCore;
import org.eclipse.wst.server.core.ServerUtil;
import org.eclipse.wst.ws.internal.parser.wsil.WebServicesParser;
import org.eclipse.wst.ws.internal.wsrt.IContext;
import org.eclipse.wst.ws.internal.wsrt.ISelection;
import org.eclipse.wst.ws.internal.wsrt.IWebServiceClient;
import org.eclipse.wst.ws.internal.wsrt.IWebServiceRuntime;
import org.eclipse.wst.ws.internal.wsrt.SimpleContext;
import org.eclipse.wst.ws.internal.wsrt.WebServiceClientInfo;
import org.eclipse.wst.ws.internal.wsrt.WebServiceScenario;
import org.eclipse.wst.ws.internal.wsrt.WebServiceState;
public class ClientRuntimeSelectionWidgetDefaultingCommand extends AbstractDataModelOperation
{
private TypeRuntimeServer clientIds_;
private boolean clientIdsFixed_ = false;
private String clientRuntimeId_;
private String clientProjectName_;
private String clientEarProjectName_;
protected boolean clientNeedEAR_ = true;
private String clientComponentType_;
private FacetMatcher clientFacetMatcher_;
private IContext context_;
private ISelection selection_;
private IWebServiceClient webServiceClient_;
private ResourceContext resourceContext_;
private boolean test_;
//A note on initialSelections ...
//The difference between clientInitialProject/Component_ and initialInitialSelection is that
//clientInitialProject/Component_ comes from the ObjectSelectionOutputCommand while initialInitialSelection
//is the actual thing that was selected before the wizard was launched. In the runtime/j2ee/project
//defaulting algorithm, clientInitialSelection will be given first priority. If, however, it is
//deemed that clientInitialProject is not a valid initial selection, initialInitialSelection
//will be given second priority. Things that could make an initialSelection invalid include
//1. The containing project contains the Web service for which we are trying to create a client
//2. The containing project has a J2EE level, server target, and project type combination which
// is not supported by any of the registered Web service runtimes.
private IProject clientInitialProject_;
private String wsdlURI_;
public ClientRuntimeSelectionWidgetDefaultingCommand()
{
super();
}
public void setClientTypeRuntimeServer( TypeRuntimeServer ids )
{
clientIds_ = ids;
}
public TypeRuntimeServer getClientTypeRuntimeServer()
{
return clientIds_;
}
public void setClientIdsFixed(boolean b)
{
clientIdsFixed_ = b;
}
public String getClientRuntimeId()
{
return clientRuntimeId_;
}
public String getClientProjectName()
{
return clientProjectName_;
}
public String getClientEarProjectName()
{
return clientEarProjectName_;
}
public void setClientEarProjectName(String name)
{
clientEarProjectName_ = name;
}
public String getClientComponentType()
{
return clientComponentType_;
}
public IWebServiceClient getWebService()
{
return webServiceClient_;
}
public IContext getContext()
{
return context_;
}
public ISelection getSelection()
{
return selection_;
}
public IStatus execute(IProgressMonitor monitor, IAdaptable adaptable)
{
IEnvironment env = getEnvironment();
try
{
if (clientIdsFixed_)
{
// Set the clientRuntime based on the runtime, server, and initial
// selection.
DefaultRuntimeTriplet drt = getDefaultClientRuntimeForFixedRuntimeAndServer(clientInitialProject_);
clientFacetMatcher_ = drt.getFacetMatcher();
clientProjectName_ = drt.getProjectName();
clientRuntimeId_ = drt.getRuntimeId();
}
else
{
// Set the runtime based on the initial selection
DefaultRuntimeTriplet drt = getDefaultRuntime(clientInitialProject_, clientIds_.getTypeId(), true);
clientFacetMatcher_ = drt.getFacetMatcher();
clientProjectName_ = drt.getProjectName();
clientRuntimeId_ = drt.getRuntimeId();
if (clientRuntimeId_ == null)
{
// return and error.
}
clientIds_.setRuntimeId(WebServiceRuntimeExtensionUtils2.getClientRuntimeDescriptorById(clientRuntimeId_).getRuntime()
.getId());
}
// Set the project
if (clientProjectName_ == null)
{
// Project name did not get set when the runtime was set, so set it now
clientProjectName_ = getDefaultClientProjectName();
}
IProject clientProject = ProjectUtilities.getProject(clientProjectName_);
if (!clientProject.exists())
{
// Set the project template
clientComponentType_ = getDefaultClientProjectTemplate();
}
else
{
//Set it to an empty String
clientComponentType_ = "";
}
// Set the server
if (!clientIdsFixed_)
{
IStatus serverStatus = setClientDefaultServer();
if (serverStatus.getSeverity() == Status.ERROR)
{
env.getStatusHandler().reportError(serverStatus);
return serverStatus;
}
}
setDefaultClientEarProject();
// Calculate default IWebServiceClient
setDefaultsForExtension(env);
return Status.OK_STATUS;
} catch (Exception e)
{
// Catch all Exceptions in order to give some feedback to the user
IStatus errorStatus = StatusUtils.errorStatus(NLS.bind(ConsumptionUIMessages.MSG_ERROR_TASK_EXCEPTED,
new String[] { e.getMessage() }), e);
env.getStatusHandler().reportError(errorStatus);
return errorStatus;
}
}
private void setDefaultClientEarProject()
{
//Don't need an ear if this is a Java project, or if the selected template is jst.utility
IProject clientProject = ProjectUtilities.getProject(clientProjectName_);
if (clientProject.exists())
{
clientNeedEAR_ = !(FacetUtils.isJavaProject(clientProject));
}
else
{
clientNeedEAR_ = !(FacetUtils.isUtilityTemplate(clientComponentType_));
}
//If clientNeedEAR_ is still true, it means that we're not dealing with a Java project
//or Java project type. Check the server.
if (clientNeedEAR_)
{
// Determine if an ear selection is needed based on the server type.
String serverId = clientIds_.getServerId();
if (serverId != null)
{
// Use the server type
String serverTargetId = ServerUtils.getRuntimeTargetIdFromFactoryId(serverId);
if (serverTargetId != null && serverTargetId.length() > 0)
{
if (!ServerUtils.isTargetValidForEAR(serverTargetId, "13"))
{
// Default the EAR selection to be empty
clientNeedEAR_ = false;
}
}
}
}
if (clientNeedEAR_)
{
clientEarProjectName_ = getDefaultClientEarProjectName();
}
else
{
clientEarProjectName_ = "";
}
}
private IStatus setClientDefaultServer()
{
//Choose an existing server the module is already associated with if possible
IProject clientProject = ProjectUtilities.getProject(clientProjectName_);
IServer[] configuredServers = ServerUtil.getServersByModule(ServerUtils.getModule(clientProject), null);
if (configuredServers!=null && configuredServers.length>0)
{
clientIds_.setServerId(configuredServers[0].getServerType().getId());
clientIds_.setServerInstanceId(configuredServers[0].getId());
return Status.OK_STATUS;
}
//If the project exists, choose a suitable server or server type based on the existing project's runtime or facets
if (clientProject.exists())
{
IServer server = getServerFromProject(clientProjectName_, clientFacetMatcher_);
if (server != null)
{
clientIds_.setServerId(server.getServerType().getId());
clientIds_.setServerInstanceId(server.getId());
return Status.OK_STATUS;
}
else
{
IServerType serverType = getServerTypeFromProject(clientProjectName_, clientFacetMatcher_);
if (serverType != null)
{
clientIds_.setServerId(serverType.getId());
return Status.OK_STATUS;
}
}
}
//Haven't picked a server/server type on the basis of the project. Pick a server/server type
//that is compatible with the clientRuntimeId.
IServer server = getServerFromClientRuntimeId();
if (server!=null)
{
clientIds_.setServerId(server.getServerType().getId());
clientIds_.setServerInstanceId(server.getId());
return Status.OK_STATUS;
}
IServerType serverType = getServerTypeFromClientRuntimeId();
if (serverType != null)
{
clientIds_.setServerId(serverType.getId());
return Status.OK_STATUS;
}
//No suitable server was found. Popup an error.
String runtimeLabel = WebServiceRuntimeExtensionUtils2.getRuntimeLabelById(clientIds_.getRuntimeId());
String serverLabels = getServerLabels(clientRuntimeId_);
IStatus status = StatusUtils.errorStatus(NLS.bind(ConsumptionUIMessages.MSG_ERROR_NO_SERVER_RUNTIME, new String[]{runtimeLabel, serverLabels}) );
return status;
}
private IServer getServerFromClientRuntimeId()
{
IServer[] servers = ServerCore.getServers();
if (servers != null && servers.length > 0) {
for (int i = 0; i < servers.length; i++)
{
String serverFactoryId = servers[i].getServerType().getId();
if (WebServiceRuntimeExtensionUtils2.doesClientRuntimeSupportServer(clientRuntimeId_, serverFactoryId))
{
return servers[i];
}
}
}
return null;
}
private IServerType getServerTypeFromClientRuntimeId()
{
String[] serverTypes = WebServiceRuntimeExtensionUtils2.getServerFactoryIdsByClientRuntime(clientRuntimeId_);
if (serverTypes!=null && serverTypes.length>0)
{
//Return the preferred one if it is in the list
PersistentServerRuntimeContext context = WebServiceConsumptionUIPlugin.getInstance().getServerRuntimeContext();
String preferredServerFactoryId = context.getServerFactoryId();
for (int i=0; i<serverTypes.length; i++)
{
if (serverTypes[i].equals(preferredServerFactoryId))
{
return ServerCore.findServerType(serverTypes[i]);
}
}
return ServerCore.findServerType(serverTypes[0]);
}
return null;
}
protected IServer getServerFromProject(String projectName, FacetMatcher facetMatcher)
{
IServer server = null;
IProject project = ProjectUtilities.getProject(projectName);
IServer[] servers = ServerCore.getServers();
if (servers.length > 0)
{
// Get the runtime.
org.eclipse.wst.common.project.facet.core.runtime.IRuntime fRuntime = null;
IFacetedProject fProject = null;
if (project != null && project.exists())
{
try
{
fProject = ProjectFacetsManager.create(project);
if (fProject != null)
{
fRuntime = fProject.getRuntime();
}
} catch (CoreException ce)
{
}
}
if (fRuntime != null)
{
// Get an existing server that has the same runtime.
IRuntime sRuntime = FacetUtil.getRuntime(fRuntime);
for (int i = 0; i < servers.length; i++)
{
IServer thisServer = servers[i];
if (thisServer.getRuntime().getId().equals(sRuntime.getId()))
{
server = thisServer;
break;
}
}
}
// If an existing server could not be chosen on the basis of the project's
// runtime,
// try to find an existing server using the project's facets and the
// facetsToAdd.
if (server == null)
{
if (project!=null && project.exists())
{
Set facets = FacetUtils.getFacetsForProject(project.getName());
if (facets != null)
{
Set facetsClone = new HashSet();
facetsClone.addAll(facets);
if (facetMatcher.getFacetsToAdd() != null)
{
Iterator itr = facetMatcher.getFacetsToAdd().iterator();
while (itr.hasNext())
{
facetsClone.add(itr.next());
}
}
server = getServerFromFacets(facetsClone);
}
}
}
}
return server;
}
protected IServer getServerFromFacets(Set facets)
{
IServer server = null;
Set runtimes = FacetUtils.getRuntimes(new Set[]{facets});
Iterator itr = runtimes.iterator();
IServer[] servers = ServerCore.getServers();
outer:
while(itr.hasNext())
{
org.eclipse.wst.common.project.facet.core.runtime.IRuntime fRuntime = (org.eclipse.wst.common.project.facet.core.runtime.IRuntime)itr.next();
IRuntime sRuntime = FacetUtil.getRuntime(fRuntime);
for (int i=0; i<servers.length; i++)
{
IServer thisServer = servers[i];
if (thisServer.getRuntime().getId().equals(sRuntime.getId()))
{
server = thisServer;
break outer;
}
}
}
return server;
}
protected IServerType getServerTypeFromProject(String projectName, FacetMatcher facetMatcher)
{
IServerType serverType = null;
IProject project = ProjectUtilities.getProject(projectName);
IServerType[] serverTypes = ServerCore.getServerTypes();
//Get the runtime.
org.eclipse.wst.common.project.facet.core.runtime.IRuntime fRuntime = null;
IFacetedProject fProject = null;
if (project != null && project.exists())
{
try
{
fProject = ProjectFacetsManager.create(project);
if (fProject != null)
{
fRuntime = fProject.getRuntime();
}
} catch (CoreException ce)
{
}
}
if (fRuntime != null)
{
//Get a server type that has the same runtime type.
IRuntime sRuntime = FacetUtil.getRuntime(fRuntime);
for (int i=0; i<serverTypes.length; i++)
{
IServerType thisServerType = serverTypes[i];
if (thisServerType.getRuntimeType().getId().equals(sRuntime.getRuntimeType().getId()))
{
serverType = thisServerType;
break;
}
}
}
//If a server type could not be chosen on the basis of the project's runtime,
//try to find a server type using the project's facets and the facetsToAdd.
if (serverType == null)
{
if (project != null && project.exists())
{
Set facets = FacetUtils.getFacetsForProject(project.getName());
if (facets != null)
{
Set facetsClone = new HashSet();
facetsClone.addAll(facets);
if (facetMatcher.getFacetsToAdd() != null)
{
Iterator itr = facetMatcher.getFacetsToAdd().iterator();
while (itr.hasNext())
{
facetsClone.add(itr.next());
}
}
serverType = getServerTypeFromFacets(facetsClone);
}
}
}
return serverType;
}
protected IServerType getServerTypeFromFacets(Set facets)
{
IServerType serverType = null;
Set runtimes = FacetUtils.getRuntimes(new Set[]{facets});
Iterator itr = runtimes.iterator();
IServerType[] serverTypes = ServerCore.getServerTypes();
outer:
while(itr.hasNext())
{
org.eclipse.wst.common.project.facet.core.runtime.IRuntime fRuntime = (org.eclipse.wst.common.project.facet.core.runtime.IRuntime)itr.next();
IRuntime sRuntime = FacetUtil.getRuntime(fRuntime);
for (int i=0; i<serverTypes.length; i++)
{
IServerType thisServerType = serverTypes[i];
if (thisServerType.getRuntimeType().getId().equals(sRuntime.getRuntimeType().getId()))
{
serverType = thisServerType;
break outer;
}
}
}
return serverType;
}
protected IServer getServerFromProjectType(String templateId, FacetMatcher facetMatcher)
{
IServer server = null;
Set facets = FacetUtils.getInitialFacetVersionsFromTemplate(templateId);
if (facetMatcher.getFacetsToAdd() != null)
{
Iterator itr = facetMatcher.getFacetsToAdd().iterator();
while (itr.hasNext())
{
facets.add(itr.next());
}
}
server = getServerFromFacets(facets);
return server;
}
protected IServerType getServerTypeFromProjectType(String templateId, FacetMatcher facetMatcher)
{
IServerType serverType = null;
Set facets = FacetUtils.getInitialFacetVersionsFromTemplate(templateId);
if (facetMatcher.getFacetsToAdd() != null)
{
Iterator itr = facetMatcher.getFacetsToAdd().iterator();
while (itr.hasNext())
{
facets.add(itr.next());
}
}
//TODO Instead of passing in a single set of facets, pass in multiple sets, if the
//jst.java facet is one of them and the clientRuntimeId allows newer.
serverType = getServerTypeFromFacets(facets);
return serverType;
}
private String getDefaultClientProjectTemplate()
{
String[] templates = WebServiceRuntimeExtensionUtils2.getClientProjectTemplates(clientIds_.getTypeId(), clientIds_.getRuntimeId());
//Pick the Web one if it's there, otherwise pick the first one.
//Walk the list of client project types in the project topology preference
ProjectTopologyContext ptc= WebServiceConsumptionUIPlugin.getInstance().getProjectTopologyContext();
String[] preferredTemplateIds = ptc.getClientTypes();
for (int j=0; j<preferredTemplateIds.length; j++)
{
for (int i=0; i<templates.length; i++)
{
String templateId = templates[i];
if (templateId.equals(preferredTemplateIds[j]))
{
boolean matches = WebServiceRuntimeExtensionUtils2.doesClientRuntimeSupportTemplate(clientRuntimeId_, templateId);
if (matches)
{
return templates[i];
}
}
}
}
//Didn't find a template id in the preferred list that worked.
//Return the first one that is a match.
for (int j = 0; j < templates.length; j++)
{
String templateId = templates[j];
boolean matches = WebServiceRuntimeExtensionUtils2.doesClientRuntimeSupportTemplate(clientRuntimeId_, templateId);
if (matches)
{
return templates[j];
}
}
//Still nothing, return the first one if available.
if (templates.length > 0)
return templates[0];
return "";
}
private String getDefaultClientProjectName()
{
IProject[] projects = FacetUtils.getAllProjects();
ClientRuntimeDescriptor desc = WebServiceRuntimeExtensionUtils2.getClientRuntimeDescriptorById(clientRuntimeId_);
RequiredFacetVersion[] rfvs = desc.getRequiredFacetVersions();
//Check each project and its facetRuntime for compatibility with the clientRuntime
for (int i=0; i<projects.length; i++)
{
Set facetVersions = FacetUtils.getFacetsForProject(projects[i].getName());
org.eclipse.wst.common.project.facet.core.runtime.IRuntime fRuntime = null;
String fRuntimeName = null;
fRuntime = FacetUtils.getFacetRuntimeForProject(projects[i].getName());
if (fRuntime != null)
{
fRuntimeName = fRuntime.getName();
}
if (facetVersions != null)
{
//FacetMatcher fm = FacetUtils.match(rfvs, facetVersions);
FacetMatcher fm = FacetMatchCache.getInstance().getMatchForProject(true, clientRuntimeId_, projects[i].getName());
boolean facetRuntimeMatches = true;
if (fRuntimeName != null)
{
facetRuntimeMatches = FacetUtils.isFacetRuntimeSupported(rfvs, fRuntimeName);
}
if (fm.isMatch() && facetRuntimeMatches)
{
clientFacetMatcher_ = fm;
return projects[i].getName();
}
}
}
//No project was suitable, return a new project name
return ResourceUtils.getDefaultWebProjectName();
}
private DefaultRuntimeTriplet getDefaultClientRuntimeForFixedRuntimeAndServer(IProject project)
{
String[] clientRuntimes = WebServiceRuntimeExtensionUtils2.getClientRuntimesByType(clientIds_.getTypeId());
ArrayList validClientRuntimes = new ArrayList();
for (int i=0; i<clientRuntimes.length; i++ )
{
ClientRuntimeDescriptor desc = WebServiceRuntimeExtensionUtils2.getClientRuntimeDescriptorById(clientRuntimes[i]);
if (desc.getRuntime().getId().equals(clientIds_.getRuntimeId()))
{
//Check if this client runtime supports the server
if (WebServiceRuntimeExtensionUtils2.doesClientRuntimeSupportServer(desc.getId(), clientIds_.getServerId()))
{
validClientRuntimes.add(desc.getId());
if (project != null && project.exists())
{
//RequiredFacetVersion[] rfv = desc.getRequiredFacetVersions();
Set facetVersions = FacetUtils.getFacetsForProject(project.getName());
if (facetVersions != null)
{
//FacetMatcher fm = FacetUtils.match(rfv, facetVersions);
FacetMatcher fm = FacetMatchCache.getInstance().getMatchForProject(true, clientRuntimes[i], project.getName());
if (fm.isMatch())
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(fm);
drt.setProjectName(project.getName());
drt.setRuntimeId(desc.getId());
return drt;
}
}
}
}
}
}
if (validClientRuntimes.size() > 0)
{
//We couldn't match to the initially selected project so return the first valid runtime.
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(null);
drt.setProjectName(null);
drt.setRuntimeId(((String[])validClientRuntimes.toArray(new String[0]))[0]);
return drt;
}
else
{
//There are no client runtimes that match the fixed runtime and server. Fall back to original algorithm
clientIdsFixed_ = false;
return getDefaultRuntime(project, clientIds_.getTypeId(), true);
}
}
protected DefaultRuntimeTriplet getDefaultRuntime(IProject project, String typeId, boolean isClient)
{
String[] runtimes = null;
if (isClient)
{
runtimes = WebServiceRuntimeExtensionUtils2.getClientRuntimesByType(typeId);
}
else
{
runtimes = WebServiceRuntimeExtensionUtils2.getServiceRuntimesByServiceType(typeId);
}
//Split the array of service/client runtimes into one containing the preferred set and one containing the rest.
PersistentServerRuntimeContext context = WebServiceConsumptionUIPlugin.getInstance().getServerRuntimeContext();
String preferredRuntimeId = context.getRuntimeId();
ArrayList preferredRuntimeIdsList = new ArrayList();
ArrayList otherRuntimeIdsList = new ArrayList();
for (int k=0; k<runtimes.length; k++ )
{
String descRuntimeId = null;
if (isClient)
{
ClientRuntimeDescriptor desc = WebServiceRuntimeExtensionUtils2.getClientRuntimeDescriptorById(runtimes[k]);
descRuntimeId = desc.getRuntime().getId();
}
else
{
ServiceRuntimeDescriptor desc = WebServiceRuntimeExtensionUtils2.getServiceRuntimeDescriptorById(runtimes[k]);
descRuntimeId = desc.getRuntime().getId();
}
if (descRuntimeId.equals(preferredRuntimeId))
{
preferredRuntimeIdsList.add(runtimes[k]);
}
else
{
otherRuntimeIdsList.add(runtimes[k]);
}
}
String[] preferredRuntimeIds = (String[])preferredRuntimeIdsList.toArray(new String[0]);
String[] otherRuntimeIds = (String[])otherRuntimeIdsList.toArray(new String[0]);
Set facetVersions = null;
org.eclipse.wst.common.project.facet.core.runtime.IRuntime fRuntime = null;
String fRuntimeName = null;
//If the initially selected project exists and facets can be inferred from it, look for
//a service/client runtime that matches the project's facets and also, if possible, its facet runtime.
//Preference should be given to the preferred service/client runtimes.
if (project != null && project.exists())
{
facetVersions = FacetUtils.getFacetsForProject(project.getName());
fRuntime = FacetUtils.getFacetRuntimeForProject(project.getName());
fRuntimeName = null;
if (fRuntime != null)
{
fRuntimeName = fRuntime.getName();
}
if (facetVersions != null)
{
//1. First check to see if one of the preferred service/client runtimes matches the existing
//project's facets and runtime.
for (int p = 0; p < preferredRuntimeIds.length; p++)
{
RequiredFacetVersion[] prfv = null;
if (isClient)
{
prfv = WebServiceRuntimeExtensionUtils2.getClientRuntimeDescriptorById(preferredRuntimeIds[p]).getRequiredFacetVersions();
}
else
{
prfv = WebServiceRuntimeExtensionUtils2.getServiceRuntimeDescriptorById(preferredRuntimeIds[p]).getRequiredFacetVersions();
}
//FacetMatcher fm = FacetUtils.match(prfv, facetVersions);
FacetMatcher fm = FacetMatchCache.getInstance().getMatchForProject(isClient, preferredRuntimeIds[p], project.getName());
boolean facetRuntimeMatches = true;
if (fRuntimeName != null)
{
facetRuntimeMatches = FacetUtils.isFacetRuntimeSupported(prfv, fRuntimeName);
}
if (fm.isMatch() && facetRuntimeMatches)
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(fm);
drt.setProjectName(project.getName());
drt.setRuntimeId(preferredRuntimeIds[p]);
return drt;
}
}
//2. Second, check to see if one of the other clientRuntimes matches the existing
//project's facets and runtime.
for (int i=0; i<otherRuntimeIds.length; i++)
{
RequiredFacetVersion[] rfv = null;
if (isClient)
{
rfv = WebServiceRuntimeExtensionUtils2.getClientRuntimeDescriptorById(otherRuntimeIds[i]).getRequiredFacetVersions();
}
else
{
rfv = WebServiceRuntimeExtensionUtils2.getServiceRuntimeDescriptorById(otherRuntimeIds[i]).getRequiredFacetVersions();
}
FacetMatcher fm = FacetMatchCache.getInstance().getMatchForProject(isClient, otherRuntimeIds[i], project.getName());
boolean facetRuntimeMatches = true;
if (fRuntimeName != null)
{
facetRuntimeMatches = FacetUtils.isFacetRuntimeSupported(rfv, fRuntimeName);
}
if (fm.isMatch() && facetRuntimeMatches)
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(fm);
drt.setProjectName(project.getName());
drt.setRuntimeId(otherRuntimeIds[i]);
return drt;
}
}
//3. Third, check to see if one of the preferred clientRuntimes matches the existing
//project's facets.
for (int p = 0; p < preferredRuntimeIds.length; p++)
{
FacetMatcher fm = FacetMatchCache.getInstance().getMatchForProject(isClient, preferredRuntimeIds[p], project.getName());
if (fm.isMatch())
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(fm);
drt.setProjectName(project.getName());
drt.setRuntimeId(preferredRuntimeIds[p]);
return drt;
}
}
//4. Fourth, check to see if the one the other clientRuntimes matches the existing
//project's facets.
for (int i=0; i<otherRuntimeIds.length; i++)
{
FacetMatcher fm = FacetMatchCache.getInstance().getMatchForProject(isClient, otherRuntimeIds[i], project.getName());
if (fm.isMatch())
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(fm);
drt.setProjectName(project.getName());
drt.setRuntimeId(otherRuntimeIds[i]);
return drt;
}
}
}
}
//Haven't returned yet so this means that the intitially selected project cannot be used
//to influence the selection of the service/client runtime.
//If this is client defaulting, use the preferred client project types to
//influence the selection of a runtime.
if (isClient)
{
ProjectTopologyContext ptc = WebServiceConsumptionUIPlugin.getInstance().getProjectTopologyContext();
String[] preferredTemplateIds = ptc.getClientTypes();
for (int n=0; n<preferredTemplateIds.length; n++)
{
String preferredTemplateId = preferredTemplateIds[n];
//Set templateFacetVersions = FacetUtils.getInitialFacetVersionsFromTemplate(preferredTemplateId);
for (int m=0; m<preferredRuntimeIds.length; m++)
{
//If this clientRuntime supports this template, choose it and exit.
//ClientRuntimeDescriptor desc = WebServiceRuntimeExtensionUtils2.getClientRuntimeDescriptorById(preferredRuntimeIds[m]);
boolean matches = WebServiceRuntimeExtensionUtils2.doesClientRuntimeSupportTemplate(preferredRuntimeIds[m], preferredTemplateId);
if (matches)
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(null);
drt.setProjectName(null);
drt.setRuntimeId(preferredRuntimeIds[m]);
return drt;
}
}
}
}
//Still haven't returned. Return the first preferred service/client runtime id.
if (preferredRuntimeIds.length > 0)
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(null);
drt.setProjectName(null);
drt.setRuntimeId(preferredRuntimeIds[0]);
return drt;
}
if (runtimes.length > 0)
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(null);
drt.setProjectName(null);
drt.setRuntimeId(runtimes[0]);
return drt;
}
else
{
DefaultRuntimeTriplet drt = new DefaultRuntimeTriplet();
drt.setFacetMatcher(null);
drt.setProjectName(null);
drt.setRuntimeId(null);
return drt;
}
}
/**
*
* @param project
* @return
*/
protected SelectionListChoices getProjectEARChoice(IProject project)
{
String[] flexProjects = getAllFlexibleProjects();
SelectionList list = new SelectionList(flexProjects, 0);
return new SelectionListChoices(list, null);
}
protected SelectionList getEARProjects()
{
String[] flexProjects = getAllFlexibleProjects();
return new SelectionList(flexProjects, 0);
}
protected IResource getResourceFromInitialSelection(IStructuredSelection selection)
{
if (selection != null && selection.size() == 1)
{
Object obj = selection.getFirstElement();
if (obj != null)
{
try
{
IResource resource = ResourceUtils.getResourceFromSelection(obj);
return resource;
} catch(CoreException e)
{
return null;
}
}
}
return null;
}
protected String getDefaultClientEarProjectName()
{
IProject clientProject = ProjectUtilities.getProject(clientProjectName_);
IVirtualComponent[] earComps = J2EEUtils.getReferencingEARComponents(clientProject);
if (earComps.length>0)
{
//Pick the first one
return earComps[0].getName();
}
//Either project does not exist or component is not associated with any EARs, so pick the first EAR you see with the correct J2EE version.
IVirtualComponent[] allEarComps = J2EEUtils.getAllEARComponents();
if (allEarComps.length>0)
{
//TODO Choose an existing EAR that can be added to the server and who's J2EE level in consistent with
//that of the selected project, if applicable. Picking the first one for now.
return allEarComps[0].getName();
}
else
{
//there are no Ears.
return ResourceUtils.getDefaultClientEARProjectName();
}
}
private String getServerLabels(String clientRuntimeId)
{
String[] validServerFactoryIds = WebServiceRuntimeExtensionUtils2.getServerFactoryIdsByClientRuntime(clientRuntimeId);
//String[] validServerLabels = new String[validServerFactoryIds.length];
StringBuffer validServerLabels = new StringBuffer();
for (int i=0; i<validServerFactoryIds.length; i++)
{
if (i>0)
{
validServerLabels.append(", ");
}
validServerLabels.append(WebServiceRuntimeExtensionUtils.getServerLabelById(validServerFactoryIds[i]));
}
return validServerLabels.toString();
}
/*
* Update the client project, client project type and clientRuntime as needed.
* Used by ServerRuntimeSelectionWidgetDefaultingCommand to update client side
* stuff based on service side defaults.
*/
protected void updateClientProject(String projectName, String serviceTypeId)
{
boolean isEJB = false;
String implId = WebServiceRuntimeExtensionUtils2.getWebServiceImplIdFromTypeId(serviceTypeId);
isEJB = (implId.equals("org.eclipse.jst.ws.wsImpl.ejb"));
String[] updatedNames = ResourceUtils.getClientProjectComponentName(projectName, projectName, isEJB);
clientProjectName_ = updatedNames[0];
IProject clientProject = ProjectUtilities.getProject(clientProjectName_);
if (clientProject.exists())
{
clientComponentType_ = "";
clientRuntimeId_ = WebServiceRuntimeExtensionUtils2.getClientRuntimeId(clientIds_, projectName, clientComponentType_);
}
else
{
//Pick a client project type based on project topology preferences. Modify the clientRuntimeId_
//as needed.
String[] templateIds = WebServiceRuntimeExtensionUtils2.getClientProjectTemplates(clientIds_.getTypeId(), clientIds_.getRuntimeId());
ProjectTopologyContext ptc = WebServiceConsumptionUIPlugin.getInstance().getProjectTopologyContext();
String[] preferredTemplateIds = ptc.getClientTypes();
outer:
for (int j = 0; j < preferredTemplateIds.length; j++)
{
for (int i = 0; i < templateIds.length; i++)
{
String templateId = templateIds[i];
if (templateId.equals(preferredTemplateIds[j]))
{
//Get a clientRuntimeId for this template
String newClientRuntimeId = WebServiceRuntimeExtensionUtils2.getClientRuntimeId(clientIds_, clientProjectName_, templateId);
if (newClientRuntimeId.length() > 0)
{
clientRuntimeId_ = newClientRuntimeId;
clientComponentType_ = templateId;
break outer;
}
}
}
}
}
}
private void setDefaultsForExtension(IEnvironment env)
{
IWebServiceRuntime wsrt = WebServiceRuntimeExtensionUtils2.getClientRuntime(clientRuntimeId_);
if (wsrt != null)
{
WebServiceClientInfo wsInfo = new WebServiceClientInfo();
wsInfo.setServerFactoryId(clientIds_.getServerId());
wsInfo.setServerInstanceId(clientIds_.getServerInstanceId());
wsInfo.setState(WebServiceState.UNKNOWN_LITERAL);
wsInfo.setWebServiceRuntimeId(clientIds_.getRuntimeId());
wsInfo.setWsdlURL(wsdlURI_);
webServiceClient_ = wsrt.getWebServiceClient(wsInfo);
WebServiceScenario scenario = WebServiceScenario.CLIENT_LITERAL;
if (resourceContext_ != null)
{
context_ = new SimpleContext(true, true, true, true, true, true, test_, false, scenario, resourceContext_.isOverwriteFilesEnabled(), resourceContext_
.isCreateFoldersEnabled(), resourceContext_.isCheckoutFilesEnabled());
}
}
}
public void setClientInitialSelection(IStructuredSelection selection)
{
if (clientInitialProject_ == null)
{
clientInitialProject_ = getProjectFromInitialSelection(selection);
}
}
public void setClientInitialProject(IProject clientInitialProject)
{
clientInitialProject_ = clientInitialProject;
}
/**
* @param initialInitialSelection_ The initialInitialSelection_ to set.
*/
public void setInitialInitialSelection(IStructuredSelection initialInitialSelection)
{
// TODO Delete this method and corresponding mappings
// if no longer required.
}
public boolean getClientNeedEAR()
{
return clientNeedEAR_;
}
/**
* @param parser_ The parser_ to set.
*/
public void setWebServicesParser(WebServicesParser parser) {
// TODO Delete this method and corresponding mappings
// if no longer required.
}
public void setWsdlURI(String wsdlURI)
{
wsdlURI_ = wsdlURI;
}
public void setTestService(boolean testService)
{
test_ = testService;
}
public void setResourceContext( ResourceContext resourceContext )
{
resourceContext_ = resourceContext;
}
private IProject getProjectFromInitialSelection(IStructuredSelection selection)
{
if (selection != null && selection.size() == 1)
{
Object obj = selection.getFirstElement();
if (obj != null)
{
try
{
IResource resource = ResourceUtils.getResourceFromSelection(obj);
if (resource==null)
return null;
IProject p = ResourceUtils.getProjectOf(resource.getFullPath());
return p;
} catch(CoreException e)
{
return null;
}
}
}
return null;
}
protected String[] getAllFlexibleProjects()
{
Vector v = new Vector();
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (int i = 0; i < projects.length; i++)
{
if (!projects[i].getName().equals("Servers") && !projects[i].getName().startsWith("."))
{
v.add(projects[i].getName());
}
}
return (String[])v.toArray(new String[0]);
}
protected class FacetRuntimeMatcher
{
FacetMatcher facetMatcher;
boolean runtimeMatches;
public FacetMatcher getFacetMatcher()
{
return facetMatcher;
}
public void setFacetMatcher(FacetMatcher facetMatcher)
{
this.facetMatcher = facetMatcher;
}
public boolean isRuntimeMatches()
{
return runtimeMatches;
}
public void setRuntimeMatches(boolean runtimeMatches)
{
this.runtimeMatches = runtimeMatches;
}
}
protected class DefaultRuntimeTriplet
{
FacetMatcher facetMatcher_;
String projectName_;
String runtimeId_;
public DefaultRuntimeTriplet()
{
}
public FacetMatcher getFacetMatcher()
{
return facetMatcher_;
}
public void setFacetMatcher(FacetMatcher facetMatcher_)
{
this.facetMatcher_ = facetMatcher_;
}
public String getProjectName()
{
return projectName_;
}
public void setProjectName(String projectName_)
{
this.projectName_ = projectName_;
}
public String getRuntimeId()
{
return runtimeId_;
}
public void setRuntimeId(String runtimeId_)
{
this.runtimeId_ = runtimeId_;
}
}
}