Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9a3e479ba839c5018858e722b4e7620558aa9045 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/***************************************************************************
 * Copyright (c) 2004-2007 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.cdo.util;

import org.eclipse.emf.cdo.CDOSession;
import org.eclipse.emf.cdo.CDOView;
import org.eclipse.emf.cdo.eresource.CDOResourceFactory;
import org.eclipse.emf.cdo.eresource.EresourcePackage;
import org.eclipse.emf.cdo.internal.protocol.CDOIDImpl;
import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.CDOProtocolConstants;

import org.eclipse.net4j.ConnectorException;
import org.eclipse.net4j.IConnector;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.Resource.Factory.Registry;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
import org.eclipse.emf.internal.cdo.CDOSessionImpl;
import org.eclipse.emf.internal.cdo.CDOStateMachine;
import org.eclipse.emf.internal.cdo.CDOViewImpl;
import org.eclipse.emf.internal.cdo.InternalCDOObject;
import org.eclipse.emf.internal.cdo.bundle.OM;
import org.eclipse.emf.internal.cdo.protocol.CDOClientProtocolFactory;
import org.eclipse.emf.internal.cdo.util.FSMUtil;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;

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

/**
 * @author Eike Stepper
 */
public final class CDOUtil
{
  public static final String EXT_POINT_NAME = "persistent_package";

  private static Map<String, CDOPackageType> packageTypes;

  public static final String CDO_VERSION_SUFFIX = "-CDO";

  private CDOUtil()
  {
  }

  /**
   * Can only be used with Eclipse running!
   */
  public static synchronized Map<String, CDOPackageType> getPackageTypes()
  {
    if (packageTypes == null)
    {
      packageTypes = new HashMap();
      IExtensionRegistry registry = Platform.getExtensionRegistry();

      // Collect native packages
      for (IConfigurationElement element : registry.getConfigurationElementsFor(OM.BUNDLE_ID, EXT_POINT_NAME))
      {
        String uri = element.getAttribute("uri");
        if (!StringUtil.isEmpty(uri) && !uri.equals(EresourcePackage.eINSTANCE.getNsURI()))
        {
          packageTypes.put(uri, CDOPackageType.NATIVE);
        }
      }

      // Collect legacy and converted packages
      for (IConfigurationElement element : registry.getConfigurationElementsFor(EcorePlugin.getPlugin().getBundle()
          .getSymbolicName(), EcorePlugin.GENERATED_PACKAGE_PPID))
      {
        String uri = element.getAttribute("uri");
        if (!StringUtil.isEmpty(uri) && !packageTypes.containsKey(uri)
            && !uri.equals(EresourcePackage.eINSTANCE.getNsURI()))
        {
          String bundleName = element.getContributor().getName();
          Bundle bundle = Platform.getBundle(bundleName);
          String version = (String)bundle.getHeaders().get(Constants.BUNDLE_VERSION);
          if (version.endsWith(CDOUtil.CDO_VERSION_SUFFIX))
          {
            packageTypes.put(uri, CDOPackageType.CONVERTED);
          }
          else
          {
            packageTypes.put(uri, CDOPackageType.LEGACY);
          }
        }
      }
    }

    return packageTypes;
  }

  public static CDOSession openSession(IConnector connector, String repositoryName, EPackage.Registry delegate)
      throws ConnectorException
  {
    CDOSessionImpl session = new CDOSessionImpl(delegate);
    session.setConnector(connector);
    session.setRepositoryName(repositoryName);
    LifecycleUtil.activate(session);
    return session;
  }

  public static CDOSession openSession(IConnector connector, String repositoryName) throws ConnectorException
  {
    return openSession(connector, repositoryName, null);
  }

  public static CDOView getView(ResourceSet resourceSet)
  {
    EList<Adapter> adapters = resourceSet.eAdapters();
    for (Adapter adapter : adapters)
    {
      if (adapter instanceof CDOView)
      {
        return (CDOView)adapter;
      }
    }

    return null;
  }

  public static void prepareResourceSet(ResourceSet resourceSet)
  {
    CDOResourceFactory factory = CDOResourceFactory.INSTANCE;
    Registry registry = resourceSet.getResourceFactoryRegistry();
    Map<String, Object> map = registry.getProtocolToFactoryMap();
    map.put(CDOProtocolConstants.PROTOCOL_NAME, factory);
  }

  public static void prepareContainer(IManagedContainer container)
  {
    container.registerFactory(new CDOClientProtocolFactory());
  }

