Index: apichanges.xml =================================================================== RCS file: /cvs/j2eeserver/apichanges.xml,v retrieving revision 1.36 diff -a -u -r1.36 apichanges.xml --- apichanges.xml 4 Oct 2007 07:43:47 -0000 1.36 +++ apichanges.xml 20 Nov 2007 19:52:15 -0000 @@ -109,6 +109,28 @@ + + + + Adding InstanceProperties.createInstanceProperties(String, String, + String, String, Map<String, String>) method. + + + + + + +

+ The InstanceProperties.createInstanceProperties(String, String, String, String) + method does not provide any way how to pass other initial properties + required by the plugin. This is usually needed and workarounded + in many plugins. New method provides additional parameter containing + the any plugin required properties. +

+
+ + +
Index: nbproject/project.properties =================================================================== RCS file: /cvs/j2eeserver/nbproject/project.properties,v retrieving revision 1.28 diff -a -u -r1.28 project.properties --- nbproject/project.properties 9 Nov 2007 18:00:58 -0000 1.28 +++ nbproject/project.properties 20 Nov 2007 19:52:17 -0000 @@ -39,7 +39,7 @@ is.autoload=true javac.source=1.5 -spec.version.base=1.34.0 +spec.version.base=1.35.0 javadoc.overview=${basedir}/api/doc/overview.html javadoc.arch=${basedir}/arch.xml Index: src/org/netbeans/modules/j2ee/deployment/impl/ServerRegistry.java =================================================================== RCS file: /cvs/j2eeserver/src/org/netbeans/modules/j2ee/deployment/impl/ServerRegistry.java,v retrieving revision 1.55 diff -a -u -r1.55 ServerRegistry.java --- src/org/netbeans/modules/j2ee/deployment/impl/ServerRegistry.java 4 Oct 2007 07:44:19 -0000 1.55 +++ src/org/netbeans/modules/j2ee/deployment/impl/ServerRegistry.java 20 Nov 2007 19:52:28 -0000 @@ -292,11 +292,18 @@ * @param username username used by the deployment manager. * @param password password used by the deployment manager. * @param displayName display name wich represents server instance in IDE. + * @param initialProperties any other properties to set during the instance creation. + * If the map contains any of InstanceProperties.URL_ATTR, + * InstanceProperties.USERNAME_ATTR, InstanceProperties.PASSWORD_ATTR + * or InstanceProperties.DISPLAY_NAME_ATTR they will be ignored + * - the explicit parameter values are always used. + * null is accepted. + * * @throws InstanceCreationException when instance with same url is already * registered. */ public void addInstance(String url, String username, String password, - String displayName) throws InstanceCreationException { + String displayName, Map initialproperties) throws InstanceCreationException { // should never have empty url; UI should have prevented this if (url == null || url.equals("")) { //NOI18N Logger.getLogger("global").log(Level.INFO, NbBundle.getMessage(ServerRegistry.class, "MSG_EmptyUrl")); @@ -304,7 +311,7 @@ } checkInstanceAlreadyExists(url); - if (!addInstanceImpl(url, username, password, displayName)) { + if (!addInstanceImpl(url, username, password, displayName, initialproperties)) { throw new InstanceCreationException(NbBundle.getMessage(ServerRegistry.class, "MSG_FailedToCreateInstance", displayName)); } } @@ -344,17 +351,28 @@ /** * Add a new server instance in the server registry. * - * @param url URL to access deployment manager. - * @param username username used by the deployment manager. - * @param password password used by the deployment manager. - * @param displayName display name wich represents server instance in IDE. + * @param url URL to access deployment manager. + * @param username username used by the deployment manager. + * @param password password used by the deployment manager. + * @param displayName display name wich represents server instance in IDE. + * @param initialProperties any other properties to set during the instance creation. + * If the map contains any of InstanceProperties.URL_ATTR, + * InstanceProperties.USERNAME_ATTR, InstanceProperties.PASSWORD_ATTR + * or InstanceProperties.DISPLAY_NAME_ATTR they will be ignored + * - the explicit parameter values are always used. + * null is accepted. * * @return true if the server instance was created successfully, - * false otherwise. + * false otherwise. */ - private synchronized boolean addInstanceImpl(String url, String username, - String password, String displayName) { - if (instancesMap().containsKey(url)) return false; + private synchronized boolean addInstanceImpl(String url, String username, + String password, String displayName, Map initialProperties) { + if (instancesMap().containsKey(url)) { + return false; + } + + Map properties = cleanInitialProperties(initialProperties); + for (Iterator i = serversMap().values().iterator(); i.hasNext();) { Server server = (Server) i.next(); try { @@ -366,8 +384,15 @@ // whether the instance is not corrupted - see #46929 ServerString str = new ServerString(server.getShortName(),url,null); writeInstanceToFile(url,username,password); - if (displayName != null) instance.getInstanceProperties().setProperty( + if (displayName != null) { + instance.getInstanceProperties().setProperty( InstanceProperties.DISPLAY_NAME_ATTR, displayName); + } + + for (Map.Entry entry : properties.entrySet()) { + instance.getInstanceProperties().setProperty(entry.getKey(), entry.getValue()); + } + DeploymentManager manager = server.getDisconnectedDeploymentManager(url); if (manager != null) { fireInstanceListeners(url, true); @@ -387,6 +412,19 @@ } return false; } + + private Map cleanInitialProperties(Map initialProperties) { + if (initialProperties == null) { + return Collections.emptyMap(); + } + + Map properties = new HashMap(initialProperties); + properties.remove(InstanceProperties.URL_ATTR); + properties.remove(InstanceProperties.USERNAME_ATTR); + properties.remove(InstanceProperties.PASSWORD_ATTR); + properties.remove(InstanceProperties.DISPLAY_NAME_ATTR); + return properties; + } public void addInstance(FileObject fo) { String url = (String) fo.getAttribute(URL_ATTR); @@ -394,7 +432,7 @@ String password = (String) fo.getAttribute(PASSWORD_ATTR); String displayName = (String) fo.getAttribute(InstanceProperties.DISPLAY_NAME_ATTR); // System.err.println("Adding instance " + fo); - addInstanceImpl(url, username, password, displayName); + addInstanceImpl(url, username, password, displayName, null); } public Collection getInstances(InstanceListener il) { @@ -514,12 +552,23 @@ String password = defaultServerProp.getProperty(PASSWORD_ATTR); String targetName = defaultServerProp.getProperty(TARGETNAME_ATTR); + Map defaults = new HashMap(); + for (Enumeration e = defaultServerProp.propertyNames(); e.hasMoreElements(); ) { + String name = (String) e.nextElement(); + String value = defaultServerProp.getProperty(name); + if (value != null) { + defaults.put(name, value); + } + } + try { if (url != null) { InstanceProperties instProp = InstanceProperties.getInstanceProperties(url); - if (instProp == null) - instProp = InstanceProperties.createInstanceProperties(url, user, password); - instProp.setProperties(defaultServerProp); + if (instProp == null) { + instProp = InstanceProperties.createInstanceProperties(url, + user, password, null, defaults); + } + //instProp.setProperties(defaultServerProp); ServerInstance inst = getServerInstance(url); if (inst != null) Index: src/org/netbeans/modules/j2ee/deployment/plugins/api/InstanceProperties.java =================================================================== RCS file: /cvs/j2eeserver/src/org/netbeans/modules/j2ee/deployment/plugins/api/InstanceProperties.java,v retrieving revision 1.22 diff -a -u -r1.22 InstanceProperties.java --- src/org/netbeans/modules/j2ee/deployment/plugins/api/InstanceProperties.java 4 Oct 2007 07:44:41 -0000 1.22 +++ src/org/netbeans/modules/j2ee/deployment/plugins/api/InstanceProperties.java 20 Nov 2007 19:52:33 -0000 @@ -180,13 +180,40 @@ */ public static InstanceProperties createInstanceProperties(String url, String username, String password, String displayName) throws InstanceCreationException { + + return createInstanceProperties(url, username, password, displayName, null); + } + + /** + * Create new instance and returns instance properties for the server instance. + * + * @param url the url connection string to get the instance deployment manager. + * @param username username which is used by the deployment manager. + * @param password password which is used by the deployment manager. + * @param displayName display name which is used by IDE to represent this + * server instance. + * @param initialProperties any other properties to set during the instance creation. + * If the map contains any of InstanceProperties.URL_ATTR, + * InstanceProperties.USERNAME_ATTR, InstanceProperties.PASSWORD_ATTR + * or InstanceProperties.DISPLAY_NAME_ATTR they will be ignored + * - the explicit parameter values are always used. + * null is accepted. + * + * @return the InstanceProperties object, null if + * instance does not exists. + * @exception InstanceCreationException when instance with same url already + * registered. + * @since 1.35.0 + */ + public static InstanceProperties createInstanceProperties(String url, String username, + String password, String displayName, Map initialProperties) throws InstanceCreationException { ServerRegistry registry = ServerRegistry.getInstance(); - registry.addInstance(url, username, password, displayName); + registry.addInstance(url, username, password, displayName, initialProperties); ServerInstance inst = registry.getServerInstance(url); InstanceProperties ip = inst.getInstanceProperties(); return ip; } - + /** * Returns list of URL strings of all registered instances * @return array of URL strings Index: test/unit/src/org/netbeans/modules/j2ee/deployment/impl/ServerInstanceTest.java =================================================================== RCS file: /cvs/j2eeserver/test/unit/src/org/netbeans/modules/j2ee/deployment/impl/ServerInstanceTest.java,v retrieving revision 1.6 diff -a -u -r1.6 ServerInstanceTest.java --- test/unit/src/org/netbeans/modules/j2ee/deployment/impl/ServerInstanceTest.java 4 Oct 2007 07:45:00 -0000 1.6 +++ test/unit/src/org/netbeans/modules/j2ee/deployment/impl/ServerInstanceTest.java 20 Nov 2007 19:52:35 -0000 @@ -69,7 +69,7 @@ // setup ServerRegistry registry = ServerRegistry.getInstance(); String url = "fooservice:testStartStopInstance"; - registry.addInstance(url, "user", "password", "TestInstance"); + registry.addInstance(url, "user", "password", "TestInstance", null); ServerInstance instance = registry.getServerInstance(url); ServerTarget target = instance.getServerTarget("Target 1"); Index: test/unit/src/org/netbeans/modules/j2ee/deployment/impl/TargetServerTest.java =================================================================== RCS file: /cvs/j2eeserver/test/unit/src/org/netbeans/modules/j2ee/deployment/impl/TargetServerTest.java,v retrieving revision 1.7 diff -a -u -r1.7 TargetServerTest.java --- test/unit/src/org/netbeans/modules/j2ee/deployment/impl/TargetServerTest.java 4 Oct 2007 07:45:01 -0000 1.7 +++ test/unit/src/org/netbeans/modules/j2ee/deployment/impl/TargetServerTest.java 20 Nov 2007 19:52:36 -0000 @@ -85,7 +85,7 @@ if (name != null) url += "_"+name; try { - registry.addInstance(url, "user", "password", "TestInstance"); + registry.addInstance(url, "user", "password", "TestInstance", null); } catch (IOException ioe) { throw new RuntimeException(ioe); } server = new ServerString(registry.getServerInstance(url).getServerTarget("Target 1")); Index: test/unit/src/org/netbeans/modules/j2ee/deployment/plugins/api/InstancePropertiesTest.java =================================================================== RCS file: test/unit/src/org/netbeans/modules/j2ee/deployment/plugins/api/InstancePropertiesTest.java diff -N test/unit/src/org/netbeans/modules/j2ee/deployment/plugins/api/InstancePropertiesTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/unit/src/org/netbeans/modules/j2ee/deployment/plugins/api/InstancePropertiesTest.java 20 Nov 2007 19:52:37 -0000 @@ -0,0 +1,168 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * 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. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun 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 2007 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.j2ee.deployment.plugins.api; + +import java.util.HashMap; +import java.util.Map; +import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener; +import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; +import org.netbeans.modules.j2ee.deployment.impl.ServerRegistryTestBase; + +/** + * + * @author Petr Hejl + */ +public class InstancePropertiesTest extends ServerRegistryTestBase { + + private static final String TEST_URL_PREFIX = "fooservice:"; + + private static final String TEST_USERNAME = "username"; + + private static final String TEST_PASSWORD = "password"; + + private static final String TEST_DISPLAY_NAME = "name"; + + public InstancePropertiesTest(String name) { + super(name); + } + + public void testCreateProperties() throws InstanceCreationException { + String url = TEST_URL_PREFIX + "createProperties"; + + Map expected = new HashMap(); + expected.put(InstanceProperties.URL_ATTR, url); + expected.put(InstanceProperties.USERNAME_ATTR, TEST_USERNAME); + expected.put(InstanceProperties.PASSWORD_ATTR, TEST_PASSWORD); + expected.put(InstanceProperties.DISPLAY_NAME_ATTR, TEST_DISPLAY_NAME); + + InstanceListener listener = new TestInstanceListener(url, expected); + ServerRegistry.getInstance().addInstanceListener(listener); + try { + InstanceProperties props = InstanceProperties.createInstanceProperties( + url, TEST_USERNAME, TEST_PASSWORD, TEST_DISPLAY_NAME); + assertPropertiesEquals(expected, props); + } finally { + ServerRegistry.getInstance().removeInstanceListener(listener); + } + + try { + InstanceProperties.createInstanceProperties( + url, TEST_USERNAME, TEST_PASSWORD, TEST_DISPLAY_NAME); + fail("Duplicate instance created"); + } catch (InstanceCreationException ex) { + // expected + } + } + + public void testCreatePropertiesWithDefaults() throws InstanceCreationException { + Map defaults = new HashMap(); + defaults.put("property1", "value1"); + defaults.put("property2", "value2"); + defaults.put("property3", "value3"); + defaults.put(InstanceProperties.URL_ATTR, "ignored"); + defaults.put(InstanceProperties.USERNAME_ATTR, "ignored"); + defaults.put(InstanceProperties.PASSWORD_ATTR, "ignored"); + defaults.put(InstanceProperties.DISPLAY_NAME_ATTR, "ignored"); + + String url = TEST_URL_PREFIX + "createPropertiesExtended"; + + Map expected = new HashMap(); + expected.put(InstanceProperties.URL_ATTR, url); + expected.put(InstanceProperties.USERNAME_ATTR, TEST_USERNAME); + expected.put(InstanceProperties.PASSWORD_ATTR, TEST_PASSWORD); + expected.put(InstanceProperties.DISPLAY_NAME_ATTR, TEST_DISPLAY_NAME); + expected.put("property1", "value1"); + expected.put("property2", "value2"); + expected.put("property3", "value3"); + + InstanceListener listener = new TestInstanceListener(url, expected); + ServerRegistry.getInstance().addInstanceListener(listener); + try { + InstanceProperties props = InstanceProperties.createInstanceProperties( + url, TEST_USERNAME, TEST_PASSWORD, TEST_DISPLAY_NAME, defaults); + + assertPropertiesEquals(expected, props); + } finally { + ServerRegistry.getInstance().removeInstanceListener(listener); + } + + try { + InstanceProperties.createInstanceProperties( + url, TEST_USERNAME, TEST_PASSWORD, TEST_DISPLAY_NAME); + fail("Duplicate instance created"); // NOI18N + } catch (InstanceCreationException ex) { + // expected + } + } + + private static void assertPropertiesEquals(Map expected, InstanceProperties props) { + for (Map.Entry entry : expected.entrySet()) { + assertEquals(entry.getValue(), props.getProperty(entry.getKey())); + } + } + + private static class TestInstanceListener implements InstanceListener { + + private final String name; + + private final Map expected = new HashMap(); + + public TestInstanceListener(String name, Map expected) { + this.name = name; + this.expected.putAll(expected); + } + + public void instanceAdded(String serverInstanceID) { + if (name.equals(serverInstanceID)) { + InstanceProperties props = InstanceProperties.getInstanceProperties(serverInstanceID); + assertNotNull(props); + + assertPropertiesEquals(expected, props); + } + } + + public void changeDefaultInstance(String oldServerInstanceID, String newServerInstanceID) { + } + + public void instanceRemoved(String serverInstanceID) { + } + + } +}