private static void waitForStart(String portString, int timeToWait) throws Exception { int waitTime = 0; int port = Integer.parseInt(portString); NetworkServerControl derbyServer = new NetworkServerControl(InetAddress.getByName("localhost"), port); while (waitTime < timeToWait) { try { derbyServer.ping(); return; } catch (Exception e) { Thread currentThread = Thread.currentThread(); synchronized (currentThread) { try { currentThread.wait(1000); waitTime += 1000; if (waitTime >= timeToWait) { System.out.println("Giving up on wait, waited: " + waitTime); throw e; } } catch (InterruptedException ie) { } } } } }
private static void listProperties(String portString) throws Exception { int port = Integer.parseInt(portString); NetworkServerControl derbyServer = new NetworkServerControl(InetAddress.getByName("localhost"), port); Properties p = derbyServer.getCurrentProperties(); p.list(System.out); }
/** * Tries to check if the Network Server is up and running by calling ping If successful, then it * returns else tries for 50 seconds before giving up and throwing an exception. * * @throws Exception when there is a problem with testing if the Network Server is up and running */ private static void waitForStart() throws Exception { // Server instance for testing connection org.apache.derby.drda.NetworkServerControl server = null; // Use NetworkServerControl.ping() to wait for // NetworkServer to come up. We could have used // NetworkServerControl to start the server but the property is // easier. server = new NetworkServerControl(); System.out.println("Testing if Network Server is up and running!"); for (int i = 0; i < 10; i++) { try { Thread.currentThread().sleep(5000); server.ping(); } catch (Exception e) { System.out.println("Try #" + i + " " + e.toString()); if (i == 9) { System.out.println("Giving up trying to connect to Network Server!"); throw e; } } } System.out.println("Derby Network Server now running"); }
public boolean isStarted() { try { networkServerControl.ping(); return true; } catch (Exception e) { return false; } }
public static void main(String[] args) /* throws GlassFishException */ { logger.info(String.format("Launching %s...", APPLICATION_NAME)); // Determine installation path File installationPath = new File(Launcher.class.getProtectionDomain().getCodeSource().getLocation().getPath()); String installationDirectoryPath = (installationPath.isDirectory() ? installationPath : installationPath.getParentFile()) .getAbsolutePath(); logger.info(String.format("Installation directory path: %s", installationDirectoryPath)); logger.info("Starting Java DB (Apache Derby)..."); NetworkServerControl serverControl; try { serverControl = new NetworkServerControl(InetAddress.getByName("localhost"), 1527); serverControl.start(null); } catch (Exception e) { throw new RuntimeException("Cannot start Java DB (Apache Derby)!", e); } logger.info(String.format("Creating database %s...", DATABASE_NAME)); String driver = "org.apache.derby.jdbc.ClientDriver"; String connectionURL = "jdbc:derby:" + /* installationDirectoryPath + "\\" + */ DATABASE_NAME + ";create=true"; Connection connection = null; try { Class.forName(driver); } catch (java.lang.ClassNotFoundException e) { throw new RuntimeException(e); } try { connection = DriverManager.getConnection(connectionURL); } catch (Throwable e) { throw new RuntimeException(e); } finally { try { connection.close(); } catch (SQLException e) { // Ignore exception } } logger.info("Starting Oracle Glassfish Embedded and deploying application..."); GlassFishProperties glassfishProperties = new GlassFishProperties(); String configFilePath = installationDirectoryPath + "\\" + GLASSFISH_CONFIG_FILE_NAME; String configFileURI = new File(configFilePath).toURI().toString(); logger.info(String.format("Configuration file URI: %s", configFileURI)); glassfishProperties.setConfigFileURI(configFileURI); glassfishProperties.setConfigFileReadOnly(true); try { GlassFishRuntime glassfishRuntime = GlassFishRuntime.bootstrap(); GlassFish glassfish = glassfishRuntime.newGlassFish(glassfishProperties); glassfish.start(); // Deploy plenus Server package to GlassFish File applicationPackage = new File(installationDirectoryPath + "\\" + APPLICATION_PACKAGE_FILE_NAME); Deployer deployer = glassfish.getDeployer(); deployer.deploy( applicationPackage); // Can be invoked instead the variant above because other parameters // are optional. System.out.println(String.format("Press <<Enter>> to stop the %s...", APPLICATION_NAME)); // Wait for <<Enter>> try { new BufferedReader(new java.io.InputStreamReader(System.in)).readLine(); } catch (IOException e) { // Ignore exception } // Teardown GlassFish glassfish.dispose(); glassfishRuntime.shutdown(); } catch (GlassFishException e) { throw new RuntimeException("Glassfish error!", e); } logger.info("Stopping Java DB (Apache Derby)..."); try { serverControl.shutdown(); } catch (Exception e) { throw new RuntimeException("Cannot stop Java DB (Apache Derby)!", e); } }
public void stop() throws Exception { networkServerControl.shutdown(); }
public void start() throws Exception { networkServerControl = new NetworkServerControl(InetAddress.getByName("localhost"), port, userName, password); networkServerControl.start(new PrintWriter(System.out)); }