  public static String extractResourcePath(URI uri)
  {
    if (!CDOProtocolConstants.PROTOCOL_NAME.equals(uri.scheme()))
    {
      return null;
    }

    if (uri.hasAuthority())
    {
      return null;
    }

    if (!uri.isHierarchical())
    {
      return null;
    }

    if (!uri.hasAbsolutePath())
    {
      return null;
    }

    return uri.path();
  }

  public static CDOID extractResourceID(URI uri)
  {
    if (!CDOProtocolConstants.PROTOCOL_NAME.equals(uri.scheme()))
    {
      return null;
    }

    if (uri.hasAuthority())
    {
      return null;
    }

    if (!uri.isHierarchical())
    {
      return null;
    }

    if (uri.hasAbsolutePath())
    {
      return null;
    }

    try
    {
      String path = uri.path();
      return CDOIDImpl.parse(path);
    }
    catch (RuntimeException ex)
    {
      return null;
    }
  }

  public static URI createResourceURI(String path)
  {
    return URI.createURI(CDOProtocolConstants.PROTOCOL_NAME + ":" + path);
  }

  public static EPackage createEPackage(String name, String nsPrefix, String nsURI)
  {
    EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setName(name);
    ePackage.setNsPrefix(nsPrefix);
    ePackage.setNsURI(nsURI);
    return ePackage;
  }

  public static EClass createEClass(EPackage ePackage, String name, boolean isAbstract, boolean isInterface)
  {
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    eClass.setName(name);
    eClass.setAbstract(isAbstract);
    eClass.setInterface(isInterface);
    ePackage.getEClassifiers().add(eClass);
    return eClass;
  }

  public static EAttribute createEAttribute(EClass eClass, String name, EClassifier type)
  {
    EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
    eAttribute.setName(name);
    eAttribute.setEType(type);
    eClass.getEStructuralFeatures().add(eAttribute);
    return eAttribute;
  }

  public static EReference createEReference(EClass eClass, String name, EClassifier type, boolean isRequired,
      boolean isMany)
  {
    EReference eReference = EcoreFactory.eINSTANCE.createEReference();
    eReference.setName(name);
    eReference.setEType(type);
    eReference.setLowerBound(isRequired ? 1 : 0);
    eReference.setUpperBound(isMany ? -1 : 0);
    eClass.getEStructuralFeatures().add(eReference);
    return eReference;
  }

  /**
   * Returns a self-contained copy of the eObject with all proxies resolved.
   * 
   * @param eObject
   *          the object to copy.
   * @return the copy.
   * @see EcoreUtil#copy(EObject)
   */
  @Deprecated
  public static EObject copy(EObject eObject, CDOView view)
  {
    Copier copier = new CDOCopier(view);
    EObject result = copier.copy(eObject);
    copier.copyReferences();
    return result;
  }

  public static void load(EObject eObject, CDOView view)
  {
    InternalCDOObject cdoObject = FSMUtil.adapt(eObject, view);
    CDOStateMachine.INSTANCE.read(cdoObject);

    for (Iterator<InternalCDOObject> it = FSMUtil.iterator(cdoObject.eContents(), (CDOViewImpl)view); it.hasNext();)
    {
      InternalCDOObject content = it.next();
      load(content, view);
    }
  }

  /**
   * @author Eike Stepper
   */
  @Deprecated
  public static final class CDOCopier extends Copier
  {
    private static final long serialVersionUID = 1L;

    private CDOView view;

    public CDOCopier(CDOView view)
    {
      this.view = view;
    }

    @Override
    protected void copyReference(EReference eReference, EObject eObject, EObject copyEObject)
    {
      resolve(eReference, eObject);
      super.copyReference(eReference, eObject, copyEObject);
    }

    @Override
    protected void copyContainment(EReference eReference, EObject eObject, EObject copyEObject)
    {
      resolve(eReference, eObject);
      super.copyContainment(eReference, eObject, copyEObject);
    }

    protected void resolve(EReference eReference, EObject eObject)
    {
      if (eObject.eIsSet(eReference))
      {
        if (eReference.isMany())
        {
          @SuppressWarnings("unchecked")
          List<EObject> list = (List<EObject>)eObject.eGet(eReference);
          for (EObject element : list)
          {
            if (element.eIsProxy())
            {
              EcoreUtil.resolve(element, view.getResourceSet());
            }
          }
        }
        else
        {
          EObject childEObject = (EObject)eObject.eGet(eReference);
          if (childEObject.eIsProxy())
          {
            EcoreUtil.resolve(childEObject, view.getResourceSet());
          }
        }
      }
    }
  }
}

Back to the top