Java tutorial
/* * The DecidR Development Team 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 de.decidr.model.workflowmodel.deployment; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import org.apache.commons.io.IOUtils; import org.w3c.dom.Node; import de.decidr.model.URLGenerator; import de.decidr.model.XmlTools; import de.decidr.model.entities.ServerLoadView; import de.decidr.model.enums.ServerTypeEnum; import de.decidr.model.schema.bpel.dd.TDeployment; import de.decidr.model.schema.bpel.executable.TProcess; import de.decidr.model.schema.wsdl.ObjectFactory; import de.decidr.model.schema.wsdl.TDefinitions; import de.decidr.model.schema.wsdl.TDocumented; import de.decidr.model.schema.wsdl.TPort; import de.decidr.model.schema.wsdl.TService; import de.decidr.model.workflowmodel.transformation.TransformConstants; import de.decidr.model.workflowmodel.transformation.helper.TransformationHelper; /** * This class builds the package, i.e. the zip file needed for deployment * * @author Modood Alvi * @author David Kruzic * @author Oliver Sonnauer * @version 0.1 */ public class PackageBuilder { /** * @param workFlowId * @param bpel * @param wsdl * @param dd * depoyment descriptor * @param serverLocation * location for service endpoints * @return the zip'ed package as byte array * @throws JAXBException * @throws IOException */ public static byte[] getPackage(long workFlowId, TProcess bpel, TDefinitions wsdl, TDeployment dd, HashMap<String, List<ServerLoadView>> serverMap) throws JAXBException, IOException { ByteArrayOutputStream zipOut = new ByteArrayOutputStream(); TransformationHelper th = new TransformationHelper(); de.decidr.model.schema.bpel.executable.ObjectFactory of = new de.decidr.model.schema.bpel.executable.ObjectFactory(); de.decidr.model.schema.wsdl.ObjectFactory ofwasl = new de.decidr.model.schema.wsdl.ObjectFactory(); de.decidr.model.schema.bpel.dd.ObjectFactory ofdd = new de.decidr.model.schema.bpel.dd.ObjectFactory(); JAXBElement<TDefinitions> def = ofwasl.createDefinitions(wsdl); JAXBElement<TDeployment> d = ofdd.createDeploy(dd); String bpelFilename = "BPELPROCESS_" + workFlowId + ".bpel"; String wsdlFilename = TransformConstants.FILENAME_WSDL_PROCESS_NAME_TEMPLATE.replace("?", workFlowId + ""); String ddFilename = "deploy.xml"; ZipOutputStream zip_out_stream = new ZipOutputStream(zipOut); // Add BPEL to zip zip_out_stream.putNextEntry(new ZipEntry(bpelFilename)); String fileBPEL = th.jaxbObjectToStringWithWorkflowNamespace(of.createProcess(bpel), bpel.getTargetNamespace(), TransformationHelper.BPEL_CLASSES); zip_out_stream.write(fileBPEL.getBytes()); zip_out_stream.closeEntry(); // Add WSDL to zip zip_out_stream.putNextEntry(new ZipEntry(wsdlFilename)); String fileWSDL = th.jaxbObjectToStringWithWorkflowNamespace(def, bpel.getTargetNamespace(), TransformationHelper.WSDL_CLASSES); zip_out_stream.write(fileWSDL.getBytes()); zip_out_stream.closeEntry(); // Add Deployment Descriptor to zip zip_out_stream.putNextEntry(new ZipEntry(ddFilename)); String fileDD = th.jaxbObjectToStringWithWorkflowNamespace(d, bpel.getTargetNamespace(), TransformationHelper.DD_CLASSES); zip_out_stream.write(fileDD.getBytes()); zip_out_stream.closeEntry(); // Add the EMail WSDL to package and modify the service endpoint zip_out_stream.putNextEntry(new ZipEntry(TransformConstants.FILENAME_WSDL_EMAIL)); zip_out_stream.write(getEmailWsdl( URLGenerator.getEmailWSUrl(serverMap.get(ServerTypeEnum.Ode.toString()).get(0).getLocation()))); zip_out_stream.closeEntry(); // Add the HT WSDL to package and modify the service endpoint zip_out_stream.putNextEntry(new ZipEntry(TransformConstants.FILENAME_WSDL_HUMANTASK)); zip_out_stream.write(getHTWsdl( URLGenerator.getHumanTaskWSUrl(serverMap.get(ServerTypeEnum.Ode.toString()).get(0).getLocation()))); zip_out_stream.closeEntry(); // Add the PS WSDL to package and modify the service endpoint zip_out_stream.putNextEntry(new ZipEntry(TransformConstants.FILENAME_WSDL_PROXY)); zip_out_stream.write(getPSWsdl(URLGenerator .getProxyServiceWSUrl(serverMap.get(ServerTypeEnum.Ode.toString()).get(0).getLocation()))); zip_out_stream.closeEntry(); // Add DecidrTypes schema to package zip_out_stream.putNextEntry(new ZipEntry(TransformConstants.FILENAME_XSD_DECIDR_TYPES)); zip_out_stream.write(loadSchema(TransformConstants.FILENAME_XSD_DECIDR_TYPES)); zip_out_stream.closeEntry(); zip_out_stream.close(); return zipOut.toByteArray(); } /** * @param name * of the resource * @return the content of the resource as byte array * @throws IOException */ private static byte[] loadSchema(String name) throws IOException { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); ByteArrayOutputStream result = new ByteArrayOutputStream(); try { IOUtils.copy(in, result); } finally { if (in != null) { in.close(); } } return result.toByteArray(); } /** * @param emailServiceLocation * URL of the EmailWS endpoint * @return EmailWS WSDL as byte array * @throws IOException */ private static byte[] getEmailWsdl(String emailServiceLocation) throws IOException { byte[] emailWSDL = loadSchema(TransformConstants.FILENAME_WSDL_EMAIL); TDefinitions emailService = XmlTools.unmarshalJaxbEntity(emailWSDL, TDefinitions.class); modifyServiceEndpoint(emailService, emailServiceLocation); JAXBElement<TDefinitions> jax = new ObjectFactory().createDefinitions(emailService); return XmlTools.marshalJaxbEntityIntoByteArray(jax); } /** * @param htServiceLocation * URL of the HumanTask WS endpoint * @return HumanTaskWS WSDL as byte array * @throws IOException */ private static byte[] getHTWsdl(String htServiceLocation) throws IOException { byte[] htWSDL = loadSchema(TransformConstants.FILENAME_WSDL_HUMANTASK); TDefinitions htService = XmlTools.unmarshalJaxbEntity(htWSDL, TDefinitions.class); modifyServiceEndpoint(htService, htServiceLocation); JAXBElement<TDefinitions> jax = new ObjectFactory().createDefinitions(htService); return XmlTools.marshalJaxbEntityIntoByteArray(jax); } /** * @param psServiceLocation * URL of the ProxyService WS endpoint * @return ProxyWS WSDL as byte array * @throws IOException */ private static byte[] getPSWsdl(String psServiceLocation) throws IOException { byte[] psWSDL = loadSchema(TransformConstants.FILENAME_WSDL_PROXY); TDefinitions psService = XmlTools.unmarshalJaxbEntity(psWSDL, TDefinitions.class); modifyServiceEndpoint(psService, psServiceLocation); JAXBElement<TDefinitions> jax = new ObjectFactory().createDefinitions(psService); return XmlTools.marshalJaxbEntityIntoByteArray(jax); } /** * Replaces all location attributes that match "unknow" with the new * endpoint * * @param service * parsed wsdl * @param newEndpoint * the new endpoint url */ private static void modifyServiceEndpoint(TDefinitions service, String newEndpoint) { List<TDocumented> topLevelList = service.getAnyTopLevelOptionalElement(); List<TPort> portList = null; for (TDocumented node : topLevelList) { if (node instanceof TService) { TService serviceEntry = (TService) node; portList = serviceEntry.getPort(); } } for (TPort port : portList) { for (Object portDOM : port.getAny()) { if (portDOM instanceof Node) { Node location = ((Node) portDOM).getAttributes().getNamedItem("location"); if (location.getNodeValue().startsWith("unknown")) { location.setNodeValue(newEndpoint); } } } } } }