Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 26214203aade03dec082c00141b975f1833805fc (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
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (c) 2013 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:
 *    Christian W. Damus (CEA LIST) - initial API and implementation
 */
package org.eclipse.emf.cdo.ui.internal.admin;

import org.eclipse.emf.cdo.ui.internal.admin.messages.Messages;

import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.io.IOUtil;

import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Christian W. Damus (CEA LIST)
 */
public abstract class StoreType
{
  private static final List<StoreType> INSTANCES = Collections.unmodifiableList(Arrays.asList(initStoreTypes()));

  private final String id;

  private final String name;

  public StoreType(String id, String name)
  {
    this.id = id;
    this.name = name;
  }

  public static List<StoreType> getInstances()
  {
    return INSTANCES;
  }

  public String getID()
  {
    return id;
  }

  public String getName()
  {
    return name;
  }

  public abstract String getStoreTypeID();

  public String getStoreXML(Map<String, Object> storeProperties)
  {
    String template = getTemplate();
    return fillTemplate(template, storeProperties);
  }

  protected abstract String fillTemplate(String xmlTemplate, Map<String, Object> storeProperties);

  protected String replaceVariables(String template, Map<String, String> variables)
  {
    String result = template;
    for (Map.Entry<String, String> entry : variables.entrySet())
    {
      result = result.replace("{{" + entry.getKey() + "}}", entry.getValue()); //$NON-NLS-1$ //$NON-NLS-2$
    }
    return result;
  }

  private String getTemplate()
  {
    URL url = getClass().getResource(getStoreTypeID() + ".template.xml"); //$NON-NLS-1$
    return IOUtil.readText(url);
  }

  @Override
  public String toString()
  {
    return getName();
  }

  private static StoreType[] initStoreTypes()
  {
    // TODO: Make these contributable by store UI plug-ins
    return new StoreType[] { new Database("h2", Messages.StoreType_0, "org.h2.jdbcx.JdbcDataSource", "jdbc:h2:%s"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    };
  }

  /**
   * @author Christian W. Damus (CEA LIST)
   */
  public static class Database extends StoreType
  {
    public static final String PROPERTY_PATH = "path"; //$NON-NLS-1$

    public static final String PROPERTY_CONNECTION_KEEP_ALIVE_PERIOD = "connectionKeepAlivePeriod"; //$NON-NLS-1$

    public static final String DEFAULT_CONNECTION_KEEP_ALIVE_PERIOD = "60"; //$NON-NLS-1$

    public static final String PROPERTY_READER_POOL_CAPACITY = "readerPoolCapacity"; //$NON-NLS-1$

    public static final String DEFAULT_READER_POOL_CAPACITY = "15"; //$NON-NLS-1$

    public static final String PROPERTY_WRITER_POOL_CAPACITY = "writerPoolCapacity"; //$NON-NLS-1$

    public static final String DEFAULT_WRITER_POOL_CAPACITY = "15"; //$NON-NLS-1$

    private final String adapter;

    private final String dataSourceClassName;

    private final String urlPattern;

    public Database(String id, String name, String dataSourceClassName, String urlPattern)
    {
      super("db." + id, name); //$NON-NLS-1$
      adapter = id;
      this.dataSourceClassName = dataSourceClassName;
      this.urlPattern = urlPattern;
    }

    @Override
    public String getStoreTypeID()
    {
      return "db"; //$NON-NLS-1$
    }

    public String getAdapter()
    {
      return adapter;
    }

    public String getDataSourceClassName()
    {
      return dataSourceClassName;
    }

    public String getDataSourceURL(String storePath)
    {
      return String.format(urlPattern, storePath);
    }

    @Override
    protected String fillTemplate(String xmlTemplate, Map<String, Object> storeProperties)
    {
      Map<String, String> variables = new java.util.HashMap<String, String>();
      variables.put("adapter", getAdapter()); //$NON-NLS-1$
      variables.put("dataSource.class", getDataSourceClassName()); //$NON-NLS-1$
      variables.put("dataSource.url", getDataSourceURL((String)storeProperties.get(PROPERTY_PATH))); //$NON-NLS-1$
      variables.put("keepAlive", //$NON-NLS-1$
          defaultString(storeProperties, PROPERTY_CONNECTION_KEEP_ALIVE_PERIOD, DEFAULT_CONNECTION_KEEP_ALIVE_PERIOD));
      variables.put("readerPool", //$NON-NLS-1$
          defaultString(storeProperties, PROPERTY_READER_POOL_CAPACITY, DEFAULT_READER_POOL_CAPACITY));
      variables.put("writerPool", //$NON-NLS-1$
          defaultString(storeProperties, PROPERTY_WRITER_POOL_CAPACITY, DEFAULT_WRITER_POOL_CAPACITY));
      return replaceVariables(xmlTemplate, variables);
    }

    private String defaultString(Map<String, Object> storeProperties, String key, String defaultValue)
    {
      String value = (String)storeProperties.get(key);
      return StringUtil.isEmpty(value) ? defaultValue : value;
    }
  }
}

Back to the top