Skip to content

Commit

Permalink
FORGE-801: Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Feb 26, 2013
1 parent c5f47dd commit c7922ff
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.resource.transaction;

import java.util.Set;

import org.jboss.forge.resource.Resource;

/**
* A {@link ChangeSet} object contains the modified resources of a specific transaction
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
public interface ChangeSet
{
/**
* @return a set of the modified resources
*/
Set<Resource<?>> getModifiedResources();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.resource.transaction;

import org.jboss.forge.resource.ResourceException;

/**
* The ResourceTransaction interface allows operations to be performed on transactions
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
public interface ResourceTransaction
{
/**
* Attempt to commit this transaction, discarding the current {@link ChangeSet}.
*
* @throws ResourceException if any operation fails
*/
void commit() throws ResourceException;

/**
* Rolls back this transaction. This transaction should not be used again
*
* This also discards the current {@link ChangeSet}
*
* @throws ResourceException
*/
void rollback() throws ResourceException;

/**
* @return A {@link ChangeSet} object containing which resources were modified during this transaction
*/
ChangeSet getChangeSet();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.resource.transaction;

import org.jboss.forge.container.services.Exported;
import org.jboss.forge.resource.ResourceException;

/**
* The ResourceTransactionManager interface allows
*
* <ul>
* <li>Starting a {@link ResourceTransaction}</li>
* <li>Getting the current {@link ResourceTransaction}</li>
* </ul>
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
@Exported
public interface ResourceTransactionManager
{
/**
* Start a new transaction.
*
* If a transaction was already started, a {@link ResourceException} will be thrown
*
* @return a new {@link ResourceTransaction} instance
* @throws ResourceException if a transaction was already started
*/
ResourceTransaction startTransaction() throws ResourceException;

/**
* @return The current transaction or null if {@link ResourceTransactionManager#startTransaction()} was not
* previously called
*/
ResourceTransaction getCurrentTransaction();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.resource;

import java.util.Set;
import java.util.TreeSet;

import org.jboss.forge.resource.transaction.ChangeSet;

public class ChangeSetImpl implements ChangeSet
{
private Set<Resource<?>> changeSet = new TreeSet<Resource<?>>();

public ChangeSetImpl()
{
}

public void addResource(Resource<?> resource)
{
changeSet.add(resource);
}

public void removeResource(Resource<?> resource)
{
changeSet.remove(resource);
}

@Override
public Set<Resource<?>> getModifiedResources()
{
return changeSet;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@
import javax.inject.Singleton;

import org.jboss.forge.container.AddonRegistry;
import org.jboss.forge.container.services.Exported;
import org.jboss.forge.container.services.ExportedInstance;
import org.jboss.forge.resource.events.ResourceEvent;
import org.jboss.forge.resource.transaction.ResourceTransaction;
import org.jboss.forge.resource.transaction.ResourceTransactionManager;

/**
* @author <a href="http://community.jboss.org/people/kenfinni">Ken Finnigan</a>
* @author Mike Brock <cbrock@redhat.com>
*/
@Exported
@Singleton
public class ResourceFactoryImpl implements ResourceFactory
public class ResourceFactoryImpl implements ResourceFactory, ResourceTransactionManager
{
@Inject
private BeanManager manager;

@Inject
private AddonRegistry registry;

private ResourceTransactionImpl transaction;

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <E, T extends Resource<E>> T create(final Class<T> type, final E underlyingResource)
Expand Down Expand Up @@ -79,7 +81,8 @@ public <E> Resource<E> create(E underlyingResource)
ResourceGenerator generator = instance.get();
if (generator.handles(Resource.class, underlyingResource))
{
generated.add(generator.getResource(this, Resource.class, underlyingResource));
Resource resource = generator.getResource(this, Resource.class, underlyingResource);
generated.add(resource);
}
instance.release(generator);
}
Expand All @@ -90,9 +93,17 @@ public <E> Resource<E> create(E underlyingResource)
for (Resource<?> resource : generated)
{
if (result == null || result.getClass().isAssignableFrom(resource.getClass()))
{
result = (Resource<E>) resource;
}
}
}
// Transaction Hook
if (result != null && transaction != null)
{
result = transaction.decorateResource(result);
}

return result;
}

Expand All @@ -109,4 +120,26 @@ public ResourceFactory fireEvent(ResourceEvent event)
return this;
}

@Override
public ResourceTransaction getCurrentTransaction()
{
return transaction;
}

@Override
public ResourceTransaction startTransaction() throws ResourceException
{
if (transaction != null)
{
throw new ResourceException("Transaction already exists!");
}
transaction = new ResourceTransactionImpl(this);
return transaction;
}

void unsetTransaction()
{
transaction = null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.resource;

import java.io.File;
import java.io.IOException;

import org.jboss.forge.resource.transaction.ChangeSet;
import org.jboss.forge.resource.transaction.ResourceTransaction;

public class ResourceTransactionImpl implements ResourceTransaction
{
private File tempWorkspace;

private ChangeSetImpl changeSet = new ChangeSetImpl();
private ResourceFactoryImpl factoryImpl;

public ResourceTransactionImpl(ResourceFactoryImpl factoryImpl) throws ResourceException
{
try
{
tempWorkspace = File.createTempFile("Forge_RT", null);
}
catch (IOException io)
{
throw new ResourceException(io);
}
tempWorkspace.delete();
tempWorkspace.mkdir();
this.factoryImpl = factoryImpl;
}

@Override
public void commit() throws ResourceException
{
factoryImpl.unsetTransaction();
}

@Override
public void rollback() throws ResourceException
{
factoryImpl.unsetTransaction();
}

/**
* Decorates a Resource
*
* @param resource
* @return
*/
public <TYPE> Resource<TYPE> decorateResource(Resource<TYPE> resource)
{
changeSet.addResource(resource);
// TODO: Create a proxy to the FileResource
return resource;
}

@Override
public ChangeSet getChangeSet()
{
return changeSet;
}

}
Loading

0 comments on commit c7922ff

Please sign in to comment.