diff options
author | nhauge | 2010-05-12 14:39:14 +0000 |
---|---|---|
committer | nhauge | 2010-05-12 14:39:14 +0000 |
commit | 56e01d0dca257b9b5b6785f20f85ad0c8f30fd6e (patch) | |
tree | 1fbc08f38a379bde0eb97dfd05f57c17ca5bf6e5 /jpa | |
parent | 2de5671e51f3b8645b45253384829bba6a6d225d (diff) | |
download | webtools.dali-56e01d0dca257b9b5b6785f20f85ad0c8f30fd6e.tar.gz webtools.dali-56e01d0dca257b9b5b6785f20f85ad0c8f30fd6e.tar.xz webtools.dali-56e01d0dca257b9b5b6785f20f85ad0c8f30fd6e.zip |
310718 - Generate jaxb.properties file on MOXy schema generation if it doesn't already exist in the project.
Diffstat (limited to 'jpa')
6 files changed, 160 insertions, 32 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/Main.java b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/Main.java index 3576799b41..2438b94bc6 100644 --- a/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/Main.java +++ b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/Main.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.IOException; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import javax.xml.bind.JAXBException; @@ -22,6 +23,7 @@ import javax.xml.transform.stream.StreamResult; import org.eclipse.jpt.eclipselink.jaxb.core.schemagen.internal.JptEclipseLinkJaxbCoreMessages; import org.eclipse.persistence.jaxb.JAXBContext; +import org.eclipse.persistence.jaxb.JAXBContextFactory; /** * Generate a EclipseLink JAXB Schema @@ -92,8 +94,9 @@ public class Main Class[] sourceClasses = this.buildSourceClasses(this.sourceClassNames, loader); - jaxbContext = (JAXBContext)JAXBContext.newInstance(sourceClasses); - + //call MOXy JAXBContextFactory directly. This eliminates the need to have the JAXB properties file in place + //in time for the generation. + jaxbContext = (JAXBContext)JAXBContextFactory.createContext(sourceClasses, Collections.<String,Object>emptyMap()); } catch (JAXBException ex) { this.handleJaxbException(ex); diff --git a/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/jpt_eclipselink_jaxb_core.properties b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/jpt_eclipselink_jaxb_core.properties index 047632e98a..f7914dbf32 100644 --- a/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/jpt_eclipselink_jaxb_core.properties +++ b/jpa/plugins/org.eclipse.jpt.eclipselink.jaxb.core.schemagen/src/org/eclipse/jpt/eclipselink/jaxb/core/schemagen/internal/jpt_eclipselink_jaxb_core.properties @@ -18,4 +18,4 @@ javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory CONTEXT_FACTORY_NOT_FOUND = \nThe JAXBContextFactory {0} \n\ configured in the jaxb.properties file could not be located on the project classpath. \n\ The JAXB provider that defines this factory should be added to the project classpath, \n\ -or the JAXBContextFactory property should be removed to use the default provider. +or the jaxb.properties file should be removed to use the default provider. diff --git a/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/jpt_jaxb_core.properties b/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/jpt_jaxb_core.properties index 4176246858..e6367e13a7 100644 --- a/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/jpt_jaxb_core.properties +++ b/jpa/plugins/org.eclipse.jpt.jaxb.core.schemagen/src/org/eclipse/jpt/jaxb/core/schemagen/internal/jpt_jaxb_core.properties @@ -16,4 +16,4 @@ NOT_FOUND = \n\tNot found: {0} CONTEXT_FACTORY_NOT_FOUND = \nThe JAXBContextFactory {0} \n\ configured in the jaxb.properties file could not be located on the project classpath. \n\ The JAXB provider that defines this factory should be added to the project classpath, \n\ -or the JAXBContextFactory property should be removed to use the default provider. +or the jaxb.properties file should be removed to use the default provider. diff --git a/jpa/plugins/org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF b/jpa/plugins/org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF index 03de7f0f19..b64e592722 100644 --- a/jpa/plugins/org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF +++ b/jpa/plugins/org.eclipse.jpt.jaxb.ui/META-INF/MANIFEST.MF @@ -18,7 +18,8 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.4.0,4.0.0)", org.eclipse.jpt.ui;bundle-version="[2.0.0,3.0.0)", org.eclipse.jpt.utility;bundle-version="[1.2.0,2.0.0)", org.eclipse.ui;bundle-version="[3.4.0,4.0.0)", - org.eclipse.ui.ide;bundle-version="[3.4.0,4.0.0)" + org.eclipse.ui.ide;bundle-version="[3.4.0,4.0.0)", + org.eclipse.jpt.core;bundle-version="[2.3.0,3.0.0)" Export-Package: org.eclipse.jpt.jaxb.core.internal;x-internal:=true, org.eclipse.jpt.jaxb.ui;x-internal:=true, org.eclipse.jpt.jaxb.ui.internal;x-internal:=true, diff --git a/jpa/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/core/internal/SchemaGenerator.java b/jpa/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/core/internal/SchemaGenerator.java index 92b25a46c2..d27c374d21 100644 --- a/jpa/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/core/internal/SchemaGenerator.java +++ b/jpa/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/core/internal/SchemaGenerator.java @@ -9,11 +9,18 @@ *******************************************************************************/ package org.eclipse.jpt.jaxb.core.internal; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; @@ -30,12 +37,16 @@ import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; import org.eclipse.debug.core.ILaunchManager; import org.eclipse.debug.core.ILaunchesListener2; +import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; import org.eclipse.jdt.launching.IRuntimeClasspathEntry; import org.eclipse.jdt.launching.IVMInstall; import org.eclipse.jdt.launching.JavaRuntime; +import org.eclipse.jpt.core.internal.utility.jdt.JDTTools; import org.eclipse.jpt.jaxb.ui.JptJaxbUiPlugin; import org.eclipse.osgi.service.datalocation.Location; import org.osgi.framework.Bundle; @@ -51,6 +62,10 @@ public class SchemaGenerator static public String ECLIPSELINK_JAXB_SCHEMA_GEN_PACKAGE_NAME = "org.eclipse.jpt.eclipselink.jaxb.core.schemagen"; //$NON-NLS-1$ static public String ECLIPSELINK_JAXB_SCHEMA_GEN_CLASS = ECLIPSELINK_JAXB_SCHEMA_GEN_PACKAGE_NAME + ".Main"; //$NON-NLS-1$ + static public String ECLIPSELINK_JAXB_CONTEXT_FACTORY = "org.eclipse.persistence.jaxb.JAXBContextFactory"; //$NON-NLS-1$ + static public String ECLIPSELINK_JAXB_PROPERTIES_FILE_CONTENTS = "javax.xml.bind.context.factory=" + ECLIPSELINK_JAXB_CONTEXT_FACTORY; //$NON-NLS-1$ + static public String JAXB_PROPERTIES_FILE_NAME = "jaxb.properties"; //$NON-NLS-1$ + static public String JAXB_SCHEMA_GEN_JAR = JAXB_SCHEMA_GEN_PACKAGE_NAME + "_"; //$NON-NLS-1$ static public String ECLIPSELINK_JAXB_SCHEMA_GEN_JAR = ECLIPSELINK_JAXB_SCHEMA_GEN_PACKAGE_NAME + "_"; //$NON-NLS-1$ @@ -62,25 +77,25 @@ public class SchemaGenerator private ILaunchConfigurationWorkingCopy launchConfig; private ILaunch launch; - private final IProject project; + private final IJavaProject javaProject; private final String targetSchemaName; private final String[] sourceClassNames; - private final String mainType; - private final boolean useMoxy; + private String mainType; + private boolean useMoxy; private final boolean isDebug = false; // ********** static methods ********** public static void generate( - IProject project, + IJavaProject javaProject, String targetSchemaName, String[] sourceClassNames, boolean useMoxy, IProgressMonitor monitor) { - if (project == null) { + if (javaProject == null) { throw new NullPointerException(); } - new SchemaGenerator(project, + new SchemaGenerator(javaProject, targetSchemaName, sourceClassNames, useMoxy, @@ -90,13 +105,13 @@ public class SchemaGenerator // ********** constructors ********** protected SchemaGenerator( - IProject project, + IJavaProject javaProject, String targetSchemaName, String[] sourceClassNames, boolean useMoxy, @SuppressWarnings("unused") IProgressMonitor monitor) { super(); - this.project = project; + this.javaProject = javaProject; this.targetSchemaName = targetSchemaName; this.sourceClassNames = sourceClassNames; this.useMoxy = useMoxy; @@ -123,14 +138,127 @@ public class SchemaGenerator } protected void generate() { - String projectLocation = this.getProject().getLocation().toString(); - + // generate jaxb.properties file if necessary + if (this.useMoxy){ + if (!isJaxbPropertiesFilePresent()){ + this.generateJaxbPropertiesFile(); + } + else if (!isJaxbContextMoxy(getJaxbPropertiesFile())){ + //properties file actually specifies a different implementation + //override wizard setting and fall back to generic generation + this.useMoxy = false; + this.mainType = JAXB_SCHEMA_GEN_CLASS; + } + } + String projectLocation = getProject().getLocation().toString(); this.initializeLaunchConfiguration(projectLocation); - this.addLaunchListener(); this.launch = this.saveAndLaunchConfig(); } + /** + * Returns the first "jaxb.properties" file that is found in a valid source + * folder in the project. + * + * Returns null if no "jaxb.properties" file is found. + */ + private IFile getJaxbPropertiesFile() { + return getJaxbPropertiesFileFromPackageRoots(JDTTools.getJavaSourceFolders(this.javaProject)); + } + + private IFile getJaxbPropertiesFileFromPackageRoots(Iterable<IPackageFragmentRoot> packageFragmentRoots){ + Object[] objects = null; + IJavaElement[] javaElements; + try { + for (IPackageFragmentRoot pfr : packageFragmentRoots) { + javaElements = pfr.getChildren(); + for (IJavaElement javaElement : javaElements) { + objects = ((IPackageFragment) javaElement).getNonJavaResources(); + for (Object object : objects) { + IResource resource = (IResource) object; + if (resource.getName().equals(JAXB_PROPERTIES_FILE_NAME)) { + // jaxb.properties has been found + return (IFile)resource; + } + } + } + } + } catch (JavaModelException jme) { + throw new RuntimeException(jme); + } + return null; + } + + private boolean isJaxbPropertiesFilePresent(){ + return getJaxbPropertiesFile()!= null; + } + + private boolean isJaxbContextMoxy(IFile propertyFile){ + + InputStream in = null; + try { + in = propertyFile.getContents(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + String line = reader.readLine(); + //jaxb.properties will only contain one property entry, the JAXBContextFactory + String propertyValue = line.substring(line.indexOf("=") + 1); + if (propertyValue.equals(ECLIPSELINK_JAXB_CONTEXT_FACTORY)){ + return true; + } + } catch (CoreException ce){ + throw new RuntimeException(ce); + } catch (IOException ioe){ + throw new RuntimeException(ioe); + } finally { + if (in != null){ + try{ + in.close(); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } + } + } + return false; + } + + private void generateJaxbPropertiesFile(){ + IPackageFragment packageFragment = findPackageFragementForSourceClassName(this.sourceClassNames[0]); + + IFolder folder = (IFolder)packageFragment.getResource(); + IFile file = folder.getFile("jaxb.properties"); + + byte[] bytes; + try { + bytes = ECLIPSELINK_JAXB_PROPERTIES_FILE_CONTENTS.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + InputStream contents = new ByteArrayInputStream(bytes); + + try { + //the input stream will be closed as a result of calling create + file.create(contents, IResource.NONE, null); + } catch (CoreException ce) { + throw new RuntimeException(ce); + } + } + + private IPackageFragment findPackageFragementForSourceClassName(String sourceClassName) { + String packageName = sourceClassName.substring(0, sourceClassName.lastIndexOf('.')); + + //Find the existing package fragment where we want to generate + for (IPackageFragmentRoot pfr : JDTTools.getJavaSourceFolders(this.javaProject)) { + //use the package of the first source class as the package for generation + IPackageFragment packageFragment = pfr.getPackageFragment(packageName); + if (packageFragment.exists()){ + return packageFragment; + } + } + //the existing package fragment was not found + throw new IllegalStateException("Java package must exist for source class"); + } + private void initializeLaunchConfiguration(String projectLocation) { this.specifyJRE(this.jre.getName(), this.jre.getVMInstallType().getId()); @@ -146,7 +274,7 @@ public class SchemaGenerator String jarName = (this.useMoxy) ? ECLIPSELINK_JAXB_SCHEMA_GEN_JAR : JAXB_SCHEMA_GEN_JAR; - this.specifyClasspathProperties(this.getJavaProject(), this.buildBootstrapJarPath(jarName)); + this.specifyClasspathProperties(this.javaProject, this.buildBootstrapJarPath(jarName)); } protected void postGenerate() { @@ -154,7 +282,7 @@ public class SchemaGenerator if ( ! this.isDebug) { this.removeLaunchConfiguration(LAUNCH_CONFIG_NAME); } - this.project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); + this.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); } catch (CoreException e) { throw new RuntimeException(e); @@ -330,17 +458,13 @@ public class SchemaGenerator return projectEntry; } - - protected IProject getProject() { - return this.project; - } - public IJavaProject getJavaProject() { - return JavaCore.create(this.project); + public IProject getProject() { + return this.javaProject.getProject(); } private IVMInstall getProjectJRE() throws CoreException { - return JavaRuntime.getVMInstall(this.getJavaProject()); + return JavaRuntime.getVMInstall(this.javaProject); } protected ILaunch getLaunch() { diff --git a/jpa/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/schemagen/SchemaGeneratorWizard.java b/jpa/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/schemagen/SchemaGeneratorWizard.java index a4c3a9949b..a563e46e25 100644 --- a/jpa/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/schemagen/SchemaGeneratorWizard.java +++ b/jpa/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/schemagen/SchemaGeneratorWizard.java @@ -89,7 +89,7 @@ public class SchemaGeneratorWizard extends Wizard implements IExportWizard { String[] sourceClassNames = this.buildSourceClassNames(this.getAllCheckedItems()); WorkspaceJob genEntitiesJob = new GenerateSchemaJob( - javaProject.getProject(), + javaProject, sourceClassNames, this.getTargetSchema(), this.usesMoxy()); @@ -190,27 +190,27 @@ public class SchemaGeneratorWizard extends Wizard implements IExportWizard { // ********** generate schema job ********** static class GenerateSchemaJob extends WorkspaceJob { - private final IProject project; + private final IJavaProject javaProject; private final String[] sourceClassNames; private final String targetSchema; private final boolean useMoxy; - GenerateSchemaJob(IProject project, String[] sourceClassNames, String targetSchema, boolean useMoxy) { + GenerateSchemaJob(IJavaProject project, String[] sourceClassNames, String targetSchema, boolean useMoxy) { super(JptJaxbUiMessages.SchemaGeneratorWizard_generatingSchema); - this.project = project ; + this.javaProject = project ; this.sourceClassNames = sourceClassNames; this.targetSchema = targetSchema; this.useMoxy = useMoxy; IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory(); - this.setRule(ruleFactory.modifyRule(project)); + this.setRule(ruleFactory.modifyRule(javaProject.getProject())); } @Override public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException { try{ - SchemaGenerator.generate(project, this.targetSchema, this.sourceClassNames, this.useMoxy, monitor); + SchemaGenerator.generate(javaProject, this.targetSchema, this.sourceClassNames, this.useMoxy, monitor); } catch(OperationCanceledException e) { //user canceled generation |