Ejemplo n.º 1
0
 @Override
 public void run() {
   while (true) {
     if (Server.isRunning() == false) {
       Server.setLock(true);
       try {
         serverSocket.close();
         if (Server.getClients() > 0) {
           try {
             for (MiniServer subClient : util.clients()) {
               synchronized (subClient) {
                 System.out.println(util.getDate() + subClient.getCId() + " disconnected.");
                 subClient.sendMessage("Server Closed");
                 subClient.close();
               }
             }
             util.clients().clear();
           } catch (ConcurrentModificationException e) {
             e.printStackTrace();
           }
         }
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
       Server.setLock(false);
       break;
     }
     try {
       Thread.sleep(10);
     } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
 }
Ejemplo n.º 2
0
  @Override
  protected void doExecute() throws MojoExecutionException, MojoFailureException {
    final Log log = getLog();
    final File deploymentFile = file();
    // The deployment must exist before we do anything
    if (!deploymentFile.exists()) {
      throw new MojoExecutionException(
          String.format(
              "The deployment '%s' could not be found.", deploymentFile.getAbsolutePath()));
    }
    // Validate the environment
    final Path jbossHome = extractIfRequired(deploymentFile.getParentFile().toPath());
    if (!Files.isDirectory(jbossHome)) {
      throw new MojoExecutionException(
          String.format("JBOSS_HOME '%s' is not a valid directory.", jbossHome));
    }
    final StandaloneCommandBuilder commandBuilder =
        StandaloneCommandBuilder.of(jbossHome)
            .setJavaHome(javaHome)
            .addModuleDirs(modulesPath.getModulePaths());

    // Set the JVM options
    if (javaOpts != null) {
      commandBuilder.setJavaOptions(javaOpts);
    } else if (jvmArgs != null) {
      commandBuilder.setJavaOptions(jvmArgs.split("\\s+"));
    }

    if (serverConfig != null) {
      commandBuilder.setServerConfiguration(serverConfig);
    }

    if (propertiesFile != null) {
      commandBuilder.setPropertiesFile(propertiesFile);
    }

    if (serverArgs != null) {
      commandBuilder.addServerArguments(serverArgs);
    }

    // Check for management overrides
    final ModelControllerClientConfiguration clientConfiguration = getClientConfiguration();
    final String host = clientConfiguration.getHost();
    final int port = clientConfiguration.getPort();
    if (host != null) {
      commandBuilder.setBindAddressHint("management", host);
    }
    if (port > 0) {
      commandBuilder.addServerArguments("-Djboss.management.http.port=" + port);
    }

    // Print some server information
    log.info(String.format("JAVA_HOME=%s", commandBuilder.getJavaHome()));
    log.info(String.format("JBOSS_HOME=%s%n", commandBuilder.getWildFlyHome()));
    Server server = null;
    try (final ManagementClient client = createClient()) {
      // Create the server
      server = Server.create(commandBuilder, client);
      // Start the server
      log.info("Server is starting up. Press CTRL + C to stop the server.");
      server.start(startupTimeout);
      // Deploy the application
      server.checkServerState();
      if (server.isRunning()) {
        log.info(String.format("Deploying application '%s'%n", deploymentFile.getName()));
        final Deployment deployment =
            StandaloneDeployment.create(client, deploymentFile, name, getType(), null, null);
        switch (executeDeployment(client, deployment, jbossHome)) {
          case REQUIRES_RESTART:
            {
              client.execute(ServerOperations.createOperation(ServerOperations.RELOAD));
              break;
            }
          case SUCCESS:
            break;
        }
      } else {
        throw new DeploymentFailureException("Cannot deploy to a server that is not running.");
      }
      while (server.isRunning()) {
        TimeUnit.SECONDS.sleep(1L);
      }
    } catch (Exception e) {
      throw new MojoExecutionException("The server failed to start", e);
    } finally {
      if (server != null) server.stop();
    }
  }