001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.webengine.jaxrs.scan;
013
014import java.io.IOException;
015import java.util.Set;
016
017import javax.ws.rs.core.Application;
018
019import org.osgi.framework.Bundle;
020
021/**
022 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
023 */
024public abstract class DynamicApplication extends Application {
025
026    protected abstract Bundle getBundle();
027
028    protected String getPackageBase() {
029        String packageBase = getClass().getName();
030        int i = packageBase.lastIndexOf('.');
031        if (i > -1) {
032            packageBase = packageBase.substring(0, i);
033        }
034        return packageBase.replace('.', '/');
035    }
036
037    @Override
038    public Set<Class<?>> getClasses() {
039        try {
040            Scanner scanner = new Scanner(getBundle(), getPackageBase());
041            scanner.scan();
042            return scanner.getClasses();
043        } catch (ReflectiveOperationException | IOException e) {
044            throw new RuntimeException("Failed to scan classes", e);
045        }
046    }
047
048}