Author: oberhack Date: Tue Aug 31 20:41:32 2004 New Revision: 37291 Added: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/MetroStudioLaunch.java avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/IMerlinBuilder.java avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/MerlinBuilderFactory.java avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/MerlinTypeBuilder.java avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/ILaunchConfigConstants.java avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/MetroLaunchConfigurationDelegate.java avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/MetroLaunchShortcut.java Removed: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/avalon/ Log:
Added: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/MetroStudioLaunch.java ============================================================================== --- (empty file) +++ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/MetroStudioLaunch.java Tue Aug 31 20:41:32 2004 @@ -0,0 +1,174 @@ +/* + + Copyright 2004. The Apache Software Foundation. + + Licensed 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.metro.studio.eclipse.launch; + +import java.net.URL; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +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.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>* + * The main plugin class to be used in the desktop. + */ +public class MetroStudioLaunch extends AbstractUIPlugin +{ + //The shared instance. + private static MetroStudioLaunch plugin; + //Resource bundle. + private ResourceBundle resourceBundle; + + public static final String PLUGIN_ID = "org.apache.metro.studio.launch"; + public static final String METRO_CONFIG_TYPE = + MetroStudioLaunch.PLUGIN_ID + ".metroLaunchConfigurationDelegate"; //$NON-NLS-1$ + + public static final String MERLIN_PROJECT_NATURE_ID = + PLUGIN_ID + ".merlinProjectNature"; + public static final String MERLIN_PROJECT_CONFIG_NATURE_ID = + PLUGIN_ID + ".merlinConfigNature"; + public static final String MERLIN_BUILDER_ID = + PLUGIN_ID + ".merlinBuilder"; + public static final String ATTR_MERLIN_CONTAINER_ID = "merlinContainerID"; //$NON-NLS-1$ + /** + * The constructor. + */ + public MetroStudioLaunch() + { + super(); + plugin = this; + try + { + // activate EnterpriseDeveloper if present + // Platform.getPlugin("biz.softwarefabrik.j4ee.core"); + // MerlinBuilderFactory.addBuilder(new MerlinTypeBuilder()); + + resourceBundle = + ResourceBundle.getBundle( + "org.apache.avalon.ide.eclipse.merlin.launch.MerlinDeveloperLaunchResources"); + } catch (MissingResourceException x) + { + resourceBundle = null; + } + } + + /** + * Returns the shared instance. + */ + public static MetroStudioLaunch getDefault() + { + return plugin; + } + + /** + * Returns the workspace instance. + */ + public static IWorkspace getWorkspace() + { + return ResourcesPlugin.getWorkspace(); + } + + /** + * Returns the string from the plugin's resource bundle, or 'key' if not + * found. + */ + public static String getResourceString(String key) + { + ResourceBundle bundle = MetroStudioLaunch.getDefault().getResourceBundle(); + try + { + return (bundle != null ? bundle.getString(key) : key); + } catch (MissingResourceException e) + { + return key; + } + } + + /** + * Returns the plugin's resource bundle, + */ + public ResourceBundle getResourceBundle() + { + return resourceBundle; + } + + public static void log(IStatus status) + { + getDefault().getLog().log(status); + } + + public static void log(Throwable e, String message) + { + boolean isDebugging = true; // change to false for production + IStatus status = + new Status( + IStatus.ERROR, + ResourcesPlugin.getPlugin().getBundle().getSymbolicName(), + IStatus.ERROR, + message, + e); + + log(status); + if (isDebugging) + { + System.out.println(message + ": " + e.getMessage()); //$NON-NLS-1$ + } + } + + public IPath getPluginLocation() + { + return getPluginLocation(MetroStudioLaunch.PLUGIN_ID); + } + + public IPath getPluginLocation(String pluginId) + { + try + { + URL installURL = + MetroStudioLaunch.getDefault().getBundle().getEntry("/"); + return new Path(Platform.resolve(installURL).getFile()); + } catch (Exception e) + { + log(e, "getPluginLocation() handling Exception"); //$NON-NLS-1$ + return null; + } + } + + /** + * This method is called upon plug-in activation + */ + public void start(BundleContext context) throws Exception { + super.start(context); + } + + /** + * This method is called when the plug-in is stopped + */ + public void stop(BundleContext context) throws Exception { + super.stop(context); + } +} Added: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/IMerlinBuilder.java ============================================================================== --- (empty file) +++ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/IMerlinBuilder.java Tue Aug 31 20:41:32 2004 @@ -0,0 +1,29 @@ +/* + * Created on 15.03.2004 + * + * To change the template for this generated file go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +package org.apache.metro.studio.eclipse.launch.builder; + +import java.util.List; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IProgressMonitor; + +/** + * @author Andreas Develop + * + * To change the template for this generated type comment go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +public interface IMerlinBuilder +{ + + /** + * @param pKind + * @param pArgs + * @param pMonitor + */ + public void build(int pKind, IProject project, List pFiles, IProgressMonitor pMonitor); +} Added: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/MerlinBuilderFactory.java ============================================================================== --- (empty file) +++ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/MerlinBuilderFactory.java Tue Aug 31 20:41:32 2004 @@ -0,0 +1,99 @@ +/* + * One has to add the builder to a project first. + * This is done, when the project is build in + * MerlinDeveloperCore ProjectResource.addBuilder() + */ +package org.apache.metro.studio.eclipse.launch.builder; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResourceDelta; +import org.eclipse.core.resources.IncrementalProjectBuilder; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; + +/** + * @author Andreas Develop + * + * To change the template for this generated type comment go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +public class MerlinBuilderFactory extends IncrementalProjectBuilder +{ + private static List builderList = new ArrayList(); + + /** + * + */ + public MerlinBuilderFactory() + { + super(); + builderList.add(new MerlinTypeBuilder()); + } + + /* + * Add builders to the build process. This method is also called + * by other plug-in (e.g. EnterpriseDeveloper). + * Registration of builders is done in the main plug-in class (eg. EnterpriseDeveloperCore.java) + * + */ + public static void addBuilder(IMerlinBuilder builder) + { + + builderList.add(builder); + + } + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IncrementalProjectBuilder#build(int, java.util.Map, org.eclipse.core.runtime.IProgressMonitor) + */ + protected IProject[] build(int pKind, Map pArgs, IProgressMonitor pMonitor) + throws CoreException + { + if(getDelta(getProject())==null) return null; + + IResourceDelta delta[] = getDelta(getProject()).getAffectedChildren(); + List files = getChangedResource(delta); + + + Iterator it = builderList.iterator(); + while (it.hasNext()) + { + IMerlinBuilder builder = (IMerlinBuilder) it.next(); + builder.build(pKind, getProject(), files, pMonitor); + } + IJavaProject proj = JavaCore.create(getProject()); + proj.getProject().refreshLocal(IProject.DEPTH_INFINITE, null); + return null; + } + + /* + * retrieves the changed resource. + * only returns changed java resources (java source files) + */ + private List getChangedResource(IResourceDelta delta[]) + { + + List res = new ArrayList(); + + for (int i = 0; delta.length > i; i++) + { + if (delta[i].getAffectedChildren().length > 0) + { + res.addAll(getChangedResource(delta[i].getAffectedChildren())); + } else + { + res.add(delta[i].getResource()); + } + } + + return res; + } + +} Added: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/MerlinTypeBuilder.java ============================================================================== --- (empty file) +++ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/builder/MerlinTypeBuilder.java Tue Aug 31 20:41:32 2004 @@ -0,0 +1,614 @@ +/* + * Created on 06.05.2004 + * + * To change the template for this generated file go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +package org.apache.metro.studio.eclipse.launch.builder; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.avalon.meta.info.Type; +import org.apache.avalon.meta.info.Service; +import org.apache.avalon.meta.info.builder.BuildException; +import org.apache.avalon.meta.info.builder.tags.ServiceTag; +import org.apache.avalon.meta.info.builder.tags.TypeTag; +import org.apache.avalon.meta.info.writer.SerializedServiceWriter; +import org.apache.avalon.meta.info.writer.SerializedTypeWriter; +import org.apache.avalon.meta.info.writer.ServiceWriter; +import org.apache.avalon.meta.info.writer.TypeWriter; +import org.apache.avalon.meta.info.writer.XMLServiceWriter; +import org.apache.avalon.meta.info.writer.XMLTypeWriter; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.IProgressMonitor; + +import com.thoughtworks.qdox.JavaDocBuilder; +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.JavaSource; + +/** + * @author Andreas Develop + * + * To change the template for this generated type comment go to + * Window - Preferences - Java - Code Generation - Code and Comments + */ +public class MerlinTypeBuilder implements IMerlinBuilder +{ + protected JavaClass[] allClasses; + private IResource m_resource; + /** + * XML output type code. + */ + public static final int XML_TYPE = 0; + + /** + * Serial output type code. + */ + public static final int SER_TYPE = 1; + + /** + * A utility object that writes out info as xml files. + */ + private static final TypeWriter XML_WRITER = new XMLTypeWriter(); + + /** + * A utility object that writes out info as serialized object files. + */ + private static final TypeWriter SERIAL_WRITER = new SerializedTypeWriter(); + + /** + * A utility object that writes out a service as xml files. + */ + private static final ServiceWriter XML_SERVICE_WRITER = new XMLServiceWriter(); + + /** + * A utility object that writes out a service as serialized object files. + */ + private static final ServiceWriter SERIAL_SERVICE_WRITER = new SerializedServiceWriter(); + + /** + * The destination directory for metadata files. + */ + private File m_destDir; + + /** + * Variable that indicates the output type. + */ + private int m_format; + + /** + * The preferred postfix value. + */ + private String m_postfix = ".xinfo"; + + /** + * Variable that indicates whether the output + * will be generated only when the source file is + * newer than the destination file. + */ + private boolean m_force = true; + + /** + * Set the desitation directory to generate output files to. + * + * @param destDir The destination directory + */ + public void setDestDir(final File destDir) + { + m_destDir = destDir; + } + + /** + * Specify the output format. Must be one of xml or serialized. + * + * @param format the output format + */ + public void setFormat(final String format) + { + m_format = MerlinTypeBuilder.XML_TYPE; + } + + /** + * Set force to be true indicating that destination + * files should always be regenerated. + * + * @param force the flag for forcing output + */ + public void setForce(boolean force) + { + m_force = force; + } + + /** + * Set the file type to be used for meta info type + * documents. May be one of "xinfo" or "xtype". + * + * @param postfix the postfix value + */ + public void setPostfix(String postfix) + { + if (postfix.equalsIgnoreCase("xtype") || postfix.equalsIgnoreCase("xinfo")) + { + m_postfix = "." + postfix; + } else + { + final String error = + "Illegal postfix value: " + + postfix + + ". " + + "Recognized values include 'xinfo' and 'xtype'."; + log(error); + } + } + + /** + * Execute generator task. + * @exception BuildException if a build error occurs + */ + public void execute() throws Exception + { + validate(); + + final String message = "Writing descriptors using '" + getOutputDescription() + "' format."; + log(message); + + addInnerClasses(); + + try + { + Counter counter = writeMetaData(); + final String update = + "Processed " + + counter.getTypes() + + " Types and " + + counter.getServices() + + " Services from a total of " + + counter.getCount() + + " classes."; + log(update); + } catch (final IllegalArgumentException e) + { + //MetroStudioLaunch.log(update, e); + } + } + + /** + * allClasses contains only non-inner classes, so here we (recursively) extract out all inner + * classes and explictly add them. + */ + private void addInnerClasses() + { + ArrayList expList = new ArrayList(); + for (int i = 0; i < allClasses.length; i++) + { + addWithInnerClasses(expList, (JavaClass) allClasses[i]); + } + allClasses = (JavaClass[])expList.toArray(new JavaClass[expList.size()]); + } + + private void addWithInnerClasses(ArrayList list, JavaClass javaClass) + { + list.add(javaClass); + + final JavaClass[] innerClasses = javaClass.getInnerClasses(); + for (int i = 0; i < innerClasses.length; i++) + { + addWithInnerClasses(list, innerClasses[i]); + } + } + + /** + * Validate that the parameters are valid. + */ + private void validate() + { + if (null == m_destDir) + { + final String message = "DestDir (" + m_destDir + ") not specified"; + log(message); + } + if (!m_destDir.isDirectory()) + { + final String message = "DestDir (" + m_destDir + ") is not a directory."; + log(message); + } + + if (!m_destDir.exists() && !m_destDir.mkdirs()) + { + final String message = "DestDir (" + m_destDir + ") could not be created."; + log(message); + } + } + + /** + * Return a description of output format to print as debug message. + * + * @return the output formats descriptive name + */ + private String getOutputDescription() + { + if (SER_TYPE == m_format) + { + return "serial"; + } else + { + return "xml"; + } + } + + /** + * Output the metadata files. + * @return the count holder + * @throws IOException If a problem writing output + */ + private Counter writeMetaData() throws IOException + { + int services = 0; + int types = 0; + final int size = allClasses.length; + for (int i = 0; i < size; i++) + { + final JavaClass javaClass = (JavaClass) allClasses[i]; + if (javaClass.isInterface()) + { + Service service = new ServiceTag(javaClass).getService(); + if (service == null) + { + continue; + } + + services++; + + // + // it is a service so we can fo ahead and build a + // a service descriptor + // + + final String classname = javaClass.getFullyQualifiedName(); + final File source = javaClass.getParentSource().getFile(); + final File dest = getOutputFileForService(classname); + + if (!m_force) + { + if (dest.exists() && dest.lastModified() >= source.lastModified()) + { + continue; + } + } + final File parent = dest.getParentFile(); + if (null != parent) + { + if (!parent.exists() && !parent.mkdirs()) + { + final String message = "Failed to create output directory: " + parent; + log(message); + } + } + writeService(service); + } else + { + Type type = null; + TypeTag tag = new TypeTag(javaClass); + type = tag.getType(); + if (type == null) + { + continue; + } + + types++; + + // + // it is a type implementation so we can fo ahead and build a + // a type descriptor + // + + final String classname = javaClass.getFullyQualifiedName(); + final JavaSource src = javaClass.getParentSource(); + final File source = src.getFile(); + final File dest = getOutputFileForClass(classname); + + if (!m_force) + { + if (dest.exists() && dest.lastModified() >= source.lastModified()) + { + continue; + } + } + + final File parent = dest.getParentFile(); + if (null != parent) + { + if (!parent.exists() && !parent.mkdirs()) + { + final String message = "Failed to create output directory: " + parent; + log(message); + } + } + writeType(type); + } + } + return new Counter(size, services, types); + } + + /** + * Write Service to a file. + * + * @param service the Service descriptor + * @throws IOException if an error occurs while writing to file + */ + private void writeService(final Service service) throws IOException + { + final String fqn = service.getReference().getClassname(); + final File file = getOutputFileForService(fqn); + final OutputStream outputStream = new FileOutputStream(file); + try + { + getServiceWriter().writeService(service, outputStream); + } catch (final Exception e) + { + log("Error writing service to " + file + ". Cause: " + e); + } finally + { + shutdownStream(outputStream); + } + } + + /** + * Write Type to a file. + * + * @param type the Type descriptor + * @throws IOException if unable to write info out + */ + private void writeType(final Type type) throws IOException + { + final String fqn = type.getInfo().getClassname(); + final File file = getOutputFileForClass(fqn); + final OutputStream outputStream = new FileOutputStream(file); + try + { + getTypeWriter().writeType(type, outputStream); + } catch (final Exception e) + { + log("Error writing " + file + ". Cause: " + e); + } finally + { + shutdownStream(outputStream); + } + } + + /** + * Return the correct info writer depending on + * what format the info will be output as. The + * implementation will return either a servialized + * wtiter or an xml writer based on the format + * established by the client. + * + * @return the TypeWriter to output info with + */ + private TypeWriter getTypeWriter() + { + if (SER_TYPE == m_format) + { + return SERIAL_WRITER; + } else + { + return XML_WRITER; + } + } + + /** + * Return the correct service writer depending on + * what format the service will be output as. The + * implementation will return either a serial + * wtiter or an xml writer based on the format + * established by the client. + * + * @return the ServiceWriter to output info with + */ + private ServiceWriter getServiceWriter() + { + if (SER_TYPE == m_format) + { + return SERIAL_SERVICE_WRITER; + } else + { + return XML_SERVICE_WRITER; + } + } + + /** + * Determine the file for the [EMAIL PROTECTED] Task}. + * + * @param classname the fully qualified name of file to generate + * @return the file for info + * @throws IOException if unable to determine base file + */ + private File getOutputFileForClass(final String classname) throws IOException + { + String filename = classname.replace('.', File.separatorChar); + + if (SER_TYPE == m_format) + { + filename += ".stype"; + } else + { + filename += m_postfix; + } + return new File(m_destDir, filename).getCanonicalFile(); + } + + /** + * Determine the file for specified [EMAIL PROTECTED] Service}. + * + * @param classname the fully qualified name of file to generate + * @return the file for the service descriptor + * @throws IOException if unable to determine base file + */ + private File getOutputFileForService(final String classname) throws IOException + { + String filename = classname.replace('.', File.separatorChar); + + if (SER_TYPE == m_format) + { + filename += ".sservice"; + } else + { + filename += ".xservice"; + } + return new File(m_destDir, filename).getCanonicalFile(); + } + + /** + * Close the specified output stream. + * + * @param outputStream the output stream + */ + private void shutdownStream(final OutputStream outputStream) + { + if (null != outputStream) + { + try + { + outputStream.close(); + } catch (IOException e) + { + // ignore + } + } + } + + /** + * Return the destination directory in which files are generated. + * + * @return the destination directory in which files are generated. + */ + protected final File getDestDir() + { + return m_destDir; + } + + /** + * Internal utility class that aggregates the number of services, the + * number of types, and the total component count. + */ + private class Counter + { + private int m_services; + private int m_types; + private int m_count; + Counter(int count, int services, int types) + { + m_count = count; + m_services = services; + m_types = types; + } + protected int getServices() + { + return m_services; + } + protected int getTypes() + { + return m_types; + } + protected int getCount() + { + return m_count; + } + } + + /** + * + */ + public MerlinTypeBuilder() + { + super(); + } + + /** + * Main build method. Only work on changed java files, which are marked to + * be persistent. + * + * @see org.apache.avalon.ide.eclipse.merlin.builder.IMerlinBuilder#build(int, + * java.util.Map, org.eclipse.core.runtime.IProgressMonitor) + */ + public void build(int pKind, IProject project, List pResources, IProgressMonitor pMonitor) + { + if (!isBuildingAllowed()) + { + return; + } + pMonitor.subTask("Source"); + for (int i = 0; pResources.size() > i; i++) + { + m_resource = (IResource) pResources.get(i); + + if (m_resource.getFileExtension().toLowerCase().equals("java")) + { + try + { + // MetroStudioLaunch.clearMarkers(m_resource); + String path = m_resource.getLocation().toString(); + JavaDocBuilder builder = new JavaDocBuilder(); + builder.addSource(new File(path)); + allClasses = builder.getClasses(); + if(allClasses.length>0) + { + JavaClass clazz = allClasses[0]; + int clazzNameLen = clazz.getFullyQualifiedName().length() + 5; + + String str = m_resource.getLocation().toString(); + str = str.substring(0, str.length()-clazzNameLen); + File file = new File(str); + + setDestDir(file); + setForce(true); + setPostfix("xinfo"); + execute(); + + } + } catch (FileNotFoundException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + } + + private void log(String mes){ + System.out.println(mes); + } + /** + * @return + */ + private boolean isBuildingAllowed() + { + boolean value = true; + /* + try + { + value = (EnterpriseDeveloperCore.getDefault().getPreferenceStore() + .getBoolean(BUILD_SOURCE_PROPERTY)); + } catch (Exception e) + { + EnterpriseDeveloperCore.log(e); + } + */ + return value; + } + + + +} Added: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/ILaunchConfigConstants.java ============================================================================== --- (empty file) +++ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/ILaunchConfigConstants.java Tue Aug 31 20:41:32 2004 @@ -0,0 +1,30 @@ +/* + + Copyright 2004. The Apache Software Foundation. + +Licensed 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.metro.studio.eclipse.launch.config; + +/** + * @author <a href="mailto:[EMAIL PROTECTED]">Metro Development Team</a> + * 19.08.2004 + * last change: + * + */ +public interface ILaunchConfigConstants +{ + public final String EXIT_CODE = "exitCode"; + +} Added: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/MetroLaunchConfigurationDelegate.java ============================================================================== --- (empty file) +++ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/MetroLaunchConfigurationDelegate.java Tue Aug 31 20:41:32 2004 @@ -0,0 +1,307 @@ +/* + + Copyright 2004. The Apache Software Foundation. + + Licensed 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.metro.studio.eclipse.launch.config; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Vector; + +import org.apache.metro.studio.eclipse.launch.MetroStudioLaunch; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.debug.core.DebugException; +import org.eclipse.debug.core.ILaunch; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; +import org.eclipse.debug.core.ILaunchManager; +import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; +import org.eclipse.debug.core.model.IProcess; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate; +import org.eclipse.jdt.launching.ExecutionArguments; +import org.eclipse.jdt.launching.IRuntimeClasspathEntry; +import org.eclipse.jdt.launching.IVMInstall; +import org.eclipse.jdt.launching.IVMRunner; +import org.eclipse.jdt.launching.JavaRuntime; +import org.eclipse.jdt.launching.LibraryLocation; +import org.eclipse.jdt.launching.VMRunnerConfiguration; +import org.eclipse.osgi.framework.internal.core.Constants; +import org.eclipse.osgi.util.ManifestElement; +import org.osgi.framework.BundleException; + +/** + * + * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team </a> + * + */ +public class MetroLaunchConfigurationDelegate extends + AbstractJavaLaunchConfigurationDelegate implements + ILaunchConfigurationDelegate +{ + private static final String METRO_HOME_KEY = "merlin.home"; + + final String CLASS_TO_LAUNCH = "org.apache.avalon.merlin.cli.Main"; + + public void launch(ILaunchConfiguration config, String mode, + ILaunch launch, IProgressMonitor monitor) throws CoreException + { + + if (monitor == null) + { + monitor = new NullProgressMonitor(); + } + IVMInstall vmInstall = getVMInstall(config); + IVMRunner vmRunner = getVMRunner(mode, config, vmInstall); + + // Program & VM args + String programArguments = getProgramArguments(config); + String vmArguments = getVMArguments(config); + ExecutionArguments executionArguments = new ExecutionArguments( + vmArguments, programArguments); + + // Classpath + String[] classpath = getClasspath(config); + + // Bootpath + String[] bootClasspath = getBootClasspath(vmInstall); + String[] vmArgs = new String[1]; + vmArgs[0] = executionArguments.getVMArguments(); + if (bootClasspath == null) + { + abort("Boot classpath not resolved", null, 5); //$NON-NLS-1$ + } + + VMRunnerConfiguration runnerConfig = new VMRunnerConfiguration( + CLASS_TO_LAUNCH, classpath); + runnerConfig.setVMArguments(vmArgs); + runnerConfig.setProgramArguments(executionArguments + .getProgramArgumentsArray()); + runnerConfig.setBootClassPath(bootClasspath); + + vmRunner.run(runnerConfig, launch, monitor); + // get return code + // setExitCode(config, launch); + + monitor.done(); + } + + private void setExitCode(ILaunchConfiguration config, ILaunch launch) + { + try + { + IProcess[] process = launch.getProcesses(); + int exit = process[0].getExitValue(); + ILaunchConfigurationWorkingCopy workingCopy = config.getWorkingCopy(); + workingCopy.setAttribute(ILaunchConfigConstants.EXIT_CODE, exit); + // dont need to save + + } catch (DebugException e) + { + MetroStudioLaunch.log(e, "C'ant get exit code"); + } catch (CoreException e) + { + MetroStudioLaunch.log(e, "C'ant get process exit code"); + } + } + private String[] getBootClasspath(IVMInstall vm) + { + ArrayList runtimeClasspaths = new ArrayList(); + LibraryLocation[] vmLibs = JavaRuntime.getLibraryLocations(vm); + for (int i = 0; i < vmLibs.length; i++) + { + IRuntimeClasspathEntry runtimeEntry = JavaRuntime + .newArchiveRuntimeClasspathEntry(((LibraryLocation) (vmLibs[i])) + .getSystemLibraryPath()); + runtimeEntry + .setSourceAttachmentPath(((LibraryLocation) (vmLibs[i])) + .getSystemLibrarySourcePath()); + runtimeEntry + .setSourceAttachmentRootPath(((LibraryLocation) (vmLibs[i])) + .getPackageRootPath()); + runtimeEntry + .setClasspathProperty(IRuntimeClasspathEntry.STANDARD_CLASSES); + runtimeClasspaths.add(runtimeEntry.getLocation()); + } + + if (runtimeClasspaths.size() == 0) + { + return null; + } else + { + return (String[]) runtimeClasspaths + .toArray(new String[runtimeClasspaths.size()]); + } + } + + public String[] getClasspath(ILaunchConfiguration configuration) + { + ArrayList runtimeClasspaths = new ArrayList(); + LibraryLocation[] serverLibs = this.getLibraryLocations(); + for (int i = 0; i < serverLibs.length; i++) + { + IRuntimeClasspathEntry runtimeEntry = JavaRuntime + .newArchiveRuntimeClasspathEntry(((LibraryLocation) (serverLibs[i])) + .getSystemLibraryPath()); + runtimeEntry + .setSourceAttachmentPath(((LibraryLocation) (serverLibs[i])) + .getSystemLibrarySourcePath()); + runtimeEntry + .setSourceAttachmentRootPath(((LibraryLocation) (serverLibs[i])) + .getPackageRootPath()); + runtimeEntry + .setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES); + runtimeClasspaths.add(runtimeEntry.getLocation()); + } + + if (runtimeClasspaths.size() == 0) + { + return null; + } else + { + return (String[]) runtimeClasspaths + .toArray(new String[runtimeClasspaths.size()]); + } + + } + + /** + * + */ + protected LibraryLocation[] getLibraryLocations() + { + /* + * String header = bundle.getHeaders().get(Constants.BUNDLE_CLASSPATH); + * ManifestElement[] elements = ManifestElement.parseHeader( + * Constants.BUNDLE_CLASSPATH, header); + */ + Vector libraryLocations = new Vector(); + try + { + String header = (String) Platform.getBundle( + MetroStudioLaunch.PLUGIN_ID).getHeaders().get( + Constants.BUNDLE_CLASSPATH); + ManifestElement[] elements = ManifestElement.parseHeader( + Constants.BUNDLE_CLASSPATH, header); + IPath pluginPath = MetroStudioLaunch.getDefault() + .getPluginLocation(); + + for (int i = 0; i < elements.length; i++) + { + libraryLocations.add(new LibraryLocation( + // pluginPath.append(libraries[i].getPath()), + pluginPath.append(elements[i].getValue()), Path.EMPTY, + Path.EMPTY)); + } + + pluginPath = pluginPath.append("/lib/hibernate"); + String[] path = pluginPath.toFile().list(); + if (path != null) + { + for (int i = 0; i < path.length; i++) + { + if (path[i].endsWith(".jar")) + { //$NON-NLS-1$ + libraryLocations.add(new LibraryLocation(pluginPath + .append(path[i]), Path.EMPTY, Path.EMPTY)); + } + } + } + } catch (BundleException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return (LibraryLocation[]) libraryLocations + .toArray(new LibraryLocation[libraryLocations.size()]); + } + + public String getVMArguments(ILaunchConfiguration configuration) + { + String fallback = System.getProperty("user.home") + "/.merlin"; + String home = System.getProperty(METRO_HOME_KEY, fallback); + + return "-Djava.security.policy=" + home + + "/bin/security.policy -Dmerlin.home=" + home; + + } + + public String getProgramArguments(ILaunchConfiguration config) + { + + StringBuffer param = new StringBuffer(); + try + { + // create path to output location + IProject project = MetroStudioLaunch.getWorkspace().getRoot() + .getProject(config.getName()); + IJavaProject proj = JavaCore.create(project); + param.append('"'); + param.append(project.getLocation().append( + proj.getOutputLocation().lastSegment()).toString()); + param.append('"'); + } catch (JavaModelException e) + { + e.printStackTrace(); + } + return param.toString() + " -execute -debug"; + } + + private IVMRunner getVMRunner(String mode, ILaunchConfiguration config, + IVMInstall vmInstall) throws CoreException + { + // Virtual machine + IVMRunner vmRunner = vmInstall.getVMRunner(mode); + if (vmRunner == null) + { + if (mode == ILaunchManager.DEBUG_MODE) + { + abort( + MessageFormat + .format( + "JRE {0} does not support debug mode.", new String[] { vmInstall.getName() }), null, 3); //$NON-NLS-1$ + } else + { + abort( + MessageFormat + .format( + "JRE {0} does not support run mode.", new String[] { vmInstall.getName() }), null, 4); //$NON-NLS-1$ + } + } + return vmRunner; + } + + protected void abort(String message, Throwable exception, int code) + throws CoreException + { + throw new CoreException(new Status(IStatus.ERROR, ResourcesPlugin + .getPlugin().getBundle().getSymbolicName(), code, message, + exception)); + } + +} + Added: avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/MetroLaunchShortcut.java ============================================================================== --- (empty file) +++ avalon/trunk/central/laboratory/studio/eclipse/launch/src/org/apache/metro/studio/eclipse/launch/config/MetroLaunchShortcut.java Tue Aug 31 20:41:32 2004 @@ -0,0 +1,199 @@ +/* + + Copyright 2004. The Apache Software Foundation. + + Licensed 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.metro.studio.eclipse.launch.config; + +import org.apache.metro.studio.eclipse.core.templateengine.BlockProjectManager; +import org.apache.metro.studio.eclipse.launch.MetroStudioLaunch; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectNature; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.ILaunchConfigurationType; +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; +import org.eclipse.debug.core.ILaunchManager; +import org.eclipse.debug.ui.DebugUITools; +import org.eclipse.debug.ui.ILaunchShortcut; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; + +/** + * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team </a> + * + */ +public class MetroLaunchShortcut implements ILaunchShortcut +{ + private int exitCode = 0; + + /** + * extract the project out of the selected editor and perfom the launching + */ + public void launch(IEditorPart editor, String mode) + { + IEditorInput input = editor.getEditorInput(); + IProject project = (IProject) input.getAdapter(IProject.class); + launch(project, mode); + } + + /** + * extract the project out of the selection in project tree and perfom the + * launching + */ + public void launch(ISelection selection, String mode) + { + if (selection instanceof IStructuredSelection) + { + Object sel = ((IStructuredSelection) selection).getFirstElement(); + IProject project = null; + if (sel instanceof IJavaProject) + { + project = ((IJavaProject) sel).getProject(); + } + // TODO: extract file selections too + launch(project, mode); + } + } + + /** + * Launch the selected project. + * + * @param project + * @param mode + */ + protected void launch(IProject project, String mode) + { + + try + { + // check whether it is a Metro project + IProjectNature nature = BlockProjectManager.getNature(project); + if (nature == null) + return; + + // find the config for the given project. Otherwhise + // create a new config + ILaunchConfiguration config = findLaunchConfiguration(mode, nature); + if (config != null) + { + DebugUITools.saveAndBuildBeforeLaunch(); + config.launch(mode, null); + setExitCode(config.getAttribute(ILaunchConfigConstants.EXIT_CODE, 0)); + int i = getExitCode(); + } + } catch (CoreException e) + { + MetroStudioLaunch.log(e, "Core exception while launching " + + project.getName()); + } + } + + /** + * Check, whether a config for the given project is already + * present. If not create a new one. + * @param mode + * @param nature + * @return ILaunchConfiguration config + */ + protected ILaunchConfiguration findLaunchConfiguration(String mode, + IProjectNature nature) + { + + ILaunchConfigurationType configType = getMerlinContainerLaunchConfigType(); + try + { + // get all configs + ILaunchConfiguration[] configs = getLaunchManager() + .getLaunchConfigurations(configType); + for (int i = 0; i < configs.length; i++) + { + ILaunchConfiguration config = configs[i]; + if (nature.getProject().getName().equals(config.getName())) + { + // found the config for the project + return config; + } + } + // config is not there. Create a new one + return createConfiguration(nature); + } catch (CoreException e) + { + MetroStudioLaunch + .log(e, "findLaunchConfiguration(String mode) handling CoreException"); //$NON-NLS-1$ + } + + return null; + } + + /** + * Create a new config object + * @param nature + * @return + */ + protected ILaunchConfiguration createConfiguration(IProjectNature nature) + { + ILaunchConfiguration config = null; + try + { + ILaunchConfigurationType type = getMerlinContainerLaunchConfigType(); + String instanceName = nature.getProject().getName(); + ILaunchConfigurationWorkingCopy workingCopy = type.newInstance( + null, instanceName); + config = workingCopy.doSave(); + } catch (CoreException e) + { + MetroStudioLaunch.log(e, + "createConfiguration() handling CoreException"); //$NON-NLS-1$ + } + return config; + } + + protected ILaunchConfigurationType getMerlinContainerLaunchConfigType() + { + + return getLaunchManager() + .getLaunchConfigurationType( + MetroStudioLaunch.METRO_CONFIG_TYPE); + } + + /** + * get the LaunchManager + * @return + */ + protected ILaunchManager getLaunchManager() + { + return DebugPlugin.getDefault().getLaunchManager(); + } + + /** + * @return Returns the exitCode. + */ + public int getExitCode() + { + return exitCode; + } + /** + * @param exitCode The exitCode to set. + */ + public void setExitCode(int exitCode) + { + this.exitCode = exitCode; + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]