[bug 366825] 100% CPU for long time in ASTUtils.createCompilationUnit
Downport fix for bug 347973 into 3.3.2 maintenance branch (part2)
diff --git a/bundles/org.eclipse.jst.ws.jaxws.utils/META-INF/MANIFEST.MF b/bundles/org.eclipse.jst.ws.jaxws.utils/META-INF/MANIFEST.MF
index 50783b1..158933e 100755
--- a/bundles/org.eclipse.jst.ws.jaxws.utils/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.jst.ws.jaxws.utils/META-INF/MANIFEST.MF
@@ -1,39 +1,40 @@
-Manifest-Version: 1.0

-Bundle-ManifestVersion: 2

-Bundle-Name: %Bundle-Name.0

-Bundle-Vendor: %Bundle-Vendor.0

-Bundle-SymbolicName: org.eclipse.jst.ws.jaxws.utils;singleton:=true

-Bundle-Version: 1.0.0.qualifier

-Bundle-Localization: plugin

-Bundle-RequiredExecutionEnvironment: J2SE-1.5

-Require-Bundle: org.eclipse.core.runtime,

- org.eclipse.jdt.core,

- org.eclipse.core.resources,

- org.eclipse.text,

- org.eclipse.ui,

- org.eclipse.ui.ide,

- org.eclipse.wst.common.project.facet.core

-Export-Package: org.eclipse.jst.ws.jaxws.utils;

-  x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,

-   org.eclipse.jst.ws.jaxws.dom.runtime,

-   org.eclipse.jst.ws.jaxws.dom.runtime.tests,

-   org.eclipse.jst.ws.jaxws.testutils,

-   org.eclipse.jst.ws.jaxws.utils.tests",

- org.eclipse.jst.ws.jaxws.utils.annotations;

-  x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,

-   org.eclipse.jst.ws.jaxws.dom.integration.tests,

-   org.eclipse.jst.ws.jaxws.dom.runtime,

-   org.eclipse.jst.ws.jaxws.dom.runtime.tests,

-   org.eclipse.jst.ws.jaxws.utils.tests",

- org.eclipse.jst.ws.jaxws.utils.clazz;x-friends:="org.eclipse.jst.ws.jaxws.utils.tests",

- org.eclipse.jst.ws.jaxws.utils.exception;x-friends:="org.eclipse.jst.ws.jaxws.testutils",

- org.eclipse.jst.ws.jaxws.utils.facets;x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,org.eclipse.jst.ws.jaxws.dom.runtime",

- org.eclipse.jst.ws.jaxws.utils.internal.annotations.impl;x-friends:="org.eclipse.jst.ws.jaxws.dom.runtime.tests,org.eclipse.jst.ws.jaxws.utils.tests",

- org.eclipse.jst.ws.jaxws.utils.logging;

-  x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,

-   org.eclipse.jst.ws.jaxws.dom.runtime,

-   org.eclipse.jst.ws.jaxws.dom.runtime.tests,

-   org.eclipse.jst.ws.jaxws.dom.ui,

-   org.eclipse.jst.ws.jaxws.testutils",

- org.eclipse.jst.ws.jaxws.utils.resources;x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,org.eclipse.jst.ws.jaxws.dom.runtime.tests,org.eclipse.jst.ws.jaxws.utils.tests"

-Bundle-Activator: org.eclipse.jst.ws.jaxws.utils.internal.plugin.JaxwsUtilsPlugin

