Skip to content

Commit

Permalink
IDEX-2533: added commands api
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene committed Jun 3, 2015
1 parent 8db0317 commit 7ef8e6a
Show file tree
Hide file tree
Showing 13 changed files with 1,305 additions and 15 deletions.
@@ -0,0 +1,154 @@
/*******************************************************************************
* Copyright (c) 2012-2015 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.api.local;

import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;

import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.machine.server.command.CommandImpl;
import org.eclipse.che.api.machine.server.dao.CommandDao;
import org.eclipse.che.api.machine.shared.ManagedCommand;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static java.lang.String.format;
import static org.eclipse.che.commons.lang.Strings.isNullOrEmpty;

/**
* In memory implementation of {@link CommandDao}
*
* @author Eugene Voevodin
*/
@Singleton
public class LocalCommandDaoImpl implements CommandDao {

private final Map<String, ManagedCommand> commands;
private final ReadWriteLock lock;

@Inject
public LocalCommandDaoImpl(@Named("codenvy.local.infrastructure.commands") Set<ManagedCommand> commands) {
this.commands = new HashMap<>();
lock = new ReentrantReadWriteLock();
try {
for (ManagedCommand command : commands) {
create(command);
}
} catch (Exception ex) {
// fail if can't validate this instance properly
throw new RuntimeException(ex);
}
}

@Override
public void create(ManagedCommand command) throws ConflictException, ServerException {
lock.writeLock().lock();
try {
if (commands.containsKey(command.getId())) {
throw new ConflictException("Command with id " + command.getId() + " already exists");

}
commands.put(command.getId(), command);
} finally {
lock.writeLock().unlock();
}
}

@Override
public void update(ManagedCommand update) throws NotFoundException, ServerException, ConflictException {
lock.writeLock().lock();
try {
final CommandImpl target = (CommandImpl)commands.get(update.getId());
if (target == null) {
throw new NotFoundException("Command " + update.getId() + " was not found");
}
for (ManagedCommand command : commands.values()) {
if (!command.equals(target) &&
command.getName().equals(update.getName()) &&
command.getWorkspaceId().equals(update.getWorkspaceId()) &&
command.getCreator().equals(update.getCreator())) {
throw new ConflictException(format("Command with name '%s' in workspace '%s' for user '%s' already exists",
update.getName(),
update.getWorkspaceId(),
update.getCreator()));
}
}
if (!isNullOrEmpty(update.getName())) {
target.setName(update.getName());
}
if (!isNullOrEmpty(update.getCommandLine())) {
target.setCommandLine(update.getCommandLine());
}
if (!isNullOrEmpty(update.getVisibility())) {
target.setVisibility(update.getVisibility());
}
if (update.getWorkingDir() != null) {
target.setWorkingDir(update.getWorkingDir());
}
} finally {
lock.writeLock().unlock();
}
}

@Override
public void remove(String id) throws ServerException {
lock.writeLock().lock();
try {
commands.remove(id);
} finally {
lock.writeLock().unlock();
}
}

@Override
public ManagedCommand getCommand(String id) throws NotFoundException, ServerException {
lock.readLock().lock();
try {
final ManagedCommand command = commands.get(id);
if (command == null) {
throw new NotFoundException("Command " + id + " was not found");
}
return command;
} finally {
lock.readLock().unlock();
}
}

@Override
public List<ManagedCommand> getCommands(final String workspaceId,
final String creator,
int skipCount,
int maxItems) throws ServerException {
lock.readLock().lock();
try {
return FluentIterable.from(commands.values())
.filter(new Predicate<ManagedCommand>() {
@Override
public boolean apply(ManagedCommand command) {
return command.getCreator().equals(creator) || command.getWorkspaceId().equals(workspaceId);
}
})
.toList();
} finally {
lock.readLock().unlock();
}
}
}
Expand Up @@ -17,10 +17,13 @@
import org.eclipse.che.api.account.server.dao.AccountDao;
import org.eclipse.che.api.account.server.dao.Subscription;
import org.eclipse.che.api.auth.AuthenticationDao;
import org.eclipse.che.api.machine.server.command.CommandImpl;
import org.eclipse.che.api.machine.server.dao.CommandDao;
import org.eclipse.che.api.machine.server.recipe.GroupImpl;
import org.eclipse.che.api.machine.server.recipe.PermissionsImpl;
import org.eclipse.che.api.machine.server.recipe.RecipeImpl;
import org.eclipse.che.api.machine.server.dao.RecipeDao;
import org.eclipse.che.api.machine.shared.ManagedCommand;
import org.eclipse.che.api.machine.shared.recipe.Group;
import org.eclipse.che.api.machine.shared.recipe.Recipe;
import org.eclipse.che.api.user.server.TokenValidator;
Expand All @@ -40,6 +43,7 @@
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;

@DynaModule
public class LocalInfrastructureModule extends AbstractModule {
Expand All @@ -55,6 +59,7 @@ protected void configure() {
// bind(FactoryStore.class).to(InMemoryFactoryStore.class);
bind(TokenValidator.class).to(DummyTokenValidator.class);
bind(RecipeDao.class).to(LocalRecipeDaoImpl.class);
bind(CommandDao.class).to(LocalCommandDaoImpl.class);
}


Expand Down Expand Up @@ -150,6 +155,26 @@ Set<Recipe> recipes() {
.withTags(asList("java", "busybox"))
.withPermissions(new PermissionsImpl(null, asList(group)));

return Collections.unmodifiableSet(new HashSet<>(asList(recipe1, recipe2)));
return unmodifiableSet(new HashSet<>(asList(recipe1, recipe2)));
}

@Provides
@Named("codenvy.local.infrastructure.commands")
Set<ManagedCommand> commands() {
final ManagedCommand command1 = new CommandImpl().withId("command123")
.withName("mci")
.withCreator("codenvy")
.withWorkspaceId("workspace123")
.withCommandLine("mvn clean install")
.withVisibility("private")
.withType("maven");
final ManagedCommand command2 = new CommandImpl().withId("command234")
.withName("ab")
.withCreator("codenvy")
.withWorkspaceId("workspace123")
.withCommandLine("ant build")
.withVisibility("public")
.withType("ant");
return unmodifiableSet(new HashSet<>(asList(command1, command2)));
}
}
Expand Up @@ -21,6 +21,12 @@ public class Constants {
public static final String LINK_REL_GET_RECIPES_BY_CREATOR = "get created recipes";
public static final String LINK_REL_SEARCH_RECIPES = "search recipes";
public static final String LINK_REL_UPDATE_RECIPE = "update recipe";
public static final String LINK_REL_CREATE_COMMAND = "create command";
public static final String LINK_REL_REMOVE_COMMAND = "remove command";
public static final String LINK_REL_GET_COMMAND = "get command";
public static final String LINK_REL_GET_ALL_COMMANDS = "get workspace commands";
public static final String LINK_REL_UPDATE_COMMAND = "update command";

private Constants() { }
private Constants() {
}
}

0 comments on commit 7ef8e6a

Please sign in to comment.