Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 83de081bec8c618da10676a7b71c4c9846439b0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.examples.server;

import org.eclipse.emf.cdo.examples.internal.server.OM;
import org.eclipse.emf.cdo.examples.server.DemoConfiguration.Mode;

import org.eclipse.net4j.acceptor.IAcceptor;
import org.eclipse.net4j.tcp.TCPUtil;
import org.eclipse.net4j.util.concurrent.Worker;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.lifecycle.Lifecycle;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
import org.eclipse.net4j.util.om.OMPlatform;
import org.eclipse.net4j.util.om.log.EclipseLoggingBridge;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class DemoServer extends Lifecycle
{
  public static final String PROP_BROWSER_PORT = OM.BUNDLE_ID + ".browser.port"; //$NON-NLS-1$

  public static final int PORT = 3003;

  public static final int MAX_IDLE_MINUTES = 15;

  public static final long MAX_IDLE_MILLIS = MAX_IDLE_MINUTES * 60 * 1000;

  public static final DemoServer INSTANCE = new DemoServer();

  private IAcceptor acceptor;

  private Map<String, DemoConfiguration> configs = new HashMap<String, DemoConfiguration>();

  private Cleaner cleaner = new Cleaner();

  private DemoServer()
  {
  }

  public IAcceptor getAcceptor()
  {
    return acceptor;
  }

  public DemoConfiguration[] getConfigs()
  {
    synchronized (configs)
    {
      return configs.values().toArray(new DemoConfiguration[configs.size()]);
    }
  }

  public DemoConfiguration getConfig(String name)
  {
    synchronized (configs)
    {
      return configs.get(name);
    }
  }

  public DemoConfiguration addConfig(Mode mode)
  {
    DemoConfiguration config = new DemoConfiguration(mode, null);
    config.activate();

    synchronized (configs)
    {
      configs.put(config.getName(), config);
    }

    return config;
  }

  @Override
  protected void doActivate() throws Exception
  {
    super.doActivate();
    OMPlatform.INSTANCE.removeLogHandler(EclipseLoggingBridge.INSTANCE);
    OM.LOG.info("Demo server starting");

    IPluginContainer container = IPluginContainer.INSTANCE;
    acceptor = TCPUtil.getAcceptor(container, "0.0.0.0:" + PORT);

    String port = OMPlatform.INSTANCE.getProperty(PROP_BROWSER_PORT);
    if (port != null)
    {
      container.getElement("org.eclipse.emf.cdo.server.db.browsers", "default", port); //$NON-NLS-1$ //$NON-NLS-2$
    }

    cleaner.activate();
    OM.LOG.info("Demo server started");
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    OM.LOG.info("Demo server stopping");
    cleaner.deactivate();

    for (DemoConfiguration config : getConfigs())
    {
      config.deactivate();
    }

    configs.clear();

    if (acceptor != null)
    {
      LifecycleUtil.deactivate(acceptor);
      acceptor = null;
    }

    OM.LOG.info("Demo server stopped");
    super.doDeactivate();
  }

  /**
   * @author Eike Stepper
   */
  private final class Cleaner extends Worker
  {
    @Override
    protected void work(WorkContext context) throws Exception
    {
      for (DemoConfiguration config : getConfigs())
      {
        cleanIfNeeded(config);
      }

      context.nextWork(2000L);
    }

    protected void cleanIfNeeded(DemoConfiguration config)
    {
      if (config.getTimeoutMillis() == 0)
      {
        synchronized (configs)
        {
          configs.remove(config.getName());
        }

        config.deactivate();
      }
    }
  }
}

Back to the top