Java tutorial
/** * htdbc - HyperText DataBase Connection * * Copyright (C) 2014 by softwarewerke.com <info@softwarewerke.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.softwarewerke.htdbc; import com.google.common.reflect.ClassPath; import com.softwarewerke.htdbc.misc.Konfig; import java.io.File; import java.io.IOException; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.HashSet; import java.util.Set; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.ws.rs.Path; import javax.ws.rs.core.Application; import org.apache.log4j.Logger; /** * The resteasy Application defines classes that are resource endpoints. * @author peter */ public class App extends Application implements ServletContextListener { @Override public Set getSingletons() { Logger log = Logger.getLogger(getClass()); Set set = new HashSet(); ClassPath cp; try { cp = ClassPath.from(getClass().getClassLoader()); } catch (IOException ex) { log.fatal(ex); return set; } String p = getClass().getPackage().getName(); for (ClassPath.ClassInfo n : cp.getAllClasses()) { if (!n.getPackageName().startsWith(p)) { continue; } try { Class cl = n.load(); if (cl.getAnnotation(Path.class) == null) { continue; } if (cl.getAnnotation(Singleton.class) == null) { continue; } if (!Main.isDevelopment() && cl.getAnnotation(IsDev.class) != null) { continue; } set.add(cl.newInstance()); } catch (ClassFormatError ignore) { } catch (Exception ex) { log.error(ex); } } set.add(new AppException()); return set; } @Override public Set<Class<?>> getClasses() { Logger log = Logger.getLogger(getClass()); Set set = new HashSet(); ClassPath cp; try { cp = ClassPath.from(getClass().getClassLoader()); } catch (IOException ex) { log.fatal(ex); return set; } String p = getClass().getPackage().getName(); for (ClassPath.ClassInfo n : cp.getAllClasses()) { if (!n.getPackageName().startsWith(p)) { continue; } try { Class cl = n.load(); if (cl.getAnnotation(Path.class) == null) { continue; } if (cl.getAnnotation(Singleton.class) != null) { continue; } if (!Main.isDevelopment() && cl.getAnnotation(IsDev.class) != null) { continue; } set.add(cl); } catch (ClassFormatError ignore) { } catch (Exception ex) { log.error(ex); } } return set; } @Override public void contextInitialized(ServletContextEvent e) { Konfig k = new Konfig(); File f = new File(e.getServletContext().getRealPath("/") + "/WEB-INF/config.properties"); try { k.load(f); } catch (IOException ex) { System.err.println(ex); } Ds.init(k.extract()); } @Override public void contextDestroyed(ServletContextEvent e) { } /** * Annotation for marking singleton java-ws resources. * * @author peter */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Singleton { } /** * Resources annotated with @IsDev are only available in development mode. */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface IsDev { } }