diff -r b7b62cba2b0b o.n.bootstrap/arch.xml --- a/o.n.bootstrap/arch.xml Tue Feb 05 18:31:04 2013 +0100 +++ b/o.n.bootstrap/arch.xml Fri Feb 08 14:49:44 2013 +0100 @@ -586,6 +586,15 @@
  • + +

    + One can disable the CLI server (listening on commands + from subsequent invocations) by setting property + org.netbeans.CLIHandler.server to false. +

    +
    +
  • +
  • Contains class name of a class that can serve as provider of the writable layer of the diff -r b7b62cba2b0b o.n.bootstrap/src/org/netbeans/Bundle.properties --- a/o.n.bootstrap/src/org/netbeans/Bundle.properties Tue Feb 05 18:31:04 2013 +0100 +++ b/o.n.bootstrap/src/org/netbeans/Bundle.properties Fri Feb 08 14:49:44 2013 +0100 @@ -55,6 +55,8 @@ \n\ Click OK to continue in spite of previous error. MSG_CannotWriteTitle=Warning +MSG_AlreadyRunning=The application is already running. +MSG_AlreadyRunningTitle=Warning EXC_not_a_module={0} is not a valid NetBeans module. diff -r b7b62cba2b0b o.n.bootstrap/src/org/netbeans/CLIHandler.java --- a/o.n.bootstrap/src/org/netbeans/CLIHandler.java Tue Feb 05 18:31:04 2013 +0100 +++ b/o.n.bootstrap/src/org/netbeans/CLIHandler.java Fri Feb 08 14:49:44 2013 +0100 @@ -282,6 +282,7 @@ static final class Status { public static final int CANNOT_CONNECT = -255; public static final int CANNOT_WRITE = -254; + public static final int ALREADY_RUNNING = -253; private final File lockFile; private final int port; @@ -608,77 +609,88 @@ final RandomAccessFile os = raf; raf.seek(0L); - - server = new Server(new FileAndLock(os, lock), arr, block, handlers, failOnUnknownOptions); - int p = server.getLocalPort(); + + int p; + if ("false".equals(System.getProperty("org.netbeans.CLIHandler.server"))) { // NOI18N + server = null; + p = 0; + } else { + server = new Server(new FileAndLock(os, lock), arr, block, handlers, failOnUnknownOptions); + p = server.getLocalPort(); + } os.writeInt(p); os.getChannel().force(true); enterState(20, block); - Task parael = secureCLIPort.post(new Runnable() { // NOI18N - @Override - public void run() { - SecureRandom random = null; - enterState(95, block); - try { - random = SecureRandom.getInstance("SHA1PRNG"); // NOI18N - } catch (NoSuchAlgorithmException e) { - // #36966: IBM JDK doesn't have it. + Task parael; + if (server != null) { + parael = secureCLIPort.post(new Runnable() { // NOI18N + @Override + public void run() { + SecureRandom random = null; + enterState(95, block); try { - random = SecureRandom.getInstance("IBMSecureRandom"); // NOI18N - } catch (NoSuchAlgorithmException e2) { - // OK, disable server... - server.stopServer(); + random = SecureRandom.getInstance("SHA1PRNG"); // NOI18N + } catch (NoSuchAlgorithmException e) { + // #36966: IBM JDK doesn't have it. + try { + random = SecureRandom.getInstance("IBMSecureRandom"); // NOI18N + } catch (NoSuchAlgorithmException e2) { + // OK, disable server... + server.stopServer(); + } + } + + enterState(96, block); + + if (random != null) { + random.nextBytes(arr); + } + + enterState(97, block); + + try { + os.write(arr); + os.getChannel().force(true); + + enterState(27,block); + // if this turns to be slow due to lookup of getLocalHost + // address, it can be done asynchronously as nobody needs + // the address in the stream if the server is listening + byte[] host; + try { + if (block != null && block.intValue() == 667) { + // this is here to emulate #64004 + throw new UnknownHostException("dhcppc0"); // NOI18N + } + host = InetAddress.getLocalHost().getAddress(); + } catch (UnknownHostException unknownHost) { + if (!"dhcppc0".equals(unknownHost.getMessage())) { // NOI18N, see above + // if we just cannot get the address, we can go on + unknownHost.printStackTrace(); + } + host = new byte[] {127, 0, 0, 1}; + } + os.write(host); + } catch (IOException ex) { + ex.printStackTrace(); + } + try { + os.getChannel().force(true); + } catch (IOException ex) { + // ignore } } - - enterState(96, block); - - if (random != null) { - random.nextBytes(arr); - } - - enterState(97, block); - - try { - os.write(arr); - os.getChannel().force(true); - - enterState(27,block); - // if this turns to be slow due to lookup of getLocalHost - // address, it can be done asynchronously as nobody needs - // the address in the stream if the server is listening - byte[] host; - try { - if (block != null && block.intValue() == 667) { - // this is here to emulate #64004 - throw new UnknownHostException("dhcppc0"); // NOI18N - } - host = InetAddress.getLocalHost().getAddress(); - } catch (UnknownHostException unknownHost) { - if (!"dhcppc0".equals(unknownHost.getMessage())) { // NOI18N, see above - // if we just cannot get the address, we can go on - unknownHost.printStackTrace(); - } - host = new byte[] {127, 0, 0, 1}; - } - os.write(host); - } catch (IOException ex) { - ex.printStackTrace(); - } - try { - os.getChannel().force(true); - } catch (IOException ex) { - // ignore - } - } - }); + }); + } else { + parael = Task.EMPTY; + } int execCode = processInitLevelCLI (args, handlers, failOnUnknownOptions); enterState(0, block); - return new Status(lockFile, server.getLocalPort(), execCode, parael); + return new Status(lockFile, p, execCode, parael); } catch (IOException ex) { if (!"EXISTS".equals(ex.getMessage())) { // NOI18N ex.printStackTrace(); @@ -695,6 +707,9 @@ } is = raf; port = is.readInt(); + if (port == 0) { + return new Status(lockFile, 0, Status.ALREADY_RUNNING, Task.EMPTY); + } enterState(22, block); key = new byte[KEY_LENGTH]; is.readFully(key); diff -r b7b62cba2b0b o.n.bootstrap/src/org/netbeans/MainImpl.java --- a/o.n.bootstrap/src/org/netbeans/MainImpl.java Tue Feb 05 18:31:04 2013 +0100 +++ b/o.n.bootstrap/src/org/netbeans/MainImpl.java Fri Feb 08 14:49:44 2013 +0100 @@ -228,6 +228,14 @@ return result.getExitCode(); } } + if (result.getExitCode () == CLIHandler.Status.ALREADY_RUNNING) { + JOptionPane.showMessageDialog(null, + MessageFormat.format(ResourceBundle.getBundle("org/netbeans/Bundle").getString("MSG_AlreadyRunning"), user), + ResourceBundle.getBundle("org/netbeans/Bundle").getString("MSG_AlreadyRunningTitle"), + JOptionPane.OK_OPTION + ); + return result.getExitCode(); + } if (methodToCall != null) { String className = System.getProperty("netbeans.mainclass", "org.netbeans.core.startup.Main"); // NOI18N diff -r b7b62cba2b0b o.n.bootstrap/test/unit/src/org/netbeans/CLIHandlerNoServerTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/o.n.bootstrap/test/unit/src/org/netbeans/CLIHandlerNoServerTest.java Fri Feb 08 14:49:44 2013 +0100 @@ -0,0 +1,100 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 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 2013 Sun Microsystems, Inc. + */ +package org.netbeans; + +import java.io.File; +import org.netbeans.junit.NbTestCase; + +/** + * + * @author Jaroslav Tulach + */ +public class CLIHandlerNoServerTest extends NbTestCase { + + public CLIHandlerNoServerTest(String name) { + super(name); + } + + @Override + protected int timeOut() { + return 15000; + } + + @Override + protected void setUp() throws Exception { + System.setProperty("org.netbeans.CLIHandler.server", "false"); + + // setups a temporary file + String p = getWorkDirPath(); + if (p == null) { + p = System.getProperty("java.io.tmpdir"); + } + String tmp = p; + assertNotNull(tmp); + System.getProperties().put("netbeans.user", tmp); + + File f = new File(tmp, "lock"); + if (f.exists()) { + assertTrue("Clean up previous mess", f.delete()); + assertTrue(!f.exists()); + } + } + + + public void testCannotStartForTheSecondTime() { + CLIHandler.Status res = CLIHandlerTest.cliInitialize( + new String[0], + new CLIHandler[0], + CLIHandlerTest.nullInput, CLIHandlerTest.nullOutput, CLIHandlerTest.nullOutput + ); + + assertEquals("Started", 0, res.getExitCode()); + + CLIHandler.Status snd = CLIHandlerTest.cliInitialize( + new String[0], + new CLIHandler[0], + CLIHandlerTest.nullInput, CLIHandlerTest.nullOutput, CLIHandlerTest.nullOutput + ); + + assertEquals("Can't start for the second time", CLIHandler.Status.ALREADY_RUNNING, snd.getExitCode()); + } +} diff -r b7b62cba2b0b o.n.bootstrap/test/unit/src/org/netbeans/CLIHandlerTest.java --- a/o.n.bootstrap/test/unit/src/org/netbeans/CLIHandlerTest.java Tue Feb 05 18:31:04 2013 +0100 +++ b/o.n.bootstrap/test/unit/src/org/netbeans/CLIHandlerTest.java Fri Feb 08 14:49:44 2013 +0100 @@ -58,8 +58,8 @@ */ public class CLIHandlerTest extends NbTestCase { - private static ByteArrayInputStream nullInput = new ByteArrayInputStream(new byte[0]); - private static ByteArrayOutputStream nullOutput = new ByteArrayOutputStream(); + final static ByteArrayInputStream nullInput = new ByteArrayInputStream(new byte[0]); + final static ByteArrayOutputStream nullOutput = new ByteArrayOutputStream(); private Logger LOG; @@ -826,19 +826,19 @@ // Utility methods // - private static CLIHandler.Status cliInitialize(String[] args, CLIHandler handler, InputStream is, OutputStream os, OutputStream err, Integer lock) { + static CLIHandler.Status cliInitialize(String[] args, CLIHandler handler, InputStream is, OutputStream os, OutputStream err, Integer lock) { return cliInitialize(args, handler, is, os, err, lock, System.getProperty ("user.dir")); } - private static CLIHandler.Status cliInitialize(String[] args, CLIHandler handler, InputStream is, OutputStream os, OutputStream err, Integer lock, String currentDir) { + static CLIHandler.Status cliInitialize(String[] args, CLIHandler handler, InputStream is, OutputStream os, OutputStream err, Integer lock, String currentDir) { return cliInitialize(args, Collections.nCopies(1, handler), is, os, err, lock, currentDir); } - private static CLIHandler.Status cliInitialize(String[] args, CLIHandler[] arr, InputStream is, OutputStream os, OutputStream err) { + static CLIHandler.Status cliInitialize(String[] args, CLIHandler[] arr, InputStream is, OutputStream os, OutputStream err) { return cliInitialize(args, Arrays.asList(arr), is, os, err, null); } - private static CLIHandler.Status cliInitialize(String[] args, List coll, InputStream is, OutputStream os, java.io.OutputStream err, Integer lock) { + static CLIHandler.Status cliInitialize(String[] args, List coll, InputStream is, OutputStream os, java.io.OutputStream err, Integer lock) { return cliInitialize (args, coll, is, os, err, lock, System.getProperty ("user.dir")); } - private static CLIHandler.Status cliInitialize(String[] args, List coll, InputStream is, OutputStream os, java.io.OutputStream err, Integer lock, String currentDir) { + static CLIHandler.Status cliInitialize(String[] args, List coll, InputStream is, OutputStream os, java.io.OutputStream err, Integer lock, String currentDir) { return CLIHandler.initialize(new CLIHandler.Args(args, is, os, err, currentDir), lock, coll, false, true, null); }