Java tutorial
/* * Copyright (c) 2011 Yahoo! Inc. All rights reserved. * * 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. See accompanying LICENSE file. */ package io.s4.meter.controller; import java.io.InputStream; import org.apache.commons.configuration.ConfigurationConverter; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.ConfigurationUtils; import org.apache.commons.configuration.PropertiesConfiguration; import com.google.inject.AbstractModule; import com.google.inject.Binder; import com.google.inject.Provides; import com.google.inject.Singleton; import com.google.inject.name.Names; /** * Configures the controller. * * Reads a properties file, provides a {@link Communicator} singleton, and * configures Guice bindings. * * @author Leo Neumeyer */ public class ControllerModule extends AbstractModule { protected PropertiesConfiguration config = null; private void loadProperties(Binder binder) { try { InputStream is = this.getClass().getResourceAsStream("/s4-meter.properties"); config = new PropertiesConfiguration(); config.load(is); System.out.println(ConfigurationUtils.toString(config)); // TODO - validate properties. /* Make all properties injectable. Do we need this? */ Names.bindProperties(binder, ConfigurationConverter.getProperties(config)); } catch (ConfigurationException e) { binder.addError(e); e.printStackTrace(); } } @Override protected void configure() { if (config == null) loadProperties(binder()); bind(Controller.class).asEagerSingleton(); bind(String.class).annotatedWith(Names.named("generator.module")) .toInstance(config.getString("generator.module")); } /** * @return the communicator singleton. */ @Provides @Singleton Communicator provideCommunicator() { Communicator comm = new RestletCommunicator(config.getStringArray("generator.hostnames"), config.getStringArray("generator.ports"), config.getString("generator.classURI").trim(), config.getString("generator.instanceURI").trim(), config.getString("generator.actionURI").trim(), config.getString("generator.eventGeneratorClass"), config.getStringArray("generator.dependentClasses")); return comm; } }