Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29db35d419a46c2f7decf21a9b2e7c3b791c581a (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
/***************************************************************************
 * 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.eresource.impl;

import org.eclipse.emf.cdo.eresource.CDOResourceFactory;
import org.eclipse.emf.cdo.eresource.EresourceFactory;
import org.eclipse.emf.cdo.protocol.util.ImplementationError;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

/**
 * @author Eike Stepper
 */
public class CDOResourceFactoryImpl implements Resource.Factory, CDOResourceFactory
{
  // @Singleton
  public static final CDOResourceFactoryImpl INSTANCE = new CDOResourceFactoryImpl();

  private static final String RESOURCE_SET_CLASS_NAME = ResourceSetImpl.class.getName();

  public CDOResourceFactoryImpl()
  {
  }

  public Resource createResource(URI uri)
  {
    CDOResourceImpl resource = (CDOResourceImpl)EresourceFactory.eINSTANCE.createCDOResource();
    resource.setURI(uri);
    resource.setExisting(isExistingResource());
    return resource;
  }

  private boolean isExistingResource()
  {
    StackTraceElement[] elements = Thread.currentThread().getStackTrace();
    if (elements.length >= 6)
    {
      if (isResourceSetMethod(elements[6], "getResource"))
      {
        return true;
      }

      if (isResourceSetMethod(elements[4], "createResource"))
      {
        return false;
      }
    }

    throw new ImplementationError("Call stack is in unexpected state");
  }

  private boolean isResourceSetMethod(StackTraceElement element, String methodName)
  {
    return methodName.equals(element.getMethodName()) && RESOURCE_SET_CLASS_NAME.equals(element.getClassName());
  }
}

Back to the top