source: java/main/src/main/java/com/framsticks/hosting/ServerInstance.java @ 77

Last change on this file since 77 was 77, checked in by psniegowski, 11 years ago

Add new java codebase.

File size: 2.4 KB
Line 
1package com.framsticks.hosting;
2
3import com.framsticks.core.*;
4import com.framsticks.params.FramsClass;
5import com.framsticks.params.ParamBuilder;
6import com.framsticks.params.types.CompositeParam;
7import com.framsticks.core.LocalInstance;
8import com.framsticks.util.Future;
9import org.apache.log4j.Logger;
10
11/**
12 * @author Piotr Sniegowski
13 */
14public 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}
Note: See TracBrowser for help on using the repository browser.