mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
improve RepositoryManager api
This commit is contained in:
@@ -10,8 +10,8 @@ package sonia.scm;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.repository.BasicRepositoryManager;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.RepositoryType;
|
||||
import sonia.scm.util.ServiceUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
@@ -20,7 +20,6 @@ import sonia.scm.util.Util;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -68,10 +67,7 @@ public class BasicContextProvider implements SCMContextProvider
|
||||
manager.close();
|
||||
}
|
||||
|
||||
for (RepositoryManager manager : repositoryManagerMap.values())
|
||||
{
|
||||
manager.close();
|
||||
}
|
||||
repositoryManager.close();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,7 +78,7 @@ public class BasicContextProvider implements SCMContextProvider
|
||||
public void init()
|
||||
{
|
||||
loadGroupManagers();
|
||||
loadRepositoryManagers();
|
||||
loadRepositoryManager();
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
@@ -117,26 +113,13 @@ public class BasicContextProvider implements SCMContextProvider
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public RepositoryManager getRepositoryManager(String type)
|
||||
public RepositoryManager getRepositoryManager()
|
||||
{
|
||||
return repositoryManagerMap.get(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<RepositoryType> getRepositoryTypes()
|
||||
{
|
||||
return repositoryTypes;
|
||||
return repositoryManager;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -194,20 +177,16 @@ public class BasicContextProvider implements SCMContextProvider
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
private void loadRepositoryManagers()
|
||||
private void loadRepositoryManager()
|
||||
{
|
||||
repositoryManagerMap = new HashMap<String, RepositoryManager>();
|
||||
repositoryTypes = new ArrayList<RepositoryType>();
|
||||
repositoryManager = ServiceUtil.getService(RepositoryManager.class);
|
||||
|
||||
List<RepositoryManager> repositoryManagers =
|
||||
ServiceUtil.getServices(RepositoryManager.class);
|
||||
|
||||
for (RepositoryManager manager : repositoryManagers)
|
||||
if (repositoryManager == null)
|
||||
{
|
||||
manager.init(this);
|
||||
repositoryManagerMap.put(manager.getType().getName(), manager);
|
||||
repositoryTypes.add(manager.getType());
|
||||
repositoryManager = new BasicRepositoryManager();
|
||||
}
|
||||
|
||||
repositoryManager.init(this);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
@@ -219,8 +198,5 @@ public class BasicContextProvider implements SCMContextProvider
|
||||
private Map<String, GroupManager> groupManagerMap;
|
||||
|
||||
/** Field description */
|
||||
private Map<String, RepositoryManager> repositoryManagerMap;
|
||||
|
||||
/** Field description */
|
||||
private List<RepositoryType> repositoryTypes;
|
||||
private RepositoryManager repositoryManager;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.Collection;
|
||||
* @param <T>
|
||||
* @param <E>
|
||||
*/
|
||||
public interface Manager<T, E extends Exception> extends Initable, Closeable
|
||||
public interface Handler<T, E extends Exception> extends Initable, Closeable
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -78,7 +78,7 @@ public interface Manager<T, E extends Exception> extends Initable, Closeable
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public T get(String name);
|
||||
public T get(String id);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
@@ -87,12 +87,4 @@ public interface Manager<T, E extends Exception> extends Initable, Closeable
|
||||
* @return
|
||||
*/
|
||||
public Collection<T> getAll();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isConfigured();
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import sonia.scm.repository.RepositoryType;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
@@ -57,16 +58,7 @@ public interface SCMContextProvider extends Closeable
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
public RepositoryManager getRepositoryManager(String type);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<RepositoryType> getRepositoryTypes();
|
||||
public RepositoryManager getRepositoryManager();
|
||||
}
|
||||
|
||||
@@ -9,13 +9,13 @@ package sonia.scm.group;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.Manager;
|
||||
import sonia.scm.Handler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public interface GroupManager extends Manager<Group, GroupException>
|
||||
public interface GroupManager extends Handler<Group, GroupException>
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.util.ServiceUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class BasicRepositoryManager extends AbstractRepositoryManager
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public BasicRepositoryManager()
|
||||
{
|
||||
handlerMap = new HashMap<String, RepositoryHandler>();
|
||||
types = new ArrayList<RepositoryType>();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
for (RepositoryHandler manager : handlerMap.values())
|
||||
{
|
||||
manager.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public void create(Repository repository)
|
||||
throws RepositoryException, IOException
|
||||
{
|
||||
getHandler(repository).create(repository);
|
||||
fireEvent(repository, RepositoryEvent.CREATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public void delete(Repository repository)
|
||||
throws RepositoryException, IOException
|
||||
{
|
||||
getHandler(repository).delete(repository);
|
||||
fireEvent(repository, RepositoryEvent.DELETE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
@Override
|
||||
public void init(SCMContextProvider context)
|
||||
{
|
||||
List<RepositoryHandler> handlerList =
|
||||
ServiceUtil.getServices(RepositoryHandler.class);
|
||||
|
||||
if (Util.isNotEmpty(handlerList))
|
||||
{
|
||||
for (RepositoryHandler handler : handlerList)
|
||||
{
|
||||
RepositoryType type = handler.getType();
|
||||
|
||||
types.add(type);
|
||||
handlerMap.put(type.getName(), handler);
|
||||
handler.init(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public void modify(Repository repository)
|
||||
throws RepositoryException, IOException
|
||||
{
|
||||
getHandler(repository).modify(repository);
|
||||
fireEvent(repository, RepositoryEvent.MODIFY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryException
|
||||
*/
|
||||
@Override
|
||||
public void refresh(Repository repository)
|
||||
throws RepositoryException, IOException
|
||||
{
|
||||
getHandler(repository).refresh(repository);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Repository get(String id)
|
||||
{
|
||||
Repository repository = null;
|
||||
|
||||
for (RepositoryHandler handler : handlerMap.values())
|
||||
{
|
||||
repository = handler.get(id);
|
||||
|
||||
if (repository != null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Collection<Repository> getAll()
|
||||
{
|
||||
Set<Repository> repositories = new HashSet<Repository>();
|
||||
|
||||
for (RepositoryHandler handler : handlerMap.values())
|
||||
{
|
||||
Collection<Repository> handlerRepositories = handler.getAll();
|
||||
|
||||
if (handlerRepositories != null)
|
||||
{
|
||||
repositories.addAll(handlerRepositories);
|
||||
}
|
||||
}
|
||||
|
||||
return repositories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Collection<RepositoryType> getTypes()
|
||||
{
|
||||
return types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws RepositoryHandlerNotFoundException
|
||||
*/
|
||||
private RepositoryHandler getHandler(Repository repository)
|
||||
throws RepositoryHandlerNotFoundException
|
||||
{
|
||||
String type = repository.getType();
|
||||
RepositoryHandler handler = handlerMap.get(type);
|
||||
|
||||
if (handler == null)
|
||||
{
|
||||
throw new RepositoryHandlerNotFoundException(
|
||||
"could not find handler for ".concat(type));
|
||||
}
|
||||
|
||||
return handler;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Map<String, RepositoryHandler> handlerMap;
|
||||
|
||||
/** Field description */
|
||||
private List<RepositoryType> types;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ import javax.xml.bind.annotation.XmlType;
|
||||
@XmlRootElement(name = "repositories")
|
||||
@XmlType(propOrder =
|
||||
{
|
||||
"type", "name", "contact", "description", "permissions"
|
||||
"id", "type", "name", "contact", "description", "permissions"
|
||||
})
|
||||
public class Repository implements Serializable
|
||||
{
|
||||
@@ -49,11 +49,14 @@ public class Repository implements Serializable
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
* @param type
|
||||
* @param name
|
||||
*/
|
||||
public Repository(String type, String name)
|
||||
public Repository(String id, String type, String name)
|
||||
{
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
}
|
||||
@@ -62,15 +65,18 @@ public class Repository implements Serializable
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
* @param type
|
||||
* @param name
|
||||
* @param contact
|
||||
* @param description
|
||||
* @param permissions
|
||||
*/
|
||||
public Repository(String type, String name, String contact,
|
||||
public Repository(String id, String type, String name, String contact,
|
||||
String description, Permission... permissions)
|
||||
{
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.contact = contact;
|
||||
@@ -107,6 +113,17 @@ public class Repository implements Serializable
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -164,6 +181,17 @@ public class Repository implements Serializable
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public void setId(String id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -205,6 +233,9 @@ public class Repository implements Serializable
|
||||
/** Field description */
|
||||
private String description;
|
||||
|
||||
/** Field description */
|
||||
private String id;
|
||||
|
||||
/** Field description */
|
||||
private String name;
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.Handler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public interface RepositoryHandler
|
||||
extends Handler<Repository, RepositoryException>
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isConfigured();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public RepositoryType getType();
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class RepositoryHandlerNotFoundException extends RepositoryException
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final long serialVersionUID = 5270463060802850944L;
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public RepositoryHandlerNotFoundException() {}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
public RepositoryHandlerNotFoundException(String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -9,15 +9,20 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.Handler;
|
||||
import sonia.scm.ListenerSupport;
|
||||
import sonia.scm.Manager;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public interface RepositoryManager
|
||||
extends Manager<Repository, RepositoryException>, ListenerSupport<RepositoryListener>
|
||||
extends Handler<Repository, RepositoryException>,
|
||||
ListenerSupport<RepositoryListener>
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -26,5 +31,5 @@ public interface RepositoryManager
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public RepositoryType getType();
|
||||
public Collection<RepositoryType> getTypes();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user