1 | package geniusweb.simplerunner;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.lang.reflect.Constructor;
|
---|
5 | import java.lang.reflect.InvocationTargetException;
|
---|
6 | import java.net.URI;
|
---|
7 | import java.net.URISyntaxException;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import geniusweb.actions.Action;
|
---|
12 | import geniusweb.actions.PartyId;
|
---|
13 | import geniusweb.inform.Inform;
|
---|
14 | import geniusweb.party.Party;
|
---|
15 | import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
|
---|
16 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
|
---|
17 | import geniusweb.references.Reference;
|
---|
18 | import tudelft.utilities.repository.NoResourcesNowException;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * A connectionfactory that only accepts URLs of the form
|
---|
22 | * <code>classpath:org/my/package/class</code>
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class ClassPathConnectionFactory implements ProtocolToPartyConnFactory {
|
---|
26 | private static final String SCHEME = "classpath";
|
---|
27 | private static int serialcounter = 1;
|
---|
28 | private final static URI PROTOCOLURI = uri("protocol:protocol");
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public ProtocolToPartyConn connect(Reference reference) {
|
---|
32 | // set up the whole other party including the connection to it.
|
---|
33 | String classpath = getClassPath(reference.getURI());
|
---|
34 | Party party = instantiate(classpath);
|
---|
35 |
|
---|
36 | BasicConnection<Inform, Action> party2protocol = new BasicConnection<Inform, Action>(
|
---|
37 | reference, PROTOCOLURI);
|
---|
38 | BasicConnectionWithParty protocol2party = new BasicConnectionWithParty(
|
---|
39 | reference,
|
---|
40 | uri("classpath:" + reference.getURI().getSchemeSpecificPart()
|
---|
41 | + "." + (serialcounter++)));
|
---|
42 | party2protocol.init(action -> protocol2party.notifyListeners(action));
|
---|
43 | protocol2party.init(info -> party2protocol.notifyListeners(info));
|
---|
44 |
|
---|
45 | party.connect(party2protocol);
|
---|
46 |
|
---|
47 | return protocol2party;
|
---|
48 | }
|
---|
49 |
|
---|
50 | private static URI uri(String ref) {
|
---|
51 | try {
|
---|
52 | return new URI(ref);
|
---|
53 | } catch (URISyntaxException e) {
|
---|
54 | throw new IllegalStateException("failed to create basic URI", e);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | *
|
---|
60 | * @param classpath
|
---|
61 | * @return instance of the given {@link Party} on the classpath
|
---|
62 | * @throws IllegalArgumentException if the Party can not be instantiated, eg
|
---|
63 | * because of a ClassCastException,
|
---|
64 | * ClassNotFoundException,
|
---|
65 | * IllegalAccessException etc. Such an
|
---|
66 | * exception is considered a bug by the
|
---|
67 | * programmer, because this is a
|
---|
68 | * stand-alone runner.
|
---|
69 | */
|
---|
70 | @SuppressWarnings("unchecked")
|
---|
71 | private Party instantiate(String classpath) {
|
---|
72 | System.out.println("Connecting with party '" + classpath + "'");
|
---|
73 | Class<Party> partyclass;
|
---|
74 | try {
|
---|
75 | partyclass = (Class<Party>) Class.forName(classpath);
|
---|
76 | Constructor<Party> ctor = partyclass.getConstructor();
|
---|
77 | return ctor.newInstance(new Object[] {});
|
---|
78 | } catch (ClassNotFoundException | NoSuchMethodException
|
---|
79 | | SecurityException | InstantiationException
|
---|
80 | | IllegalAccessException | IllegalArgumentException
|
---|
81 | | InvocationTargetException e) {
|
---|
82 | throw new IllegalArgumentException(
|
---|
83 | "Failed to create party connection", e);
|
---|
84 | }
|
---|
85 |
|
---|
86 | }
|
---|
87 |
|
---|
88 | private String getClassPath(URI uri) {
|
---|
89 | if (!SCHEME.equals(uri.getScheme())) {
|
---|
90 | throw new IllegalArgumentException("Required the " + SCHEME
|
---|
91 | + " protocol but found " + uri.getScheme());
|
---|
92 | }
|
---|
93 | String path = uri.getSchemeSpecificPart();
|
---|
94 | if (path == null) {
|
---|
95 | throw new IllegalArgumentException(
|
---|
96 | "Expected classpath but found null in " + uri);
|
---|
97 | }
|
---|
98 | return path;
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public List<ProtocolToPartyConn> connect(List<Reference> references)
|
---|
103 | throws IOException, NoResourcesNowException {
|
---|
104 | List<ProtocolToPartyConn> connections = new LinkedList<>();
|
---|
105 | for (Reference partyref : references) {
|
---|
106 | connections.add(connect(partyref));
|
---|
107 | }
|
---|
108 | return connections;
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | class BasicConnectionWithParty extends BasicConnection<Action, Inform>
|
---|
114 | implements ProtocolToPartyConn {
|
---|
115 |
|
---|
116 | private PartyId id;
|
---|
117 |
|
---|
118 | public BasicConnectionWithParty(Reference reference, URI uri) {
|
---|
119 | super(reference, uri);
|
---|
120 | this.id = new PartyId(uri.toString().replace("classpath:", "")
|
---|
121 | .replaceAll("\\.", "_"));
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public PartyId getParty() {
|
---|
126 | return id;
|
---|
127 | }
|
---|
128 |
|
---|
129 | }
|
---|