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