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