Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-1797: Add JPA based SnapshotDao implementation #1937

Merged
merged 1 commit into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ public interface Snapshot {
*/
String getType();

/**
* Snapshot namespace, which allows snapshot to be
* related to the certain workspace machine.
*/
String getNamespace();

/**
* Creation date of the snapshot
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ public interface SnapshotDto extends Snapshot, Hyperlinks {

SnapshotDto withId(String id);

void setNamespace(String owner);

SnapshotDto withNamespace(String namespace);

void setType(String type);

SnapshotDto withType(String type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public static SnapshotDto asDto(Snapshot snapshot) {
.withCreationDate(snapshot.getCreationDate())
.withDev(snapshot.isDev())
.withId(snapshot.getId())
.withNamespace(snapshot.getNamespace())
.withWorkspaceId(snapshot.getWorkspaceId())
.withLinks(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,12 @@ public MachineImpl getDevMachine(String workspaceId) throws NotFoundException, M
* @throws MachineException
* if other error occur
*/
public SnapshotImpl save(String machineId, String namespace, String description)
public SnapshotImpl save(String machineId, String description)
throws NotFoundException, MachineException {
final Instance machine = getInstance(machineId);
final SnapshotImpl snapshot = SnapshotImpl.builder()
.generateId()
.setType(machine.getConfig().getType())
.setNamespace(namespace)
.setWorkspaceId(machine.getWorkspaceId())
.setDescription(description)
.setDev(machine.getConfig().isDev())
Expand All @@ -521,8 +520,6 @@ public SnapshotImpl save(String machineId, String namespace, String description)
*
* @param machineId
* id of machine for saving
* @param namespace
* snapshot namespace (e.g. owner)
* @param description
* optional description that should help to understand purpose of new snapshot in future
* @return {@link SnapshotImpl} that will be stored in background
Expand All @@ -533,14 +530,13 @@ public SnapshotImpl save(String machineId, String namespace, String description)
* @throws MachineException
* if other error occur
*/
public SnapshotImpl saveSync(String machineId, String namespace, String description) throws MachineException,
SnapshotException,
NotFoundException {
public SnapshotImpl saveSync(String machineId, String description) throws MachineException,
SnapshotException,
NotFoundException {
final Instance machine = getInstance(machineId);
final SnapshotImpl snapshot = SnapshotImpl.builder()
.generateId()
.setType(machine.getConfig().getType())
.setNamespace(namespace)
.setWorkspaceId(machine.getWorkspaceId())
.setDescription(description)
.setDev(machine.getConfig().isDev())
Expand Down Expand Up @@ -569,16 +565,14 @@ public SnapshotImpl getSnapshot(String snapshotId) throws NotFoundException, Sna
/**
* Gets list of Snapshots by project.
*
* @param owner
* id of owner of machine
* @param workspaceId
* workspace binding
* @return list of Snapshots
* @throws SnapshotException
* if error occur
*/
public List<SnapshotImpl> getSnapshots(String owner, String workspaceId) throws SnapshotException {
return snapshotDao.findSnapshots(owner, workspaceId);
public List<SnapshotImpl> getSnapshots(String workspaceId) throws SnapshotException {
return snapshotDao.findSnapshots(workspaceId);
}

/**
Expand All @@ -601,17 +595,15 @@ public void removeSnapshot(String snapshotId) throws NotFoundException, Snapshot
}

/**
* Removes Snapshots by owner, workspace and project.
* Removes Snapshots workspace.
*
* @param owner
* owner of required snapshots
* @param workspaceId
* workspace binding
* @throws SnapshotException
* error occur
*/
public void removeSnapshots(String owner, String workspaceId) throws SnapshotException {
for (SnapshotImpl snapshot : snapshotDao.findSnapshots(owner, workspaceId)) {
public void removeSnapshots(String workspaceId) throws SnapshotException {
for (SnapshotImpl snapshot : snapshotDao.findSnapshots(workspaceId)) {
try {
removeSnapshot(snapshot.getId());
} catch (NotFoundException ignored) {
Expand Down Expand Up @@ -824,7 +816,7 @@ private SnapshotImpl doSaveMachine(SnapshotImpl snapshot, Instance machine) thro
machine.getId());

snapshotWithKey = new SnapshotImpl(snapshot);
snapshotWithKey.setMachineSourceImpl(machine.saveToSnapshot(machine.getOwner()));
snapshotWithKey.setMachineSource(new MachineSourceImpl(machine.saveToSnapshot(machine.getOwner())));

try {
SnapshotImpl oldSnapshot = snapshotDao.getSnapshot(snapshot.getWorkspaceId(),
Expand All @@ -833,7 +825,7 @@ private SnapshotImpl doSaveMachine(SnapshotImpl snapshot, Instance machine) thro
snapshotDao.removeSnapshot(oldSnapshot.getId());
machineInstanceProviders.getProvider(oldSnapshot.getType()).removeInstanceSnapshot(oldSnapshot.getMachineSource());
} catch (NotFoundException ignored) {
//DO nothing if we has no snapshots or when provider not found
//DO nothing if we has no snapshots or when provider not found
} catch (SnapshotException se) {
LOG.error("Failed to delete snapshot: {}, because {}",
snapshot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.eclipse.che.api.machine.shared.dto.MachineProcessDto;
import org.eclipse.che.api.machine.shared.dto.NewSnapshotDescriptor;
import org.eclipse.che.api.machine.shared.dto.SnapshotDto;
import org.eclipse.che.commons.env.EnvironmentContext;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -146,7 +145,7 @@ public List<SnapshotDto> getSnapshots(@ApiParam(value = "Workspace ID", required

requiredNotNull(workspaceId, "Parameter workspace");

final List<SnapshotImpl> snapshots = machineManager.getSnapshots(EnvironmentContext.getCurrent().getSubject().getUserId(), workspaceId);
final List<SnapshotImpl> snapshots = machineManager.getSnapshots(workspaceId);

return snapshots.stream()
.map(DtoConverter::asDto)
Expand Down Expand Up @@ -175,9 +174,6 @@ public SnapshotDto saveSnapshot(@ApiParam(value = "Machine ID")

requiredNotNull(newSnapshotDescriptor, "Snapshot description");
return linksInjector.injectLinks(DtoConverter.asDto(machineManager.save(machineId,
EnvironmentContext.getCurrent()
.getSubject()
.getUserId(),
newSnapshotDescriptor.getDescription())),
getServiceContext());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*******************************************************************************
* Copyright (c) 2012-2016 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.machine.server.jpa;

import com.google.inject.persist.Transactional;

import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.jdbc.jpa.DuplicateKeyException;
import org.eclipse.che.api.machine.server.exception.SnapshotException;
import org.eclipse.che.api.machine.server.model.impl.SnapshotImpl;
import org.eclipse.che.api.machine.server.spi.SnapshotDao;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import java.util.List;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

/**
* JPA based {@link SnapshotDao} implementation.
*
* @author Yevhenii Voevodin
*/
@Singleton
public class JpaSnapshotDao implements SnapshotDao {

@Inject
private Provider<EntityManager> managerProvider;

@Override
@Transactional
public SnapshotImpl getSnapshot(String snapshotId) throws NotFoundException, SnapshotException {
requireNonNull(snapshotId, "Required non-null snapshotId");
try {
final SnapshotImpl snapshot = managerProvider.get().find(SnapshotImpl.class, snapshotId);
if (snapshot == null) {
throw new NotFoundException(format("Snapshot with id '%s' doesn't exist", snapshotId));
}
return snapshot;
} catch (RuntimeException x) {
throw new SnapshotException(x.getLocalizedMessage(), x);
}
}

@Override
@Transactional
public SnapshotImpl getSnapshot(String workspaceId, String envName, String machineName) throws NotFoundException, SnapshotException {
requireNonNull(workspaceId, "Required non-null workspace id");
requireNonNull(envName, "Required non-null environment name");
requireNonNull(machineName, "Required non-null machine name");
try {
return managerProvider.get()
.createNamedQuery("Snapshot.getByMachine", SnapshotImpl.class)
.setParameter("workspaceId", workspaceId)
.setParameter("envName", envName)
.setParameter("machineName", machineName)
.getSingleResult();
} catch (NoResultException x) {
throw new NotFoundException(format("Snapshot for machine '%s:%s:%s' doesn't exist",
workspaceId,
envName,
machineName));
} catch (RuntimeException x) {
throw new SnapshotException(x.getLocalizedMessage(), x);
}
}

@Override
@Transactional
public List<SnapshotImpl> findSnapshots(String workspaceId) throws SnapshotException {
requireNonNull(workspaceId, "Required non-null workspace id");
try {
return managerProvider.get()
.createNamedQuery("Snapshot.findSnapshots", SnapshotImpl.class)
.setParameter("workspaceId", workspaceId)
.getResultList();
} catch (RuntimeException x) {
throw new SnapshotException(x.getLocalizedMessage(), x);
}
}

@Override
public void saveSnapshot(SnapshotImpl snapshot) throws SnapshotException {
requireNonNull(snapshot, "Required non-null snapshot");
try {
doSave(snapshot);
} catch (DuplicateKeyException x) {
throw new SnapshotException(format("Snapshot with id '%s' or for machine '%s:%s:%s' already exists",
snapshot.getId(),
snapshot.getWorkspaceId(),
snapshot.getEnvName(),
snapshot.getMachineName()));
} catch (RuntimeException x) {
throw new SnapshotException(x.getLocalizedMessage(), x);
}
}

@Override
public void removeSnapshot(String snapshotId) throws NotFoundException, SnapshotException {
requireNonNull(snapshotId, "Required non-null snapshot id");
try {
doRemove(snapshotId);
} catch (RuntimeException x) {
throw new SnapshotException(x.getLocalizedMessage(), x);
}
}

@Transactional
protected void doSave(SnapshotImpl snapshot) {
managerProvider.get().persist(snapshot);
}

@Transactional
protected void doRemove(String snapshotId) throws NotFoundException {
final EntityManager manager = managerProvider.get();
final SnapshotImpl snapshot = manager.find(SnapshotImpl.class, snapshotId);
if (snapshot == null) {
throw new NotFoundException(format("Snapshot with id '%s' doesn't exist", snapshotId));
}
manager.remove(snapshot);
}
}
Loading