Skip to content

Commit

Permalink
Added support for aliases to MySqlDataSourceModule
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Dec 7, 2010
1 parent 7988f57 commit 67ba4be
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Scopes;
import com.google.common.collect.ImmutableList;
import org.weakref.jmx.guice.MBeanModule;

import javax.sql.DataSource;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Collections;

public class MySqlDataSourceModule extends MBeanModule
{
private final Class<? extends Annotation> annotation;
private final List<Class<? extends Annotation>> aliases;
private final String propertyPrefix;

public MySqlDataSourceModule(Class<? extends Annotation> annotation, String propertyPrefix)
public MySqlDataSourceModule(String propertyPrefix, Class<? extends Annotation> annotation, Class<? extends Annotation>... aliases)
{
if (annotation == null) {
throw new NullPointerException("annotation is null");
Expand All @@ -26,6 +30,11 @@ public MySqlDataSourceModule(Class<? extends Annotation> annotation, String prop
}
this.annotation = annotation;
this.propertyPrefix = propertyPrefix;
if (aliases != null) {
this.aliases = ImmutableList.copyOf(aliases);
} else {
this.aliases = Collections.emptyList();
}
}

@Override
Expand All @@ -37,6 +46,12 @@ public void configureMBeans()
// Bind the datasource
bind(DataSource.class).annotatedWith(annotation).toProvider(new MySqlDataSourceProvider(annotation)).in(Scopes.SINGLETON);
export(DataSource.class).annotatedWith(annotation).withGeneratedName();

// Bind aliases
Key<DataSource> key = Key.get(DataSource.class, annotation);
for (Class<? extends Annotation> alise : aliases) {
bind(DataSource.class).annotatedWith(alise).to(key);
}
}

private static class MySqlDataSourceProvider implements Provider<MySqlDataSource>
Expand Down

0 comments on commit 67ba4be

Please sign in to comment.