+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: %Bundle-Name.0
+Bundle-Vendor: %Bundle-Vendor.0
+Bundle-SymbolicName: org.eclipse.jst.ws.jaxws.utils;singleton:=true
+Bundle-Version: 1.0.0.qualifier
+Bundle-Localization: plugin
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Require-Bundle: org.eclipse.core.runtime,
+ org.eclipse.jdt.core,
+ org.eclipse.core.resources,
+ org.eclipse.text,
+ org.eclipse.ui,
+ org.eclipse.ui.ide,
+ org.eclipse.wst.common.project.facet.core
+Export-Package: org.eclipse.jst.ws.jaxws.utils;
+  x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,
+   org.eclipse.jst.ws.jaxws.dom.runtime,
+   org.eclipse.jst.ws.jaxws.dom.runtime.tests,
+   org.eclipse.jst.ws.jaxws.testutils,
+   org.eclipse.jst.ws.jaxws.utils.tests",
+ org.eclipse.jst.ws.jaxws.utils.annotations;
+  x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,
+   org.eclipse.jst.ws.jaxws.dom.integration.tests,
+   org.eclipse.jst.ws.jaxws.dom.runtime,
+   org.eclipse.jst.ws.jaxws.dom.runtime.tests,
+   org.eclipse.jst.ws.jaxws.utils.tests",
+ org.eclipse.jst.ws.jaxws.utils.clazz;x-friends:="org.eclipse.jst.ws.jaxws.utils.tests",
+ org.eclipse.jst.ws.jaxws.utils.exception;x-friends:="org.eclipse.jst.ws.jaxws.testutils",
+ org.eclipse.jst.ws.jaxws.utils.facets;x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,org.eclipse.jst.ws.jaxws.dom.runtime",
+ org.eclipse.jst.ws.jaxws.utils.internal.annotations.impl;x-friends:="org.eclipse.jst.ws.jaxws.dom.runtime.tests,org.eclipse.jst.ws.jaxws.utils.tests",
+ org.eclipse.jst.ws.jaxws.utils.logging;
+  x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,
+   org.eclipse.jst.ws.jaxws.dom.runtime,
+   org.eclipse.jst.ws.jaxws.dom.runtime.tests,
+   org.eclipse.jst.ws.jaxws.dom.ui,
+   org.eclipse.jst.ws.jaxws.testutils",
+ org.eclipse.jst.ws.jaxws.utils.operation;x-friends:="org.eclipse.jst.ws.jaxws.dom.runtime,org.eclipse.jst.ws.jaxws.dom.runtime.tests,org.eclipse.jst.ws.jaxws.utils.tests",
+ org.eclipse.jst.ws.jaxws.utils.resources;x-friends:="org.eclipse.jst.ws.jaxws.dom.integration,org.eclipse.jst.ws.jaxws.dom.runtime.tests,org.eclipse.jst.ws.jaxws.utils.tests"
+Bundle-Activator: org.eclipse.jst.ws.jaxws.utils.internal.plugin.JaxwsUtilsPlugin
diff --git a/bundles/org.eclipse.jst.ws.jaxws.utils/src/org/eclipse/jst/ws/jaxws/utils/operation/IOperationRunner.java b/bundles/org.eclipse.jst.ws.jaxws.utils/src/org/eclipse/jst/ws/jaxws/utils/operation/IOperationRunner.java
new file mode 100644
index 0000000..7f9f150
--- /dev/null
+++ b/bundles/org.eclipse.jst.ws.jaxws.utils/src/org/eclipse/jst/ws/jaxws/utils/operation/IOperationRunner.java
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Copyright (c) 2011 by SAP AG, Walldorf. 
+ * 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:
+ *     SAP AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jst.ws.jaxws.utils.operation;
+
+import org.eclipse.jface.operation.IRunnableWithProgress;
+
+/**
+ * Utility interface for running operations
+ * 
+ * @author Danail Branekov
+ */
+public interface IOperationRunner
+{
+	/**
+	 * Runs the operation specified
+	 * 
+	 * @param runnable
+	 *            the operation to run
+	 */
+	public void run(final IRunnableWithProgress runnable);
+}
diff --git a/bundles/org.eclipse.jst.ws.jaxws.utils/src/org/eclipse/jst/ws/jaxws/utils/operation/OperationInJobRunner.java b/bundles/org.eclipse.jst.ws.jaxws.utils/src/org/eclipse/jst/ws/jaxws/utils/operation/OperationInJobRunner.java
new file mode 100644
index 0000000..b7cbc4b
--- /dev/null
+++ b/bundles/org.eclipse.jst.ws.jaxws.utils/src/org/eclipse/jst/ws/jaxws/utils/operation/OperationInJobRunner.java
@@ -0,0 +1,77 @@
+/*******************************************************************************
+ * Copyright (c) 2011 by SAP AG, Walldorf. 
+ * 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:
+ *     SAP AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jst.ws.jaxws.utils.operation;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.ISchedulingRule;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jst.ws.jaxws.utils.StatusUtils;
+import org.eclipse.jst.ws.jaxws.utils.logging.ILogger;
+import org.eclipse.jst.ws.jaxws.utils.logging.Logger;
+
+/**
+ * Implementation of the {@link IOperationRunner} interface which runs the operation in a job which the supplied scheduling rule
+ * 
+ * @author Danail Branekov
+ */
+public class OperationInJobRunner implements IOperationRunner
+{
+	private final String jobName;
+	private final ISchedulingRule jobRule;
+	private final ILogger logger;
+
+	/**
+	 * Constructor
+	 * 
+	 * @param jobName
+	 *            the name of the job which performs the operation
+	 * @param jobRule
+	 *            the scheduling rule of the job
+	 */
+	public OperationInJobRunner(final String jobName, final ISchedulingRule jobRule)
+	{
+		this.jobName = jobName;
+		this.jobRule = jobRule;
+		this.logger = new Logger();
+	}
+
+	public void run(final IRunnableWithProgress runnable)
+	{
+		final Job job = new Job(jobName)
+		{
+			@Override
+			protected IStatus run(IProgressMonitor monitor)
+			{
+				try
+				{
+					runnable.run(monitor);
+				}
+				catch (InvocationTargetException e)
+				{
+					logger.logError(e.getMessage(), e);
+					return StatusUtils.statusError(e.getMessage(), e);
+				}
+				catch (InterruptedException e)
+				{
+					return Status.CANCEL_STATUS;
+				}
+				return Status.OK_STATUS;
+			}
+		};
+		job.setRule(this.jobRule);
+		job.schedule();
+	}
+}