001// Licensed to the Apache Software Foundation (ASF) under one
002// or more contributor license agreements.  See the NOTICE file
003// distributed with this work for additional information
004// regarding copyright ownership.  The ASF licenses this file
005// to you under the Apache License, Version 2.0 (the
006// "License"); you may not use this file except in compliance
007// with the License.  You may obtain a copy of the License at
008//
009// http://www.apache.org/licenses/LICENSE-2.0
010//
011// Unless required by applicable law or agreed to in writing,
012// software distributed under the License is distributed on an
013// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
014// KIND, either express or implied.  See the License for the
015// specific language governing permissions and limitations
016// under the License.
017package org.apache.tapestry5.internal.services.rest;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.HashSet;
022import java.util.Set;
023
024import org.apache.tapestry5.ioc.services.ClassNameLocator;
025import org.apache.tapestry5.services.rest.MappedEntityManager;
026
027/**
028 * Default {@link MappedEntityManager} implementation.
029 */
030public class MappedEntityManagerImpl implements MappedEntityManager
031{
032    
033    private final Set<Class<?>> entities;
034    
035    public MappedEntityManagerImpl(Collection<String> packages, final ClassNameLocator classNameLocator) 
036    {
037        
038        Set<Class<?>> classes = new HashSet<>();
039        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
040        
041        for (String packageName : packages)
042        {
043            for (String className : classNameLocator.locateClassNames(packageName))
044            {
045                try
046                {
047                    Class<?> entityClass = contextClassLoader.loadClass(className);
048                    classes.add(entityClass);
049                }
050                catch (ClassNotFoundException ex)
051                {
052                    throw new RuntimeException(ex);
053                }
054            }
055        }
056        
057        entities = Collections.unmodifiableSet(new HashSet<>(classes));
058        
059    }
060
061    @Override
062    public Set<Class<?>> getEntities() 
063    {
064        return entities;
065    }
066
067}