/**
  * Sets the attribute value in the underlying {@link IServer}
  *
  * @param attributeName
  * @param attributeValue
  * @throws CoreException
  */
 private void configureServerAttribute(final String attributeName, boolean attributeValue)
     throws CoreException {
   final IServerWorkingCopy workingCopy = getServer().createWorkingCopy();
   workingCopy.setAttribute(attributeName, attributeValue);
   // setServerRestartState(true);
   workingCopy.save(true, new NullProgressMonitor());
 }
    @Override
    public Change perform(IProgressMonitor progressMonitor) throws CoreException {
      IServerWorkingCopy wc = null;

      wc = server.createWorkingCopy();
      if (wc != null) {
        wc.saveAll(true, null);
      }
      return null;
    }
 @Test
 public void testMetadata() {
   try {
     IServer s = ServerCreationTestUtils.createMockServerWithRuntime(serverType, serverType);
     IServerWorkingCopy wc = s.createWorkingCopy();
     wc.setAttribute(IDeployableServer.DEPLOY_DIRECTORY_TYPE, IDeployableServer.DEPLOY_METADATA);
     s = wc.save(true, null);
     String[] folders = getAdditions().getDeployLocationFolders(s);
     assertEquals(1, folders.length);
     assertTrue(folders[0].contains("metadata"));
   } catch (CoreException ce) {
     fail("Unable to save changes to server");
   }
 }
  @Test
  public void testMetadataWithCustomModule() {
    try {
      // Create the server
      IServer s = ServerCreationTestUtils.createMockServerWithRuntime(serverType, serverType);
      IServerWorkingCopy wc = s.createWorkingCopy();
      wc.setAttribute(IDeployableServer.DEPLOY_DIRECTORY_TYPE, IDeployableServer.DEPLOY_METADATA);
      s = wc.save(true, null);

      // Create the project
      projNum++;
      String projName = "Project" + projNum;
      IDataModel dm =
          CreateProjectOperationsUtility.getWebDataModel(
              projName, null, null, null, null, JavaEEFacetConstants.WEB_24, false);
      try {
        OperationTestCase.runAndVerify(dm);
      } catch (Exception e) {
        fail("Unable to create test web project: " + e.getMessage());
      }
      IProject p = ResourceUtils.findProject(projName);
      assertTrue(p.exists());
      IModule projMod = ServerUtil.getModule(p);

      // Set the custom setting. For now, just override output name
      wc = s.createWorkingCopy();
      AbstractPublishingTest.setCustomDeployOverride(wc, projMod, "newName.war", null, null);
      s = wc.save(true, null);

      // verify no change vs standard metadata
      String[] folders = getAdditions().getDeployLocationFolders(s);
      assertEquals(1, folders.length);
      assertTrue(folders[0].contains("metadata"));

      // Change it to now make some other output folder
      wc = s.createWorkingCopy();
      AbstractPublishingTest.setCustomDeployOverride(
          wc, projMod, "newName.war", "/home/user/deploy", null);
      s = wc.save(true, null);
      folders = getAdditions().getDeployLocationFolders(s);
      assertEquals(2, folders.length);
      assertTrue(folders[0].contains("metadata"));
      assertTrue(folders[1].equals("/home/user/deploy"));

    } catch (CoreException ce) {
      fail("Unable to save changes to server");
    }
  }
 protected RemoteServer getRemoteServer() {
   if (serverWC != null) {
     return (RemoteServer) serverWC.loadAdapter(RemoteServer.class, null);
   } else {
     return null;
   }
 }
  protected IStatus validateServer(IProgressMonitor monitor) {
    String host = serverWC.getHost();

    if (CoreUtil.isNullOrEmpty(host)) {
      return LiferayServerUI.createErrorStatus(Msgs.specifyHostname);
    }

    String username = remoteServerWC.getUsername();

    if (CoreUtil.isNullOrEmpty(username)) {
      return LiferayServerUI.createErrorStatus(Msgs.specifyUsernamePassword);
    }

    String port = remoteServerWC.getHTTPPort();

    if (CoreUtil.isNullOrEmpty(port)) {
      return LiferayServerUI.createErrorStatus(Msgs.specifyHTTPPort);
    }

    IStatus status = remoteServerWC.validate(monitor);

    if (status != null && status.getSeverity() == IStatus.ERROR) {
      fragment.lastServerStatus =
          new Status(
              IStatus.WARNING, status.getPlugin(), status.getMessage(), status.getException());
    } else {
      fragment.lastServerStatus = status;
    }

    return status;
  }
 public void undo() {
   if (listener != null) combo.removeModifyListener(listener);
   wc.setAttribute(key, oldVal);
   if (combo != null && !combo.isDisposed()) combo.setText(oldVal);
   if (listener != null) combo.addModifyListener(listener);
   postOp(POST_UNDO);
 }
 public IStatus redo() {
   if (listener != null) combo.removeModifyListener(listener);
   wc.setAttribute(key, newVal);
   if (combo != null && !combo.isDisposed()) combo.setText(newVal);
   if (listener != null) combo.addModifyListener(listener);
   postOp(POST_REDO);
   return Status.OK_STATUS;
 }
  public void setServer(IServerWorkingCopy newServer) {
    if (newServer == null) {
      serverWC = null;
      remoteServerWC = null;
    } else {
      serverWC = newServer;
      remoteServerWC =
          (IRemoteServerWorkingCopy) serverWC.loadAdapter(IRemoteServerWorkingCopy.class, null);

      serverWC.addPropertyChangeListener(this);
    }

    disableValidation = true;
    initialize();
    disableValidation = false;
    validate();
  }
 public static void savePreferencesToServerWorkingCopy(
     IServerWorkingCopy wc, DeploymentPreferences prefs) {
   try {
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     DeploymentPreferencesLoader.savePreferences(bos, prefs);
     String asXML = new String(bos.toByteArray());
     wc.setAttribute(DeploymentPreferencesLoader.DEPLOYMENT_PREFERENCES_KEY, asXML);
   } catch (IOException ioe) {
     // Should never happen since this is a simple byte array output stream
     JBossServerCorePlugin.log(
         new Status(
             IStatus.ERROR,
             JBossServerCorePlugin.PLUGIN_ID,
             "Could not save module deployment preferences to server "
                 + wc.getOriginal().getName(),
             ioe)); //$NON-NLS-1$
   }
 }
 /**
  * Checks if this started server has already a configured proxy and returns the associated port if
  * it is still free, or associate a new port if the previous port is not avaiable anymore, or
  * create a new port if none existed before.
  *
  * @param startedServer
  * @return the proxy port
  * @throws CoreException
  */
 private int getProxyPort(final IServer startedServer) throws CoreException {
   final IServer liveReloadServer = getServer();
   @SuppressWarnings("unchecked")
   final Map<String, Integer> proxyPorts =
       liveReloadServer.getAttribute(PROXY_PORTS, new HashMap<String, Integer>());
   final String startedServerId = startedServer.getId();
   if (proxyPorts.containsKey(startedServerId)) {
     final int port = proxyPorts.get(startedServerId);
     if (!SocketUtil.isPortInUse(port)) {
       return port;
     }
   }
   final IServerWorkingCopy swc = getServer().createWorkingCopy();
   final int port = SocketUtil.findUnusedPort(50000, 60000);
   proxyPorts.put(startedServerId, port);
   swc.setAttribute(PROXY_PORTS, proxyPorts);
   swc.saveAll(true, null);
   return port;
 }
  @Test
  public void testRemoteGetsNoAdditionsInvalidMode() {
    // This test ensures that 'remote' servers are not handled by the standard listeners
    IServer s = ServerCreationTestUtils.createMockServerWithRuntime(serverType, serverType);
    try {
      IServerWorkingCopy wc = s.createWorkingCopy();
      wc.setAttribute(IDeployableServer.SERVER_MODE, "mock5unknown");
      s = wc.save(true, null);
    } catch (CoreException ce) {
      fail("Could not set server mode to non-local");
    }
    boolean accepts = new LocalJBoss7DeploymentScannerAdditions().accepts(s);
    assertFalse(accepts);
    accepts = new JMXServerDeploymentScannerAdditions().accepts(s);
    assertFalse(accepts);

    Job j1 = new LocalJBoss7DeploymentScannerAdditions().getUpdateDeploymentScannerJob(s);
    assertNull(j1);
    Job j2 = new JMXServerDeploymentScannerAdditions().getUpdateDeploymentScannerJob(s);
    assertNull(j2);
  }
  public void propertyChange(PropertyChangeEvent evt) {
    if (IRemoteServer.ATTR_HOSTNAME.equals(evt.getPropertyName())
        || IRemoteServer.ATTR_HTTP_PORT.equals(evt.getPropertyName())
        || IRemoteServer.ATTR_USERNAME.equals(evt.getPropertyName())
        || IRemoteServer.ATTR_PASSWORD.equals(evt.getPropertyName())
        || IRemoteServer.ATTR_LIFERAY_PORTAL_CONTEXT_PATH.equals(evt.getPropertyName())
        || IRemoteServer.ATTR_SERVER_MANAGER_CONTEXT_PATH.equals(evt.getPropertyName())) {

      LiferayServerCore.updateConnectionSettings(
          (IRemoteServer) serverWC.loadAdapter(IRemoteServer.class, null));
    }
  }
  @Test
  public void testNoAdditionsRemovalsFromSettings() {
    // This test ensures that 'remote' servers are not handled by the standard listeners
    IServer s = ServerCreationTestUtils.createMockServerWithRuntime(serverType, serverType);
    try {
      IServerWorkingCopy wc = s.createWorkingCopy();
      wc.setAttribute(IJBossToolingConstants.PROPERTY_ADD_DEPLOYMENT_SCANNERS, false);
      wc.setAttribute(IJBossToolingConstants.PROPERTY_REMOVE_DEPLOYMENT_SCANNERS, false);
      s = wc.save(true, null);
    } catch (CoreException ce) {
      fail("Could not set server mode to non-local");
    }

    // Accepts will also check if the proper server type is here
    ServerExtendedProperties props =
        (ServerExtendedProperties) s.loadAdapter(ServerExtendedProperties.class, null);
    boolean usesManagement =
        props != null
            && props.getMultipleDeployFolderSupport()
                == ServerExtendedProperties.DEPLOYMENT_SCANNER_AS7_MANAGEMENT_SUPPORT;
    boolean usesJMX =
        props != null
            && props.getMultipleDeployFolderSupport()
                == ServerExtendedProperties.DEPLOYMENT_SCANNER_JMX_SUPPORT;

    boolean accepts = new LocalJBoss7DeploymentScannerAdditions().accepts(s);
    assertEquals(accepts, usesManagement);
    accepts = new JMXServerDeploymentScannerAdditions().accepts(s);
    assertEquals(accepts, usesJMX);

    Job j1 = new LocalJBoss7DeploymentScannerAdditions().getUpdateDeploymentScannerJob(s);
    assertNull(j1);
    Job j2 = new JMXServerDeploymentScannerAdditions().getUpdateDeploymentScannerJob(s);
    assertNull(j2);
    Job j3 = new LocalJBoss7DeploymentScannerAdditions().getRemoveDeploymentScannerJob(s);
    assertNull(j3);
    Job j4 = new JMXServerDeploymentScannerAdditions().getRemoveDeploymentScannerJob(s);
    assertNull(j4);
  }
  private IStatus updateServerRefs(
      IModule[] modulesToRemove, IServer affectedServer, IProgressMonitor monitor)
      throws CoreException {
    if (modulesToRemove == null || modulesToRemove.length == 0) {
      return Status.OK_STATUS;
    }
    monitor.beginTask(MODS_FROM_SERVERS, modulesToRemove.length * 100);
    for (int j = 0; j < modulesToRemove.length; j++) {
      IServerWorkingCopy wc = null;
      try {
        wc = affectedServer.createWorkingCopy();
        List list = Arrays.asList(affectedServer.getModules());
        if (list.contains(modulesToRemove[j])) {
          ServerUtil.modifyModules(wc, null, new IModule[] {modulesToRemove[j]}, null);
        }
      } catch (CoreException ce) {
        // Add it to a multistatus list?
        throw ce;
        // J2EEPlugin.logError(ce);
      } finally {
        try {
          if (wc != null) {
            IServer newServer = wc.saveAll(true, null);
            int state = newServer.getServerState();
            if (state == IServer.STATE_STARTED) {
              newServer.publish(
                  IServer.PUBLISH_INCREMENTAL,
                  new NullProgressMonitor()); // TODO use child progress?
            }
          }

        } catch (CoreException ce) {
          throw ce;
        }
      }
    }
    return Status.OK_STATUS;
  }
 public ServerWorkingCopyPropertyComboCommand(
     IServerWorkingCopy wc,
     String commandName,
     Combo combo,
     String newVal,
     String attributeKey,
     ModifyListener listener) {
   super(wc, commandName);
   this.wc = wc;
   this.combo = combo;
   this.key = attributeKey;
   this.newVal = newVal;
   this.listener = listener;
   if (key != null) this.oldVal = wc.getAttribute(attributeKey, ""); // $NON-NLS-1$
 }
 public void execute() {
   wc.setAttribute(key, newVal);
   postOp(POST_EXECUTE);
 }