move xml backend classes to own artifcat

This commit is contained in:
Sebastian Sdorra
2012-03-04 11:24:12 +01:00
parent 9663e80826
commit 5c8968905f
20 changed files with 53 additions and 1 deletions

View File

@@ -0,0 +1,196 @@
/**
* 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.group.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.group.Group;
//~--- 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 = "group-db")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlGroupDatabase
{
/**
* Constructs ...
*
*/
public XmlGroupDatabase()
{
long c = System.currentTimeMillis();
creationTime = c;
lastModified = c;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param group
*/
public void add(Group group)
{
groupMap.put(group.getName(), group);
}
/**
* Method description
*
*
* @param groupname
*
* @return
*/
public boolean contains(String groupname)
{
return groupMap.containsKey(groupname);
}
/**
* Method description
*
*
* @param groupname
*
* @return
*/
public Group remove(String groupname)
{
return groupMap.remove(groupname);
}
/**
* Method description
*
*
* @return
*/
public Collection<Group> values()
{
return groupMap.values();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param groupname
*
* @return
*/
public Group get(String groupname)
{
return groupMap.get(groupname);
}
/**
* 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 */
private Long creationTime;
/** Field description */
@XmlJavaTypeAdapter(XmlGroupMapAdapter.class)
@XmlElement(name = "groups")
private Map<String, Group> groupMap = new LinkedHashMap<String, Group>();
/** Field description */
private Long lastModified;
}

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.group.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.group.Group;
//~--- 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 = "groups")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlGroupList implements Iterable<Group>
{
/**
* Constructs ...
*
*/
public XmlGroupList() {}
/**
* Constructs ...
*
*
*
* @param groupMap
*/
public XmlGroupList(Map<String, Group> groupMap)
{
this.groups = new LinkedList<Group>(groupMap.values());
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public Iterator<Group> iterator()
{
return groups.iterator();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public LinkedList<Group> getGroups()
{
return groups;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param groups
*/
public void setGroups(LinkedList<Group> groups)
{
this.groups = groups;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@XmlElement(name = "group")
private LinkedList<Group> groups;
}

View File

@@ -0,0 +1,505 @@
/**
* 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.group.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.HandlerEvent;
import sonia.scm.SCMContextProvider;
import sonia.scm.TransformFilter;
import sonia.scm.group.AbstractGroupManager;
import sonia.scm.group.Group;
import sonia.scm.group.GroupAllreadyExistExeption;
import sonia.scm.group.GroupException;
import sonia.scm.group.GroupListener;
import sonia.scm.search.SearchRequest;
import sonia.scm.search.SearchUtil;
import sonia.scm.security.SecurityContext;
import sonia.scm.store.Store;
import sonia.scm.store.StoreFactory;
import sonia.scm.util.CollectionAppender;
import sonia.scm.util.SecurityUtil;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class XmlGroupManager extends AbstractGroupManager
{
/** Field description */
public static final String STORE_NAME = "groups";
/** Field description */
public static final String TYPE = "xml";
/** the logger for XmlGroupManager */
private static final Logger logger =
LoggerFactory.getLogger(XmlGroupManager.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param securityContextProvider
* @param storeFactory
* @param groupListenerProvider
*/
@Inject
public XmlGroupManager(Provider<SecurityContext> securityContextProvider,
StoreFactory storeFactory,
Provider<Set<GroupListener>> groupListenerProvider)
{
this.securityContextProvider = securityContextProvider;
this.store = storeFactory.getStore(XmlGroupDatabase.class, STORE_NAME);
this.groupListenerProvider = groupListenerProvider;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws IOException
*/
@Override
public void close() throws IOException
{
// do nothing
}
/**
* Method description
*
*
* @param group
*
* @throws GroupException
* @throws IOException
*/
@Override
public void create(Group group) throws GroupException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("create group {} of type {}", group.getName(),
group.getType());
}
SecurityUtil.assertIsAdmin(securityContextProvider);
if (groupDB.contains(group.getName()))
{
throw new GroupAllreadyExistExeption();
}
String type = group.getType();
if (Util.isEmpty(type))
{
group.setType(TYPE);
}
group.setCreationDate(System.currentTimeMillis());
synchronized (XmlGroupManager.class)
{
groupDB.add(group.clone());
storeDB();
}
fireEvent(group, HandlerEvent.CREATE);
}
/**
* Method description
*
*
* @param group
*
* @throws GroupException
* @throws IOException
*/
@Override
public void delete(Group group) throws GroupException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("delete group {} of type {}", group.getName(),
group.getType());
}
SecurityUtil.assertIsAdmin(securityContextProvider);
String name = group.getName();
if (groupDB.contains(name))
{
synchronized (XmlGroupManager.class)
{
groupDB.remove(name);
storeDB();
}
fireEvent(group, HandlerEvent.DELETE);
}
else
{
throw new GroupException("user does not exists");
}
}
/**
* Method description
*
*
* @param context
*/
@Override
public void init(SCMContextProvider context)
{
groupDB = store.get();
if (groupDB == null)
{
groupDB = new XmlGroupDatabase();
}
Set<GroupListener> listeners = groupListenerProvider.get();
if (Util.isNotEmpty(listeners))
{
addListeners(listeners);
}
}
/**
* Method description
*
*
* @param group
*
* @throws GroupException
* @throws IOException
*/
@Override
public void modify(Group group) throws GroupException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("modify group {} of type {}", group.getName(),
group.getType());
}
SecurityUtil.assertIsAdmin(securityContextProvider);
String name = group.getName();
if (groupDB.contains(name))
{
group.setLastModified(System.currentTimeMillis());
synchronized (XmlGroupManager.class)
{
groupDB.remove(name);
groupDB.add(group.clone());
storeDB();
}
fireEvent(group, HandlerEvent.MODIFY);
}
else
{
throw new GroupException("group does not exists");
}
}
/**
* Method description
*
*
* @param group
*
* @throws GroupException
* @throws IOException
*/
@Override
public void refresh(Group group) throws GroupException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("refresh group {} of type {}", group.getName(),
group.getType());
}
SecurityUtil.assertIsAdmin(securityContextProvider);
Group fresh = groupDB.get(group.getName());
if (fresh == null)
{
throw new GroupException("group does not exists");
}
fresh.copyProperties(group);
}
/**
* Method description
*
*
* @param searchRequest
*
* @return
*/
@Override
public Collection<Group> search(final SearchRequest searchRequest)
{
if (logger.isDebugEnabled())
{
logger.debug("search group with query {}", searchRequest.getQuery());
}
return SearchUtil.search(searchRequest, groupDB.values(),
new TransformFilter<Group>()
{
@Override
public Group accept(Group group)
{
Group result = null;
if (SearchUtil.matchesOne(searchRequest, group.getName(),
group.getDescription()))
{
result = group.clone();
}
return result;
}
});
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param id
*
* @return
*/
@Override
public Group get(String id)
{
Group group = groupDB.get(id);
if (group != null)
{
group = group.clone();
}
return group;
}
/**
* Method description
*
*
* @return
*/
@Override
public Collection<Group> getAll()
{
return getAll(null);
}
/**
* Method description
*
*
* @param comparator
*
* @return
*/
@Override
public Collection<Group> getAll(Comparator<Group> comparator)
{
SecurityUtil.assertIsAdmin(securityContextProvider);
List<Group> groups = new ArrayList<Group>();
for (Group group : groupDB.values())
{
groups.add(group.clone());
}
if (comparator != null)
{
Collections.sort(groups, comparator);
}
return groups;
}
/**
* Method description
*
*
*
* @param comparator
* @param start
* @param limit
*
* @return
*/
@Override
public Collection<Group> getAll(Comparator<Group> comparator, int start,
int limit)
{
SecurityUtil.assertIsAdmin(securityContextProvider);
return Util.createSubCollection(groupDB.values(), comparator,
new CollectionAppender<Group>()
{
@Override
public void append(Collection<Group> collection, Group item)
{
collection.add(item.clone());
}
}, start, limit);
}
/**
* Method description
*
*
* @param start
* @param limit
*
* @return
*/
@Override
public Collection<Group> getAll(int start, int limit)
{
return getAll(null, start, limit);
}
/**
* Method description
*
*
* @param member
*
* @return
*/
@Override
public Collection<Group> getGroupsForMember(String member)
{
LinkedList<Group> groups = new LinkedList<Group>();
for (Group group : groupDB.values())
{
if (group.isMember(member))
{
groups.add(group.clone());
}
}
return groups;
}
/**
* Method description
*
*
* @return
*/
@Override
public Long getLastModified()
{
return groupDB.getLastModified();
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
private void storeDB()
{
groupDB.setLastModified(System.currentTimeMillis());
store.set(groupDB);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private XmlGroupDatabase groupDB;
/** Field description */
private Provider<Set<GroupListener>> groupListenerProvider;
/** Field description */
private Provider<SecurityContext> securityContextProvider;
/** Field description */
private Store<XmlGroupDatabase> store;
}

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.group.xml;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.group.Group;
//~--- JDK imports ------------------------------------------------------------
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
*
* @author Sebastian Sdorra
*/
public class XmlGroupMapAdapter
extends XmlAdapter<XmlGroupList, Map<String, Group>>
{
/**
* Method description
*
*
* @param groupMap
*
* @return
*
* @throws Exception
*/
@Override
public XmlGroupList marshal(Map<String, Group> groupMap) throws Exception
{
return new XmlGroupList(groupMap);
}
/**
* Method description
*
*
* @param groups
*
* @return
*
* @throws Exception
*/
@Override
public Map<String, Group> unmarshal(XmlGroupList groups) throws Exception
{
Map<String, Group> groupMap = new LinkedHashMap<String, Group>();
for (Group group : groups)
{
groupMap.put(group.getName(), group);
}
return groupMap;
}
}

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.RepositoryHook;
import sonia.scm.repository.RepositoryHookEvent;
/**
*
* @author Sebastian Sdorra
*/
public class RepositoryHookTask implements Runnable
{
/** the logger for RepositoryHookTask */
private static final Logger logger =
LoggerFactory.getLogger(RepositoryHookTask.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param hook
* @param event
*/
public RepositoryHookTask(RepositoryHook hook, RepositoryHookEvent event)
{
this.hook = hook;
this.event = event;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
@Override
public void run()
{
if (logger.isDebugEnabled())
{
Object[] args = new Object[] { event.getType(), hook.getClass().getName(),
event.getRepository().getName() };
logger.debug("execute async {} hook {} for repository {}", args);
}
hook.onEvent(event);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private RepositoryHookEvent event;
/** Field description */
private RepositoryHook hook;
}

View File

@@ -0,0 +1,276 @@
/**
* 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.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 = "repository-db")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlRepositoryDatabase
{
/**
* Constructs ...
*
*/
public XmlRepositoryDatabase()
{
long c = System.currentTimeMillis();
creationTime = c;
lastModified = c;
}
//~--- 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());
}
/**
* 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;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Long creationTime;
/** Field description */
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;
}

File diff suppressed because it is too large Load Diff

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,169 @@
/**
* 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.store;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
/**
*
* @author Sebastian Sdorra
*
* @param <T>
*/
public class JAXBStore<T> implements Store<T>
{
/** the logger for JAXBStore */
private static final Logger logger = LoggerFactory.getLogger(JAXBStore.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param type
* @param configFile
*/
public JAXBStore(Class<T> type, File configFile)
{
this.type = type;
try
{
context = JAXBContext.newInstance(type);
this.configFile = configFile;
}
catch (JAXBException ex)
{
throw new StoreException(ex);
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public T get()
{
if (logger.isDebugEnabled())
{
logger.debug("load {} from store {}", type, configFile);
}
T result = null;
if (configFile.exists())
{
try
{
result = (T) context.createUnmarshaller().unmarshal(configFile);
}
catch (JAXBException ex)
{
throw new StoreException(ex);
}
}
return result;
}
/**
* Method description
*
*
* @return
*/
public Class<T> getType()
{
return type;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param object
*/
@Override
public void set(T object)
{
if (logger.isDebugEnabled())
{
logger.debug("store {} to {}", object.getClass().getName(),
configFile.getPath());
}
try
{
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(object, configFile);
}
catch (JAXBException ex)
{
throw new StoreException(ex);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private File configFile;
/** Field description */
private JAXBContext context;
/** Field description */
private Class<T> type;
}

View File

@@ -0,0 +1,128 @@
/**
* 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.store;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContextProvider;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class JAXBStoreFactory implements StoreFactory
{
/** Field description */
public static final String CONFIGDIRECTORY_NAME = "config";
/** Field description */
public static final String FILE_EXTENSION = ".xml";
/** the logger for JAXBStoreFactory */
private static final Logger logger =
LoggerFactory.getLogger(JAXBStoreFactory.class);
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws IOException
*/
@Override
public void close() throws IOException
{
// do nothing
}
/**
* Method description
*
*
* @param context
*/
@Override
public void init(SCMContextProvider context)
{
configDirectory = new File(context.getBaseDirectory(),
CONFIGDIRECTORY_NAME);
IOUtil.mkdirs(configDirectory);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param type
* @param name
* @param <T>
*
* @return
*/
@Override
public <T> Store<T> getStore(Class<T> type, String name)
{
File configFile = new File(configDirectory, name.concat(FILE_EXTENSION));
if (logger.isDebugEnabled())
{
logger.debug("create store for {} at {}", type.getName(),
configFile.getPath());
}
return new JAXBStore<T>(type, configFile);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private File configDirectory;
}

View File

@@ -0,0 +1,196 @@
/**
* 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.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
{
/**
* Constructs ...
*
*/
public XmlUserDatabase()
{
long c = System.currentTimeMillis();
creationTime = c;
lastModified = c;
}
//~--- methods --------------------------------------------------------------
/**
* 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 */
private Long creationTime;
/** Field description */
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,569 @@
/**
* 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.HandlerEvent;
import sonia.scm.SCMContextProvider;
import sonia.scm.TransformFilter;
import sonia.scm.search.SearchRequest;
import sonia.scm.search.SearchUtil;
import sonia.scm.security.ScmSecurityException;
import sonia.scm.store.Store;
import sonia.scm.store.StoreFactory;
import sonia.scm.user.AbstractUserManager;
import sonia.scm.user.User;
import sonia.scm.user.UserAllreadyExistException;
import sonia.scm.user.UserException;
import sonia.scm.user.UserListener;
import sonia.scm.util.AssertUtil;
import sonia.scm.util.CollectionAppender;
import sonia.scm.util.IOUtil;
import sonia.scm.util.SecurityUtil;
import sonia.scm.util.Util;
import sonia.scm.web.security.WebSecurityContext;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
/**
*
* @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 ANONYMOUS_PATH =
"/sonia/scm/config/anonymous-account.xml";
/** Field description */
public static final String STORE_NAME = "users";
/** 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
* @param storeFactory
* @param userListenerProvider
*/
@Inject
public XmlUserManager(Provider<WebSecurityContext> scurityContextProvider,
StoreFactory storeFactory,
Provider<Set<UserListener>> userListenerProvider)
{
this.scurityContextProvider = scurityContextProvider;
this.store = storeFactory.getStore(XmlUserDatabase.class, STORE_NAME);
this.userListenerProvider = userListenerProvider;
}
//~--- 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
{
if (logger.isInfoEnabled())
{
logger.info("create user {} of type {}", user.getName(), user.getType());
}
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(user.getName());
}
String type = user.getType();
if (Util.isEmpty(type))
{
user.setType(TYPE);
}
AssertUtil.assertIsValid(user);
user.setCreationDate(System.currentTimeMillis());
synchronized (XmlUserManager.class)
{
userDB.add(user.clone());
storeDB();
}
fireEvent(user, HandlerEvent.CREATE);
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
* @throws UserException
*/
@Override
public void delete(User user) throws UserException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("delete user {} of type {}", user.getName(), user.getType());
}
SecurityUtil.assertIsAdmin(scurityContextProvider);
String name = user.getName();
if (userDB.contains(name))
{
synchronized (XmlUserManager.class)
{
userDB.remove(name);
storeDB();
}
fireEvent(user, HandlerEvent.DELETE);
}
else
{
throw new UserException("user does not exists");
}
}
/**
* Method description
*
*
* @param context
*/
@Override
public void init(SCMContextProvider context)
{
userDB = store.get();
if (userDB == null)
{
userDB = new XmlUserDatabase();
createDefaultAccounts();
}
Set<UserListener> listeners = userListenerProvider.get();
if (Util.isNotEmpty(listeners))
{
addListeners(listeners);
}
}
/**
* Method description
*
*
* @param user
*
* @throws IOException
* @throws UserException
*/
@Override
public void modify(User user) throws UserException, IOException
{
if (logger.isInfoEnabled())
{
logger.info("modify user {} of type {}", user.getName(), user.getType());
}
User currentUser = SecurityUtil.getCurrentUser(scurityContextProvider);
if (!user.getName().equals(currentUser.getName()) &&!currentUser.isAdmin())
{
throw new ScmSecurityException("admin account is required");
}
String name = user.getName();
if (userDB.contains(name))
{
AssertUtil.assertIsValid(user);
user.setLastModified(System.currentTimeMillis());
synchronized (XmlUserManager.class)
{
userDB.remove(name);
userDB.add(user.clone());
storeDB();
}
fireEvent(user, HandlerEvent.MODIFY);
}
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
{
if (logger.isInfoEnabled())
{
logger.info("refresh user {} of type {}", user.getName(), user.getType());
}
SecurityUtil.assertIsAdmin(scurityContextProvider);
User fresh = userDB.get(user.getName());
if (fresh == null)
{
throw new UserException("user does not exists");
}
fresh.copyProperties(user);
}
/**
* Method description
*
*
* @param searchRequest
*
* @return
*/
@Override
public Collection<User> search(final SearchRequest searchRequest)
{
if (logger.isDebugEnabled())
{
logger.debug("search user with query {}", searchRequest.getQuery());
}
return SearchUtil.search(searchRequest, userDB.values(),
new TransformFilter<User>()
{
@Override
public User accept(User user)
{
User result = null;
if (SearchUtil.matchesOne(searchRequest, user.getName(),
user.getDisplayName(), user.getMail()))
{
result = user.clone();
}
return result;
}
});
}
//~--- 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()
{
return getAll(null);
}
/**
* Method description
*
*
* @param comparator
*
* @return
*/
@Override
public Collection<User> getAll(Comparator<User> comparator)
{
SecurityUtil.assertIsAdmin(scurityContextProvider);
List<User> users = new ArrayList<User>();
for (User user : userDB.values())
{
users.add(user.clone());
}
if (comparator != null)
{
Collections.sort(users, comparator);
}
return users;
}
/**
* Method description
*
*
*
* @param comaparator
* @param start
* @param limit
*
* @return
*/
@Override
public Collection<User> getAll(Comparator<User> comaparator, int start,
int limit)
{
SecurityUtil.assertIsAdmin(scurityContextProvider);
return Util.createSubCollection(userDB.values(), comaparator,
new CollectionAppender<User>()
{
@Override
public void append(Collection<User> collection, User item)
{
collection.add(item.clone());
}
}, start, limit);
}
/**
* Method description
*
*
* @param start
* @param limit
*
* @return
*/
@Override
public Collection<User> getAll(int start, int limit)
{
return getAll(null, start, limit);
}
/**
* Method description
*
*
* @return
*/
@Override
public Long getLastModified()
{
return userDB.getLastModified();
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param unmarshaller
* @param path
*/
private void createDefaultAccount(Unmarshaller unmarshaller, String path)
{
InputStream input = XmlUserManager.class.getResourceAsStream(path);
try
{
User user = (User) unmarshaller.unmarshal(input);
user.setCreationDate(System.currentTimeMillis());
userDB.add(user);
storeDB();
}
catch (Exception ex)
{
logger.error("could not create account", ex);
}
finally
{
IOUtil.close(input);
}
}
/**
* Method description
*
*/
private void createDefaultAccounts()
{
try
{
JAXBContext context = JAXBContext.newInstance(User.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
createDefaultAccount(unmarshaller, ADMIN_PATH);
createDefaultAccount(unmarshaller, ANONYMOUS_PATH);
}
catch (JAXBException ex)
{
logger.error(ex.getMessage(), ex);
}
}
/**
* Method description
*
*/
private void storeDB()
{
userDB.setLastModified(System.currentTimeMillis());
store.set(userDB);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Provider<WebSecurityContext> scurityContextProvider;
/** Field description */
private Store<XmlUserDatabase> store;
/** Field description */
private XmlUserDatabase userDB;
/** Field description */
private Provider<Set<UserListener>> userListenerProvider;
}

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;
}
}