1 | package com.framsticks.hosting; |
---|
2 | |
---|
3 | import com.framsticks.core.*; |
---|
4 | import com.framsticks.params.CompositeParam; |
---|
5 | import com.framsticks.params.ConstructionException; |
---|
6 | import com.framsticks.params.FramsClass; |
---|
7 | import com.framsticks.params.FramsClassBuilder; |
---|
8 | import com.framsticks.params.Param; |
---|
9 | import com.framsticks.core.LocalInstance; |
---|
10 | import com.framsticks.util.dispatching.Future; |
---|
11 | |
---|
12 | import org.apache.commons.configuration.Configuration; |
---|
13 | import org.apache.log4j.Logger; |
---|
14 | |
---|
15 | /** |
---|
16 | * @author Piotr Sniegowski |
---|
17 | */ |
---|
18 | public class ServerInstance extends LocalInstance { |
---|
19 | |
---|
20 | private final static Logger log = Logger.getLogger(ServerInstance.class.getName()); |
---|
21 | |
---|
22 | Entity hosted; |
---|
23 | |
---|
24 | public ServerInstance() { |
---|
25 | } |
---|
26 | |
---|
27 | @Override |
---|
28 | protected void run() { |
---|
29 | super.run(); |
---|
30 | assert hosted != null; |
---|
31 | hosted.start(); |
---|
32 | } |
---|
33 | |
---|
34 | @Override |
---|
35 | public void configure(Configuration config) { |
---|
36 | super.configure(config); |
---|
37 | |
---|
38 | Configuration hostedConfig = config.subset("hosted.entity"); |
---|
39 | hosted = Program.configureEntity(hostedConfig); |
---|
40 | if (hosted == null) { |
---|
41 | log.fatal("failed to create hosted entity"); |
---|
42 | return; |
---|
43 | } |
---|
44 | hosted.setName("hosted"); |
---|
45 | hosted.configure(hostedConfig); |
---|
46 | root = new Node((CompositeParam) Param.build().name("root").id("root").type("o" + hosted.getClass().getCanonicalName()).finish(), hosted); |
---|
47 | } |
---|
48 | |
---|
49 | @Override |
---|
50 | public FramsClass getInfoFromCache(String id) { |
---|
51 | assert isActive(); |
---|
52 | if (id == null) { |
---|
53 | return null; |
---|
54 | } |
---|
55 | FramsClass cached = registry.getInfoFromCache(id); |
---|
56 | if (cached != null) { |
---|
57 | return cached; |
---|
58 | } |
---|
59 | try { |
---|
60 | Class<?> nativeClass = Class.forName(id); |
---|
61 | FramsClass framsClass = FramsClassBuilder.buildForClass(nativeClass); |
---|
62 | |
---|
63 | if (!framsClass.getId().equals(id)) { |
---|
64 | log.error("no matching id"); |
---|
65 | return null; |
---|
66 | } |
---|
67 | |
---|
68 | registry.registerReflectedClass(null, id, nativeClass); |
---|
69 | registry.putInfoIntoCache(framsClass); |
---|
70 | return framsClass; |
---|
71 | } catch (ClassNotFoundException ignored) { |
---|
72 | } catch (ConstructionException e) { |
---|
73 | log.error("failed to use info from cache: " + e); |
---|
74 | } |
---|
75 | |
---|
76 | return null; |
---|
77 | } |
---|
78 | |
---|
79 | @Override |
---|
80 | protected void fetchInfo(Path path, Future<FramsClass> future) { |
---|
81 | assert isActive(); |
---|
82 | |
---|
83 | FramsClass framsClass = getInfoFromCache(path.getTop().getObject().getClass().getCanonicalName()); |
---|
84 | if (framsClass == null) { |
---|
85 | log.error("failed to create frams class for: " + path.getTop().getObject().getClass()); |
---|
86 | future.result(null, new Exception()); |
---|
87 | return; |
---|
88 | } |
---|
89 | future.result(framsClass, null); |
---|
90 | |
---|
91 | } |
---|
92 | |
---|
93 | @Override |
---|
94 | protected void tryRegisterOnChangeEvents(Path path) { |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|