Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3d2cbcd35c95e61b7dffddbc0b21982300c5b70b (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * 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.internal.cdo;

import org.eclipse.emf.cdo.CDOSession;

import org.eclipse.emf.internal.cdo.util.CDOPackageRegistryImpl;

import org.eclipse.net4j.signal.failover.IFailOverStrategy;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.factory.Factory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @author Eike Stepper
 */
public class CDOSessionFactory extends Factory
{
  public static final String PRODUCT_GROUP = "org.eclipse.emf.cdo.sessions";

  public static final String TYPE = "cdo";

  private static final String TRUE = Boolean.TRUE.toString();

  public CDOSessionFactory()
  {
    super(PRODUCT_GROUP, TYPE);
  }

  public CDOSession create(String description)
  {
    try
    {
      URI uri = new URI(description);
      String query = uri.getQuery();
      if (StringUtil.isEmpty(query))
      {
        throw new IllegalArgumentException("Query is empty: " + description);
      }

      Map<String, String> result = new HashMap<String, String>();
      StringTokenizer tokenizer = new StringTokenizer(query, "&");
      while (tokenizer.hasMoreTokens())
      {
        String parameter = tokenizer.nextToken();
        if (!StringUtil.isEmpty(parameter))
        {
          int pos = parameter.indexOf('=');
          if (pos == -1)
          {
            String key = parameter.trim();
            result.put(key, "");
          }
          else
          {
            String key = parameter.substring(0, pos).trim();
            String value = parameter.substring(pos + 1);
            result.put(key, value);
          }
        }
      }

      String repositoryName = result.get("repositoryName");
      boolean automaticPackageRegistry = TRUE.equals(result.get("automaticPackageRegistry"));
      return createSession(repositoryName, automaticPackageRegistry, null);
    }
    catch (URISyntaxException ex)
    {
      throw new IllegalArgumentException(ex);
    }
  }

  public static CDOSession get(IManagedContainer container, String description)
  {
    return (CDOSession)container.getElement(PRODUCT_GROUP, TYPE, description);
  }

  /**
   * @since 2.0
   */
  public static CDOSessionImpl createSession(String repositoryName, boolean automaticPackageRegistry,
      IFailOverStrategy failOverStrategy)
  {
    CDOSessionImpl session = new CDOSessionImpl();
    if (automaticPackageRegistry)
    {
      CDOPackageRegistryImpl.Eager packageRegistry = new CDOPackageRegistryImpl.Eager();
      packageRegistry.setSession(session);
      session.setPackageRegistry(packageRegistry);
    }

    session.setRepositoryName(repositoryName);
    session.setFailOverStrategy(failOverStrategy);
    return session;
  }
}

Back to the top