diff --git a/java.api.common/src/org/netbeans/modules/java/api/common/util/CommonProjectUtils.java b/java.api.common/src/org/netbeans/modules/java/api/common/util/CommonProjectUtils.java --- a/java.api.common/src/org/netbeans/modules/java/api/common/util/CommonProjectUtils.java +++ b/java.api.common/src/org/netbeans/modules/java/api/common/util/CommonProjectUtils.java @@ -42,6 +42,8 @@ package org.netbeans.modules.java.api.common.util; +import java.net.URL; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import javax.lang.model.element.TypeElement; @@ -53,6 +55,7 @@ import org.netbeans.api.java.source.ElementHandle; import org.netbeans.api.java.source.SourceUtils; import org.netbeans.modules.java.api.common.project.ui.customizer.MainClassChooser; +import org.netbeans.spi.project.libraries.LibraryImplementation3; import org.openide.filesystems.FileObject; /** @@ -133,4 +136,40 @@ return SourceUtils.isMainClass(className, cpInfo); } + + /** + * Creates a temporary LibraryImplementation that can subsequently be used with + * both Ant and Maven based Java projects. + * @param classpath local library classpath for use by Ant + * @param src local library sources for use by Ant + * @param javadoc local Javadoc path for use by Ant + * @param mavendeps list of maven dependencies in the form of groupId:artifactId:version:type, + * for example org.eclipse.persistence:eclipselink:2.3.2:jar + * @param mavenrepos list of maven repositories in the form of layout:url, + * for example default:http://download.eclipse.org/rt/eclipselink/maven.repo/ + * @return LibraryImplementation representing the information passed as parameters + */ + public static LibraryImplementation3 createTransientJavaLibraryImplementation( + URL[] classpath, URL[] src, URL[] javadoc, + String[] mavendeps, String[] mavenrepos) { + TransientLibraryImplementation lib = new TransientLibraryImplementation(); + // volumes: "classpath","src","javadoc" + lib.setContent("classpath", Arrays.asList(classpath)); + lib.setContent("src", Arrays.asList(src)); + lib.setContent("javadoc", Arrays.asList(javadoc)); + // properties: "maven-dependencies", "maven-repositories" + lib.getProperties().put("maven-dependencies", getPropertyString(mavendeps)); + lib.getProperties().put("maven-repositories", getPropertyString(mavenrepos)); + return lib; + } + + private static String getPropertyString(String[] values) { + StringBuilder result = new StringBuilder(); + for (String value: values) { + result.append(value); + result.append('\n'); + } + return result.toString(); + } + } diff --git a/java.api.common/src/org/netbeans/modules/java/api/common/util/TransientLibraryImplementation.java b/java.api.common/src/org/netbeans/modules/java/api/common/util/TransientLibraryImplementation.java new file mode 100644 --- /dev/null +++ b/java.api.common/src/org/netbeans/modules/java/api/common/util/TransientLibraryImplementation.java @@ -0,0 +1,174 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.api.common.util; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.netbeans.spi.project.libraries.LibraryImplementation3; + +/** + * + * @author petrjiricka + */ +class TransientLibraryImplementation implements LibraryImplementation3 { + + public TransientLibraryImplementation() {} + + private Map properties = new HashMap(3); + private String displayName = ""; // NOI18N + private String name = ""; // NOI18N + private String description; + private String localizingBundle; + private List classpathVolume = new ArrayList(4); + private List srcVolume = new ArrayList(4); + private List javadocVolume = new ArrayList(4); + private PropertyChangeSupport pcs = new PropertyChangeSupport(this); + + @Override + public Map getProperties() { + return properties; + } + + @Override + public void setProperties(Map properties) { + this.properties = properties; + pcs.firePropertyChange(PROP_PROPERTIES, null, null); + } + + @Override + public void setDisplayName(String displayName) { + String oldDisplayName = this.displayName; + this.displayName = displayName; + pcs.firePropertyChange(PROP_DESCRIPTION, oldDisplayName, this.displayName); + + } + + @Override + public String getDisplayName() { + return displayName; + } + + @Override + public String getType() { + return "j2se"; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public String getLocalizingBundle() { + return localizingBundle; + } + + @Override + public List getContent(String volumeType) throws IllegalArgumentException { + // volumes: "classpath","src","javadoc" + if ("classpath".equals(volumeType)) { return classpathVolume; } + if ("src".equals(volumeType)) { return srcVolume; } + if ("javadoc".equals(volumeType)) { return javadocVolume; } + throw new IllegalArgumentException(volumeType); + } + + @Override + public void setName(String name) { + String oldName = this.name; + this.name = name; + pcs.firePropertyChange(PROP_NAME, oldName, this.name); + } + + @Override + public void setDescription(String text) { + String oldDescription = description; + this.description = text; + pcs.firePropertyChange(PROP_DESCRIPTION, oldDescription, description); + } + + @Override + public void setLocalizingBundle(String resourceName) { + this.localizingBundle = resourceName; + } + + @Override + public void addPropertyChangeListener(PropertyChangeListener l) { + pcs.addPropertyChangeListener(l); + } + + @Override + public void removePropertyChangeListener(PropertyChangeListener l) { + pcs.removePropertyChangeListener(l); + } + + @Override + public void setContent(String volumeType, List path) throws IllegalArgumentException { + // volumes: "classpath","src","javadoc" + if ("classpath".equals(volumeType)) { + classpathVolume = path; + pcs.firePropertyChange(PROP_CONTENT, null, null); + return; + } + if ("src".equals(volumeType)) { + srcVolume = path; + pcs.firePropertyChange(PROP_CONTENT, null, null); + return; + } + if ("javadoc".equals(volumeType)) { + javadocVolume = path; + pcs.firePropertyChange(PROP_CONTENT, null, null); + return; + } + throw new IllegalArgumentException(volumeType); + } + +} diff --git a/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/util/CommonProjectUtilsTest.java b/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/util/CommonProjectUtilsTest.java new file mode 100644 --- /dev/null +++ b/java.api.common/test/unit/src/org/netbeans/modules/java/api/common/util/CommonProjectUtilsTest.java @@ -0,0 +1,121 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.api.common.util; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.platform.JavaPlatform; +import org.netbeans.api.project.libraries.Library; +import org.netbeans.spi.project.libraries.LibraryFactory; +import org.netbeans.spi.project.libraries.LibraryImplementation; +import org.openide.filesystems.FileObject; + +/** + * + * @author petrjiricka + */ +public class CommonProjectUtilsTest { + + public CommonProjectUtilsTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of createTransientJavaLibraryImplementation method, of class CommonProjectUtils. + */ + @Test + public void testCreateTransientJavaLibraryImplementation() throws MalformedURLException { + System.out.println("createTransientJavaLibraryImplementation"); + // volumes: "classpath","src","javadoc" + // properties: "maven-dependencies", "maven-repositories" + URL[] classpath = new URL[] {new URL("file:///a/cp/cp1.jar"), new URL("file:///a/cp/b/cp2.jar")}; + URL[] src = new URL[] {new URL("file:///a/src/x/src1/"), new URL("file:///a/srcplus/src2.zip"), new URL("file:///a/src/src3")}; + URL[] javadoc = new URL[] {new URL("http://example.com/my/remote/javadoc")}; + String[] mavendeps = new String[] { + "com.sun.jersey.contribs:jersey-multipart:1.8:jar", + "com.sun.jersey.contribs:jersey-guice:1.8:jar", + "com.sun.jersey.contribs:jersey-simple-server:1.8:jar", + "com.sun.jersey.contribs.jersey-oauth:oauth-client:1.8:jar"}; + String[] mavenrepos = new String[] {"default:http://download.eclipse.org/rt/eclipselink/maven.repo/"}; + assertTransientJavaLibrary(classpath, src, javadoc, mavendeps, mavenrepos); + } + + private void assertTransientJavaLibrary(URL[] classpath, URL[] src, URL[] javadoc, + String[] mavendeps, String[] mavenrepos) { + LibraryImplementation resultImpl = CommonProjectUtils.createTransientJavaLibraryImplementation(classpath, src, javadoc, mavendeps, mavenrepos); + Library result = LibraryFactory.createLibrary(resultImpl); + List resClasspath = result.getContent("classpath"); + List resSrc = result.getContent("src"); + List resJavadoc = result.getContent("javadoc"); + String resMavendeps = result.getProperties().get("maven-dependencies"); + String resMavenrepos = result.getProperties().get("maven-repositories"); + assertArrayEquals(classpath, resClasspath.toArray()); + assertArrayEquals(src, resSrc.toArray()); + assertArrayEquals(javadoc, resJavadoc.toArray()); + assertArrayEquals(mavendeps, resMavendeps.split("([\\s])+")); + assertArrayEquals(mavenrepos, resMavenrepos.split("([\\s])+")); + } +}