My favorites | Sign in
Project Home Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* @(#)StubItest.java 8 Apr 2009
*
* Copyright © 2009 Andrew Phillips.
*
* 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.qrmedia.commons.multispi.example;

import static com.google.common.collect.Iterables.get;

import java.util.Set;

import javax.inject.Named;
import javax.inject.Singleton;

import org.junit.Test;

import uk.gov.mi6.Agent;
import uk.gov.mi6.LicenseToKill;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.qrmedia.commons.multispi.MultiSpi;
import com.qrmedia.commons.multispi.config.MultiSpiBuilder;

/**
* Using {@link MultiSpi} to create Guice providers.
*
* @author anphilli
* @since 8 Apr 2009
*
*/
public class GuiceAgentExample extends AbstractModule {

@Test
public void go() {
Injector injector = Guice.createInjector(this);

Set<?> agentNames = injector.getInstance(Key.get(new TypeLiteral<Set<String>>() {},
Names.named("agentNames")));
System.out.format("Agent names: %s (class of items in set: '%s')%n", agentNames,
get(agentNames, 0).getClass());

Set<?> agentClasses = injector.getInstance(Key.get(new TypeLiteral<Set<Class<? extends Agent>>>() {}));
System.out.format("Agent classes: %s (class of items in set: '%s')%n", agentClasses,
get(agentClasses, 0).getClass());

Set<?> agents = injector.getInstance(Key.get(new TypeLiteral<Set<Agent>>() {}));
System.out.format("Agents: %s%n", agents);
}

@Override
protected void configure() {
// using @Provides
}

@Provides
@Singleton
@Named("agentNames")
Set<String> provideAgentNames(MultiSpi multiSpi) {
return multiSpi.findImplementationNames(Agent.class);
}

@Provides
@Singleton
Set<Class<? extends Agent>> provideAgentClasses(MultiSpi multiSpi) throws ClassCastException, ClassNotFoundException {
return multiSpi.findImplementations(Agent.class);
}

@Provides
@Singleton
Set<Agent> provideAgents(MultiSpi multiSpi) throws ClassNotFoundException, InstantiationException {
return multiSpi.loadImplementations(Agent.class);
}

@SuppressWarnings("unused")
@Provides
@Singleton
private MultiSpi provideMultiSpi() {
return new MultiSpiBuilder().withMetaInfServicesScanning()
.withAnnotationScanning(LicenseToKill.class, "uk.gov").build();
}
}

Change log

r1868 by sharedocs1 on Feb 12, 2011   Diff
Added a Guice example
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3356 bytes, 100 lines
Powered by Google Project Hosting