diff options
author | kchan | 2007-05-09 21:00:24 +0000 |
---|---|---|
committer | kchan | 2007-05-09 21:00:24 +0000 |
commit | d84f61453d492f507e04c22433739ac459ae6910 (patch) | |
tree | c69141bdd8026bd991670acd0ed1c4c84fd083f2 /bundles/org.eclipse.jst.ws.consumption/src | |
parent | 77732ac759e82cbf8dcb4efd83be0db545a207ca (diff) | |
download | webtools.webservices-d84f61453d492f507e04c22433739ac459ae6910.tar.gz webtools.webservices-d84f61453d492f507e04c22433739ac459ae6910.tar.xz webtools.webservices-d84f61453d492f507e04c22433739ac459ae6910.zip |
[182274] Remove wst.ws dependency on emf.codegen by introducing IMerger to handle skeleton merge.
Diffstat (limited to 'bundles/org.eclipse.jst.ws.consumption/src')
4 files changed, 210 insertions, 115 deletions
diff --git a/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/Consumption.properties b/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/Consumption.properties index 36754615e..4fa96db5c 100644 --- a/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/Consumption.properties +++ b/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/Consumption.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2004, 2006 IBM Corporation and others. +# Copyright (c) 2004, 2007 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 @@ -11,6 +11,7 @@ # -------- -------- ----------------------------------------------------------- # 20060217 126757 rsinha@ca.ibm.com - Rupam Kuehner # 20060401 128827 kathy@ca.ibm.com - Kathy Chan +# 20070509 182274 kathy@ca.ibm.com - Kathy Chan ############################################################################### # @@ -188,6 +189,7 @@ MSG_FACETS={0}, {1} MSG_ERROR_SETTING_RUNTIME=Unable to bind project {0} to the {1} runtime. # -# SkeletonMergeCommand Messages +# Skeleton Merge Messages # MSG_ERROR_FILE_MERGE=Error in merging file {0}. +MSG_ERROR_FILE_MERGE_LOAD=Error in loading file {0} for merge. diff --git a/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/ConsumptionMessages.java b/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/ConsumptionMessages.java index aad43c21f..3588bbe77 100644 --- a/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/ConsumptionMessages.java +++ b/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/ConsumptionMessages.java @@ -109,6 +109,7 @@ public final class ConsumptionMessages extends NLS { public static String MSG_ERROR_FIXED_FACETS; public static String MSG_FACETS; public static String MSG_ERROR_SETTING_RUNTIME; + public static String MSG_ERROR_FILE_MERGE_LOAD; public static String MSG_ERROR_FILE_MERGE; diff --git a/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/command/common/SkeletonMergeCommand.java b/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/command/common/SkeletonMergeCommand.java deleted file mode 100644 index 30f1da3f2..000000000 --- a/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/command/common/SkeletonMergeCommand.java +++ /dev/null @@ -1,113 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2006 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 - * -------- -------- ----------------------------------------------------------- - * 20060330 128827 kathy@ca.ibm.com - Kathy Chan - * 20060403 128827 kathy@ca.ibm.com - Kathy Chan - * 20060524 141925 kathy@ca.ibm.com - Kathy Chan - *******************************************************************************/ -package org.eclipse.jst.ws.internal.consumption.command.common; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; - -import org.eclipse.core.resources.IProject; -import org.eclipse.core.runtime.CoreException; -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.core.runtime.Status; -import org.eclipse.jem.util.emf.workbench.ProjectUtilities; -import org.eclipse.jst.ws.internal.consumption.ConsumptionMessages; -import org.eclipse.jst.ws.internal.plugin.WebServicePlugin; -import org.eclipse.osgi.util.NLS; -import org.eclipse.wst.command.internal.env.common.FileResourceUtils; -import org.eclipse.wst.command.internal.env.core.common.StatusUtils; -import org.eclipse.wst.command.internal.env.core.context.ResourceContext; -import org.eclipse.wst.common.environment.IEnvironment; -import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation; -import org.eclipse.wst.ws.internal.common.MergeUtils; -import org.eclipse.wst.ws.internal.plugin.WSPlugin; -import org.eclipse.wst.ws.internal.preferences.PersistentMergeContext; - -public class SkeletonMergeCommand extends AbstractDataModelOperation -{ - private String[] urls_; - private String projectName_; - - public SkeletonMergeCommand(String projectName) - { - projectName_ = projectName; - } - - public void setUrls(String[] urls) - { - this.urls_ = urls; - } - - // Merge the content of the skeleton files (represented by urls_ with the previous version stored. - // The Web service extensions triggers the storing of the content of the old skeleton file by - // calling WebServiceInfo.setImplURLs() before the new skeleton is generated in the extension's - // develop() method. - - public IStatus execute(IProgressMonitor monitor, IAdaptable info) - { - - IStatus status = Status.OK_STATUS; - IEnvironment environment = getEnvironment(); - ResourceContext context = WebServicePlugin.getInstance().getResourceContext(); - PersistentMergeContext mergeContext = WSPlugin.getInstance().getMergeContext(); - if (mergeContext.isSkeletonMergeEnabled()) { - // merge the skeleton implementation file with the model stored earlier - - String mergedContent; - String filename; - InputStream inStream = null; - if (urls_ != null) { - for (int i = 0; i < urls_.length; i++) { - filename = MergeUtils.getFileFromURL(urls_[i]); - if (filename != null) { - mergedContent = MergeUtils.mergeFile(filename); - if (mergedContent != null) { - // write the merged content back into the file, respecting file overwrite preference - IPath targetPath = new Path(filename); - IProject project = ProjectUtilities.getProject(projectName_); - byte[] buf = null; - if (project != null) { - try { - buf = mergedContent.getBytes(project.getDefaultCharset()); - } catch (Throwable e) { - buf = mergedContent.getBytes(); - } - } else { - buf = mergedContent.getBytes(); - } - inStream = new ByteArrayInputStream(buf); - - if (inStream != null) { - try { - FileResourceUtils.createFileAtLocation(context, targetPath.makeAbsolute(), inStream, - monitor, environment.getStatusHandler()); - } catch (CoreException e) { - status = StatusUtils.errorStatus(NLS.bind(ConsumptionMessages.MSG_ERROR_FILE_MERGE, urls_), e); - environment.getStatusHandler().reportError( status ); - } - } - } - } - } - } - } - - return status; - } -} diff --git a/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/common/JavaMerger.java b/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/common/JavaMerger.java new file mode 100644 index 000000000..01e696600 --- /dev/null +++ b/bundles/org.eclipse.jst.ws.consumption/src/org/eclipse/jst/ws/internal/consumption/common/JavaMerger.java @@ -0,0 +1,205 @@ +/******************************************************************************* + * Copyright (c) 2007 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 + * -------- -------- ----------------------------------------------------------- + * 20070509 182274 kathy@ca.ibm.com - Kathy Chan + *******************************************************************************/ +package org.eclipse.jst.ws.internal.consumption.common; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.URL; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.FileLocator; +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.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.emf.codegen.merge.java.JControlModel; +import org.eclipse.emf.codegen.merge.java.JMerger; +import org.eclipse.emf.codegen.merge.java.facade.FacadeHelper; +import org.eclipse.emf.codegen.util.CodeGenUtil; +import org.eclipse.jst.ws.internal.consumption.ConsumptionMessages; +import org.eclipse.jst.ws.internal.plugin.WebServicePlugin; +import org.eclipse.osgi.util.NLS; +import org.eclipse.wst.command.internal.env.common.FileResourceUtils; +import org.eclipse.wst.command.internal.env.core.common.StatusUtils; +import org.eclipse.wst.command.internal.env.core.context.ResourceContext; +import org.eclipse.wst.common.environment.IStatusHandler; +import org.eclipse.wst.ws.internal.plugin.WSPlugin; +import org.eclipse.wst.ws.internal.preferences.PersistentMergeContext; +import org.eclipse.wst.ws.internal.wsrt.IMerger; +import org.eclipse.wst.ws.internal.wsrt.Merger; +import org.osgi.framework.Bundle; + +/* + * JavaMerger handles the merging of workspace files using JMerger. If the URIs specified is not a file + * in the workspace, it will be ignored. + */ +public class JavaMerger extends Merger implements IMerger { + + private static final String WST_WS = "org.eclipse.wst.ws"; + private static final String MERGE_XML = "merge.xml"; + private static final String JMERGER = "jmerger"; + + private int size = 0; + private IFile[] workspaceFiles; + private JMerger[] mergeModels; + private JControlModel jMergeControlModel = null; + private IStatus loadStatus; + + + /* + * Convert the URI strings in the uris array to Eclipse IFiles and store in the workspaceFiles array. + * Load the workspaceFiles into JMerger and store in the mergerModels array. + * If the merger model cannot be obtained, null is stored in the mergerModels. + * @see org.eclipse.wst.ws.internal.wsrt.IMerger#load(java.lang.String[]) + */ + + public IStatus load(IFile[] files) { + + loadStatus = Status.OK_STATUS; + if (files != null) { + + PersistentMergeContext mergeContext = WSPlugin.getInstance().getMergeContext(); + if (mergeContext.isSkeletonMergeEnabled()) { + // initialize JMerger + initialize(); + + if (jMergeControlModel != null) { + // JMerger initialized properly + size = files.length; + workspaceFiles = new IFile[size]; + mergeModels = new JMerger[size]; + + for (int i = 0; i < size; i++) { + JMerger jMerger = null; + IFile file = null; + InputStream inStream = null; + + file = files[i]; + if (file != null && file.exists()) { + try { + inStream = file.getContents(); + if (inStream != null) { + jMerger = new JMerger(jMergeControlModel); + jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(inStream)); + } + } catch (CoreException e) { + loadStatus = StatusUtils.errorStatus(NLS.bind(ConsumptionMessages.MSG_ERROR_FILE_MERGE_LOAD, file), e); + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (IOException e) { + // ignore exceptions in closing file + } + } + } + } + workspaceFiles[i] = file; + mergeModels[i] = jMerger; + } + } + } + } + return loadStatus; + } + + /** + * Initializes the JMerge model + * File merge.xml in WebServicePlugin contains the rules for the merge. + * If the merge.xml file is not found, jMergeControlModel is leave as null. + */ + private void initialize() { + if (jMergeControlModel == null) { + FacadeHelper facadeHelper = CodeGenUtil.instantiateFacadeHelper(JMerger.DEFAULT_FACADE_HELPER_CLASS); + jMergeControlModel = new JControlModel(); + + Bundle wsBundle = Platform.getBundle(WST_WS); + IPath mergePath = new Path( JMERGER ).append( MERGE_XML ); + URL fileURL = FileLocator.find( wsBundle, mergePath, null); + jMergeControlModel.initialize(facadeHelper, fileURL.toString()); + } + return; + } + + /** + * Merge the current contents of the workspaceFiles with the mergeModels that had been stored earlier. + * Then write the merged contents back to the file. + * @param monitor + * @param statusHandler + * @return IStatus + */ + public IStatus merge(IProgressMonitor monitor, IStatusHandler statusHandler) { + + IStatus status = Status.OK_STATUS; + + if (loadStatus.getSeverity() == IStatus.ERROR) { + return loadStatus; + } + + PersistentMergeContext mergeContext = WSPlugin.getInstance().getMergeContext(); + if (mergeContext.isSkeletonMergeEnabled()) { + ResourceContext resourceContext = WebServicePlugin.getInstance().getResourceContext(); + for (int i = 0; i < size; i++) { + String mergedContent = null; + JMerger jMerger = null; + IFile file = null; + InputStream inStream = null; + byte[] buf = null; + + jMerger = mergeModels[i]; + file = workspaceFiles[i]; + if (file != null && jMerger != null && file.exists()) { + // a JMerger model of the file exists, merge the contents + try { + inStream = file.getContents(); + jMerger.setSourceCompilationUnit(jMerger.createCompilationUnitForInputStream(inStream)); + jMerger.merge(); + mergedContent = jMerger.getTargetCompilationUnitContents(); + if (mergedContent != null) { + try { + buf = mergedContent.getBytes(file.getCharset()); + } catch (UnsupportedEncodingException e) { + buf = mergedContent.getBytes(); + } + inStream = new ByteArrayInputStream(buf); + IPath targetPath = file.getLocation(); + if (targetPath != null) { + FileResourceUtils.createFileAtLocation(resourceContext, targetPath.makeAbsolute(), inStream, + monitor, statusHandler); + } + } + } catch (CoreException e) { + status = StatusUtils.errorStatus(NLS.bind(ConsumptionMessages.MSG_ERROR_FILE_MERGE, file), e); + } finally { + try { + if (inStream != null) { + inStream.close(); + } + } catch (IOException e) { + // ignore exceptions in closing file + } + } + } + } + + } + return status; + } + +} |