Skip to main content
summaryrefslogtreecommitdiffstats
blob: a798e8a9efea869071a4a59eba82bc1f8fd83bea (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * 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:
 *     Martin Fluegge - initial API and implementation
 */
package org.eclipse.emf.cdo.dawn.util.connection;

import org.eclipse.emf.cdo.dawn.internal.util.bundle.OM;
import org.eclipse.emf.cdo.dawn.util.exceptions.DawnInvalidIdException;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewSet;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.tcp.TCPUtil;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.om.OMPlatform;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.spi.cdo.InternalCDOView;

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

/**
 * @author Martin Fluegge
 */
public class CDOConnectionUtil
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, CDOConnectionUtil.class);

  public static CDOConnectionUtil instance = new CDOConnectionUtil();

  private CDOSession currentSession;

  private String repositoryName;

  private String protocol;

  private String host;

  private Map<String, CDOTransaction> transactions;

  private IConnector connector;

  static
  {
    if (!OMPlatform.INSTANCE.isOSGiRunning())
    {
      Net4jUtil.prepareContainer(IPluginContainer.INSTANCE);
      TCPUtil.prepareContainer(IPluginContainer.INSTANCE);
      CDONet4jUtil.prepareContainer(IPluginContainer.INSTANCE);
    }
  }

  public CDOConnectionUtil()
  {
  }

  public void init(String repositoryName, String protocol, String host)
  {
    this.repositoryName = repositoryName;
    this.protocol = protocol;
    this.host = host;
    setConnector(Net4jUtil.getConnector(IPluginContainer.INSTANCE, protocol, host));
  }

  public void registerPackages(List<EPackage> packages)
  {
    if (packages == null)
    {
      return;
    }

    for (EPackage pack : packages)
    {
      pack.eClass();
    }
  }

  /**
   * opens the session if it is not opened.
   */
  public CDOSession openSession()
  {
    if (!CDOUtil.isLegacyModeDefault())
    {
      CDOUtil.setLegacyModeDefault(true);
    }

    currentSession = (CDOSession)IPluginContainer.INSTANCE.getElement("org.eclipse.emf.cdo.sessions", "cdo", protocol
        + "://" + host + "?repositoryName=" + repositoryName);

    return currentSession;
  }

  public void closeCurrentSession()
  {
    getCurrentSession().close();
  }

  /**
   * opens a transaction on the given resourceSet
   */
  public CDOTransaction openCurrentTransaction(ResourceSet resourceSet, String id)
  {
    if (id == null)
    {
      throw new DawnInvalidIdException("The identifier '" + id + "' is invalid for openeing a transaction");
    }

    if (TRACER.isEnabled())
    {
      TRACER.format("Opening transaction for {0} on {1}", id, resourceSet); //$NON-NLS-1$
    }

    id = convert(id);
    CDOTransaction transaction = getCurrentSession().openTransaction(resourceSet);
    getTransactions().put(id, transaction);
    return transaction;
  }

  public void setChangeSubscribtionPolicyForCurrentTransaction(CDOAdapterPolicy policy, String id)
  {
    id = convert(id);
    getTransactions().get(id).options().addChangeSubscriptionPolicy(policy);
  }

  public CDOTransaction getCurrentTransaction(String id)
  {
    id = convert(id);
    return getTransactions().get(id);
  }

  // TODO find a better way to solve this problem
  private String convert(String id)
  {
    return id.replace("dawn", "cdo");
  }

  public CDOSession getCurrentSession()
  {
    return currentSession;
  }

  public Map<String, CDOTransaction> getTransactions()
  {
    if (transactions == null)
    {
      transactions = new HashMap<String, CDOTransaction>();
    }

    return transactions;
  }

  public CDOView openView(CDOSession session)
  {
    return session.openView();
  }

  public CDOTransaction openTransaction(CDOSession session)
  {
    return session.openTransaction();
  }

  @Deprecated
  public static void closeSession(CDOSession session)
  {
    session.close();
  }

  public CDOTransaction getOrOpenCurrentTransaction(String id, ResourceSet resourceSet, String repositoryName)
  {
    CDOTransaction transaction = getCurrentTransaction(id);
    CDOViewSet viewSet = CDOUtil.getViewSet(resourceSet);
    if (viewSet != null)
    {
      return ((InternalCDOView)viewSet.resolveView(repositoryName)).toTransaction();
    }

    if (transaction == null)
    {
      transaction = openCurrentTransaction(resourceSet, id);
    }

    return transaction;
  }

  public void setConnector(IConnector connector)
  {
    this.connector = connector;
  }

  public IConnector getConnector()
  {
    return connector;
  }
}

Back to the top