move sources from scm-web-api and scm-cli to scm-core

This commit is contained in:
Sebastian Sdorra
2010-12-05 14:00:01 +01:00
parent bc3a6305c8
commit f29cff9a13
50 changed files with 53 additions and 85 deletions

View File

@@ -0,0 +1,267 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.repository.Repository;
import sonia.scm.xml.XmlTimestampDateAdapter;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
/**
*
* @author Sebastian Sdorra
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlRepositoryDatabase
{
/**
* Method description
*
*
* @param repository
*/
public void add(Repository repository)
{
repositoryMap.put(createKey(repository), repository);
}
/**
* Method description
*
*
*
* @param type
* @param name
*
* @return
*/
public boolean contains(String type, String name)
{
return repositoryMap.containsKey(createKey(type, name));
}
/**
* Method description
*
*
* @param id
*
* @return
*/
public boolean contains(String id)
{
return get(id) != null;
}
/**
* Method description
*
*
* @param repository
*
* @return
*/
public boolean contains(Repository repository)
{
return repositoryMap.containsKey(createKey(repository));
}
/**
* Method description
*
*
* @param repository
*/
public void remove(Repository repository)
{
repositoryMap.remove(createKey(repository));
}
/**
* Method description
*
*
* @return
*/
public Collection<Repository> values()
{
return repositoryMap.values();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param type
* @param name
*
* @return
*/
public Repository get(String type, String name)
{
return repositoryMap.get(createKey(type, name));
}
/**
* Method description
*
*
* @param id
*
* @return
*/
public Repository get(String id)
{
Repository repository = null;
for (Repository r : values())
{
if (r.getId().equals(id))
{
repository = r;
break;
}
}
return repository;
}
/**
* Method description
*
*
* @return
*/
public long getCreationTime()
{
return creationTime;
}
/**
* Method description
*
*
* @return
*/
public long getLastModified()
{
return lastModified;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param creationTime
*/
public void setCreationTime(long creationTime)
{
this.creationTime = creationTime;
}
/**
* Method description
*
*
* @param lastModified
*/
public void setLastModified(long lastModified)
{
this.lastModified = lastModified;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param type
* @param name
*
* @return
*/
static String createKey(String type, String name)
{
return type.concat(":").concat(name);
}
/**
* Method description
*
*
* @param repository
*
* @return
*/
static String createKey(Repository repository)
{
return createKey(repository.getType(), repository.getName());
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlJavaTypeAdapter(XmlTimestampDateAdapter.class)
private Long creationTime;
/** Field description */
@XmlJavaTypeAdapter(XmlTimestampDateAdapter.class)
private Long lastModified;
/** Field description */
@XmlJavaTypeAdapter(XmlRepositoryMapAdapter.class)
@XmlElement(name = "repositories")
private Map<String, Repository> repositoryMap = new LinkedHashMap<String,
Repository>();
}

View File

@@ -0,0 +1,123 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.repository.Repository;
//~--- JDK imports ------------------------------------------------------------
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Sebastian Sdorra
*/
@XmlRootElement(name = "repositories")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlRepositoryList implements Iterable<Repository>
{
/**
* Constructs ...
*
*/
public XmlRepositoryList() {}
/**
* Constructs ...
*
*
*
* @param repositoryMap
*/
public XmlRepositoryList(Map<String, Repository> repositoryMap)
{
this.repositories = new LinkedList<Repository>(repositoryMap.values());
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public Iterator<Repository> iterator()
{
return repositories.iterator();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public LinkedList<Repository> getRepositories()
{
return repositories;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param repositories
*/
public void setRepositories(LinkedList<Repository> repositories)
{
this.repositories = repositories;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlElement(name = "repository")
private LinkedList<Repository> repositories;
}

View File

@@ -0,0 +1,595 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.xml;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.ConfigurationException;
import sonia.scm.HandlerEvent;
import sonia.scm.SCMContext;
import sonia.scm.SCMContextProvider;
import sonia.scm.Type;
import sonia.scm.repository.AbstractRepositoryManager;
import sonia.scm.repository.PermissionType;
import sonia.scm.repository.PermissionUtil;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryAllreadyExistExeption;
import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.RepositoryHandler;
import sonia.scm.repository.RepositoryHandlerNotFoundException;
import sonia.scm.security.SecurityContext;
import sonia.scm.user.User;
import sonia.scm.util.AssertUtil;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.xml.bind.JAXB;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class XmlRepositoryManager extends AbstractRepositoryManager
{
/** Field description */
public static final String DATABASEFILE =
"config".concat(File.separator).concat("repositories.xml");
/** Field description */
private static final Logger logger =
LoggerFactory.getLogger(XmlRepositoryManager.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
*
* @param securityContextProvider
* @param handlerSet
*/
@Inject
public XmlRepositoryManager(
Provider<SecurityContext> securityContextProvider,
Set<RepositoryHandler> handlerSet)
{
this.securityContextProvider = securityContextProvider;
handlerMap = new HashMap<String, RepositoryHandler>();
types = new HashSet<Type>();
for (RepositoryHandler handler : handlerSet)
{
addHandler(handler);
}
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws IOException
*/
@Override
public void close() throws IOException
{
for (RepositoryHandler handler : handlerMap.values())
{
IOUtil.close(handler);
}
}
/**
* Method description
*
*
* @param repository
*
* @throws IOException
* @throws RepositoryException
*/
@Override
public void create(Repository repository)
throws RepositoryException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("create repository {} of type {}", repository.getName(),
repository.getType());
}
assertIsAdmin();
AssertUtil.assertIsValid(repository);
if (repositoryDB.contains(repository))
{
throw new RepositoryAllreadyExistExeption();
}
repository.setId(UUID.randomUUID().toString());
repository.setCreationDate(System.currentTimeMillis());
getHandler(repository).create(repository);
synchronized (XmlRepositoryDatabase.class)
{
repositoryDB.add(repository.clone());
storeDB();
}
fireEvent(repository, HandlerEvent.CREATE);
}
/**
* Method description
*
*
* @param repository
*
* @throws IOException
* @throws RepositoryException
*/
@Override
public void delete(Repository repository)
throws RepositoryException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("delete repository {} of type {}", repository.getName(),
repository.getType());
}
assertIsOwner(repository);
if (repositoryDB.contains(repository))
{
getHandler(repository).delete(repository);
synchronized (XmlRepositoryDatabase.class)
{
repositoryDB.remove(repository);
storeDB();
}
}
else
{
throw new RepositoryException(
"repository ".concat(repository.getName()).concat(" not found"));
}
fireEvent(repository, HandlerEvent.DELETE);
}
/**
* Method description
*
*
* @param context
*/
@Override
public void init(SCMContextProvider context)
{
File directory = context.getBaseDirectory();
repositoryDBFile = new File(directory, DATABASEFILE);
if (repositoryDBFile.exists())
{
loadDB();
}
else
{
IOUtil.mkdirs(repositoryDBFile.getParentFile());
repositoryDB = new XmlRepositoryDatabase();
repositoryDB.setCreationTime(System.currentTimeMillis());
}
}
/**
* Method description
*
*
* @param repository
*
* @throws IOException
* @throws RepositoryException
*/
@Override
public void modify(Repository repository)
throws RepositoryException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("modify repository {} of type {}", repository.getName(),
repository.getType());
}
AssertUtil.assertIsValid(repository);
Repository notModifiedRepository = repositoryDB.get(repository.getType(),
repository.getName());
if (notModifiedRepository != null)
{
assertIsOwner(notModifiedRepository);
getHandler(repository).modify(repository);
repository.setLastModified(System.currentTimeMillis());
synchronized (XmlRepositoryDatabase.class)
{
repositoryDB.remove(repository);
repositoryDB.add(repository.clone());
storeDB();
}
}
else
{
throw new RepositoryException(
"repository ".concat(repository.getName()).concat(" not found"));
}
fireEvent(repository, HandlerEvent.MODIFY);
}
/**
* Method description
*
*
* @param repository
*
* @throws IOException
* @throws RepositoryException
*/
@Override
public void refresh(Repository repository)
throws RepositoryException, IOException
{
AssertUtil.assertIsNotNull(repository);
assertIsReader(repository);
Repository fresh = repositoryDB.get(repository.getType(),
repository.getName());
if (fresh != null)
{
fresh.copyProperties(repository);
}
else
{
throw new RepositoryException(
"repository ".concat(repository.getName()).concat(" not found"));
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param id
*
* @return
*/
@Override
public Repository get(String id)
{
AssertUtil.assertIsNotEmpty(id);
Repository repository = repositoryDB.get(id);
if (repository != null)
{
assertIsReader(repository);
repository = repository.clone();
}
return repository;
}
/**
* Method description
*
*
* @param type
* @param name
*
* @return
*/
@Override
public Repository get(String type, String name)
{
AssertUtil.assertIsNotEmpty(type);
AssertUtil.assertIsNotEmpty(name);
Repository repository = repositoryDB.get(type, name);
if (repository != null)
{
if (isReader(repository))
{
repository = repository.clone();
}
else
{
repository = null;
}
}
return repository;
}
/**
* Method description
*
*
* @return
*/
@Override
public Collection<Repository> getAll()
{
LinkedList<Repository> repositories = new LinkedList<Repository>();
for (Repository repository : repositoryDB.values())
{
if (isReader(repository))
{
repositories.add(repository.clone());
}
}
return repositories;
}
/**
* Method description
*
*
* @param type
*
* @return
*/
@Override
public RepositoryHandler getHandler(String type)
{
return handlerMap.get(type);
}
/**
* Method description
*
*
* @return
*/
@Override
public Collection<Type> getTypes()
{
return types;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param handler
*/
private void addHandler(RepositoryHandler handler)
{
AssertUtil.assertIsNotNull(handler);
Type type = handler.getType();
AssertUtil.assertIsNotNull(type);
if (handlerMap.containsKey(type.getName()))
{
throw new ConfigurationException(
type.getName().concat("allready registered"));
}
if (logger.isInfoEnabled())
{
logger.info("added RepositoryHandler {} for type {}", handler.getClass(),
type);
}
handlerMap.put(type.getName(), handler);
handler.init(SCMContext.getContext());
types.add(type);
}
/**
* Method description
*
*
* @throws RepositoryException
*/
private void assertIsAdmin() throws RepositoryException
{
if (!getCurrentUser().isAdmin())
{
throw new RepositoryException("admin permsission required");
}
}
/**
* Method description
*
*
* @param repository
*/
private void assertIsOwner(Repository repository)
{
PermissionUtil.assertPermission(repository, getCurrentUser(),
PermissionType.OWNER);
}
/**
* Method description
*
*
* @param repository
*/
private void assertIsReader(Repository repository)
{
PermissionUtil.assertPermission(repository, getCurrentUser(),
PermissionType.READ);
}
/**
* Method description
*
*/
private void loadDB()
{
repositoryDB = JAXB.unmarshal(repositoryDBFile,
XmlRepositoryDatabase.class);
}
/**
* Method description
*
*/
private void storeDB()
{
repositoryDB.setLastModified(System.currentTimeMillis());
JAXB.marshal(repositoryDB, repositoryDBFile);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
private User getCurrentUser()
{
SecurityContext context = securityContextProvider.get();
AssertUtil.assertIsNotNull(context);
User user = context.getUser();
AssertUtil.assertIsNotNull(user);
return user;
}
/**
* Method description
*
*
* @param repository
*
* @return
*
*
* @throws RepositoryException
*/
private RepositoryHandler getHandler(Repository repository)
throws RepositoryException
{
String type = repository.getType();
RepositoryHandler handler = handlerMap.get(type);
if (handler == null)
{
throw new RepositoryHandlerNotFoundException(
"could not find handler for ".concat(type));
}
else if (!handler.isConfigured())
{
throw new RepositoryException("handler is not configured");
}
return handler;
}
/**
* Method description
*
*
* @param repository
*
* @return
*/
private boolean isReader(Repository repository)
{
return PermissionUtil.hasPermission(repository, getCurrentUser(),
PermissionType.READ);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Map<String, RepositoryHandler> handlerMap;
/** Field description */
private XmlRepositoryDatabase repositoryDB;
/** Field description */
private File repositoryDBFile;
/** Field description */
private Provider<SecurityContext> securityContextProvider;
/** Field description */
private Set<Type> types;
}

View File

@@ -0,0 +1,97 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.repository.Repository;
//~--- JDK imports ------------------------------------------------------------
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
*
* @author Sebastian Sdorra
*/
public class XmlRepositoryMapAdapter
extends XmlAdapter<XmlRepositoryList, Map<String, Repository>>
{
/**
* Method description
*
*
* @param repositoryMap
*
* @return
*
* @throws Exception
*/
@Override
public XmlRepositoryList marshal(Map<String, Repository> repositoryMap)
throws Exception
{
return new XmlRepositoryList(repositoryMap);
}
/**
* Method description
*
*
* @param repositories
*
* @return
*
* @throws Exception
*/
@Override
public Map<String, Repository> unmarshal(XmlRepositoryList repositories)
throws Exception
{
Map<String, Repository> repositoryMap = new LinkedHashMap<String,
Repository>();
for (Repository repository : repositories)
{
repositoryMap.put(XmlRepositoryDatabase.createKey(repository),
repository);
}
return repositoryMap;
}
}

View File

@@ -0,0 +1,185 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.user.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.user.User;
import sonia.scm.xml.XmlTimestampDateAdapter;
//~--- JDK imports ------------------------------------------------------------
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
/**
*
* @author Sebastian Sdorra
*/
@XmlRootElement(name = "user-db")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlUserDatabase
{
/**
* Method description
*
*
* @param user
*/
public void add(User user)
{
userMap.put(user.getName(), user);
}
/**
* Method description
*
*
* @param username
*
* @return
*/
public boolean contains(String username)
{
return userMap.containsKey(username);
}
/**
* Method description
*
*
* @param username
*
* @return
*/
public User remove(String username)
{
return userMap.remove(username);
}
/**
* Method description
*
*
* @return
*/
public Collection<User> values()
{
return userMap.values();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param username
*
* @return
*/
public User get(String username)
{
return userMap.get(username);
}
/**
* Method description
*
*
* @return
*/
public long getCreationTime()
{
return creationTime;
}
/**
* Method description
*
*
* @return
*/
public long getLastModified()
{
return lastModified;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param creationTime
*/
public void setCreationTime(long creationTime)
{
this.creationTime = creationTime;
}
/**
* Method description
*
*
* @param lastModified
*/
public void setLastModified(long lastModified)
{
this.lastModified = lastModified;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlJavaTypeAdapter(XmlTimestampDateAdapter.class)
private Long creationTime;
/** Field description */
@XmlJavaTypeAdapter(XmlTimestampDateAdapter.class)
private Long lastModified;
/** Field description */
@XmlJavaTypeAdapter(XmlUserMapAdapter.class)
@XmlElement(name = "users")
private Map<String, User> userMap = new LinkedHashMap<String, User>();
}

View File

@@ -0,0 +1,123 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.user.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.user.User;
//~--- JDK imports ------------------------------------------------------------
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Sebastian Sdorra
*/
@XmlRootElement(name = "users")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlUserList implements Iterable<User>
{
/**
* Constructs ...
*
*/
public XmlUserList() {}
/**
* Constructs ...
*
*
*
* @param userMap
*/
public XmlUserList(Map<String, User> userMap)
{
this.users = new LinkedList<User>(userMap.values());
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public Iterator<User> iterator()
{
return users.iterator();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public LinkedList<User> getUsers()
{
return users;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param users
*/
public void setUsers(LinkedList<User> users)
{
this.users = users;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlElement(name = "user")
private LinkedList<User> users;
}

View File

@@ -0,0 +1,388 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.user.xml;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContextProvider;
import sonia.scm.security.ScmSecurityException;
import sonia.scm.security.SecurityContext;
import sonia.scm.user.AbstractUserManager;
import sonia.scm.user.User;
import sonia.scm.user.UserAllreadyExistException;
import sonia.scm.user.UserException;
import sonia.scm.util.IOUtil;
import sonia.scm.util.SecurityUtil;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedList;
import javax.xml.bind.JAXB;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class XmlUserManager extends AbstractUserManager
{
/** Field description */
public static final String ADMIN_PATH = "/sonia/scm/config/admin-account.xml";
/** Field description */
public static final String DATABASEFILE =
"config".concat(File.separator).concat("users.xml");
/** Field description */
public static final String TYPE = "xml";
/** the logger for XmlUserManager */
private static final Logger logger =
LoggerFactory.getLogger(XmlUserManager.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param scurityContextProvider
*/
@Inject
public XmlUserManager(Provider<SecurityContext> scurityContextProvider)
{
this.scurityContextProvider = scurityContextProvider;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws IOException
*/
@Override
public void close() throws IOException
{
// do nothing
}
/**
* Method description
*
*
* @param username
*
* @return
*/
@Override
public boolean contains(String username)
{
return userDB.contains(username);
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
* @throws UserException
*/
@Override
public void create(User user) throws UserException, IOException
{
User currentUser = SecurityUtil.getCurrentUser(scurityContextProvider);
if (!user.equals(currentUser) &&!currentUser.isAdmin())
{
throw new ScmSecurityException("admin account is required");
}
if (userDB.contains(user.getName()))
{
throw new UserAllreadyExistException();
}
String type = user.getType();
if (Util.isEmpty(type))
{
user.setType(TYPE);
}
synchronized (XmlUserManager.class)
{
userDB.add(user.clone());
storeDB();
}
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
* @throws UserException
*/
@Override
public void delete(User user) throws UserException, IOException
{
SecurityUtil.assertIsAdmin(scurityContextProvider);
String name = user.getName();
if (userDB.contains(name))
{
synchronized (XmlUserManager.class)
{
userDB.remove(name);
storeDB();
}
}
else
{
throw new UserException("user does not exists");
}
}
/**
* Method description
*
*
* @param context
*/
@Override
public void init(SCMContextProvider context)
{
File directory = context.getBaseDirectory();
userDBFile = new File(directory, DATABASEFILE);
if (userDBFile.exists())
{
loadDB();
}
else
{
IOUtil.mkdirs(userDBFile.getParentFile());
userDB = new XmlUserDatabase();
userDB.setCreationTime(System.currentTimeMillis());
createAdminAccount();
}
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
* @throws UserException
*/
@Override
public void modify(User user) throws UserException, IOException
{
User currentUser = SecurityUtil.getCurrentUser(scurityContextProvider);
if (!user.equals(currentUser) &&!currentUser.isAdmin())
{
throw new ScmSecurityException("admin account is required");
}
String name = user.getName();
if (userDB.contains(name))
{
synchronized (XmlUserManager.class)
{
userDB.remove(name);
userDB.add(user.clone());
storeDB();
}
}
else
{
throw new UserException("user does not exists");
}
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
* @throws UserException
*/
@Override
public void refresh(User user) throws UserException, IOException
{
SecurityUtil.assertIsAdmin(scurityContextProvider);
User fresh = userDB.get(user.getName());
if (fresh == null)
{
throw new UserException("user does not exists");
}
fresh.copyProperties(user);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param id
*
* @return
*/
@Override
public User get(String id)
{
// SecurityUtil.assertIsAdmin(scurityContextProvider);
User user = userDB.get(id);
if (user != null)
{
user = user.clone();
}
return user;
}
/**
* Method description
*
*
* @return
*/
@Override
public Collection<User> getAll()
{
SecurityUtil.assertIsAdmin(scurityContextProvider);
LinkedList<User> users = new LinkedList<User>();
for (User user : userDB.values())
{
users.add(user.clone());
}
return users;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
private void createAdminAccount()
{
InputStream input = XmlUserManager.class.getResourceAsStream(ADMIN_PATH);
try
{
User user = JAXB.unmarshal(input, User.class);
userDB.add(user);
storeDB();
}
catch (Exception ex)
{
logger.error("could not create AdminAccount", ex);
}
finally
{
IOUtil.close(input);
}
}
/**
* Method description
*
*/
private void loadDB()
{
userDB = JAXB.unmarshal(userDBFile, XmlUserDatabase.class);
}
/**
* Method description
*
*/
private void storeDB()
{
userDB.setLastModified(System.currentTimeMillis());
JAXB.marshal(userDB, userDBFile);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Provider<SecurityContext> scurityContextProvider;
/** Field description */
private XmlUserDatabase userDB;
/** Field description */
private File userDBFile;
}

View File

@@ -0,0 +1,93 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.user.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.user.User;
//~--- JDK imports ------------------------------------------------------------
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
*
* @author Sebastian Sdorra
*/
public class XmlUserMapAdapter
extends XmlAdapter<XmlUserList, Map<String, User>>
{
/**
* Method description
*
*
* @param userMap
*
* @return
*
* @throws Exception
*/
@Override
public XmlUserList marshal(Map<String, User> userMap) throws Exception
{
return new XmlUserList(userMap);
}
/**
* Method description
*
*
* @param users
*
* @return
*
* @throws Exception
*/
@Override
public Map<String, User> unmarshal(XmlUserList users) throws Exception
{
Map<String, User> userMap = new LinkedHashMap<String, User>();
for (User user : users)
{
userMap.put(user.getName(), user);
}
return userMap;
}
}