/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.muse.tools.generator.projectizer;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.w3c.dom.Document;
import org.apache.muse.tools.generator.util.Capability;
import org.apache.muse.tools.generator.util.ConfigurationData;
import org.apache.muse.tools.generator.util.ConfigurationDataDescriptor;
import org.apache.muse.tools.generator.util.DeploymentDescriptorHelper;
import org.apache.muse.tools.generator.util.ServicesDescriptorHelper;
import org.apache.muse.util.FileUtils;
import org.apache.muse.ws.notification.WsnConstants;
import org.apache.muse.ws.resource.metadata.MetadataDescriptor;
import org.apache.muse.ws.resource.metadata.impl.WsrmdUtils;
import org.apache.muse.ws.wsdl.WsdlUtils;
/**
*
* A Projectizer for generating projects that target the J2EE Axis2 platform.
*
* This projectizer will create a directory structure as follow:
*
* <ul>
* <li>JavaSource/: if there is any source to generate it will go into this folder</li>
* <li>WebContent/: this is a folder that has underneath it an exploded WAR that
* has all of the descriptors required to deploy to a J2EE server. This
* also includes WSDL and muse.xml descriptors.</li>
* <li>build.xml: an Ant build script that will build in the current directory and
* generate a packaged WAR file that is ready to be deployed. This build script
* will compile what's in JavaSource, package it as a jar, put it into
* WebContent/WEB-INF/lib and then create the WAR from the contents of
* WebContent.</li>
* </ul>
*
* @author Andrew Eberbach (aeberbac)
*/
public class J2EEAxis2Projectizer extends AbstractProjectizer {
static ConfigurationDataDescriptor[] REQUIRED_PARAMETERS =
new ConfigurationDataDescriptor[] {
ConfigurationData.FILES_MAP_LIST_CONFIGURATION,
ConfigurationData.DESCRIPTOR_DOCUMENT_CONFIGURATION,
ConfigurationData.WSDL_DOCUMENT_LIST_CONFIGURATION,
ConfigurationData.OVERWRITE_CONFIGURATION,
ConfigurationData.CAPABILITIES_MAP_LIST_CONFIGURATION
};
protected Map[] _capabilitiesList = null;
protected Map[] _filesMaps = null;
protected Document _descriptor = null;
protected Document[] _wsdls = null;
protected Set[] _ignoreSets;
protected MetadataDescriptor[] _metadatas;
public void projectize(ConfigurationData configuration) throws Exception {
ConfigurationData.checkConfiguration(this, configuration);
loadParameters(configuration);
File webContentDir = new File(
_targetDirectory,
Axis2ProjectizerConstants.WEB_CONTENT_DIR);
File javaSourceDir = new File(
_targetDirectory,
Axis2ProjectizerConstants.JAVA_SRC_DIR);
createDirectoryStructure(webContentDir);
createJavaSources(javaSourceDir, _filesMaps, _ignoreSets);
createArtifacts(webContentDir);
createOverwriteManifest();
}
private void createDirectoryStructure(File webContentDir) throws Exception {
File templateDir = new File(
System.getProperty(Axis2ProjectizerConstants.MUSE_HOME_PROPERTY),
Axis2ProjectizerConstants.TEMPLATE_DIR);
copyTemplate(templateDir, webContentDir);
File libDir = new File(
webContentDir,
Axis2ProjectizerConstants.LIB_DIR);
File modulesDir = new File(
System.getProperty(Axis2ProjectizerConstants.MUSE_HOME_PROPERTY),
Axis2ProjectizerConstants.MODULES_DIR);
copyJars(Axis2ProjectizerConstants.REQUIRED_MODULES, modulesDir, libDir);
}
protected void createArtifacts(File webContentDir) throws Exception {
File descriptorFile = new File(
webContentDir,
Axis2ProjectizerConstants.DESCRIPTOR_FILE);
File wsdldir = new File(
webContentDir,
Axis2ProjectizerConstants.WSDL_DIR);
File routerEntriesDir = new File(
webContentDir,
Axis2ProjectizerConstants.ROUTER_ENTRIES_DIR);
File servicesFile = new File(
webContentDir,
Axis2ProjectizerConstants.SERVICES_FILE);
ServicesDescriptorHelper servicesHelper = new ServicesDescriptorHelper();
for(int i=0; i < _capabilitiesList.length; i++) {
Map capabilities = _capabilitiesList[i];
Document wsdl = _wsdls[i];
MetadataDescriptor rmd = _metadatas[i];
createDescriptor(
_descriptor,
wsdl,
descriptorFile,
capabilities,
Axis2ProjectizerConstants.WSDL_RELATIVE_PATH,
i);
createWSDLFile(wsdl, wsdldir);
createRMDFile(rmd, wsdl, wsdldir);
createRouterEntries(
routerEntriesDir,
WsdlUtils.getServiceName(wsdl.getDocumentElement()),
capabilities);
updateServicesDescriptor(servicesHelper, wsdl, capabilities);
}
createServicesDescriptor(servicesHelper, servicesFile);
createBuildFile(
_targetDirectory,
Axis2ProjectizerConstants.BUILD_FILE_RESOURCE,
Axis2ProjectizerConstants.BUILD_FILE);
}
protected void createServicesDescriptor(ServicesDescriptorHelper servicesHelper, File servicesFile) throws Exception {
writeToFileCheck(servicesHelper.getDocument(), servicesFile);
}
protected void loadParameters(ConfigurationData configuration) {
_capabilitiesList = (Map[])configuration.getParameter(ConfigurationData.CAPABILITIES_MAP_LIST);
_filesMaps = (Map[])configuration.getParameter(ConfigurationData.FILES_MAP_LIST);
_descriptor = (Document)configuration.getParameter(ConfigurationData.DESCRIPTOR_DOCUMENT);
_wsdls = (Document[])configuration.getParameter(ConfigurationData.WSDL_DOCUMENT_LIST);
_ignoreSets = (Set[])configuration.getParameter(ConfigurationData.IGNORE_SET_LIST);
_metadatas = (MetadataDescriptor[])configuration.getParameter(ConfigurationData.METADATA_DESCRIPTOR_LIST);
boolean overwrite = ((Boolean)configuration.getParameter(ConfigurationData.OVERWRITE)).booleanValue();
setTargetDirectory((File)configuration.getParameter(ConfigurationData.TARGET_DIRECTORY), overwrite);
}
protected void createWSDLFile(Document wsdl, File wsdldir) throws Exception {
File wsdlFile = new File(getWsdlFileName(wsdldir.getPath(), wsdl));
writeToFileCheck(wsdl, wsdlFile);
}
protected void createRMDFile(MetadataDescriptor rmd, Document wsdl, File wsdldir) throws Exception {
if(rmd == null) {
return;
}
File rmdFile = new File(getMetadataFileName(wsdldir.getPath(), wsdl));
updatePortType(wsdl, rmd, rmdFile);
writeToFileCheck(WsrmdUtils.createMetadataDocument(rmd), rmdFile);
}
protected void createBuildFile(File baseTargetDir, String buildFileResource, String buildFile) throws Exception {
InputStream buildTemplate = FileUtils.loadFromContext(this.getClass(),buildFileResource);
File build = new File(baseTargetDir, buildFile);
copyStreamCheck(buildTemplate, build);
}
protected void updateServicesDescriptor(ServicesDescriptorHelper servicesHelper, Document wsdl, Map capabilities) {
String serviceName = WsdlUtils.getServiceName(wsdl.getDocumentElement());
servicesHelper.createService(serviceName);
for (Iterator i = capabilities.values().iterator(); i.hasNext();) {
servicesHelper.addActionMappings((Capability) i.next(), serviceName);
}
}
protected void createDescriptor(Document descriptorDocument, Document wsdl, File descriptorFile, Map capabilities, String wsdlRelativePath, int resourceIndex) throws Exception {
//
// no additional muse.xml for subscriptions or SG entries
//
// TODO: add a better check for subscription manager and also add
// a check when servicegroup generation comes in
//
if (capabilities.keySet().contains(WsnConstants.SUBSCRIPTION_MGR_URI))
return;
DeploymentDescriptorHelper helper = new DeploymentDescriptorHelper(descriptorDocument, wsdl, resourceIndex);
//update the wsdl file location
helper.setWsdlFile(getWsdlFileName(wsdlRelativePath, wsdl));
//update the service name
helper.setContextPath(WsdlUtils.getServiceName(wsdl.getDocumentElement()));
//update the name of the class that is the base resource
helper.setJavaResourceClass(getResourceClass(capabilities).getName());
//add the capabilities
for(Iterator i=capabilities.values().iterator(); i.hasNext();) {
helper.addCapability((Capability)i.next());
}
writeToFileCheck(descriptorDocument, descriptorFile);
}
protected Class getResourceClass(Map capabilities) {
for(Iterator i = capabilities.values().iterator(); i.hasNext();) {
Capability capability = (Capability)i.next();
if(capability.getProperties().size() > 0) {
return org.apache.muse.ws.resource.impl.SimpleWsResource.class;
}
}
return org.apache.muse.core.SimpleResource.class;
}
public ConfigurationDataDescriptor[] getConfigurationDataDescriptions() {
return REQUIRED_PARAMETERS;
}
}