Java tutorial
/* * Copyright (C) 2012 Emily Soldal * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package me.emily.config; import static com.google.common.base.Preconditions.*; import java.util.List; import me.emily.irc.protocol.parser.Parser; import me.emily.irc.protocol.parser.ParserPriority; import me.emily.irc.protocol.parser.raws.RawNumeric; import me.emily.irc.protocol.parser.raws.RawNumericReciever; import org.reflections.Reflections; import org.reflections.util.ClasspathHelper; import org.reflections.util.ConfigurationBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap.Builder; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.google.common.collect.Ordering; import com.google.inject.AbstractModule; import com.google.inject.TypeLiteral; import com.google.inject.name.Names; import com.google.inject.util.Providers; /** * @author Emily Soldal * @created 3 Feb 2012 */ public class ProtocolConfig extends AbstractModule { private static final Logger log = LoggerFactory.getLogger(ProtocolConfig.class); @Override protected void configure() { TypeLiteral<Multimap<Integer, RawNumericReciever>> numericRecieverLiteral = new TypeLiteral<Multimap<Integer, RawNumericReciever>>() { }; bind(numericRecieverLiteral).toProvider(Providers.of(rawRecievers())); TypeLiteral<Iterable<Parser>> parseLiteral = new TypeLiteral<Iterable<Parser>>() { }; bind(parseLiteral).annotatedWith(Names.named("parsers")).toProvider(Providers.of(getParsers())); } private Multimap<Integer, RawNumericReciever> rawRecievers() { // Multimap<Integer, RawNumericReciever> ret = Multimaps. Builder<Integer, RawNumericReciever> builder = ImmutableMultimap.builder(); String packageName = RawNumericReciever.class.getPackage().getName(); Reflections r = new Reflections( new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(packageName))); for (Class<? extends RawNumericReciever> clazz : r.getSubTypesOf(RawNumericReciever.class)) { try { RawNumericReciever newInstance = clazz.newInstance(); RawNumeric numeric = clazz.getAnnotation(RawNumeric.class); checkState(numeric != null, "%s does not have a %s numeric definition!", clazz.getName(), RawNumeric.class.getSimpleName()); for (int num : numeric.value()) { builder.put(num, newInstance); } } catch (Exception e) { log.error("Failed to load raw numeric parser {}", clazz.getName()); Throwables.propagate(e); } } return builder.build(); } private ImmutableList<Parser> getParsers() { String packageName = Parser.class.getPackage().getName(); Reflections r = new Reflections( new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(packageName))); List<Parser> parsers = Lists.newArrayList(); for (Class<? extends Parser> clazz : r.getSubTypesOf(Parser.class)) { try { parsers.add(clazz.newInstance()); } catch (Exception e) { log.error("Failed to load parser {}", clazz.getName()); Throwables.propagate(e); } } ImmutableList<Parser> ret = Ordering.from(ParserPriority.PriorityComparator.instance) .immutableSortedCopy(parsers); for (Parser p : ret) { log.info("Parser: {}", p.getClass().getName()); } return ret; } }